diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2011-12-05 21:27:51 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2011-12-05 21:27:51 +0400 |
commit | e14a0eb8d0d22215d5185e023d4c8e3bbe91168b (patch) | |
tree | 545747373b1abd6c2e098d63e00eeb897ea76a15 /lib | |
parent | e3402769f341b9787d738aaf03e41d27315cd2ac (diff) | |
parent | 2d81f4880e7f27c0359d5d5b54822db2441ce6ea (diff) |
Merge branch 'gitolite'
Diffstat (limited to 'lib')
-rw-r--r-- | lib/.directory | 5 | ||||
-rw-r--r-- | lib/gitlabhq/.directory | 5 | ||||
-rw-r--r-- | lib/gitlabhq/git_host.rb | 21 | ||||
-rw-r--r-- | lib/gitlabhq/gitolite.rb | 80 | ||||
-rw-r--r-- | lib/gitosis.rb | 74 | ||||
-rw-r--r-- | lib/tasks/gitolite_rebuild.rake | 18 |
6 files changed, 129 insertions, 74 deletions
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..9a6eecb3f0a --- /dev/null +++ b/lib/gitlabhq/git_host.rb @@ -0,0 +1,21 @@ +require File.join(Rails.root, "lib", "gitlabhq", "gitolite") + +module Gitlabhq + class GitHost + def self.system + Gitlabhq::Gitolite + end + + def self.admin_uri + GIT_HOST["admin_uri"] + end + + def self.url_to_repo(path) + if !GIT_HOST["port"] or GIT_HOST["port"] == 22 + "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{path}.git" + else + "ssh://#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{GIT_HOST["port"]}/#{path}.git" + end + 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/gitosis.rb b/lib/gitosis.rb deleted file mode 100644 index 92d32d8bf73..00000000000 --- a/lib/gitosis.rb +++ /dev/null @@ -1,74 +0,0 @@ -require 'inifile' -require 'timeout' -require 'fileutils' - -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 #{GITOSIS['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 diff --git a/lib/tasks/gitolite_rebuild.rake b/lib/tasks/gitolite_rebuild.rake new file mode 100644 index 00000000000..558d4660ba8 --- /dev/null +++ b/lib/tasks/gitolite_rebuild.rake @@ -0,0 +1,18 @@ +desc "Rebuild each project at gitolite config" +task :gitolite_rebuild => :environment do + puts "Starting Projects" + Project.find_each(:batch_size => 100) do |project| + puts + puts "=== #{project.name}" + project.update_repository + puts + end + puts "Done with projects" + + puts "Starting Key" + Key.find_each(:batch_size => 100) do |project| + project.update_repository + print '.' + end + puts "Done with keys" +end |