gitolite & gitosis support

This commit is contained in:
Dmitriy Zaporozhets 2011-12-04 01:44:59 +02:00
parent c03bc6e291
commit 9e089efe5a
14 changed files with 200 additions and 98 deletions

View file

@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery
helper_method :abilities, :can? helper_method :abilities, :can?
rescue_from Gitosis::AccessDenied do |exception| rescue_from Gitlabhq::Gitosis::AccessDenied, Gitlabhq::Gitolite::AccessDenied do |exception|
render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false
end end

View file

@ -42,7 +42,7 @@ class ProjectsController < ApplicationController
format.js format.js
end end
end end
rescue Gitosis::AccessDenied rescue Gitlabhq::Gitosis::AccessDenied, Gitlabhq::Gitolite::AccessDenied
render :js => "location.href = '#{errors_gitosis_path}'" and return render :js => "location.href = '#{errors_gitosis_path}'" and return
rescue StandardError => ex rescue StandardError => ex
@project.errors.add(:base, "Cant save project. Please try again later") @project.errors.add(:base, "Cant save project. Please try again later")

View file

@ -19,7 +19,7 @@ class Key < ActiveRecord::Base
end end
def update_gitosis def update_gitosis
Gitosis.new.configure do |c| GitoProxy.system.new.configure do |c|
c.update_keys(identifier, key) c.update_keys(identifier, key)
projects.each do |project| projects.each do |project|
@ -29,7 +29,7 @@ class Key < ActiveRecord::Base
end end
def gitosis_delete_key def gitosis_delete_key
Gitosis.new.configure do |c| GitoProxy.system.new.configure do |c|
c.delete_key(identifier) c.delete_key(identifier)
projects.each do |project| projects.each do |project|

View file

@ -22,25 +22,25 @@ class Repository
end end
def url_to_repo def url_to_repo
if !GITOSIS["port"] or GITOSIS["port"] == 22 if !GIT_HOST["port"] or GIT_HOST["port"] == 22
"#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git" "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{path}.git"
else else
"ssh://#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{GITOSIS["port"]}/#{path}.git" "ssh://#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{GIT_HOST["port"]}/#{path}.git"
end end
end end
def path_to_repo def path_to_repo
GITOSIS["base_path"] + path + ".git" GIT_HOST["base_path"] + path + ".git"
end end
def update_gitosis_project def update_gitosis_project
Gitosis.new.configure do |c| GitProxy.system.new.configure do |c|
c.update_project(path, project.gitosis_writers) c.update_project(path, project.gitosis_writers)
end end
end end
def destroy_gitosis_project def destroy_gitosis_project
Gitosis.new.configure do |c| GitProxy.system.new.configure do |c|
c.destroy_project(@project) c.destroy_project(@project)
end end
end end

View file

@ -20,13 +20,13 @@
%tr %tr
%td %td
.left= f.label :path .left= f.label :path
%cite.right= "git@#{GITOSIS["host"]}:" %cite.right= "git@#{GIT_HOST["host"]}:"
%td %td
= f.text_field :path, :placeholder => "example_project", :disabled => !@project.new_record? = f.text_field :path, :placeholder => "example_project", :disabled => !@project.new_record?
%tr %tr
%td %td
.left= f.label :code .left= f.label :code
%cite.right= "http://#{GITOSIS["host"]}/" %cite.right= "http://#{GIT_HOST["host"]}/"
%td= f.text_field :code, :placeholder => "example" %td= f.text_field :code, :placeholder => "example"
%tr %tr

View file

@ -4,4 +4,4 @@ require File.expand_path('../application', __FILE__)
# Initialize the rails application # Initialize the rails application
Gitlab::Application.initialize! Gitlab::Application.initialize!
require File.join(Rails.root, "lib", "gitosis") require File.join(Rails.root, "lib", "gitlabhq", "git_host")

View file

@ -7,7 +7,8 @@ email:
host: gitlabhq.com host: gitlabhq.com
# Gitosis congiguration # Gitosis congiguration
gitosis: git_host:
system: gitolite# or gitosis
admin_uri: git@localhost:gitolite-admin admin_uri: git@localhost:gitolite-admin
base_path: /home/git/repositories/ base_path: /home/git/repositories/
host: localhost host: localhost

View file

@ -1,3 +1,3 @@
GITOSIS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["gitosis"] GIT_HOST = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git_host"]
EMAIL_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["email"] EMAIL_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["email"]
GIT_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git"] GIT_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git"]

5
lib/.directory Normal file
View file

@ -0,0 +1,5 @@
[Dolphin]
AdditionalInfoV2=Details_Size,Details_Date,CustomizedDetails
Timestamp=2011,12,4,1,34,13
Version=2
ViewMode=1

5
lib/gitlabhq/.directory Normal file
View file

@ -0,0 +1,5 @@
[Dolphin]
AdditionalInfoV2=Details_Size,Details_Date,CustomizedDetails
Timestamp=2011,12,4,1,34,17
Version=2
ViewMode=1

18
lib/gitlabhq/git_host.rb Normal file
View file

@ -0,0 +1,18 @@
require File.join(Rails.root, "lib", "gitlabhq", "gitolite")
require File.join(Rails.root, "lib", "gitlabhq", "gitosis")
module Gitlabhq
class GitHost
def self.system
if GIT_HOST["system"] == "gitolite"
Gitlabhq::Gitolite
else
Gitlabhq::Gitosis
end
end
def self.admin_uri
GIT_HOST["admin_uri"]
end
end
end

