Skip to content

Commit

Permalink
bugfixes to readonly/developer urls, and reformat gitosis.rb with tab…
Browse files Browse the repository at this point in the history
…s, cuz' I just like it that way
  • Loading branch information
root committed May 9, 2010
1 parent ff667ee commit 13a4717
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 107 deletions.
9 changes: 2 additions & 7 deletions app/views/projects/_redmine_gitosis.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
<div class="box">
<h3>Git Repository</h3>
<p>
<%= Gitosis::renderUrls(Setting.plugin_redmine_gitosis['readOnlyBaseUrls'], @project.id, true) %>
<%= Gitosis::renderUrls(Setting.plugin_redmine_gitosis['readOnlyBaseUrls'], @project.identifier, true) %>
</p>
<p>
<%= Gitosis::renderUrls(Setting.plugin_redmine_gitosis['developerBaseUrls'], @project.id, false) %>
<%= Gitosis::renderUrls(Setting.plugin_redmine_gitosis['developerBaseUrls'], @project.identifier, false) %>

</p>
<!--
<ul>
<li>Developer URL: <input style="width: 95%;" class="url-field" type="text" readonly="true" value="<%= Setting.plugin_redmine_gitosis['developerBaseUrl'] + @project.identifier %>.git"/></li>
</ul>
-->
</div>
<% end %>
10 changes: 5 additions & 5 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
description 'Enables Redmine to update a gitosis server.'
version '0.0.4'
settings :default => {
'gitosisUrl' => 'git@localhost:gitosis-admin.git',
'gitosisIdentityFile' => '/srv/projects/redmine/miner/.ssh/id_rsa',
'developerBaseUrls' => ['git@localhost:'],
'readOnlyBaseUrls' => [],
'basePath' => '/srv/projects/git/repositories/',
'gitosisUrl' => 'git@localhost:gitosis-admin.git',
'gitosisIdentityFile' => '/srv/projects/redmine/ook/.ssh/id_rsa',
'developerBaseUrls' => ['git@localhost:','https://[user]@localhost/git/'],
'readOnlyBaseUrls' => ["http://localhost/git/"],
'basePath' => '/srv/projects/git/repositories/',
},
:partial => 'redmine_gitosis'
end
Expand Down
185 changes: 90 additions & 95 deletions lib/gitosis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,105 @@
require 'net/ssh'

module Gitosis

def self.renderUrls(baseUrlList, projectId, isReadOnly)
rendered = ""
if(not defined?(baseUrlList.length))
return rendered
end
def self.renderUrls(baseUrlList, projectId, isReadOnly)
rendered = ""
if(not defined?(baseUrlList.length))
return rendered
end
if(baseUrlList.length == 0)
return rendered
end

if(baseUrlList.length == 0)
return rendered
end
rendered = rendered + "<strong>" + (isReadOnly ? "Read Only" : "Developer") + " " + (baseUrlList.length == 1 ? "URL" : "URLs") + ": </strong><br/>"
rendered = rendered + "<ul>";
for baseUrl in baseUrlList do
rendered = rendered + "<li>" + "<input style=\"width: 95%;\" class=\"url-field\" type=\"text\" readonly=\"true\" value=\"" + baseUrl + projectId + ".git\" /></li>"
end
rendered = rendered + "</ul>\n"
return rendered
end


def self.update_repositories(projects)
projects = (projects.is_a?(Array) ? projects : [projects])

lockfile=File.new(File.join(Dir.tmpdir,'redmine-gitosis_lock'),File::CREAT|File::RDONLY)
retries=2
loop do
break if lockfile.flock(File::LOCK_EX|File::LOCK_NB)
retries-=1
sleep 10
raise Lockfile::MaxTriesLockError if retries<=0
end
rendered = rendered + "<strong>" + (isReadOnly ? "Read Only" : "Developer") + " " + (baseUrlList.length == 1 ? "URL" : "URLs") + ": </strong><br/>"
rendered = rendered + "<ul>";
for baseUrl in baseUrlList do
rendered = rendered + "<li>" + "<input style=\"width: 95%;\" class=\"url-field\" type=\"text\" readonly=\"true\" value=\"" + baseUrl + projectId + ".git\" /></li>"
end
rendered = rendered + "</ul>\n"
return rendered
end

#Lockfile(File.join(Dir.tmpdir,'redmine-gitosis_lock'), :retries => 2, :sleep_inc=> 10) do
def self.update_repositories(projects)
projects = (projects.is_a?(Array) ? projects : [projects])

lockfile=File.new(File.join(Dir.tmpdir,'redmine-gitosis_lock'),File::CREAT|File::RDONLY)
retries=2
loop do
break if lockfile.flock(File::LOCK_EX|File::LOCK_NB)
retries-=1
sleep 10
raise Lockfile::MaxTriesLockError if retries<=0
end

# HANDLE GIT

# create tmp dir
local_dir = File.join(Dir.tmpdir,"redmine-gitosis-#{Time.now.to_i}")
# HANDLE GIT

Dir.mkdir local_dir
# create tmp dir
local_dir = File.join(Dir.tmpdir,"redmine-gitosis-#{Time.now.to_i}")

