1_settings.rb 10.4 KB
Newer Older
1 2
require 'gitlab' # Load lib/gitlab.rb as soon as possible

3
class Settings < Settingslogic
4
  source ENV.fetch('GITLAB_CONFIG') { "#{Rails.root}/config/gitlab.yml" }
5
  namespace Rails.env
6 7

  class << self
8 9
    def gitlab_on_standard_port?
      gitlab.port.to_i == (gitlab.https ? 443 : 80)
R
Riyad Preukschas 已提交
10
    end
11

12 13 14 15 16 17 18 19
    # get host without www, thanks to http://stackoverflow.com/a/6674363/1233435
    def get_host_without_www(url)
      url = URI.encode(url)
      uri = URI.parse(url)
      uri = URI.parse("http://#{url}") if uri.scheme.nil?
      host = uri.host.downcase
      host.start_with?('www.') ? host[4..-1] : host
    end
R
Riyad Preukschas 已提交
20 21 22

    private

23 24 25
    def build_gitlab_shell_ssh_path_prefix
      if gitlab_shell.ssh_port != 22
        "ssh://#{gitlab_shell.ssh_user}@#{gitlab_shell.ssh_host}:#{gitlab_shell.ssh_port}/"
R
Riyad Preukschas 已提交
26
      else
27 28 29 30 31
        if gitlab_shell.ssh_host.include? ':'
          "[#{gitlab_shell.ssh_user}@#{gitlab_shell.ssh_host}]:"
        else
          "#{gitlab_shell.ssh_user}@#{gitlab_shell.ssh_host}:"
        end
R
Riyad Preukschas 已提交
32 33 34
      end
    end

35 36 37 38
    def build_base_gitlab_url
      base_gitlab_url.join('')
    end

R
Riyad Preukschas 已提交
39
    def build_gitlab_url
40
      (base_gitlab_url + [gitlab.relative_url_root]).join('')
R
Riyad Preukschas 已提交
41
    end
D
Dmitriy Zaporozhets 已提交
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    # check that values in `current` (string or integer) is a contant in `modul`.
    def verify_constant_array(modul, current, default)
      values = default || []
      if !current.nil?
        values = []
        current.each do |constant|
          values.push(verify_constant(modul, constant, nil))
        end
        values.delete_if { |value| value.nil? }
      end
      values
    end

    # check that `current` (string or integer) is a contant in `modul`.
    def verify_constant(modul, current, default)
      constant = modul.constants.find{ |name| modul.const_get(name) == current }
      value = constant.nil? ? default : modul.const_get(constant)
      if current.is_a? String
        value = modul.const_get(current.upcase) rescue default
      end
      value
    end
65 66 67 68 69 70 71 72 73 74 75

    private

    def base_gitlab_url
      custom_port = gitlab_on_standard_port? ? nil : ":#{gitlab.port}"
      [ gitlab.protocol,
        "://",
        gitlab.host,
        custom_port
      ]
    end
76 77
  end
end
R
Riyad Preukschas 已提交
78 79 80 81


# Default settings
Settings['ldap'] ||= Settingslogic.new({})
82
Settings.ldap['enabled'] = false if Settings.ldap['enabled'].nil?
83

84 85 86
# backwards compatibility, we only have one host
if Settings.ldap['enabled'] || Rails.env.test?
  if Settings.ldap['host'].present?
87 88
    # We detected old LDAP configuration syntax. Update the config to make it
    # look like it was entered with the new syntax.
89
    server = Settings.ldap.except('sync_time')
90
    Settings.ldap['servers'] = {
91
      'main' => server
92
    }
93 94
  end

95
  Settings.ldap['servers'].each do |key, server|
96
    server['label'] ||= 'LDAP'
97
    server['block_auto_created_users'] = false if server['block_auto_created_users'].nil?
98 99
    server['allow_username_or_email_login'] = false if server['allow_username_or_email_login'].nil?
    server['active_directory'] = true if server['active_directory'].nil?
100
    server['provider_name'] ||= "ldap#{key}".downcase
101 102 103
    server['provider_class'] = OmniAuth::Utils.camelize(server['provider_name'])
  end
end
R
Riyad Preukschas 已提交
104

V
Valery Sizov 已提交
105

R
Riyad Preukschas 已提交
106
Settings['omniauth'] ||= Settingslogic.new({})
107
Settings.omniauth['enabled']      = false if Settings.omniauth['enabled'].nil?
108
Settings.omniauth['auto_sign_in_with_provider'] = false if Settings.omniauth['auto_sign_in_with_provider'].nil?
109 110 111
Settings.omniauth['allow_single_sign_on'] = false if Settings.omniauth['allow_single_sign_on'].nil?
Settings.omniauth['block_auto_created_users'] = true if Settings.omniauth['block_auto_created_users'].nil?
Settings.omniauth['auto_link_ldap_user'] = false if Settings.omniauth['auto_link_ldap_user'].nil?
112

R
Riyad Preukschas 已提交
113 114
Settings.omniauth['providers']  ||= []