80
lib/gitlabhq/gitolite.rb Normal file
View file

@ -0,0 +1,80 @@
require 'gitolite'
require 'timeout'
require 'fileutils'
module Gitlabhq
class Gitolite
class AccessDenied < StandardError; end
def pull
# create tmp dir
@local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}")
Dir.mkdir @local_dir
`git clone #{GitHost.admin_uri} #{@local_dir}/gitolite`
end
def push
Dir.chdir(File.join(@local_dir, "gitolite"))
`git add -A`
`git commit -am "Gitlab"`
`git push`
Dir.chdir(Rails.root)
FileUtils.rm_rf(@local_dir)
end
def configure
status = Timeout::timeout(20) do
File.open(File.join(Dir.tmpdir,"gitlabhq-gitolite.lock"), "w+") do |f|
begin
f.flock(File::LOCK_EX)
pull
yield(self)
push
ensure
f.flock(File::LOCK_UN)
end
end
end
rescue Exception => ex
raise Gitolite::AccessDenied.new("gitolite timeout")
end
def destroy_project(project)
`sudo -u git rm -rf #{project.path_to_repo}`
ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite'))
conf = ga_repo.config
conf.rm_repo(project.path)
ga_repo.save
end
#update or create
def update_keys(user, key)
File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) }
end
def delete_key(user)
File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"))
`cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub`
end
# update or create
def update_project(repo_name, name_writers)
ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite'))
conf = ga_repo.config
repo = if conf.has_repo?(repo_name)
conf.get_repo(repo_name)
else
::Gitolite::Config::Repo.new(repo_name)
end
repo.add_permission("RW+", "", name_writers) unless name_writers.blank?
conf.add_repo(repo)
ga_repo.save
end
end
end

76
lib/gitlabhq/gitosis.rb Normal file
View file

@ -0,0 +1,76 @@
require 'inifile'
require 'timeout'
require 'fileutils'
module Gitlabhq
class Gitosis
class AccessDenied < StandardError; end
def pull
# create tmp dir
@local_dir = File.join(Dir.tmpdir,"gitlabhq-gitosis-#{Time.now.to_i}")
Dir.mkdir @local_dir
`git clone #{GitHost.admin_uri} #{@local_dir}/gitosis`
end
def push
Dir.chdir(File.join(@local_dir, "gitosis"))
`git add -A`
`git commit -am "Gitlab"`
`git push`
Dir.chdir(Rails.root)
FileUtils.rm_rf(@local_dir)
end
def configure
status = Timeout::timeout(20) do
File.open(File.join(Dir.tmpdir,"gitlabhq-gitosis.lock"), "w+") do |f|
begin
f.flock(File::LOCK_EX)
pull
yield(self)
push
ensure
f.flock(File::LOCK_UN)
end
end
end
rescue Exception => ex
raise Gitosis::AccessDenied.new("gitosis timeout")
end
def destroy_project(project)
`sudo -u git rm -rf #{project.path_to_repo}`
conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf'))
conf.delete_section("group #{project.path}")
conf.write
end
#update or create
def update_keys(user, key)
File.open(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) }
end
def delete_key(user)
File.unlink(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"))
`cd #{File.join(@local_dir,'gitosis')} ; git rm keydir/#{user}.pub`
end
#update or create
def update_project(repo_name, name_writers)
# write config file
conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf'))
conf["group #{repo_name}"]['writable'] = repo_name
conf["group #{repo_name}"]['members'] = name_writers.join(' ')
conf.write
end
end
end

View file

@ -1,83 +0,0 @@
require 'gitolite'
require 'inifile'
require 'timeout'
require 'fileutils'
class Gitosis
class AccessDenied < StandardError; end
def pull
# create tmp dir
@local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}")
Dir.mkdir @local_dir
`git clone #{GITOSIS['admin_uri']} #{@local_dir}/gitolite`
end
def push
Dir.chdir(File.join(@local_dir, "gitolite"))
`git add -A`
`git commit -am "Gitlab"`
`git push`
Dir.chdir(Rails.root)
FileUtils.rm_rf(@local_dir)
end
def configure
status = Timeout::timeout(20) do
File.open(File.join(Dir.tmpdir,"gitlabhq-gitolite.lock"), "w+") do |f|
begin
f.flock(File::LOCK_EX)
pull
yield(self)
push
ensure
f.flock(File::LOCK_UN)
end
end
end
#rescue Exception => ex
#raise Gitosis::AccessDenied.new("gitolite timeout")
end
def destroy_project(project)
`sudo -u git rm -rf #{project.path_to_repo}`
conf = IniFile.new(File.join(@local_dir,'gitolite', 'conf', 'gitolite.conf'))
conf.delete_section("group #{project.path}")
conf.write
end
#update or create
def update_keys(user, key)
File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) }
end
def delete_key(user)
File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"))
`cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub`
end
#update or create
def update_project(repo_name, name_writers)
ga_repo = Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite'))
conf = ga_repo.config
repo = if conf.has_repo?(repo_name)
conf.get_repo(repo_name)
else
Gitolite::Config::Repo.new(repo_name)
end
repo.add_permission("RW+", "", name_writers) unless name_writers.blank?
conf.add_repo(repo)
ga_repo.save
end
end