From 9b337b8328a65c5e692c90ba1f817184bd054257 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Jul 2012 21:51:48 +0300 Subject: [PATCH 1/2] Epic: Gitlab configuration with default values --- Gemfile | 1 + Gemfile.lock | 2 + app/assets/stylesheets/gitlab_bootstrap.scss | 5 ++ app/helpers/application_helper.rb | 2 +- app/mailers/notify.rb | 6 +- app/models/project.rb | 2 +- app/roles/git_push.rb | 2 +- app/roles/repository.rb | 10 ++- app/views/admin/projects/_form.html.haml | 2 +- app/views/projects/_form.html.haml | 2 +- app/views/projects/_new_form.html.haml | 2 +- app/views/projects/show.html.haml | 15 +++- config/gitlab.yml.example | 18 +++-- config/initializers/00_before_all.rb | 4 - config/initializers/0_before_all.rb | 1 + config/initializers/1_settings.rb | 75 +++++++++++++++++++ .../{gitlab/10_app.rb => 2_app.rb} | 4 + .../{gitlab/20_grit_ext.rb => 3_grit_ext.rb} | 4 +- ...30_resque_queues.rb => 4_resque_queues.rb} | 0 config/initializers/devise.rb | 2 +- config/routes.rb | 8 +- lib/gitlab/git_host.rb | 8 +- lib/tasks/gitlab/status.rake | 13 ++-- spec/mailers/notify_spec.rb | 2 +- spec/models/project_spec.rb | 2 +- 25 files changed, 147 insertions(+), 45 deletions(-) delete mode 100644 config/initializers/00_before_all.rb create mode 100644 config/initializers/0_before_all.rb create mode 100644 config/initializers/1_settings.rb rename config/initializers/{gitlab/10_app.rb => 2_app.rb} (75%) rename config/initializers/{gitlab/20_grit_ext.rb => 3_grit_ext.rb} (78%) rename config/initializers/{gitlab/30_resque_queues.rb => 4_resque_queues.rb} (100%) diff --git a/Gemfile b/Gemfile index 8bdc6426..bf2320a8 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem "foreman" gem "colored" gem 'resque_mailer' gem 'tabs_on_rails' +gem 'settingslogic' group :assets do gem "sass-rails", "3.2.5" diff --git a/Gemfile.lock b/Gemfile.lock index 897f640d..e6a488f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -313,6 +313,7 @@ GEM libwebsocket (~> 0.1.3) multi_json (~> 1.0) rubyzip + settingslogic (2.0.8) shoulda-matchers (1.1.0) activesupport (>= 3.0.0) simplecov (0.6.4) @@ -416,6 +417,7 @@ DEPENDENCIES rspec-rails sass-rails (= 3.2.5) seed-fu + settingslogic shoulda-matchers simplecov six diff --git a/app/assets/stylesheets/gitlab_bootstrap.scss b/app/assets/stylesheets/gitlab_bootstrap.scss index 087db6bb..8c7d6989 100644 --- a/app/assets/stylesheets/gitlab_bootstrap.scss +++ b/app/assets/stylesheets/gitlab_bootstrap.scss @@ -158,6 +158,11 @@ table { &.small { @extend .btn-small; } + + &.active { + border-color:#aaa; + background-color:#ccc; + } } a:focus { diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 514e4661..de3f36c8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -13,7 +13,7 @@ module ApplicationHelper end def web_app_url - "#{request_protocol}://#{GIT_HOST["host"]}/" + "#{request_protocol}://#{Gitlab.config.web_host}/" end def last_commit(project) diff --git a/app/mailers/notify.rb b/app/mailers/notify.rb index 05fd17b5..37b442bc 100644 --- a/app/mailers/notify.rb +++ b/app/mailers/notify.rb @@ -2,10 +2,10 @@ class Notify < ActionMailer::Base include Resque::Mailer add_template_helper ApplicationHelper - default_url_options[:host] = EMAIL_OPTS["host"] - default_url_options[:protocol] = -> { EMAIL_OPTS["protocol"] ? EMAIL_OPTS["protocol"] : "http" }.call + default_url_options[:host] = Gitlab.config.web_host + default_url_options[:protocol] = Gitlab.config.web_protocol - default from: EMAIL_OPTS["from"] + default from: Gitlab.config.email_from def new_user_email(user_id, password) @user = User.find(user_id) diff --git a/app/models/project.rb b/app/models/project.rb index 9f51f4e4..7cbc8928 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -113,7 +113,7 @@ class Project < ActiveRecord::Base end def web_url - [GIT_HOST['host'], code].join("/") + [Gitlab.config.url, code].join("/") end def common_notes diff --git a/app/roles/git_push.rb b/app/roles/git_push.rb index d557ce4e..b4c59472 100644 --- a/app/roles/git_push.rb +++ b/app/roles/git_push.rb @@ -73,7 +73,7 @@ module GitPush id: commit.id, message: commit.safe_message, timestamp: commit.date.xmlschema, - url: "http://#{GIT_HOST['host']}/#{code}/commits/#{commit.id}", + url: "#{Gitlab.config.url}/#{code}/commits/#{commit.id}", author: { name: commit.author_name, email: commit.author_email diff --git a/app/roles/repository.rb b/app/roles/repository.rb index f61c7782..8d5b018d 100644 --- a/app/roles/repository.rb +++ b/app/roles/repository.rb @@ -68,7 +68,7 @@ module Repository end def path_to_repo - File.join(GIT_HOST["base_path"], "#{path}.git") + File.join(Gitlab.config.git_base_path, "#{path}.git") end def update_repository @@ -141,4 +141,12 @@ module Repository file_path end + + def ssh_url_to_repo + url_to_repo + end + + def http_url_to_repo + http_url = [Gitlab.config.url, "/", path, ".git"].join() + end end diff --git a/app/views/admin/projects/_form.html.haml b/app/views/admin/projects/_form.html.haml index 822dc064..41c620a0 100644 --- a/app/views/admin/projects/_form.html.haml +++ b/app/views/admin/projects/_form.html.haml @@ -13,7 +13,7 @@ Path .input .input-prepend - %span.add-on= "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:" + %span.add-on= Gitlab.config.ssh_path = f.text_field :path, :placeholder => "example_project", :disabled => !@admin_project.new_record? .clearfix = f.label :code do diff --git a/app/views/projects/_form.html.haml b/app/views/projects/_form.html.haml index 36923bed..e057155b 100644 --- a/app/views/projects/_form.html.haml +++ b/app/views/projects/_form.html.haml @@ -18,7 +18,7 @@ Git Clone .input .input-prepend - %span.add-on= "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:" + %span.add-on= Gitlab.config.ssh_path = f.text_field :path, :placeholder => "example_project", :disabled => !@project.new_record? %span.add-on= ".git" .clearfix diff --git a/app/views/projects/_new_form.html.haml b/app/views/projects/_new_form.html.haml index e420f700..9f3bfa86 100644 --- a/app/views/projects/_new_form.html.haml +++ b/app/views/projects/_new_form.html.haml @@ -17,7 +17,7 @@ Git Clone .input .input-prepend - %span.add-on= "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:" + %span.add-on= Gitlab.config.ssh_path = f.text_field :path, :placeholder => "example_project", :disabled => !@project.new_record? %span.add-on= ".git" .clearfix diff --git a/app/views/projects/show.html.haml b/app/views/projects/show.html.haml index 44f9053d..23a49873 100644 --- a/app/views/projects/show.html.haml +++ b/app/views/projects/show.html.haml @@ -4,8 +4,11 @@ .row .span7 .form-horizontal - .input-prepend + .input-prepend.project_clone_holder + %span.add-on git clone + = link_to "SSH", "#", :class => "btn small active", :"data-clone" => @project.ssh_url_to_repo + = link_to "HTTP", "#", :class => "btn small", :"data-clone" => @project.http_url_to_repo = text_field_tag :project_clone, @project.url_to_repo, :class => "one_click_select span5" .span4.right .right @@ -23,4 +26,12 @@ = render "events/event_last_push", :event => @last_push .content_list= render @events - +:javascript + $(function(){ + var link_sel = ".project_clone_holder a"; + $(link_sel).bind("click", function() { + $(link_sel).removeClass("active"); + $(this).addClass("active"); + $("#project_clone").val($(this).attr("data-clone")); + }) + }) diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index 74e841d2..679d1cfa 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -1,18 +1,20 @@ -# Gitlab application config file +# # # # # # # # # # # # # # # # # # +# Gitlab application config file # +# # # # # # # # # # # # # # # # # # + +# Web application specific settings +web: + host: localhost + port: 80 + https: false # Email used for notification # about new issues, comments email: - from: notify@gitlabhq.com - host: gitlabhq.com - - # Protocol used for links in email letters - # Value can be http or https - protocol: http # or https + from: notify@localhost # Git Hosting configuration git_host: - system: gitolite admin_uri: git@localhost:gitolite-admin base_path: /home/git/repositories/ host: localhost diff --git a/config/initializers/00_before_all.rb b/config/initializers/00_before_all.rb deleted file mode 100644 index 01498804..00000000 --- a/config/initializers/00_before_all.rb +++ /dev/null @@ -1,4 +0,0 @@ -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"] -GITLAB_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["gitlab"] diff --git a/config/initializers/0_before_all.rb b/config/initializers/0_before_all.rb new file mode 100644 index 00000000..fc0575ad --- /dev/null +++ b/config/initializers/0_before_all.rb @@ -0,0 +1 @@ +GITLAB_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")["gitlab"] diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb new file mode 100644 index 00000000..eccce9cb --- /dev/null +++ b/config/initializers/1_settings.rb @@ -0,0 +1,75 @@ +class Settings < Settingslogic + source "#{Rails.root}/config/gitlab.yml" + + class << self + def web_protocol + self.web['protocol'] ||= web.https ? "https://" : "http://" + end + + def web_host + self.web['host'] ||= 'localhost' + end + + def email_from + self.email['from'] ||= "notify@" + web_host + end + + def url + self['url'] ||= build_url + end + + def build_url + raw_url = self.web_protocol + raw_url << web.host + raw_url << ":#{web.port}" if web.port.to_i != 80 + end + + def ssh_port + git_host['port'] || 22 + end + + def ssh_user + git_host['git_user'] || 'git' + end + + def ssh_host + git_host['host'] || 'localhost' + end + + def ssh_path + if ssh_port != 22 + "ssh://#{ssh_user}@#{ssh_host}:#{ssh_port}/" + else + "#{ssh_user}@#{ssh_host}:" + end + end + + def git_base_path + git_host['base_path'] || '/home/git/repositories/' + end + + def git_upload_pack + git_host['upload_pack'] || true + end + + def git_receive_pack + git_host['receive_pack'] || true + end + + def git_bin_path + git['path'] || '/usr/bin/git' + end + + def git_max_size + git['git_max_size'] || 5242880 # 5.megabytes + end + + def git_timeout + git['git_timeout'] || 10 + end + + def gitolite_admin_uri + git['admin_uri'] || 'git@localhost:gitolite-admin' + end + end +end diff --git a/config/initializers/gitlab/10_app.rb b/config/initializers/2_app.rb similarity index 75% rename from config/initializers/gitlab/10_app.rb rename to config/initializers/2_app.rb index f6314ff0..fc97960b 100644 --- a/config/initializers/gitlab/10_app.rb +++ b/config/initializers/2_app.rb @@ -1,4 +1,8 @@ module Gitlab Version = File.read(Rails.root.join("VERSION")) Revision = `git log --pretty=format:'%h' -n 1` + + def self.config + Settings + end end diff --git a/config/initializers/gitlab/20_grit_ext.rb b/config/initializers/3_grit_ext.rb similarity index 78% rename from config/initializers/gitlab/20_grit_ext.rb rename to config/initializers/3_grit_ext.rb index ad8ea105..fd7e288a 100644 --- a/config/initializers/gitlab/20_grit_ext.rb +++ b/config/initializers/3_grit_ext.rb @@ -1,8 +1,8 @@ require 'grit' require 'pygments' -Grit::Git.git_timeout = GIT_OPTS["git_timeout"] -Grit::Git.git_max_size = GIT_OPTS["git_max_size"] +Grit::Git.git_timeout = Gitlab.config.git_timeout +Grit::Git.git_max_size = Gitlab.config.git_max_size Grit::Blob.class_eval do include Linguist::BlobHelper diff --git a/config/initializers/gitlab/30_resque_queues.rb b/config/initializers/4_resque_queues.rb similarity index 100% rename from config/initializers/gitlab/30_resque_queues.rb rename to config/initializers/4_resque_queues.rb diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 78652bc2..cb1ae0ac 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -4,7 +4,7 @@ Devise.setup do |config| # ==> Mailer Configuration # Configure the e-mail address which will be shown in Devise::Mailer, # note that it will be overwritten if you use your own mailer class with default "from" parameter. - config.mailer_sender = EMAIL_OPTS["from"] + config.mailer_sender = Gitlab.config.email_from # Configure the class responsible to send e-mails. # config.mailer = "Devise::Mailer" diff --git a/config/routes.rb b/config/routes.rb index b87009b1..2514d681 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,10 +14,10 @@ Gitlab::Application.routes.draw do # Enable Grack support mount Grack::Bundle.new({ - git_path: GIT_OPTS['path'], - project_root: GIT_HOST['base_path'], - upload_pack: GIT_HOST['upload_pack'], - receive_pack: GIT_HOST['receive_pack'] + git_path: Gitlab.config.git_bin_path, + project_root: Gitlab.config.git_base_path, + upload_pack: Gitlab.config.git_upload_pack, + receive_pack: Gitlab.config.git_receive_pack }), at: '/:path', constraints: { path: /[\w-]+\.git/ } # diff --git a/lib/gitlab/git_host.rb b/lib/gitlab/git_host.rb index 3d8d1747..76b2c7b1 100644 --- a/lib/gitlab/git_host.rb +++ b/lib/gitlab/git_host.rb @@ -7,15 +7,11 @@ module Gitlab end def self.admin_uri - GIT_HOST["admin_uri"] + Gitlab.config.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 + Gitlab.config.ssh_path + "#{path}.git" end end end diff --git a/lib/tasks/gitlab/status.rake b/lib/tasks/gitlab/status.rake index a02aef79..7db56574 100644 --- a/lib/tasks/gitlab/status.rake +++ b/lib/tasks/gitlab/status.rake @@ -3,6 +3,7 @@ namespace :gitlab do desc "GITLAB | Check gitlab installation status" task :status => :environment do puts "Starting diagnostic" + git_base_path = Gitlab.config.git_base_path print "config/database.yml............" if File.exists?(File.join Rails.root, "config", "database.yml") @@ -21,16 +22,16 @@ namespace :gitlab do end GIT_HOST = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git_host"] - print "#{GIT_HOST['base_path']}............" - if File.exists?(GIT_HOST['base_path']) + print "#{git_base_path}............" + if File.exists?(git_base_path) puts "exists".green else puts "missing".red return end - print "#{GIT_HOST['base_path']} is writable?............" - if File.stat(GIT_HOST['base_path']).writable? + print "#{git_base_path} is writable?............" + if File.stat(git_base_path).writable? puts "YES".green else puts "NO".red @@ -38,7 +39,7 @@ namespace :gitlab do end begin - `git clone #{GIT_HOST["admin_uri"]} /tmp/gitolite_gitlab_test` + `git clone #{Gitlab.config.gitolite_admin_uri} /tmp/gitolite_gitlab_test` FileUtils.rm_rf("/tmp/gitolite_gitlab_test") print "Can clone gitolite-admin?............" puts "YES".green @@ -49,7 +50,7 @@ namespace :gitlab do end print "UMASK for .gitolite.rc is 0007? ............" - unless open("#{GIT_HOST['base_path']}/../.gitolite.rc").grep(/REPO_UMASK = 0007/).empty? + unless open("#{git_base_path}/../.gitolite.rc").grep(/REPO_UMASK = 0007/).empty? puts "YES".green else puts "NO".red diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index 31b211b6..aabb4334 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -5,7 +5,7 @@ describe Notify do include EmailSpec::Matchers before :all do - default_url_options[:host] = EMAIL_OPTS['host'] + default_url_options[:host] = Gitlab.config.web_host end let(:recipient) { Factory.create(:user, :email => 'recipient@example.com') } diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index ab1baf0e..0c54ae01 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -56,7 +56,7 @@ describe Project do it "returns the full web URL for this repo" do project = Project.new(:code => "somewhere") - project.web_url.should == "#{GIT_HOST['host']}/somewhere" + project.web_url.should == "#{Gitlab.config.url}/somewhere" end describe :valid_repo? do From 1d543e64300666e2abc5cc8562e6b55458878801 Mon Sep 17 00:00:00 2001 From: randx Date: Mon, 2 Jul 2012 21:59:48 +0300 Subject: [PATCH 2/2] Finished with configs --- app/controllers/admin/users_controller.rb | 2 +- config/gitlab.yml.example | 20 +++++++++++++++----- config/initializers/1_settings.rb | 18 +++++++++++++++--- lib/gitlab/gitolite.rb | 2 +- lib/tasks/gitlab/status.rake | 1 - 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 4619cb01..4b123e4f 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -34,7 +34,7 @@ class Admin::UsersController < ApplicationController def new - @admin_user = User.new(:projects_limit => GITLAB_OPTS["default_projects_limit"]) + @admin_user = User.new(:projects_limit => Gitlab.config.default_projects_limit) end def edit diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index 679d1cfa..12c28675 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -2,6 +2,10 @@ # Gitlab application config file # # # # # # # # # # # # # # # # # # # +# +# 1. Common settings +# ========================== + # Web application specific settings web: host: localhost @@ -13,11 +17,21 @@ web: email: from: notify@localhost +# Application specific settings +# Like default project limit for user etc +app: + default_projects_limit: 10 + + +# +# 2. Advanced settings: +# ========================== + # Git Hosting configuration git_host: admin_uri: git@localhost:gitolite-admin base_path: /home/git/repositories/ - host: localhost + # host: localhost git_user: git upload_pack: true receive_pack: true @@ -33,7 +47,3 @@ git: git_max_size: 5242880 # 5.megabytes # Git timeout to read commit, in seconds git_timeout: 10 - -# Gitlab settings -gitlab: - default_projects_limit: 10 diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index eccce9cb..a71bfd25 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -33,7 +33,7 @@ class Settings < Settingslogic end def ssh_host - git_host['host'] || 'localhost' + git_host['host'] || web_host || 'localhost' end def ssh_path @@ -49,11 +49,19 @@ class Settings < Settingslogic end def git_upload_pack - git_host['upload_pack'] || true + if git_host['upload_pack'] == false + false + else + true + end end def git_receive_pack - git_host['receive_pack'] || true + if git_host['receive_pack'] == false + false + else + true + end end def git_bin_path @@ -71,5 +79,9 @@ class Settings < Settingslogic def gitolite_admin_uri git['admin_uri'] || 'git@localhost:gitolite-admin' end + + def default_projects_limit + app['default_projects_limit'] || 10 + end end end diff --git a/lib/gitlab/gitolite.rb b/lib/gitlab/gitolite.rb index efb89165..7b80d4a8 100644 --- a/lib/gitlab/gitolite.rb +++ b/lib/gitlab/gitolite.rb @@ -33,7 +33,7 @@ module Gitlab end def configure - Timeout::timeout(20) do + Timeout::timeout(30) do File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| begin f.flock(File::LOCK_EX) diff --git a/lib/tasks/gitlab/status.rake b/lib/tasks/gitlab/status.rake index 7db56574..ac712234 100644 --- a/lib/tasks/gitlab/status.rake +++ b/lib/tasks/gitlab/status.rake @@ -21,7 +21,6 @@ namespace :gitlab do return end - GIT_HOST = YAML.load_file("#{Rails.root}/config/gitlab.yml")["git_host"] print "#{git_base_path}............" if File.exists?(git_base_path) puts "exists".green