1_settings.rb 13.7 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
    def host_without_www(url)
      host(url).sub('www.', '')
14
    end
R
Riyad Preukschas 已提交
15

V
Valery Sizov 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28
    def build_gitlab_ci_url
      if gitlab_on_standard_port?
        custom_port = nil
      else
        custom_port = ":#{gitlab.port}"
      end
      [ gitlab.protocol,
        "://",
        gitlab.host,
        custom_port,
        gitlab.relative_url_root
      ].join('')
    end
R
Riyad Preukschas 已提交
29

30
    def build_gitlab_shell_ssh_path_prefix
31 32
      user_host = "#{gitlab_shell.ssh_user}@#{gitlab_shell.ssh_host}"

33
      if gitlab_shell.ssh_port != 22
34
        "ssh://#{user_host}:#{gitlab_shell.ssh_port}/"
R
Riyad Preukschas 已提交
35
      else
36
        if gitlab_shell.ssh_host.include? ':'
37
          "[#{user_host}]:"
38
        else
39
          "#{user_host}:"
40
        end
R
Riyad Preukschas 已提交
41 42 43
      end
    end

44 45 46 47
    def build_base_gitlab_url
      base_gitlab_url.join('')
    end

R
Riyad Preukschas 已提交
48
    def build_gitlab_url
49
      (base_gitlab_url + [gitlab.relative_url_root]).join('')
R
Riyad Preukschas 已提交
50
    end
D
Dmitriy Zaporozhets 已提交
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    # 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
74 75 76 77 78 79 80 81 82 83 84

    private

    def base_gitlab_url
      custom_port = gitlab_on_standard_port? ? nil : ":#{gitlab.port}"
      [ gitlab.protocol,
        "://",
        gitlab.host,
        custom_port
      ]
    end