115 116
Settings['issues_tracker']  ||= {}

117 118 119
#
# GitLab
#
R
Riyad Preukschas 已提交
120
Settings['gitlab'] ||= Settingslogic.new({})
121
Settings.gitlab['default_projects_limit'] ||= 10
122
Settings.gitlab['default_branch_protection'] ||= 2
123
Settings.gitlab['default_can_create_group'] = true if Settings.gitlab['default_can_create_group'].nil?
124
Settings.gitlab['default_theme'] = Gitlab::Themes::APPLICATION_DEFAULT if Settings.gitlab['default_theme'].nil?
D
Dmitriy Zaporozhets 已提交
125
Settings.gitlab['host']       ||= 'localhost'
D
Dmitriy Zaporozhets 已提交
126
Settings.gitlab['ssh_host']   ||= Settings.gitlab.host
127
Settings.gitlab['https']        = false if Settings.gitlab['https'].nil?
R
Riyad Preukschas 已提交
128
Settings.gitlab['port']       ||= Settings.gitlab.https ? 443 : 80
129
Settings.gitlab['relative_url_root'] ||= ENV['RAILS_RELATIVE_URL_ROOT'] || ''
R
Riyad Preukschas 已提交
130
Settings.gitlab['protocol']   ||= Settings.gitlab.https ? "https" : "http"
131 132 133 134
Settings.gitlab['email_enabled'] ||= true if Settings.gitlab['email_enabled'].nil?
Settings.gitlab['email_from'] ||= "gitlab@#{Settings.gitlab.host}"
Settings.gitlab['email_display_name'] ||= "GitLab"
Settings.gitlab['email_reply_to'] ||= "noreply@#{Settings.gitlab.host}"
135
Settings.gitlab['base_url']   ||= Settings.send(:build_base_gitlab_url)
D
Dmitriy Zaporozhets 已提交
136
Settings.gitlab['url']        ||= Settings.send(:build_gitlab_url)
D
Dmitriy Zaporozhets 已提交
137
Settings.gitlab['user']       ||= 'git'
138 139 140 141 142
Settings.gitlab['user_home']  ||= begin
  Etc.getpwnam(Settings.gitlab['user']).dir
rescue ArgumentError # no user configured
  '/home/' + Settings.gitlab['user']
end
143
Settings.gitlab['time_zone']  ||= nil
D
Dmitriy Zaporozhets 已提交
144
Settings.gitlab['signup_enabled'] ||= true if Settings.gitlab['signup_enabled'].nil?
145
Settings.gitlab['signin_enabled'] ||= true if Settings.gitlab['signin_enabled'].nil?
146
Settings.gitlab['twitter_sharing_enabled'] ||= true if Settings.gitlab['twitter_sharing_enabled'].nil?
147
Settings.gitlab['restricted_visibility_levels'] = Settings.send(:verify_constant_array, Gitlab::VisibilityLevel, Settings.gitlab['restricted_visibility_levels'], [])
148
Settings.gitlab['username_changing_enabled'] = true if Settings.gitlab['username_changing_enabled'].nil?
D
Douwe Maan 已提交
149
Settings.gitlab['issue_closing_pattern'] = '((?:[Cc]los(?:e[sd]?|ing)|[Ff]ix(?:e[sd]|ing)?|[Rr]esolv(?:e[sd]?|ing)) +(?:(?:issues? +)?#\d+(?:(?:, *| +and +)?))+)' if Settings.gitlab['issue_closing_pattern'].nil?
150
Settings.gitlab['default_projects_features'] ||= {}
151
Settings.gitlab['webhook_timeout'] ||= 10
152
Settings.gitlab['max_attachment_size'] ||= 10
153
Settings.gitlab['session_expire_delay'] ||= 10080
154 155 156
Settings.gitlab.default_projects_features['issues']         = true if Settings.gitlab.default_projects_features['issues'].nil?
Settings.gitlab.default_projects_features['merge_requests'] = true if Settings.gitlab.default_projects_features['merge_requests'].nil?
Settings.gitlab.default_projects_features['wiki']           = true if Settings.gitlab.default_projects_features['wiki'].nil?
157
Settings.gitlab.default_projects_features['snippets']       = false if Settings.gitlab.default_projects_features['snippets'].nil?
158
Settings.gitlab.default_projects_features['visibility_level']    = Settings.send(:verify_constant, Gitlab::VisibilityLevel, Settings.gitlab.default_projects_features['visibility_level'], Gitlab::VisibilityLevel::PRIVATE)
159
Settings.gitlab['repository_downloads_path'] = File.absolute_path(Settings.gitlab['repository_downloads_path'] || 'tmp/repositories', Rails.root)
160
Settings.gitlab['restricted_signup_domains'] ||= []
161
Settings.gitlab['import_sources'] ||= ['github','bitbucket','gitlab','gitorious','google_code','git']
R
Riyad Preukschas 已提交
162

