From 9e089efe5a2defa38cc94347a5051f1cfe91406b Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 4 Dec 2011 01:44:59 +0200 Subject: gitolite & gitosis support --- app/controllers/application_controller.rb | 2 +- app/controllers/projects_controller.rb | 2 +- app/models/key.rb | 4 +- app/models/repository.rb | 12 ++-- app/views/projects/_form.html.haml | 4 +- config/environment.rb | 2 +- config/gitlab.yml | 3 +- config/initializers/gitlabhq/10_load_config.rb | 2 +- lib/.directory | 5 ++ lib/gitlabhq/.directory | 5 ++ lib/gitlabhq/git_host.rb | 18 ++++++ lib/gitlabhq/gitolite.rb | 80 +++++++++++++++++++++++++ lib/gitlabhq/gitosis.rb | 76 +++++++++++++++++++++++ lib/gitosis.rb | 83 -------------------------- 14 files changed, 200 insertions(+), 98 deletions(-) create mode 100644 lib/.directory create mode 100644 lib/gitlabhq/.directory create mode 100644 lib/gitlabhq/git_host.rb create mode 100644 lib/gitlabhq/gitolite.rb create mode 100644 lib/gitlabhq/gitosis.rb delete mode 100644 lib/gitosis.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b37deafdb4e..380336f3526 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base protect_from_forgery helper_method :abilities, :can? - rescue_from Gitosis::AccessDenied do |exception| + rescue_from Gitlabhq::Gitosis::AccessDenied, Gitlabhq::Gitolite::AccessDenied do |exception| render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 717f85959b6..11b4f0bf3e3 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -42,7 +42,7 @@ class ProjectsController < ApplicationController format.js end end - rescue Gitosis::AccessDenied + rescue Gitlabhq::Gitosis::AccessDenied, Gitlabhq::Gitolite::AccessDenied render :js => "location.href = '#{errors_gitosis_path}'" and return rescue StandardError => ex @project.errors.add(:base, "Cant save project. Please try again later") diff --git a/app/models/key.rb b/app/models/key.rb index 8231c30ce1f..f2f0134cde5 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -19,7 +19,7 @@ class Key < ActiveRecord::Base end def update_gitosis - Gitosis.new.configure do |c| + GitoProxy.system.new.configure do |c| c.update_keys(identifier, key) projects.each do |project| @@ -29,7 +29,7 @@ class Key < ActiveRecord::Base end def gitosis_delete_key - Gitosis.new.configure do |c| + GitoProxy.system.new.configure do |c| c.delete_key(identifier) projects.each do |project| diff --git a/app/models/repository.rb b/app/models/repository.rb index b87a1fba4ef..a3e2e1e5758 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -22,25 +22,25 @@ class Repository end def url_to_repo - if !GITOSIS["port"] or GITOSIS["port"] == 22 - "#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git" + if !GIT_HOST["port"] or GIT_HOST["port"] == 22 + "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{path}.git" else - "ssh://#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{GITOSIS["port"]}/#{path}.git" + "ssh://#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{GIT_HOST["port"]}/#{path}.git" end end def path_to_repo - GITOSIS["base_path"] + path + ".git" + GIT_HOST["base_path"] + path + ".git" end def update_gitosis_project - Gitosis.new.configure do |c| + GitProxy.system.new.configure do |c| c.update_project(path, project.gitosis_writers) end end def destroy_gitosis_project - Gitosis.new.configure do |c| + GitProxy.system.new.configure do |c| c.destroy_project(@project) end end diff --git a/app/views/projects/_form.html.haml b/app/views/projects/_form.html.haml index 234dee971c5..ce2fd069b55 100644 --- a/app/views/projects/_form.html.haml +++ b/app/views/projects/_form.html.haml @@ -20,13 +20,13 @@ %tr %td .left= f.label :path - %cite.right= "git@#{GITOSIS["host"]}:" + %cite.right= "git@#{GIT_HOST["host"]}:" %td = f.text_field :path, :placeholder => "example_project", :disabled => !@project.new_record? %tr %td .left= f.label :code - %cite.right= "http://#{GITOSIS["host"]}/" + %cite.right= "http://#{GIT_HOST["host"]}/" %td= f.text_field :code, :placeholder => "example" %tr diff --git a/config/environment.rb b/config/environment.rb index 1c2d723e18b..9b09be0557d 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -4,4 +4,4 @@ require File.expand_path('../application', __FILE__) # Initialize the rails application Gitlab::Application.initialize! -require File.join(Rails.root, "lib", "gitosis") +require File.join(Rails.root, "lib", "gitlabhq", "git_host") diff --git a/config/gitlab.yml b/config/gitlab.yml index 3afd9c1111c..c61d382f65b 100644 --- a/config/gitlab.yml +++ b/config/gitlab.yml @@ -7,7 +7,8 @@ email: host: gitlabhq.com # Gitosis congiguration -gitosis: +git_host: + system: gitolite# or gitosis admin_uri: git@localhost:gitolite-admin base_path: /home/git/repositories/ host: localhost diff --git a/config/initializers/gitlabhq/10_load_config.rb b/config/initializers/gitlabhq/10_load_config.rb index cfda096e5e8..96919595815 100644 --- a/config/initializers/gitlabhq/10_load_config.rb +++ b/config/initializers/gitlabhq/10_load_config.rb @@ -1,3 +1,3 @@ -GITOSIS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["gitosis"] +GIT_HOST = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git_host"] EMAIL_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["email"] GIT_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git"] diff --git a/lib/.directory b/lib/.directory new file mode 100644 index 00000000000..c0d0b18ff56 --- /dev/null +++ b/lib/.directory @@ -0,0 +1,5 @@ +[Dolphin] +AdditionalInfoV2=Details_Size,Details_Date,CustomizedDetails +Timestamp=2011,12,4,1,34,13 +Version=2 +ViewMode=1 diff --git a/lib/gitlabhq/.directory b/lib/gitlabhq/.directory new file mode 100644 index 00000000000..fcaf533bacc --- /dev/null +++ b/lib/gitlabhq/.directory @@ -0,0 +1,5 @@ +[Dolphin] +AdditionalInfoV2=Details_Size,Details_Date,CustomizedDetails +Timestamp=2011,12,4,1,34,17 +Version=2 +ViewMode=1 diff --git a/lib/gitlabhq/git_host.rb b/lib/gitlabhq/git_host.rb new file mode 100644 index 00000000000..48f5a150740 --- /dev/null +++ b/lib/gitlabhq/git_host.rb @@ -0,0 +1,18 @@ +require File.join(Rails.root, "lib", "gitlabhq", "gitolite") +require File.join(Rails.root, "lib", "gitlabhq", "gitosis") + +module Gitlabhq + class GitHost + def self.system + if GIT_HOST["system"] == "gitolite" + Gitlabhq::Gitolite + else + Gitlabhq::Gitosis + end + end + + def self.admin_uri + GIT_HOST["admin_uri"] + end + end +end diff --git a/lib/gitlabhq/gitolite.rb b/lib/gitlabhq/gitolite.rb new file mode 100644 index 00000000000..de8241fe13a --- /dev/null +++ b/lib/gitlabhq/gitolite.rb @@ -0,0 +1,80 @@ +require 'gitolite' +require 'timeout' +require 'fileutils' + +module Gitlabhq + class Gitolite + class AccessDenied < StandardError; end + + def pull + # create tmp dir + @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}") + Dir.mkdir @local_dir + + `git clone #{GitHost.admin_uri} #{@local_dir}/gitolite` + end + + def push + Dir.chdir(File.join(@local_dir, "gitolite")) + `git add -A` + `git commit -am "Gitlab"` + `git push` + Dir.chdir(Rails.root) + + FileUtils.rm_rf(@local_dir) + end + + def configure + status = Timeout::timeout(20) do + File.open(File.join(Dir.tmpdir,"gitlabhq-gitolite.lock"), "w+") do |f| + begin + f.flock(File::LOCK_EX) + pull + yield(self) + push + ensure + f.flock(File::LOCK_UN) + end + end + end + rescue Exception => ex + raise Gitolite::AccessDenied.new("gitolite timeout") + end + + def destroy_project(project) + `sudo -u git rm -rf #{project.path_to_repo}` + + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) + conf = ga_repo.config + conf.rm_repo(project.path) + ga_repo.save + end + + #update or create + def update_keys(user, key) + File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } + end + + def delete_key(user) + File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub")) + `cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub` + end + + # update or create + def update_project(repo_name, name_writers) + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) + conf = ga_repo.config + + repo = if conf.has_repo?(repo_name) + conf.get_repo(repo_name) + else + ::Gitolite::Config::Repo.new(repo_name) + end + + repo.add_permission("RW+", "", name_writers) unless name_writers.blank? + conf.add_repo(repo) + + ga_repo.save + end + end +end diff --git a/lib/gitlabhq/gitosis.rb b/lib/gitlabhq/gitosis.rb new file mode 100644 index 00000000000..a3dbcc80b22 --- /dev/null +++ b/lib/gitlabhq/gitosis.rb @@ -0,0 +1,76 @@ +require 'inifile' +require 'timeout' +require 'fileutils' + +module Gitlabhq + class Gitosis + class AccessDenied < StandardError; end + + def pull + # create tmp dir + @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitosis-#{Time.now.to_i}") + + Dir.mkdir @local_dir + + `git clone #{GitHost.admin_uri} #{@local_dir}/gitosis` + end + + def push + Dir.chdir(File.join(@local_dir, "gitosis")) + `git add -A` + `git commit -am "Gitlab"` + `git push` + Dir.chdir(Rails.root) + + FileUtils.rm_rf(@local_dir) + end + + def configure + status = Timeout::timeout(20) do + File.open(File.join(Dir.tmpdir,"gitlabhq-gitosis.lock"), "w+") do |f| + begin + f.flock(File::LOCK_EX) + pull + yield(self) + push + ensure + f.flock(File::LOCK_UN) + end + end + end + rescue Exception => ex + raise Gitosis::AccessDenied.new("gitosis timeout") + end + + def destroy_project(project) + `sudo -u git rm -rf #{project.path_to_repo}` + + conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf')) + + conf.delete_section("group #{project.path}") + + conf.write + end + + #update or create + def update_keys(user, key) + File.open(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } + end + + def delete_key(user) + File.unlink(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub")) + `cd #{File.join(@local_dir,'gitosis')} ; git rm keydir/#{user}.pub` + end + + #update or create + def update_project(repo_name, name_writers) + # write config file + conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf')) + + conf["group #{repo_name}"]['writable'] = repo_name + conf["group #{repo_name}"]['members'] = name_writers.join(' ') + + conf.write + end + end +end diff --git a/lib/gitosis.rb b/lib/gitosis.rb deleted file mode 100644 index 6aa32849fc3..00000000000 --- a/lib/gitosis.rb +++ /dev/null @@ -1,83 +0,0 @@ -require 'gitolite' - -require 'inifile' -require 'timeout' -require 'fileutils' - -class Gitosis - class AccessDenied < StandardError; end - - def pull - # create tmp dir - @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}") - - Dir.mkdir @local_dir - - `git clone #{GITOSIS['admin_uri']} #{@local_dir}/gitolite` - end - - def push - Dir.chdir(File.join(@local_dir, "gitolite")) - `git add -A` - `git commit -am "Gitlab"` - `git push` - Dir.chdir(Rails.root) - - FileUtils.rm_rf(@local_dir) - end - - def configure - status = Timeout::timeout(20) do - File.open(File.join(Dir.tmpdir,"gitlabhq-gitolite.lock"), "w+") do |f| - begin - f.flock(File::LOCK_EX) - pull - yield(self) - push - ensure - f.flock(File::LOCK_UN) - end - end - end - #rescue Exception => ex - #raise Gitosis::AccessDenied.new("gitolite timeout") - end - - def destroy_project(project) - `sudo -u git rm -rf #{project.path_to_repo}` - - conf = IniFile.new(File.join(@local_dir,'gitolite', 'conf', 'gitolite.conf')) - - conf.delete_section("group #{project.path}") - - conf.write - end - - #update or create - def update_keys(user, key) - File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } - end - - def delete_key(user) - File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub")) - `cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub` - end - - #update or create - def update_project(repo_name, name_writers) - ga_repo = Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) - conf = ga_repo.config - - repo = if conf.has_repo?(repo_name) - conf.get_repo(repo_name) - else - Gitolite::Config::Repo.new(repo_name) - end - - repo.add_permission("RW+", "", name_writers) unless name_writers.blank? - - conf.add_repo(repo) - - ga_repo.save - end -end -- cgit v1.2.1