85 86 87 88 89 90 91 92 93 94 95

    # Extract the host part of the given +url+.
    def host(url)
      url = url.downcase
      url = "http://#{url}" unless url.start_with?('http')

      # Get rid of the path so that we don't even have to encode it
      url_without_path = url.sub(%r{(https?://[^\/]+)/?.*}, '\1')

      URI.parse(url_without_path).host
    end
96 97
  end
end
R
Riyad Preukschas 已提交
98 99 100 101


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

104 105 106
# backwards compatibility, we only have one host
if Settings.ldap['enabled'] || Rails.env.test?
  if Settings.ldap['host'].present?
107 108
    # We detected old LDAP configuration syntax. Update the config to make it
    # look like it was entered with the new syntax.
109
    server = Settings.ldap.except('sync_time')
110
    Settings.ldap['servers'] = {
111
      'main' => server
112
    }
113 114
  end

115
  Settings.ldap['servers'].each do |key, server|
116
    server['label'] ||= 'LDAP'
117
    server['timeout'] ||= 10.seconds
118
    server['block_auto_created_users'] = false if server['block_auto_created_users'].nil?
119 120
    server['allow_username_or_email_login'] = false if server['allow_username_or_email_login'].nil?
    server['active_directory'] = true if server['active_directory'].nil?
D
Douwe Maan 已提交
121
    server['attributes'] = {} if server['attributes'].nil?
122
    server['provider_name'] ||= "ldap#{key}".downcase
123 124 125
    server['provider_class'] = OmniAuth::Utils.camelize(server['provider_name'])
  end
end
R
Riyad Preukschas 已提交
126

V
Valery Sizov 已提交
127

R
Riyad Preukschas 已提交
128
Settings['omniauth'] ||= Settingslogic.new({})
129
Settings.omniauth['enabled']      = false if Settings.omniauth['enabled'].nil?
130
Settings.omniauth['auto_sign_in_with_provider'] = false if Settings.omniauth['auto_sign_in_with_provider'].nil?
131 132 133
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?
134
Settings.omniauth['auto_link_saml_user'] = false if Settings.omniauth['auto_link_saml_user'].nil?
135

R
Riyad Preukschas 已提交
136
Settings.omniauth['providers']  ||= []
T
tduehr 已提交
137 138 139 140
Settings.omniauth['cas3'] ||= Settingslogic.new({})
Settings.omniauth.cas3['session_duration'] ||= 8.hours
Settings.omniauth['session_tickets'] ||= Settingslogic.new({})
Settings.omniauth.session_tickets['cas3'] = 'ticket'
R
Riyad Preukschas 已提交
141

142

143 144 145
Settings['shared'] ||= Settingslogic.new({})
Settings.shared['path'] = File.expand_path(Settings.shared['path'] || "shared", Rails.root)

146 147
Settings['issues_tracker']  ||= {}

148 149 150
#
# GitLab
#
R
Riyad Preukschas 已提交
151
Settings['gitlab'] ||= Settingslogic.new({})
152
Settings.gitlab['default_projects_limit'] ||= 10
153
Settings.gitlab['default_branch_protection'] ||= 2
154
Settings.gitlab['default_can_create_group'] = true if Settings.gitlab['default_can_create_group'].nil?
155
Settings.gitlab['default_theme'] = Gitlab::Themes::APPLICATION_DEFAULT if Settings.gitlab['default_theme'].nil?
156
Settings.gitlab['host']       ||= ENV['GITLAB_HOST'] || 'localhost'
D
Dmitriy Zaporozhets 已提交
157
Settings.gitlab['ssh_host']   ||= Settings.gitlab.host
158
Settings.gitlab['https']        = false if Settings.gitlab['https'].nil?
R
Riyad Preukschas 已提交
159
Settings.gitlab['port']       ||= Settings.gitlab.https ? 443 : 80
160
Settings.gitlab['relative_url_root'] ||= ENV['RAILS_RELATIVE_URL_ROOT'] || ''
R
Riyad Preukschas 已提交
161
Settings.gitlab['protocol']   ||= Settings.gitlab.https ? "https" : "http"
162
Settings.gitlab['email_enabled'] ||= true if Settings.gitlab['email_enabled'].nil?
163 164 165
Settings.gitlab['email_from'] ||= ENV['GITLAB_EMAIL_FROM'] || "gitlab@#{Settings.gitlab.host}"
Settings.gitlab['email_display_name'] ||= ENV['GITLAB_EMAIL_DISPLAY_NAME'] || 'GitLab'
Settings.gitlab['email_reply_to'] ||= ENV['GITLAB_EMAIL_REPLY_TO'] || "noreply@#{Settings.gitlab.host}"
166
Settings.gitlab['base_url']   ||= Settings.send(:build_base_gitlab_url)
D
Dmitriy Zaporozhets 已提交
167
Settings.gitlab['url']        ||= Settings.send(:build_gitlab_url)
D
Dmitriy Zaporozhets 已提交
168
Settings.gitlab['user']       ||= 'git'
169 170 171 172 173
Settings.gitlab['user_home']  ||= begin
  Etc.getpwnam(Settings.gitlab['user']).dir
rescue ArgumentError # no user configured
  '/home/' + Settings.gitlab['user']
end
174
Settings.gitlab['time_zone']  ||= nil
D
Dmitriy Zaporozhets 已提交
175
Settings.gitlab['signup_enabled'] ||= true if Settings.gitlab['signup_enabled'].nil?
176
Settings.gitlab['signin_enabled'] ||= true if Settings.gitlab['signin_enabled'].nil?
177
Settings.gitlab['restricted_visibility_levels'] = Settings.send(:verify_constant_array, Gitlab::VisibilityLevel, Settings.gitlab['restricted_visibility_levels'], [])
178
Settings.gitlab['username_changing_enabled'] = true if Settings.gitlab['username_changing_enabled'].nil?
179
Settings.gitlab['issue_closing_pattern'] = '((?:[Cc]los(?:e[sd]?|ing)|[Ff]ix(?:e[sd]|ing)?|[Rr]esolv(?:e[sd]?|ing)) +(?:(?:issues? +)?%{issue_ref}(?:(?:, *| +and +)?)|([A-Z][A-Z0-9_]+-\d+))+)' if Settings.gitlab['issue_closing_pattern'].nil?
180
Settings.gitlab['default_projects_features'] ||= {}
181
Settings.gitlab['webhook_timeout'] ||= 10
182
Settings.gitlab['max_attachment_size'] ||= 10
183
Settings.gitlab['session_expire_delay'] ||= 10080
184 185 186
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?
187
Settings.gitlab.default_projects_features['snippets']       = false if Settings.gitlab.default_projects_features['snippets'].nil?
188
Settings.gitlab.default_projects_features['builds']         = true if Settings.gitlab.default_projects_features['builds'].nil?
189
Settings.gitlab.default_projects_features['visibility_level']    = Settings.send(:verify_constant, Gitlab::VisibilityLevel, Settings.gitlab.default_projects_features['visibility_level'], Gitlab::VisibilityLevel::PRIVATE)
190
Settings.gitlab['repository_downloads_path'] = File.join(Settings.shared['path'], 'cache/archive') if Settings.gitlab['repository_downloads_path'].nil?
191
Settings.gitlab['restricted_signup_domains'] ||= []
J
Jared Szechy 已提交
192
Settings.gitlab['import_sources'] ||= ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git']
R
Riyad Preukschas 已提交
193

V
Valery Sizov 已提交
194 195 196 197 198

#
# CI
#
Settings['gitlab_ci'] ||= Settingslogic.new({})
199 200 201 202 203
Settings.gitlab_ci['shared_runners_enabled'] = true if Settings.gitlab_ci['shared_runners_enabled'].nil?
Settings.gitlab_ci['all_broken_builds']     = true if Settings.gitlab_ci['all_broken_builds'].nil?
Settings.gitlab_ci['add_pusher']            = false if Settings.gitlab_ci['add_pusher'].nil?
Settings.gitlab_ci['url']                   ||= Settings.send(:build_gitlab_ci_url)
Settings.gitlab_ci['builds_path']           = File.expand_path(Settings.gitlab_ci['builds_path'] || "builds/", Rails.root)
V
Valery Sizov 已提交
204

D
Douwe Maan 已提交
205 206 207
#
# Reply by email
#
208
Settings['incoming_email'] ||= Settingslogic.new({})
209
Settings.incoming_email['enabled'] = false if Settings.incoming_email['enabled'].nil?
D
Douwe Maan 已提交
210

K
Kamil Trzcinski 已提交
211 212 213 214 215 216 217 218
#
# Build Artifacts
#
Settings['artifacts'] ||= Settingslogic.new({})
Settings.artifacts['enabled']      = true if Settings.artifacts['enabled'].nil?
Settings.artifacts['path']         = File.expand_path(Settings.artifacts['path'] || File.join(Settings.shared['path'], "artifacts"), Rails.root)
Settings.artifacts['max_size']    ||= 100 # in megabytes

M
Marin Jankovski 已提交
219 220 221 222
#
# Git LFS
#
Settings['lfs'] ||= Settingslogic.new({})
M
Marin Jankovski 已提交
223
Settings.lfs['enabled']      = true if Settings.lfs['enabled'].nil?
M
Marin Jankovski 已提交
224 225
Settings.lfs['storage_path'] = File.expand_path(Settings.lfs['storage_path'] || File.join(Settings.shared['path'], "lfs-objects"), Rails.root)

226 227 228
#
# Gravatar
#
R
Riyad Preukschas 已提交
229
Settings['gravatar'] ||= Settingslogic.new({})
230
Settings.gravatar['enabled']      = true if Settings.gravatar['enabled'].nil?
231 232
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'
233
Settings.gravatar['host']         = Settings.host_without_www(Settings.gravatar['plain_url'])
R
Riyad Preukschas 已提交
234

235 236 237 238 239 240
#
# Cron Jobs
#
Settings['cron_jobs'] ||= Settingslogic.new({})
Settings.cron_jobs['stuck_ci_builds_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['stuck_ci_builds_worker']['cron'] ||= '0 0 * * *'
241
Settings.cron_jobs['stuck_ci_builds_worker']['job_class'] = 'StuckCiBuildsWorker'
J
Jacob Vosmaer 已提交
242 243 244 245 246 247
Settings.cron_jobs['repo_check_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['repo_check_worker']['cron'] ||= '20 * * * *'
Settings.cron_jobs['repo_check_worker']['job_class'] = 'RepoCheckWorker'
Settings.cron_jobs['admin_email_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['admin_email_worker']['cron'] ||= '0 0 * * *'
Settings.cron_jobs['admin_email_worker']['job_class'] = 'AdminEmailWorker'
248

249 250 251 252
#
# GitLab Shell
#
Settings['gitlab_shell'] ||= Settingslogic.new({})
253
Settings.gitlab_shell['path']         ||= Settings.gitlab['user_home'] + '/gitlab-shell/'
254
Settings.gitlab_shell['hooks_path']   ||= Settings.gitlab['user_home'] + '/gitlab-shell/hooks/'
255
Settings.gitlab_shell['secret_file'] ||= Rails.root.join('.gitlab_shell_secret')
256 257
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?
258
Settings.gitlab_shell['repos_path']   ||= Settings.gitlab['user_home'] + '/repositories/'
D
Dmitriy Zaporozhets 已提交
259
Settings.gitlab_shell['ssh_host']     ||= Settings.gitlab.ssh_host
260 261 262 263
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 已提交
264

265 266 267
#
# Backup
#
R
Riyad Preukschas 已提交
268
Settings['backup'] ||= Settingslogic.new({})
D
Dmitriy Zaporozhets 已提交
269
Settings.backup['keep_time']  ||= 0
V
Valery Sizov 已提交
270
Settings.backup['pg_schema']    = nil
D
Dmitriy Zaporozhets 已提交
271
Settings.backup['path']         = File.expand_path(Settings.backup['path'] || "tmp/backups/", Rails.root)
272
Settings.backup['archive_permissions']          ||= 0600
273
Settings.backup['upload'] ||= Settingslogic.new({ 'remote_directory' => nil, 'connection' => nil })
274 275 276 277
# 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
278
Settings.backup['upload']['multipart_chunk_size'] ||= 104857600
279
Settings.backup['upload']['encryption'] ||= nil
R
Riyad Preukschas 已提交
280

281 282 283
#
# Git
#
R
Riyad Preukschas 已提交
284
Settings['git'] ||= Settingslogic.new({})
285
Settings.git['max_size']  ||= 20971520 # 20.megabytes
D
Dmitriy Zaporozhets 已提交
286
Settings.git['bin_path']  ||= '/usr/bin/git'
287
Settings.git['timeout']   ||= 10
288

289 290 291
# Important: keep the satellites.path setting until GitLab 9.0 at
# least. This setting is fed to 'rm -rf' in
# db/migrate/20151023144219_remove_satellites.rb
R
Riyad Preukschas 已提交
292
Settings['satellites'] ||= Settingslogic.new({})
R
Riyad Preukschas 已提交
293
Settings.satellites['path'] = File.expand_path(Settings.satellites['path'] || "tmp/repo_satellites/", Rails.root)
294

295

296 297 298 299
#
# Extra customization
#
Settings['extra'] ||= Settingslogic.new({})
300

301 302 303 304 305
#
# Rack::Attack settings
#
Settings['rack_attack'] ||= Settingslogic.new({})
Settings.rack_attack['git_basic_auth'] ||= Settingslogic.new({})
306
Settings.rack_attack.git_basic_auth['enabled'] = true if Settings.rack_attack.git_basic_auth['enabled'].nil?
307
Settings.rack_attack.git_basic_auth['ip_whitelist'] ||= %w{127.0.0.1}
308 309 310 311
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

312 313 314 315 316
#
# Testing settings
#
if Rails.env.test?
  Settings.gitlab['default_projects_limit']   = 42
317
  Settings.gitlab['default_can_create_group'] = true
318
  Settings.gitlab['default_can_create_team']  = false
R
Robert Speicher 已提交
319
end
320 321

# Force a refresh of application settings at startup
322 323 324 325 326 327 328
begin
  ApplicationSetting.expire
  Ci::ApplicationSetting.expire
rescue
  # Gracefully handle when Redis is not available. For example,
  # omnibus may fail here during assets:precompile.
end