forked from ericpaulbishop/redmine_git_hosting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_hosting.rb
executable file
·515 lines (424 loc) · 17.7 KB
/
git_hosting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
require 'lockfile'
require 'net/ssh'
require 'tempfile'
require 'tmpdir'
require 'gitolite_conf.rb'
require 'git_adapter_hooks.rb'
module GitHosting
def self.logger
# it may be useful to redefine this for some debugging purposes
# but by default, we're just going to use the default Rails logger
return Rails.logger
end
@@web_user = nil
def self.web_user
if @@web_user.nil?
@@web_user = (%x[whoami]).chomp.strip
end
return @@web_user
end
def self.git_user
Setting.plugin_redmine_git_hosting['gitUser']
end
@@mirror_pubkey = nil
def self.mirror_push_public_key
if @@mirror_pubkey.nil?
%x[cat '#{Setting.plugin_redmine_git_hosting['gitoliteIdentityFile']}' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/gitolite_admin_id_rsa ' ]
%x[cat '#{Setting.plugin_redmine_git_hosting['gitoliteIdentityPublicKeyFile']}' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/gitolite_admin_id_rsa.pub ' ]
%x[ #{GitHosting.git_user_runner} 'chmod 600 ~/.ssh/gitolite_admin_id_rsa' ]
%x[ #{GitHosting.git_user_runner} 'chmod 644 ~/.ssh/gitolite_admin_id_rsa.pub' ]
pubk = ( %x[cat '#{Setting.plugin_redmine_git_hosting['gitoliteIdentityPublicKeyFile']}' ] ).chomp.strip
git_user_dir = ( %x[ #{GitHosting.git_user_runner} "cd ~ ; pwd" ] ).chomp.strip
%x[ #{GitHosting.git_user_runner} 'echo "#{pubk}" > ~/.ssh/gitolite_admin_id_rsa.pub ' ]
%x[ echo '#!/bin/sh' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/run_gitolite_admin_ssh']
%x[ echo 'exec ssh -o BatchMode=yes -o StrictHostKeyChecking=no -i #{git_user_dir}/.ssh/gitolite_admin_id_rsa "$@"' | #{GitHosting.git_user_runner} "cat >> ~/.ssh/run_gitolite_admin_ssh" ]
%x[ #{GitHosting.git_user_runner} 'chmod 644 ~/.ssh/gitolite_admin_id_rsa.pub' ]
%x[ #{GitHosting.git_user_runner} 'chmod 600 ~/.ssh/gitolite_admin_id_rsa']
%x[ #{GitHosting.git_user_runner} 'chmod 700 ~/.ssh/run_gitolite_admin_ssh']
@@mirror_pubkey = pubk.split(/[\t ]+/)[0].to_s + " " + pubk.split(/[\t ]+/)[1].to_s
#settings = Setting["plugin_redmine_git_hosting"]
#settings["gitMirrorPushPublicKey"] = publicKey
#Setting["plugin_redmine_git_hosting"] = settings
end
@@mirror_pubkey
end
@@sudo_git_to_web_user_stamp = nil
@@sudo_git_to_web_user_cached = nil
def self.sudo_git_to_web_user
if not @@sudo_git_to_web_user_cached.nil? and (Time.new - @@sudo_git_to_web_user_stamp <= 0.5):
return @@sudo_git_to_web_user_cached
end
logger.info "Testing if git user(\"#{git_user}\") can sudo to web user(\"#{web_user}\")"
if git_user == web_user
@@sudo_git_to_web_user_cached = true
@@sudo_git_to_web_user_stamp = Time.new
return @@sudo_git_to_web_user_cached
end
test = %x[#{GitHosting.git_user_runner} sudo -nu #{web_user} echo "yes" ]
if test.match(/yes/)
@@sudo_git_to_web_user_cached = true
@@sudo_git_to_web_user_stamp = Time.new
return @@sudo_git_to_web_user_cached
end
logger.warn "Error while testing sudo_git_to_web_user: #{test}"
@@sudo_git_to_web_user_cached = test
@@sudo_git_to_web_user_stamp = Time.new
return @@sudo_git_to_web_user_cached
end
@@sudo_web_to_git_user_stamp = nil
@@sudo_web_to_git_user_cached = nil
def self.sudo_web_to_git_user
if not @@sudo_web_to_git_user_cached.nil? and (Time.new - @@sudo_web_to_git_user_stamp <= 0.5):
return @@sudo_web_to_git_user_cached
end
logger.info "Testing if web user(\"#{web_user}\") can sudo to git user(\"#{git_user}\")"
if git_user == web_user
@@sudo_web_to_git_user_cached = true
@@sudo_web_to_git_user_stamp = Time.new
return @@sudo_web_to_git_user_cached
end
test = %x[sudo -nu #{git_user} echo "yes"]
if test.match(/yes/)
@@sudo_web_to_git_user_cached = true
@@sudo_web_to_git_user_stamp = Time.new
return @@sudo_web_to_git_user_cached
end
logger.warn "Error while testing sudo_web_to_git_user: #{test}"
@@sudo_web_to_git_user_cached = test
@@sudo_web_to_git_user_stamp = Time.new
return @@sudo_web_to_git_user_cached
end
def self.get_full_parent_path(project, is_file_path)
parent_parts = [];
p = project
while p.parent
parent_id = p.parent.identifier.to_s
parent_parts.unshift(parent_id)
p = p.parent
end
return is_file_path ? File.join(parent_parts) : parent_parts.join("/")
end
def self.repository_name project
#return "#{get_full_parent_path(project, false)}/#{project.identifier}".sub(/^\//, "")
#return only the project identifier irrespective of parent of the project to form git url like git@server:subproject.git
return "#{project.identifier}".sub(/^\//, "")
end
def self.repository_path project
return File.join(Setting.plugin_redmine_git_hosting['gitRepositoryBasePath'], repository_name(project)) + ".git"
end
def self.add_route_for_project(p)
if defined? map
add_route_for_project_with_map p, map
else
ActionController::Routing::Routes.draw do |map|
add_route_for_project_with_map p, map
end
end
end
def self.add_route_for_project_with_map(p,m)
repo = p.repository
if repo.is_a?(Repository::Git)
#repo_name= p.parent ? File.join(GitHosting::get_full_parent_path(p, true),p.identifier) : p.identifier
#Modified repo_name to get the subproject in repositories folder i.e git@server:subproject.git
repo_name= p.identifier
repo_path = repo_name + ".git"
m.connect repo_path, :controller => 'git_http', :p1 => '', :p2 =>'', :p3 =>'', :id=>"#{p[:identifier]}", :path=>"#{repo_path}"
m.connect repo_path, :controller => 'git_http', :p1 => '', :p2 =>'', :p3 =>'', :id=>"#{p[:identifier]}", :path=>"#{repo_path}"
m.connect repo_path + "/:p1", :controller => 'git_http', :p2 => '', :p3 =>'', :id=>"#{p[:identifier]}", :path=>"#{repo_path}"
m.connect repo_path + "/:p1/:p2", :controller => 'git_http', :p3 => '', :id=>"#{p[:identifier]}", :path=>"#{repo_path}"
m.connect repo_path + "/:p1/:p2/:p3", :controller => 'git_http', :id=>"#{p[:identifier]}", :path=>"#{repo_path}"
end
end
def self.get_tmp_dir
@@git_hosting_tmp_dir ||= File.join(Dir.tmpdir, "redmine_git_hosting")
if !File.directory?(@@git_hosting_tmp_dir)
%x[mkdir -p "#{@@git_hosting_tmp_dir}"]
%x[chmod 700 "#{@@git_hosting_tmp_dir}"]
%x[chown #{web_user} "#{@@git_hosting_tmp_dir}"]
end
return @@git_hosting_tmp_dir
end
def self.git_exec_path
return File.join(get_tmp_dir(), "run_git_as_git_user")
end
def self.gitolite_ssh_path
return File.join(get_tmp_dir(), "gitolite_admin_ssh")
end
def self.git_user_runner_path
return File.join(get_tmp_dir(), "run_as_git_user")
end
def self.git_exec
if !File.exists?(git_exec_path())
update_git_exec
end
return git_exec_path()
end
def self.gitolite_ssh
if !File.exists?(gitolite_ssh_path())
update_git_exec
end
return gitolite_ssh_path()
end
def self.git_user_runner
if !File.exists?(git_user_runner_path())
update_git_exec
end
return git_user_runner_path()
end
def self.update_git_exec
logger.info "Setting up #{get_tmp_dir()}"
gitolite_key=Setting.plugin_redmine_git_hosting['gitoliteIdentityFile']
File.open(gitolite_ssh_path(), "w") do |f|
f.puts "#!/bin/sh"
f.puts "exec ssh -o BatchMode=yes -o StrictHostKeyChecking=no -i #{gitolite_key} \"$@\""
end if !File.exists?(gitolite_ssh_path())
##############################################################################################################################
# So... older versions of sudo are completely different than newer versions of sudo
# Try running sudo -i [user] 'ls -l' on sudo > 1.7.4 and you get an error that command 'ls -l' doesn't exist
# do it on version < 1.7.3 and it runs just fine. Different levels of escaping are necessary depending on which
# version of sudo you are using... which just completely CRAZY, but I don't know how to avoid it
#
# Note: I don't know whether the switch is at 1.7.3 or 1.7.4, the switch is between ubuntu 10.10 which uses 1.7.2
# and ubuntu 11.04 which uses 1.7.4. I have tested that the latest 1.8.1p2 seems to have identical behavior to 1.7.4
##############################################################################################################################
sudo_version_str=%x[ sudo -V 2>&1 | head -n1 | sed 's/^.* //g' | sed 's/[a-z].*$//g' ]
split_version = sudo_version_str.split(/\./)
sudo_version = 100*100*(split_version[0].to_i) + 100*(split_version[1].to_i) + split_version[2].to_i
sudo_version_switch = (100*100*1) + (100 * 7) + 3
File.open(git_exec_path(), "w") do |f|
f.puts '#!/bin/sh'
f.puts "if [ \"\$(whoami)\" = \"#{git_user}\" ] ; then"
f.puts ' cmd=$(printf "\\"%s\\" " "$@")'
f.puts ' cd ~'
f.puts ' eval "git $cmd"'
f.puts "else"
if sudo_version < sudo_version_switch
f.puts ' cmd=$(printf "\\\\\\"%s\\\\\\" " "$@")'
f.puts " sudo -u #{git_user} -i eval \"git $cmd\""
else
f.puts ' cmd=$(printf "\\"%s\\" " "$@")'
f.puts " sudo -u #{git_user} -i eval \"git $cmd\""
end
f.puts 'fi'
end if !File.exists?(git_exec_path())
# use perl script for git_user_runner so we can
# escape output more easily
File.open(git_user_runner_path(), "w") do |f|
f.puts '#!/usr/bin/perl'
f.puts ''
f.puts 'my $command = join(" ", @ARGV);'
f.puts ''
f.puts 'my $user = `whoami`;'
f.puts 'chomp $user;'
f.puts 'if ($user eq "' + git_user + '")'
f.puts '{'
f.puts ' exec("cd ~ ; $command");'
f.puts '}'
f.puts 'else'
f.puts '{'
f.puts ' $command =~ s/\\\\/\\\\\\\\/g;'
f.puts ' $command =~ s/"/\\\\"/g;'
f.puts ' exec("sudo -u ' + git_user + ' -i eval \"$command\"");'
f.puts '}'
end if !File.exists?(git_user_runner_path())
File.chmod(0550, git_exec_path())
File.chmod(0550, gitolite_ssh_path())
File.chmod(0550, git_user_runner_path())
end
@@lock_file = nil
def self.lock(retries)
is_locked = false
local_dir = get_tmp_dir()
if @@lock_file.nil?
@@lock_file=File.new(File.join(local_dir,'redmine_git_hosting_lock'),File::CREAT|File::RDONLY)
end
while retries > 0
is_locked = @@lock_file.flock(File::LOCK_EX|File::LOCK_NB)
retries-=1
if (!is_locked) && retries > 0
sleep 1
end
end
return is_locked
end
def self.unlock
if !@@lock_file.nil?
@@lock_file.flock(File::LOCK_UN)
end
end
def self.clone_or_pull_gitolite_admin
# clone/pull from admin repo
local_dir = get_tmp_dir()
if File.exists? "#{local_dir}/gitolite-admin"
logger.info "Fethcing changes for #{local_dir}/gitolite-admin"
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' fetch]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' merge FETCH_HEAD]
else
logger.info "Cloning gitolite-admin repository"
%x[env GIT_SSH=#{gitolite_ssh()} git clone #{git_user}@#{Setting.plugin_redmine_git_hosting['gitServer']}:gitolite-admin.git #{local_dir}/gitolite-admin]
end
%x[chmod 700 "#{local_dir}/gitolite-admin" ]
# Make sure we have our hooks setup
GitAdapterHooks.check_hooks_installed
end
def self.move_repository(old_name, new_name)
old_path = File.join(Setting.plugin_redmine_git_hosting['gitRepositoryBasePath'], "#{old_name}.git")
new_path = File.join(Setting.plugin_redmine_git_hosting['gitRepositoryBasePath'], "#{new_name}.git")
# create tmp dir, return cleanly if, for some reason, we don't have proper permissions
local_dir = get_tmp_dir()
#lock
if !lock(5)
return
end
# Make sure we have gitoite-admin cloned
clone_or_pull_gitolite_admin
# rename in conf file
conf = GitoliteConfig.new(File.join(local_dir, 'gitolite-admin', 'conf', 'gitolite.conf'))
conf.rename_repo( old_name, new_name )
conf.save
# physicaly move the repo BEFORE committing/pushing conf changes to gitolite admin repo
%x[#{git_user_runner} 'mkdir -p "#{new_path}"']
%x[#{git_user_runner} 'rmdir "#{new_path}"']
%x[#{git_user_runner} 'mv "#{old_path}" "#{new_path}"']
# commit / push changes to gitolite admin repo
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' add keydir/*]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' add conf/gitolite.conf]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' config user.email '#{Setting.mail_from}']
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' config user.name 'Redmine']
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' commit -a -m 'updated by Redmine' ]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' push ]
# unlock
unlock()
end
@@recursionCheck = false
def self.update_repositories(projects, is_repo_delete)
if(defined?(@@recursionCheck))
if(@@recursionCheck)
return
end
end
@@recursionCheck = true
logger.debug "Updating repositories..."
projects = (projects.is_a?(Array) ? projects : [projects])
# Don't bother doing anything if none of the projects we've been handed have a Git repository
unless projects.detect{|p| p.repository.is_a?(Repository::Git) }.nil?
#lock
if !lock(5)
@@recursionCheck = false
return
end
# Make sure we have gitoite-admin cloned
clone_or_pull_gitolite_admin
local_dir = get_tmp_dir()
conf = GitoliteConfig.new(File.join(local_dir, 'gitolite-admin', 'conf', 'gitolite.conf'))
orig_repos = conf.all_repos
new_repos = []
new_projects = []
changed = false
projects.select{|p| p.repository.is_a?(Repository::Git)}.each do |project|
repo_name = repository_name(project)
#check for delete -- if delete we can just
#delete repo, and ignore updating users/public keys
if is_repo_delete
if Setting.plugin_redmine_git_hosting['deleteGitRepositories'] == "true"
conf.delete_repo(repo_name)
end
else
#check whether we're adding a new repo
if orig_repos[ repo_name ] == nil
changed = true
add_route_for_project(project)
new_repos.push repo_name
new_projects.push project
end
# fetch users
users = project.member_principals.map(&:user).compact.uniq
write_users = users.select{ |user| user.allowed_to?( :commit_access, project ) }
read_users = users.select{ |user| user.allowed_to?( :view_changesets, project ) && !user.allowed_to?( :commit_access, project ) }
# write key files
users.map{|u| u.gitolite_public_keys.active}.flatten.compact.uniq.each do |key|
filename = File.join(local_dir, 'gitolite-admin/keydir',"#{key.identifier}.pub")
unless File.exists? filename
File.open(filename, 'w') {|f| f.write(key.key.gsub(/\n/,'')) }
changed = true
end
end
# delete inactives
users.map{|u| u.gitolite_public_keys.inactive}.flatten.compact.uniq.each do |key|
filename = File.join(local_dir, 'gitolite-admin/keydir',"#{key.identifier}.pub")
if File.exists? filename
%x[git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' rm keydir/#{key.identifier}.pub]
changed = true
GitolitePublicKey.destroy(key.id)
end
end
# update users
read_user_keys = []
write_user_keys = []
read_users.map{|u| u.gitolite_public_keys.active}.flatten.compact.uniq.each do |key|
read_user_keys.push key.identifier
end
write_users.map{|u| u.gitolite_public_keys.active}.flatten.compact.uniq.each do |key|
write_user_keys.push key.identifier
end
#git daemon
if (project.repository.extra.git_daemon == 1 || project.repository.extra.git_daemon == nil ) && project.is_public
read_user_keys.push "daemon"
end
conf.set_read_user repo_name, read_user_keys
conf.set_write_user repo_name, write_user_keys
end
end
if conf.changed?
conf.save
changed = true
end
if changed
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' add keydir/*]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' add conf/gitolite.conf]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' config user.email '#{Setting.mail_from}']
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' config user.name 'Redmine']
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' commit -a -m 'updated by Redmine' ]
%x[env GIT_SSH=#{gitolite_ssh()} git --git-dir='#{local_dir}/gitolite-admin/.git' --work-tree='#{local_dir}/gitolite-admin' push ]
end
# Set post recieve hooks for new projects
# We need to do this AFTER push, otherwise necessary repos may not be created yet
if new_projects.length > 0
GitAdapterHooks.setup_hooks(new_projects)
end
unlock()
end
@@recursionCheck = false
end
def self.clear_cache_for_project(project)
if project.is_a?(Project)
project = project.identifier
end
# Clear cache
old_cached=GitCache.find_all_by_proj_identifier(project)
if old_cached != nil
old_ids = old_cached.collect(&:id)
GitCache.destroy(old_ids)
end
end
def self.check_hooks_installed
installed = false
if lock(5)
installed = GitAdapterHooks.check_hooks_installed
unlock()
end
installed
end
def self.setup_hooks(projects=nil)
if lock(5)
GitAdapterHooks.setup_hooks(projects)
unlock()
end
end
def self.update_global_hook_params
if lock(5)
GitAdapterHooks.update_global_hook_params
unlock()
end
end
end