ssh_with_identity_file = File.join(local_dir, 'ssh_with_identity_file.sh')

File.open(ssh_with_identity_file, "w") do |f|
f.puts "#!/bin/bash"
f.puts "exec ssh -o stricthostkeychecking=no -i #{Setting.plugin_redmine_gitosis['gitosisIdentityFile']} \"$@\""
end
File.chmod(0755, ssh_with_identity_file)
ENV['GIT_SSH'] = ssh_with_identity_file

# clone repo
`env GIT_SSH=#{ssh_with_identity_file} git clone #{Setting.plugin_redmine_gitosis['gitosisUrl']} #{local_dir}/gitosis`
Dir.mkdir local_dir

changed = false

projects.select{|p| p.repository.is_a?(Repository::Git)}.each do |project|
# 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.gitosis_public_keys.active}.flatten.compact.uniq.each do |key|
File.open(File.join(local_dir, 'gitosis/keydir',"#{key.identifier}.pub"), 'w') {|f| f.write(key.key.gsub(/\n/,'')) }
end
ssh_with_identity_file = File.join(local_dir, 'ssh_with_identity_file.sh')

File.open(ssh_with_identity_file, "w") do |f|
f.puts "#!/bin/bash"
f.puts "exec ssh -o stricthostkeychecking=no -i #{Setting.plugin_redmine_gitosis['gitosisIdentityFile']} \"$@\""
end
File.chmod(0755, ssh_with_identity_file)
ENV['GIT_SSH'] = ssh_with_identity_file

# clone repo
`env GIT_SSH=#{ssh_with_identity_file} git clone #{Setting.plugin_redmine_gitosis['gitosisUrl']} #{local_dir}/gitosis`

# delete inactives
users.map{|u| u.gitosis_public_keys.inactive}.flatten.compact.uniq.each do |key|
File.unlink(File.join(local_dir, 'gitosis/keydir',"#{key.identifier}.pub")) rescue nil
end

# write config file
conf = IniFile.new(File.join(local_dir,'gitosis','gitosis.conf'))
original = conf.clone
name = "#{project.identifier}"

conf["group #{name}_readonly"]['readonly'] = name
conf["group #{name}_readonly"]['members'] = read_users.map{|u| u.gitosis_public_keys.active}.flatten.map{ |key| "#{key.identifier}" }.join(' ')

conf["group #{name}"]['writable'] = name
conf["group #{name}"]['members'] = write_users.map{|u| u.gitosis_public_keys.active}.flatten.map{ |key| "#{key.identifier}" }.join(' ')
unless conf.eql?(original)
conf.write
changed = true
end
changed = false

projects.select{|p| p.repository.is_a?(Repository::Git)}.each do |project|
# 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.gitosis_public_keys.active}.flatten.compact.uniq.each do |key|
File.open(File.join(local_dir, 'gitosis/keydir',"#{key.identifier}.pub"), 'w') {|f| f.write(key.key.gsub(/\n/,'')) }
end

end
if changed
# add, commit, push, and remove local tmp dir
`cd #{File.join(local_dir,'gitosis')} ; git add keydir/* gitosis.conf`
`cd #{File.join(local_dir,'gitosis')} ; git config user.email '#{Setting.mail_from}'`
`cd #{File.join(local_dir,'gitosis')} ; git config user.name 'Redmine'`
`cd #{File.join(local_dir,'gitosis')} ; git commit -a -m 'updated by Redmine Gitosis'`
`cd #{File.join(local_dir,'gitosis')} ; git push`
end

# remove local copy
`rm -Rf #{local_dir}`
# delete inactives
users.map{|u| u.gitosis_public_keys.inactive}.flatten.compact.uniq.each do |key|
File.unlink(File.join(local_dir, 'gitosis/keydir',"#{key.identifier}.pub")) rescue nil
end

# write config file
conf = IniFile.new(File.join(local_dir,'gitosis','gitosis.conf'))
original = conf.clone
name = "#{project.identifier}"

conf["group #{name}_readonly"]['readonly'] = name
conf["group #{name}_readonly"]['members'] = read_users.map{|u| u.gitosis_public_keys.active}.flatten.map{ |key| "#{key.identifier}" }.join(' ')

conf["group #{name}"]['writable'] = name
conf["group #{name}"]['members'] = write_users.map{|u| u.gitosis_public_keys.active}.flatten.map{ |key| "#{key.identifier}" }.join(' ')
unless conf.eql?(original)
conf.write
changed = true
end


#end
lockfile.flock(File::LOCK_UN)


end

end
if changed
# add, commit, push, and remove local tmp dir
`cd #{File.join(local_dir,'gitosis')} ; git add keydir/* gitosis.conf`
`cd #{File.join(local_dir,'gitosis')} ; git config user.email '#{Setting.mail_from}'`
`cd #{File.join(local_dir,'gitosis')} ; git config user.name 'Redmine'`
`cd #{File.join(local_dir,'gitosis')} ; git commit -a -m 'updated by Redmine Gitosis'`
`cd #{File.join(local_dir,'gitosis')} ; git push`
end

# remove local copy
`rm -Rf #{local_dir}`

lockfile.flock(File::LOCK_UN)

end

end

0 comments on commit 13a4717

Please sign in to comment.