Merge branch 'gitolite'

This commit is contained in:
Dmitriy Zaporozhets 2011-12-05 19:27:51 +02:00
commit e14a0eb8d0
28 changed files with 283 additions and 152 deletions

View file

@ -9,6 +9,7 @@ gem "kaminari"
gem "haml-rails"
gem "jquery-rails"
gem "grit", :git => "https://github.com/gitlabhq/grit.git"
gem "gitolite", :git => "https://github.com/gitlabhq/gitolite.git"
gem "carrierwave"
gem "six"
gem "therubyracer"

View file

@ -4,6 +4,14 @@ GIT
specs:
annotate (2.4.1.beta1)
GIT
remote: https://github.com/gitlabhq/gitolite.git
revision: 36dabd226caa40ff052677719adaacbfe667b36c
specs:
gitolite (0.0.3.alpha)
grit (~> 2.4.1)
hashery (~> 1.4.0)
GIT
remote: https://github.com/gitlabhq/grit.git
revision: ff015074ef35bd94cba943f9c0f98e161ab5851c
@ -101,6 +109,7 @@ GEM
activesupport (~> 3.0)
haml (~> 3.0)
railties (~> 3.0)
hashery (1.4.0)
hike (1.2.1)
i18n (0.6.0)
inifile (0.4.1)
@ -259,6 +268,7 @@ DEPENDENCIES
drapper
faker
git
gitolite!
grit!
haml-rails
inifile

View file

