2012-09-07 07:16:29 +02:00
|
|
|
require 'gitolite'
|
|
|
|
require 'timeout'
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class GitoliteConfig
|
2013-01-28 16:22:45 +01:00
|
|
|
include Gitlab::Popen
|
|
|
|
|
2012-09-07 19:43:51 +02:00
|
|
|
class PullError < StandardError; end
|
|
|
|
class PushError < StandardError; end
|
2013-01-19 10:58:36 +01:00
|
|
|
class BrokenGitolite < StandardError; end
|
2012-09-07 19:43:51 +02:00
|
|
|
|
2013-01-22 09:35:58 +01:00
|
|
|
attr_reader :config_tmp_dir, :tmp_dir, :ga_repo, :conf
|
2012-09-07 14:36:40 +02:00
|
|
|
|
2013-01-22 09:35:58 +01:00
|
|
|
def initialize
|
|
|
|
@tmp_dir = Rails.root.join("tmp").to_s
|
|
|
|
@config_tmp_dir = File.join(@tmp_dir,"gitlabhq-gitolite-#{Time.now.to_i}")
|
2012-09-07 08:15:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def ga_repo
|
2012-11-06 23:47:33 +01:00
|
|
|
@ga_repo ||= ::Gitolite::GitoliteAdmin.new(
|
|
|
|
File.join(config_tmp_dir,'gitolite'),
|
2012-12-15 01:16:25 +01:00
|
|
|
conf: Gitlab.config.gitolite.config_file
|
2012-11-06 23:47:33 +01:00
|
|
|
)
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def apply
|
|
|
|
Timeout::timeout(30) do
|
2013-01-22 09:35:58 +01:00
|
|
|
File.open(File.join(tmp_dir, "gitlabhq-gitolite.lock"), "w+") do |f|
|
2012-09-07 07:16:29 +02:00
|
|
|
begin
|
2012-09-07 08:15:30 +02:00
|
|
|
# Set exclusive lock
|
|
|
|
# to prevent race condition
|
2012-09-07 07:16:29 +02:00
|
|
|
f.flock(File::LOCK_EX)
|
2012-09-07 08:15:30 +02:00
|
|
|
|
|
|
|
# Pull gitolite-admin repo
|
|
|
|
# in tmp dir before do any changes
|
2013-01-22 09:35:58 +01:00
|
|
|
pull
|
2012-09-07 08:15:30 +02:00
|
|
|
|
|
|
|
# Build ga_repo object and @conf
|
|
|
|
# to access gitolite-admin configuration
|
|
|
|
@conf = ga_repo.config
|
|
|
|
|
|
|
|
# Do any changes
|
|
|
|
# in gitolite-admin
|
|
|
|
# config here
|
2012-09-07 07:16:29 +02:00
|
|
|
yield(self)
|
2012-09-07 08:15:30 +02:00
|
|
|
|
|
|
|
# Save changes in
|
|
|
|
# gitolite-admin repo
|
2012-09-28 06:42:31 +02:00
|
|
|
# before push it
|
2012-09-07 08:15:30 +02:00
|
|
|
ga_repo.save
|
|
|
|
|
|
|
|
# Push gitolite-admin repo
|
|
|
|
# to apply all changes
|
2013-01-22 09:35:58 +01:00
|
|
|
push
|
2012-09-28 06:42:31 +02:00
|
|
|
ensure
|
2012-09-07 08:15:30 +02:00
|
|
|
# Remove tmp dir
|
2012-09-28 06:42:31 +02:00
|
|
|
# removing the gitolite folder first is important to avoid
|
|
|
|
# NFS issues.
|
|
|
|
FileUtils.rm_rf(File.join(config_tmp_dir, 'gitolite'))
|
|
|
|
|
|
|
|
# Remove parent tmp dir
|
2012-09-07 14:36:40 +02:00
|
|
|
FileUtils.rm_rf(config_tmp_dir)
|
2012-09-28 06:42:31 +02:00
|
|
|
|
|
|
|
# Unlock so other task can access
|
2012-09-07 08:15:30 +02:00
|
|
|
# gitolite configuration
|
2012-09-07 07:16:29 +02:00
|
|
|
f.flock(File::LOCK_UN)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-07 19:43:51 +02:00
|
|
|
rescue PullError => ex
|
2012-09-11 22:24:53 +02:00
|
|
|
log("Pull error -> " + ex.message)
|
2012-09-07 19:43:51 +02:00
|
|
|
raise Gitolite::AccessDenied, ex.message
|
|
|
|
|
|
|
|
rescue PushError => ex
|
2012-09-11 22:24:53 +02:00
|
|
|
log("Push error -> " + " " + ex.message)
|
2012-09-07 19:43:51 +02:00
|
|
|
raise Gitolite::AccessDenied, ex.message
|
|
|
|
|
2013-01-19 10:58:36 +01:00
|
|
|
rescue BrokenGitolite => ex
|
|
|
|
log("Gitolite error -> " + " " + ex.message)
|
|
|
|
raise Gitolite::AccessDenied, ex.message
|
|
|
|
|
2013-01-19 12:57:50 +01:00
|
|
|
rescue Exception => ex
|
|
|
|
log(ex.class.name + " " + ex.message)
|
|
|
|
raise Gitolite::AccessDenied.new("gitolite timeout")
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
|
2012-09-11 22:24:53 +02:00
|
|
|
def log message
|
|
|
|
Gitlab::GitLogger.error(message)
|
|
|
|
end
|
|
|
|
|
2013-01-28 16:22:45 +01:00
|
|
|
def path_to_repo(name)
|
|
|
|
File.join(Gitlab.config.gitolite.repos_path, "#{name}.git")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_project(name)
|
|
|
|
full_path = path_to_repo(name)
|
|
|
|
FileUtils.rm_rf(full_path) if File.exists?(full_path)
|
|
|
|
conf.rm_repo(name)
|
2012-11-21 06:54:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def clean_repo repo_name
|
|
|
|
conf.rm_repo(repo_name)
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_project!(project)
|
|
|
|
apply do |config|
|
|
|
|
config.destroy_project(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_key(id, key)
|
|
|
|
File.open(File.join(config_tmp_dir, 'gitolite/keydir',"#{id}.pub"), 'w') do |f|
|
|
|
|
f.write(key.gsub(/\n/,''))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm_key(user)
|
2012-10-03 14:56:41 +02:00
|
|
|
key_path = File.join(config_tmp_dir, 'gitolite/keydir', "#{user}.pub")
|
|
|
|
ga_key = ::Gitolite::SSHKey.from_file(key_path)
|
|
|
|
ga_repo.rm_key(ga_key)
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# update or create
|
2012-11-27 07:31:15 +01:00
|
|
|
def update_project(project)
|
2012-09-07 07:16:29 +02:00
|
|
|
repo = update_project_config(project, conf)
|
|
|
|
conf.add_repo(repo, true)
|
|
|
|
end
|
|
|
|
|
2012-11-27 07:31:15 +01:00
|
|
|
def update_project!( project)
|
2012-09-07 07:16:29 +02:00
|
|
|
apply do |config|
|
2012-11-27 07:31:15 +01:00
|
|
|
config.update_project(project)
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-27 07:31:15 +01:00
|
|
|
# Updates many projects and uses project.path_with_namespace as the repo path
|
2012-09-07 07:16:29 +02:00
|
|
|
# An order of magnitude faster than update_project
|
|
|
|
def update_projects(projects)
|
|
|
|
projects.each do |project|
|
|
|
|
repo = update_project_config(project, conf)
|
|
|
|
conf.add_repo(repo, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_project_config(project, conf)
|
2012-11-22 20:51:16 +01:00
|
|
|
repo_name = project.path_with_namespace
|
2012-09-07 07:16:29 +02:00
|
|
|
|
|
|
|
repo = if conf.has_repo?(repo_name)
|
|
|
|
conf.get_repo(repo_name)
|
|
|
|
else
|
|
|
|
::Gitolite::Config::Repo.new(repo_name)
|
|
|
|
end
|
|
|
|
|
2013-01-04 07:43:25 +01:00
|
|
|
name_readers = project.team.repository_readers
|
|
|
|
name_writers = project.team.repository_writers
|
|
|
|
name_masters = project.team.repository_masters
|
2012-09-07 07:16:29 +02:00
|
|
|
|
|
|
|
pr_br = project.protected_branches.map(&:name).join("$ ")
|
|
|
|
|
|
|
|
repo.clean_permissions
|
|
|
|
|
|
|
|
# Deny access to protected branches for writers
|
|
|
|
unless name_writers.blank? || pr_br.blank?
|
|
|
|
repo.add_permission("-", pr_br.strip + "$ ", name_writers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add read permissions
|
|
|
|
repo.add_permission("R", "", name_readers) unless name_readers.blank?
|
|
|
|
|
|
|
|
# Add write permissions
|
|
|
|
repo.add_permission("RW+", "", name_writers) unless name_writers.blank?
|
|
|
|
repo.add_permission("RW+", "", name_masters) unless name_masters.blank?
|
|
|
|
|
2012-10-17 09:43:44 +02:00
|
|
|
# Add sharedRepository config
|
|
|
|
repo.set_git_config("core.sharedRepository", "0660")
|
|
|
|
|
2012-09-07 07:16:29 +02:00
|
|
|
repo
|
|
|
|
end
|
|
|
|
|
2012-09-07 08:15:30 +02:00
|
|
|
# Enable access to all repos for gitolite admin.
|
|
|
|
# We use it for accept merge request feature
|
2012-09-07 07:16:29 +02:00
|
|
|
def admin_all_repo
|
2012-12-15 01:16:25 +01:00
|
|
|
owner_name = Gitlab.config.gitolite.admin_key
|
2012-09-07 07:16:29 +02:00
|
|
|
|
|
|
|
# @ALL repos premission for gitolite owner
|
|
|
|
repo_name = "@all"
|
|
|
|
repo = if conf.has_repo?(repo_name)
|
|
|
|
conf.get_repo(repo_name)
|
|
|
|
else
|
|
|
|
::Gitolite::Config::Repo.new(repo_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
repo.add_permission("RW+", "", owner_name)
|
|
|
|
conf.add_repo(repo, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin_all_repo!
|
|
|
|
apply { |config| config.admin_all_repo }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-22 09:35:58 +01:00
|
|
|
def pull
|
|
|
|
# Create config tmp dir like "RAILS_ROOT/tmp/gitlabhq-gitolite-132545"
|
|
|
|
Dir.mkdir config_tmp_dir
|
2012-09-07 19:43:51 +02:00
|
|
|
|
2013-01-22 09:35:58 +01:00
|
|
|
# Clone gitolite-admin repo into tmp dir
|
|
|
|
popen("git clone #{Gitlab.config.gitolite.admin_uri} #{config_tmp_dir}/gitolite", tmp_dir)
|
|
|
|
|
|
|
|
# Ensure file with config presents after cloning
|
|
|
|
unless File.exists?(File.join(config_tmp_dir, 'gitolite', 'conf', 'gitolite.conf'))
|
2012-09-07 19:43:51 +02:00
|
|
|
raise PullError, "unable to clone gitolite-admin repo"
|
|
|
|
end
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
|
2013-01-22 09:35:58 +01:00
|
|
|
def push
|
2013-01-28 16:22:45 +01:00
|
|
|
output, status = popen('git add -A', tmp_conf_path)
|
2013-01-19 12:56:55 +01:00
|
|
|
raise "Git add failed." unless status.zero?
|
2012-11-08 22:32:00 +01:00
|
|
|
|
2013-01-19 12:56:55 +01:00
|
|
|
# git commit returns 0 on success, and 1 if there is nothing to commit
|
2013-01-28 16:22:45 +01:00
|
|
|
output, status = popen('git commit -m "GitLab"', tmp_conf_path)
|
2013-01-19 12:56:55 +01:00
|
|
|
raise "Git add failed." unless [0,1].include?(status)
|
2013-01-19 10:58:36 +01:00
|
|
|
|
2013-01-28 16:22:45 +01:00
|
|
|
output, status = popen('git push', tmp_conf_path)
|
2013-01-19 12:56:55 +01:00
|
|
|
|
|
|
|
if output =~ /remote\: FATAL/
|
|
|
|
raise BrokenGitolite, output
|
2013-01-19 10:58:36 +01:00
|
|
|
end
|
|
|
|
|
2013-01-19 12:56:55 +01:00
|
|
|
if status.zero? || output =~ /Everything up\-to\-date/
|
|
|
|
return true
|
2012-09-07 19:43:51 +02:00
|
|
|
else
|
|
|
|
raise PushError, "unable to push gitolite-admin repo"
|
|
|
|
end
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
2013-01-19 12:56:55 +01:00
|
|
|
|
2013-01-28 16:22:45 +01:00
|
|
|
def tmp_conf_path
|
|
|
|
File.join(config_tmp_dir,'gitolite')
|
2013-01-19 12:56:55 +01:00
|
|
|
end
|
2012-09-07 07:16:29 +02:00
|
|
|
end
|
|
|
|
end
|