2011-12-03 18:44:59 -05:00
|
|
|
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)
|
2011-12-07 16:56:57 -05:00
|
|
|
FileUtils.rm_rf(project.path_to_repo)
|
2011-12-03 18:44:59 -05:00
|
|
|
|
|
|
|
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
|
2011-12-06 18:27:07 -05:00
|
|
|
def update_project(repo_name, project)
|
2011-12-03 18:44:59 -05:00
|
|
|
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
|
|
|
|
|
2011-12-06 18:27:07 -05:00
|
|
|
name_readers = project.repository_readers
|
|
|
|
name_writers = project.repository_writers
|
|
|
|
|
|
|
|
repo.clean_permissions
|
|
|
|
repo.add_permission("R", "", name_readers) unless name_readers.blank?
|
2011-12-03 18:44:59 -05:00
|
|
|
repo.add_permission("RW+", "", name_writers) unless name_writers.blank?
|
2011-12-06 18:27:07 -05:00
|
|
|
conf.add_repo(repo, true)
|
2011-12-03 18:44:59 -05:00
|
|
|
|
|
|
|
ga_repo.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|