Commit 963746f97847cd106e5f90f830fe9dc0b5ae0102

Authored by Dmitriy Zaporozhets
1 parent b994a65f

Refactor gitolite_config

Showing 1 changed file with 43 additions and 28 deletions   Show diff stats
lib/gitlab/backend/gitolite_config.rb
@@ -4,39 +4,64 @@ require 'fileutils' @@ -4,39 +4,64 @@ require 'fileutils'
4 4
5 module Gitlab 5 module Gitlab
6 class GitoliteConfig 6 class GitoliteConfig
7 - attr_reader :config_tmp_dir 7 + attr_reader :config_tmp_dir, :ga_repo, :conf
8 8
9 - def reset_config_tmp_dir  
10 - @config_tmp_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") 9 + def config_tmp_dir
  10 + @config_tmp_dir ||= File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}")
  11 + end
  12 +
  13 + def ga_repo
  14 + @ga_repo ||= ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite'))
11 end 15 end
12 16
13 def apply 17 def apply
14 Timeout::timeout(30) do 18 Timeout::timeout(30) do
15 File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| 19 File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f|
16 begin 20 begin
  21 + # Set exclusive lock
  22 + # to prevent race condition
17 f.flock(File::LOCK_EX) 23 f.flock(File::LOCK_EX)
18 - reset_config_tmp_dir  
19 - pull 24 +
  25 + # Pull gitolite-admin repo
  26 + # in tmp dir before do any changes
  27 + pull(config_tmp_dir)
  28 +
  29 + # Build ga_repo object and @conf
  30 + # to access gitolite-admin configuration
  31 + @conf = ga_repo.config
  32 +
  33 + # Do any changes
  34 + # in gitolite-admin
  35 + # config here
20 yield(self) 36 yield(self)
21 - push 37 +
  38 + # Save changes in
  39 + # gitolite-admin repo
  40 + # before pusht it
  41 + ga_repo.save
  42 +
  43 + # Push gitolite-admin repo
  44 + # to apply all changes
  45 + push(config_tmp_dir)
  46 +
  47 + # Remove tmp dir
  48 + # wiith gitolite-admin
22 FileUtils.rm_rf(config_tmp_dir) 49 FileUtils.rm_rf(config_tmp_dir)
23 ensure 50 ensure
  51 + # unlock so other task cann access
  52 + # gitolite configuration
24 f.flock(File::LOCK_UN) 53 f.flock(File::LOCK_UN)
25 end 54 end
26 end 55 end
27 end 56 end
28 rescue Exception => ex 57 rescue Exception => ex
29 - Gitlab::Logger.error(ex.message) 58 + Gitlab::Logger.error(ex.class.name + " " + ex.message)
30 raise Gitolite::AccessDenied.new("gitolite timeout") 59 raise Gitolite::AccessDenied.new("gitolite timeout")
31 end 60 end
32 61
33 def destroy_project(project) 62 def destroy_project(project)
34 FileUtils.rm_rf(project.path_to_repo) 63 FileUtils.rm_rf(project.path_to_repo)
35 -  
36 - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite'))  
37 - conf = ga_repo.config  
38 conf.rm_repo(project.path) 64 conf.rm_repo(project.path)
39 - ga_repo.save  
40 end 65 end
41 66
42 def destroy_project!(project) 67 def destroy_project!(project)
@@ -58,12 +83,8 @@ module Gitlab @@ -58,12 +83,8 @@ module Gitlab
58 83
59 # update or create 84 # update or create
60 def update_project(repo_name, project) 85 def update_project(repo_name, project)
61 - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite'))  
62 - conf = ga_repo.config  
63 repo = update_project_config(project, conf) 86 repo = update_project_config(project, conf)
64 conf.add_repo(repo, true) 87 conf.add_repo(repo, true)
65 -  
66 - ga_repo.save  
67 end 88 end
68 89
69 def update_project!(repo_name, project) 90 def update_project!(repo_name, project)
@@ -75,15 +96,10 @@ module Gitlab @@ -75,15 +96,10 @@ module Gitlab
75 # Updates many projects and uses project.path as the repo path 96 # Updates many projects and uses project.path as the repo path
76 # An order of magnitude faster than update_project 97 # An order of magnitude faster than update_project
77 def update_projects(projects) 98 def update_projects(projects)
78 - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite'))  
79 - conf = ga_repo.config  
80 -  
81 projects.each do |project| 99 projects.each do |project|
82 repo = update_project_config(project, conf) 100 repo = update_project_config(project, conf)
83 conf.add_repo(repo, true) 101 conf.add_repo(repo, true)
84 end 102 end
85 -  
86 - ga_repo.save  
87 end 103 end
88 104
89 def update_project_config(project, conf) 105 def update_project_config(project, conf)
@@ -118,9 +134,9 @@ module Gitlab @@ -118,9 +134,9 @@ module Gitlab
118 repo 134 repo
119 end 135 end
120 136
  137 + # Enable access to all repos for gitolite admin.
  138 + # We use it for accept merge request feature
121 def admin_all_repo 139 def admin_all_repo
122 - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite'))  
123 - conf = ga_repo.config  
124 owner_name = "" 140 owner_name = ""
125 141
126 # Read gitolite-admin user 142 # Read gitolite-admin user
@@ -144,7 +160,6 @@ module Gitlab @@ -144,7 +160,6 @@ module Gitlab
144 160
145 repo.add_permission("RW+", "", owner_name) 161 repo.add_permission("RW+", "", owner_name)
146 conf.add_repo(repo, true) 162 conf.add_repo(repo, true)
147 - ga_repo.save  
148 end 163 end
149 164
150 def admin_all_repo! 165 def admin_all_repo!
@@ -153,13 +168,13 @@ module Gitlab @@ -153,13 +168,13 @@ module Gitlab
153 168
154 private 169 private
155 170
156 - def pull  
157 - Dir.mkdir config_tmp_dir  
158 - `git clone #{Gitlab.config.gitolite_admin_uri} #{config_tmp_dir}/gitolite` 171 + def pull tmp_dir
  172 + Dir.mkdir tmp_dir
  173 + `git clone #{Gitlab.config.gitolite_admin_uri} #{tmp_dir}/gitolite`
159 end 174 end
160 175
161 - def push  
162 - Dir.chdir(File.join(config_tmp_dir, "gitolite")) 176 + def push tmp_dir
  177 + Dir.chdir(File.join(tmp_dir, "gitolite"))
163 `git add -A` 178 `git add -A`
164 `git commit -am "GitLab"` 179 `git commit -am "GitLab"`
165 `git push` 180 `git push`