Commit 963746f97847cd106e5f90f830fe9dc0b5ae0102
1 parent
b994a65f
Exists in
master
and in
4 other branches
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 | 4 | |
5 | 5 | module Gitlab |
6 | 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 | 15 | end |
12 | 16 | |
13 | 17 | def apply |
14 | 18 | Timeout::timeout(30) do |
15 | 19 | File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| |
16 | 20 | begin |
21 | + # Set exclusive lock | |
22 | + # to prevent race condition | |
17 | 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 | 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 | 49 | FileUtils.rm_rf(config_tmp_dir) |
23 | 50 | ensure |
51 | + # unlock so other task cann access | |
52 | + # gitolite configuration | |
24 | 53 | f.flock(File::LOCK_UN) |
25 | 54 | end |
26 | 55 | end |
27 | 56 | end |
28 | 57 | rescue Exception => ex |
29 | - Gitlab::Logger.error(ex.message) | |
58 | + Gitlab::Logger.error(ex.class.name + " " + ex.message) | |
30 | 59 | raise Gitolite::AccessDenied.new("gitolite timeout") |
31 | 60 | end |
32 | 61 | |
33 | 62 | def destroy_project(project) |
34 | 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 | 64 | conf.rm_repo(project.path) |
39 | - ga_repo.save | |
40 | 65 | end |
41 | 66 | |
42 | 67 | def destroy_project!(project) |
... | ... | @@ -58,12 +83,8 @@ module Gitlab |
58 | 83 | |
59 | 84 | # update or create |
60 | 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 | 86 | repo = update_project_config(project, conf) |
64 | 87 | conf.add_repo(repo, true) |
65 | - | |
66 | - ga_repo.save | |
67 | 88 | end |
68 | 89 | |
69 | 90 | def update_project!(repo_name, project) |
... | ... | @@ -75,15 +96,10 @@ module Gitlab |
75 | 96 | # Updates many projects and uses project.path as the repo path |
76 | 97 | # An order of magnitude faster than update_project |
77 | 98 | def update_projects(projects) |
78 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite')) | |
79 | - conf = ga_repo.config | |
80 | - | |
81 | 99 | projects.each do |project| |
82 | 100 | repo = update_project_config(project, conf) |
83 | 101 | conf.add_repo(repo, true) |
84 | 102 | end |
85 | - | |
86 | - ga_repo.save | |
87 | 103 | end |
88 | 104 | |
89 | 105 | def update_project_config(project, conf) |
... | ... | @@ -118,9 +134,9 @@ module Gitlab |
118 | 134 | repo |
119 | 135 | end |
120 | 136 | |
137 | + # Enable access to all repos for gitolite admin. | |
138 | + # We use it for accept merge request feature | |
121 | 139 | def admin_all_repo |
122 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite')) | |
123 | - conf = ga_repo.config | |
124 | 140 | owner_name = "" |
125 | 141 | |
126 | 142 | # Read gitolite-admin user |
... | ... | @@ -144,7 +160,6 @@ module Gitlab |
144 | 160 | |
145 | 161 | repo.add_permission("RW+", "", owner_name) |
146 | 162 | conf.add_repo(repo, true) |
147 | - ga_repo.save | |
148 | 163 | end |
149 | 164 | |
150 | 165 | def admin_all_repo! |
... | ... | @@ -153,13 +168,13 @@ module Gitlab |
153 | 168 | |
154 | 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 | 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 | 178 | `git add -A` |
164 | 179 | `git commit -am "GitLab"` |
165 | 180 | `git push` | ... | ... |