gitlab-org--gitlab-foss/lib/gitosis.rb

74 lines
1.7 KiB
Ruby
Raw Normal View History

2011-10-08 17:36:38 -04:00
require 'inifile'
2011-10-09 17:15:28 -04:00
require 'timeout'
2011-10-10 17:07:40 -04:00
require 'fileutils'
2011-10-08 17:36:38 -04:00
class Gitosis
2011-10-09 17:15:28 -04:00
class AccessDenied < StandardError; end
2011-10-08 17:36:38 -04:00
def pull
# create tmp dir
2011-10-10 17:07:40 -04:00
@local_dir = File.join(Dir.tmpdir,"gitlabhq-gitosis-#{Time.now.to_i}")
2011-10-08 17:36:38 -04:00
Dir.mkdir @local_dir
2011-10-13 08:57:26 -04:00
`git clone #{GITOSIS['admin_uri']} #{@local_dir}/gitosis`
2011-10-08 17:36:38 -04:00
end
def push
2011-10-13 08:57:26 -04:00
Dir.chdir(File.join(@local_dir, "gitosis"))
`git add -A`
`git commit -am "Gitlab"`
`git push`
Dir.chdir(Rails.root)
2011-10-08 17:36:38 -04:00
2011-10-13 08:57:26 -04:00
FileUtils.rm_rf(@local_dir)
2011-10-08 17:36:38 -04:00
end
def configure
2011-10-13 08:57:26 -04:00
status = Timeout::timeout(20) do
2011-10-10 17:07:40 -04:00
File.open(File.join(Dir.tmpdir,"gitlabhq-gitosis.lock"), "w+") do |f|
f.flock(File::LOCK_EX)
2011-10-09 17:15:28 -04:00
2011-10-10 17:07:40 -04:00
pull
yield(self)
push
2011-10-09 17:15:28 -04:00
2011-10-10 17:07:40 -04:00
f.flock(File::LOCK_UN)
2011-10-09 17:15:28 -04:00
end
2011-10-10 17:07:40 -04:00
end
2011-10-09 17:15:28 -04:00
rescue Exception => ex
raise Gitosis::AccessDenied.new("gitosis timeout")
2011-10-08 17:36:38 -04:00
end
def destroy_project(project)
2011-10-13 08:57:26 -04:00
FileUtils.rm_rf(project.path_to_repo)
2011-10-08 17:36:38 -04:00
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
2011-10-09 17:15:28 -04:00
#update or create
2011-10-08 17:36:38 -04:00
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