D
Douwe Maan 已提交
163 164 165 166
#
# Reply by email
#
Settings['reply_by_email'] ||= Settingslogic.new({})
D
Douwe Maan 已提交
167
Settings.reply_by_email['enabled'] = false if Settings.reply_by_email['enabled'].nil?
D
Douwe Maan 已提交
168

169 170 171
#
# Gravatar
#
R
Riyad Preukschas 已提交
172
Settings['gravatar'] ||= Settingslogic.new({})
173
Settings.gravatar['enabled']      = true if Settings.gravatar['enabled'].nil?
174 175
Settings.gravatar['plain_url']  ||= 'http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'
Settings.gravatar['ssl_url']    ||= 'https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'
176
Settings.gravatar['host']         = Settings.get_host_without_www(Settings.gravatar['plain_url'])
R
Riyad Preukschas 已提交
177

178 179 180 181
#
# GitLab Shell
#
Settings['gitlab_shell'] ||= Settingslogic.new({})
182
Settings.gitlab_shell['path']         ||= Settings.gitlab['user_home'] + '/gitlab-shell/'
183
Settings.gitlab_shell['hooks_path']   ||= Settings.gitlab['user_home'] + '/gitlab-shell/hooks/'
184
Settings.gitlab_shell['secret_file'] ||= Rails.root.join('.gitlab_shell_secret')
185 186
Settings.gitlab_shell['receive_pack']   = true if Settings.gitlab_shell['receive_pack'].nil?
Settings.gitlab_shell['upload_pack']    = true if Settings.gitlab_shell['upload_pack'].nil?
187
Settings.gitlab_shell['repos_path']   ||= Settings.gitlab['user_home'] + '/repositories/'
D
Dmitriy Zaporozhets 已提交
188
Settings.gitlab_shell['ssh_host']     ||= Settings.gitlab.ssh_host
189 190 191 192
Settings.gitlab_shell['ssh_port']     ||= 22
Settings.gitlab_shell['ssh_user']     ||= Settings.gitlab.user
Settings.gitlab_shell['owner_group']  ||= Settings.gitlab.user
Settings.gitlab_shell['ssh_path_prefix'] ||= Settings.send(:build_gitlab_shell_ssh_path_prefix)
R
Riyad Preukschas 已提交
193

194 195 196
#
# Backup
#
R
Riyad Preukschas 已提交
197
Settings['backup'] ||= Settingslogic.new({})
D
Dmitriy Zaporozhets 已提交
198 199
Settings.backup['keep_time']  ||= 0
Settings.backup['path']         = File.expand_path(Settings.backup['path'] || "tmp/backups/", Rails.root)
200
Settings.backup['archive_permissions']          ||= 0600
201
Settings.backup['upload'] ||= Settingslogic.new({ 'remote_directory' => nil, 'connection' => nil })
202 203 204 205
# Convert upload connection settings to use symbol keys, to make Fog happy
if Settings.backup['upload']['connection']
  Settings.backup['upload']['connection'] = Hash[Settings.backup['upload']['connection'].map { |k, v| [k.to_sym, v] }]
end
206
Settings.backup['upload']['multipart_chunk_size'] ||= 104857600
R
Riyad Preukschas 已提交
207

208 209 210
#
# Git
#
R
Riyad Preukschas 已提交
211
Settings['git'] ||= Settingslogic.new({})
212
Settings.git['max_size']  ||= 20971520 # 20.megabytes
D
Dmitriy Zaporozhets 已提交
213
Settings.git['bin_path']  ||= '/usr/bin/git'
214
Settings.git['timeout']   ||= 10
215

R
Riyad Preukschas 已提交
216
Settings['satellites'] ||= Settingslogic.new({})
R
Riyad Preukschas 已提交
217
Settings.satellites['path'] = File.expand_path(Settings.satellites['path'] || "tmp/repo_satellites/", Rails.root)
218
Settings.satellites['timeout'] ||= 30
219 220 221 222 223

#
# Extra customization
#
Settings['extra'] ||= Settingslogic.new({})
224

225 226 227 228 229
#
# Rack::Attack settings
#
Settings['rack_attack'] ||= Settingslogic.new({})
Settings.rack_attack['git_basic_auth'] ||= Settingslogic.new({})
230
Settings.rack_attack.git_basic_auth['enabled'] = true if Settings.rack_attack.git_basic_auth['enabled'].nil?
231
Settings.rack_attack.git_basic_auth['ip_whitelist'] ||= %w{127.0.0.1}
232 233 234 235
Settings.rack_attack.git_basic_auth['maxretry'] ||= 10
Settings.rack_attack.git_basic_auth['findtime'] ||= 1.minute
Settings.rack_attack.git_basic_auth['bantime'] ||= 1.hour

236 237 238 239 240
#
# Testing settings
#
if Rails.env.test?
  Settings.gitlab['default_projects_limit']   = 42
241
  Settings.gitlab['default_can_create_group'] = true
242 243
  Settings.gitlab['default_can_create_team']  = false
end