@ -29,7 +29,8 @@ git clone git://github.com/gitlabhq/gitlabhq.git
cd gitlabhq/
# install this library first
sudo easy_install pygments
sudo pip install pygments
sudo apt-get install python-dev
# give your user access to remove git repo
# Ex.
@ -56,10 +57,9 @@ Install gitosis, edit `config/gitlab.yml` and start server
bundle exec rails s -e production
```
## Install Gitosis
### Create git user
```bash
sudo aptitude install gitosis
sudo adduser \
--system \
@ -70,6 +70,22 @@ sudo adduser \
--home /home/git \
git
# Add your user to git group
usermod -a -G git gitlabhq_user_name
```
## Install Gitolite
### !!! IMPORTANT !!! Gitolite umask should be 0007 so users from git group has read/write access to repo
## Install Gitosis
```bash
sudo aptitude install gitosis
ssh-keygen -t rsa
sudo -H -u git gitosis-init < ~/.ssh/id_rsa.pub
@ -79,6 +95,7 @@ sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
cd /tmp && git clone git@localhost:gitosis-admin.git
rm -rf gitosis-admin.git && cd
```
## Install ruby 1.9.2

View file

@ -3,8 +3,8 @@ class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :abilities, :can?
rescue_from Gitosis::AccessDenied do |exception|
render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false
rescue_from Gitlabhq::Gitolite::AccessDenied do |exception|
render :file => File.join(Rails.root, "public", "githost_error"), :layout => false
end
layout :layout_by_resource
@ -70,7 +70,7 @@ class ApplicationController < ActionController::Base
end
def require_non_empty_project
redirect_to @project unless @project.repo_exists?
redirect_to @project unless @project.repo_exists? && @project.has_commits?
end
def respond_with_notes

View file

@ -1,5 +1,5 @@
class ErrorsController < ApplicationController
def gitosis
render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false
def githost
render :file => File.join(Rails.root, "public", "githost_error"), :layout => false
end
end

View file

@ -8,7 +8,7 @@ class ProjectsController < ApplicationController
before_filter :add_project_abilities
before_filter :authorize_read_project!, :except => [:index, :new, :create]
before_filter :authorize_admin_project!, :only => [:edit, :update, :destroy]
before_filter :require_non_empty_project, :only => [:blob, :tree]
before_filter :require_non_empty_project, :only => [:blob, :tree, :graph]
before_filter :load_refs, :only => :tree # load @branch, @tag & @ref
def index
@ -42,8 +42,8 @@ class ProjectsController < ApplicationController
format.js
end
end
rescue Gitosis::AccessDenied
render :js => "location.href = '#{errors_gitosis_path}'" and return
rescue Gitlabhq::Gitolite::AccessDenied
render :js => "location.href = '#{errors_githost_path}'" and return
rescue StandardError => ex
@project.errors.add(:base, "Cant save project. Please try again later")
respond_to do |format|
@ -65,7 +65,7 @@ class ProjectsController < ApplicationController
end
def show
return render "projects/empty" unless @project.repo_exists?
return render "projects/empty" unless @project.repo_exists? && @project.has_commits?
limit = (params[:limit] || 20).to_i
@activities = @project.cached_updates(limit)
end

View file

@ -1,14 +1,15 @@
class RefsController < ApplicationController
before_filter :project
before_filter :ref
before_filter :define_tree_vars, :only => [:tree, :blob]
layout "project"
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :require_non_empty_project
before_filter :ref
before_filter :define_tree_vars, :only => [:tree, :blob]
layout "project"
def switch
new_path = if params[:destination] == "tree"
tree_project_ref_path(@project, params[:ref])
@ -51,6 +52,8 @@ class RefsController < ApplicationController
@commit = project.commit(@ref)
@tree = Tree.new(@commit.tree, project, @ref, params[:path])
@tree = TreeDecorator.new(@tree)
rescue
return render_404
end
def ref

View file

@ -11,29 +11,29 @@ class Key < ActiveRecord::Base
:length => { :within => 0..5000 }
before_save :set_identifier
after_save :update_gitosis
after_destroy :gitosis_delete_key
after_save :update_repository
after_destroy :repository_delete_key
def set_identifier
self.identifier = "#{user.identifier}_#{Time.now.to_i}"
end
def update_gitosis
Gitosis.new.configure do |c|
def update_repository
Gitlabhq::GitHost.system.new.configure do |c|
c.update_keys(identifier, key)
projects.each do |project|
c.update_project(project.path, project.gitosis_writers)
c.update_project(project.path, project.repository_writers)
end
end
end
def gitosis_delete_key
Gitosis.new.configure do |c|
def repository_delete_key
Gitlabhq::GitHost.system.new.configure do |c|
c.delete_key(identifier)
projects.each do |project|
c.update_project(project.path, project.gitosis_writers)
c.update_project(project.path, project.repository_writers)
end
end
end

View file

@ -40,8 +40,8 @@ class Project < ActiveRecord::Base
validate :check_limit
validate :repo_name
after_destroy :destroy_gitosis_project
after_save :update_gitosis_project
after_destroy :destroy_repository
after_save :update_repository
attr_protected :private_flag, :owner_id
@ -54,8 +54,8 @@ class Project < ActiveRecord::Base
delegate :repo,
:url_to_repo,
:path_to_repo,
:update_gitosis_project,
:destroy_gitosis_project,
:update_repository,
:destroy_repository,
:tags,
:repo_exists?,
:commit,
@ -95,6 +95,10 @@ class Project < ActiveRecord::Base
notes.where(:noteable_id => commit.id, :noteable_type => "Commit")
end
def has_commits?
!!commit
end
def add_access(user, *access)
opts = { :user => user }
access.each { |name| opts.merge!(name => true) }
@ -109,7 +113,7 @@ class Project < ActiveRecord::Base
@writers ||= users_projects.includes(:user).where(:write => true).map(&:user)
end
def gitosis_writers
def repository_writers
keys = Key.joins({:user => :users_projects}).where("users_projects.project_id = ? AND users_projects.write = ?", id, true)
keys.map(&:identifier)
end
@ -180,8 +184,8 @@ class Project < ActiveRecord::Base
end
def repo_name
if path == "gitosis-admin"
errors.add(:path, " like 'gitosis-admin' is not allowed")
if path == "gitolite-admin"
errors.add(:path, " like 'gitolite-admin' is not allowed")
end
end

View file

@ -1,3 +1,5 @@
require File.join(Rails.root, "lib", "gitlabhq", "git_host")
class Repository
attr_accessor :project
@ -22,25 +24,21 @@ class Repository
end
def url_to_repo
if !GITOSIS["port"] or GITOSIS["port"] == 22
"#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git"
else
"ssh://#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{GITOSIS["port"]}/#{path}.git"
end
Gitlabhq::GitHost.url_to_repo(path)
end
def path_to_repo
GITOSIS["base_path"] + path + ".git"
GIT_HOST["base_path"] + path + ".git"
end
def update_gitosis_project
Gitosis.new.configure do |c|
c.update_project(path, project.gitosis_writers)
def update_repository
Gitlabhq::GitHost.system.new.configure do |c|
c.update_project(path, project.repository_writers)
end
end
def destroy_gitosis_project
Gitosis.new.configure do |c|
def destroy_repository
Gitlabhq::GitHost.system.new.configure do |c|
c.destroy_project(@project)
end
end

View file

@ -4,7 +4,7 @@ class UsersProject < ActiveRecord::Base
attr_protected :project_id, :project
after_commit :update_gitosis_project
after_commit :update_repository
validates_uniqueness_of :user_id, :scope => [:project_id]
validates_presence_of :user_id
@ -13,9 +13,9 @@ class UsersProject < ActiveRecord::Base
delegate :name, :email, :to => :user, :prefix => true
def update_gitosis_project
def update_repository
Gitosis.new.configure do |c|
c.update_project(project.path, project.gitosis_writers)
c.update_project(project.path, project.repository)
end
end

View file

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

View file

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

View file

@ -6,9 +6,13 @@ email:
from: notify@gitlabhq.com
host: gitlabhq.com
# Gitosis congiguration
gitosis:
admin_uri: git@localhost:gitosis-admin.git
# Git Hosting congiguration
# You can use both gitolite & gitosis
# But gitosis wiil be deprecated &
# some new features wont work with it
git_host:
system: gitolite
admin_uri: git@localhost:gitolite-admin
base_path: /home/git/repositories/
host: localhost
git_user: git

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"]
GIT_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git"]

View file

@ -14,7 +14,7 @@ Gitlab::Application.routes.draw do
root :to => "users#index"
end
get "errors/gitosis"
get "errors/githost"
get "profile/password", :to => "profile#password"
put "profile/password", :to => "profile#password_update"
put "profile/reset_private_token", :to => "profile#reset_private_token"

View file

@ -1,19 +1,21 @@
admin = User.create(
unless User.count > 0
admin = User.create(
:email => "admin@local.host",
:name => "Administrator",
:password => "5iveL!fe",
:password_confirmation => "5iveL!fe"
)
)
admin.projects_limit = 10000
admin.admin = true
admin.save!
admin.projects_limit = 10000
admin.admin = true
admin.save!
if admin.valid?
puts %q[
Administrator account created:
if admin.valid?
puts %q[
Administrator account created:
login.........admin@local.host
password......5iveL!fe
]
login.........admin@local.host
password......5iveL!fe
]
end
end

View file

@ -0,0 +1,5 @@
Project.seed(:id, [
{ :id => 1, :name => "Gitlab HQ", :path => "gitlabhq", :code => "gitlabhq", :owner_id => 1 },
{ :id => 2, :name => "Diaspora", :path => "diaspora", :code => "diaspora", :owner_id => 1 },
{ :id => 3, :name => "Ruby on Rails", :path => "ruby_on_rails", :code => "ruby_on_rails", :owner_id => 1 }
])

View file

@ -0,0 +1,11 @@
User.seed(:id, [
{ :id => 2, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 3, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 4, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 5, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 6, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 7, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 8, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
{ :id => 9, :name => Faker::Internet.user_name, :email => Faker::Internet.email}
])

View file

@ -0,0 +1,21 @@
UsersProject.seed(:id, [
{ :id => 1, :project_id => 1, :user_id => 1, :read => true, :write => true, :admin => true },
{ :id => 2, :project_id => 1, :user_id => 2, :read => true, :write => false, :admin => false },
{ :id => 3, :project_id => 1, :user_id => 3, :read => true, :write => false, :admin => false },
{ :id => 4, :project_id => 1, :user_id => 4, :read => true, :write => false, :admin => false },
{ :id => 5, :project_id => 1, :user_id => 5, :read => true, :write => false, :admin => false },
{ :id => 6, :project_id => 2, :user_id => 1, :read => true, :write => true, :admin => true },
{ :id => 7, :project_id => 2, :user_id => 2, :read => true, :write => false, :admin => false },
{ :id => 8, :project_id => 2, :user_id => 3, :read => true, :write => false, :admin => false },
{ :id => 9, :project_id => 2, :user_id => 4, :read => true, :write => false, :admin => false },
{ :id => 11, :project_id => 2, :user_id => 5, :read => true, :write => false, :admin => false },
{ :id => 12, :project_id => 3, :user_id => 1, :read => true, :write => true, :admin => true },
{ :id => 13, :project_id => 3, :user_id => 2, :read => true, :write => false, :admin => false },
{ :id => 14, :project_id => 3, :user_id => 3, :read => true, :write => false, :admin => false },
{ :id => 15, :project_id => 3, :user_id => 4, :read => true, :write => false, :admin => false },
{ :id => 16, :project_id => 3, :user_id => 5, :read => true, :write => false, :admin => false }
])

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

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

@ -0,0 +1,21 @@
require File.join(Rails.root, "lib", "gitlabhq", "gitolite")
module Gitlabhq
class GitHost
def self.system
Gitlabhq::Gitolite
end
def self.admin_uri
GIT_HOST["admin_uri"]
end
def self.url_to_repo(path)
if !GIT_HOST["port"] or GIT_HOST["port"] == 22
"#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{path}.git"
else
"ssh://#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{GIT_HOST["port"]}/#{path}.git"
end
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

View file

@ -1,74 +0,0 @@
require 'inifile'
require 'timeout'
require 'fileutils'
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 #{GITOSIS['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

View file

@ -0,0 +1,18 @@
desc "Rebuild each project at gitolite config"
task :gitolite_rebuild => :environment do
puts "Starting Projects"
Project.find_each(:batch_size => 100) do |project|
puts
puts "=== #{project.name}"
project.update_repository
puts
end
puts "Done with projects"
puts "Starting Key"
Key.find_each(:batch_size => 100) do |project|
project.update_repository
print '.'
end
puts "Done with keys"
end

View file

@ -18,12 +18,12 @@ describe Project do
describe "Respond to" do
it { should respond_to(:readers) }
it { should respond_to(:writers) }
it { should respond_to(:gitosis_writers) }
it { should respond_to(:repository_writers) }
it { should respond_to(:admins) }
it { should respond_to(:add_access) }
it { should respond_to(:reset_access) }
it { should respond_to(:update_gitosis_project) }
it { should respond_to(:destroy_gitosis_project) }
it { should respond_to(:update_repository) }
it { should respond_to(:destroy_repository) }
it { should respond_to(:public?) }
it { should respond_to(:private?) }
it { should respond_to(:url_to_repo) }
@ -35,9 +35,9 @@ describe Project do
it { should respond_to(:commit) }
end
it "should not allow 'gitosis-admin' as repo name" do
it "should not allow 'gitolite-admin' as repo name" do
should allow_value("blah").for(:path)
should_not allow_value("gitosis-admin").for(:path)
should_not allow_value("gitolite-admin").for(:path)
end
it "should return valid url to repo" do

View file

@ -1,11 +1,11 @@
# Stubbing Project <-> gitosis path
# Stubbing Project <-> git host path
# create project using Factory only
class Project
def update_gitosis_project
def update_repository
true
end
def update_gitosis
def update_repository
true
end
@ -15,17 +15,17 @@ class Project
end
class Key
def update_gitosis
def update_repository
true
end
def gitosis_delete_key
def repository_delete_key
true
end
end
class UsersProject
def update_gitosis_project
def update_repository
true
end
end