BigW Consortium Gitlab

1_settings.rb 15.1 KB
Newer Older
1
require_dependency Rails.root.join('lib/gitlab') # Load Gitlab as soon as possible
2

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)
10
    end
11

12 13
    def host_without_www(url)
      host(url).sub('www.', '')
14
    end
15

Valery Sizov committed
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
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}/"
35
      else
36
        if gitlab_shell.ssh_host.include? ':'
37
          "[#{user_host}]:"
38
        else
39
          "#{user_host}:"
40
        end
41 42 43
      end
    end

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

48
    def build_gitlab_url
49
      (base_gitlab_url + [gitlab.relative_url_root]).join('')
50
    end
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
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?
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
126

Valery Sizov committed
127

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
Settings.omniauth['allow_single_sign_on'] = false if Settings.omniauth['allow_single_sign_on'].nil?
132
Settings.omniauth['external_providers'] = [] if Settings.omniauth['external_providers'].nil?
133 134
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?
135
Settings.omniauth['auto_link_saml_user'] = false if Settings.omniauth['auto_link_saml_user'].nil?
136

137
Settings.omniauth['providers']  ||= []
138 139 140 141
Settings.omniauth['cas3'] ||= Settingslogic.new({})
Settings.omniauth.cas3['session_duration'] ||= 8.hours
Settings.omniauth['session_tickets'] ||= Settingslogic.new({})
Settings.omniauth.session_tickets['cas3'] = 'ticket'
142

143 144 145
# Fill out omniauth-gitlab settings. It is needed for easy set up GHE or GH by just specifying url.

github_default_url = "https://github.com"
146
github_settings = Settings.omniauth['providers'].find { |provider| provider["name"] == "github" }
147 148 149 150 151 152 153 154 155 156 157 158 159 160

if github_settings
  # For compatibility with old config files (before 7.8)
  # where people dont have url in github settings
  if github_settings['url'].blank?
    github_settings['url'] = github_default_url
  end

  github_settings["args"] ||= Settingslogic.new({})

  if github_settings["url"].include?(github_default_url)
    github_settings["args"]["client_options"] = OmniAuth::Strategies::GitHub.default_options[:client_options]
  else
    github_settings["args"]["client_options"] = {
161
      "site"          => File.join(github_settings["url"], "api/v3"),
162
      "authorize_url" => File.join(github_settings["url"], "login/oauth/authorize"),
163
      "token_url"     => File.join(github_settings["url"], "login/oauth/access_token")
164 165 166
    }
  end
end
167

168 169 170
Settings['shared'] ||= Settingslogic.new({})
Settings.shared['path'] = File.expand_path(Settings.shared['path'] || "shared", Rails.root)

171 172
Settings['issues_tracker']  ||= {}

173 174 175
#
# GitLab
#
176
Settings['gitlab'] ||= Settingslogic.new({})
177
Settings.gitlab['default_projects_limit'] ||= 10
178
Settings.gitlab['default_branch_protection'] ||= 2
179
Settings.gitlab['default_can_create_group'] = true if Settings.gitlab['default_can_create_group'].nil?
180
Settings.gitlab['default_theme'] = Gitlab::Themes::APPLICATION_DEFAULT if Settings.gitlab['default_theme'].nil?
181
Settings.gitlab['host']       ||= ENV['GITLAB_HOST'] || 'localhost'
182
Settings.gitlab['ssh_host']   ||= Settings.gitlab.host
183
Settings.gitlab['https']        = false if Settings.gitlab['https'].nil?
184
Settings.gitlab['port']       ||= Settings.gitlab.https ? 443 : 80
185
Settings.gitlab['relative_url_root'] ||= ENV['RAILS_RELATIVE_URL_ROOT'] || ''
186
Settings.gitlab['protocol']   ||= Settings.gitlab.https ? "https" : "http"
187
Settings.gitlab['email_enabled'] ||= true if Settings.gitlab['email_enabled'].nil?
188 189 190
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}"
191
Settings.gitlab['base_url']   ||= Settings.send(:build_base_gitlab_url)
192
Settings.gitlab['url']        ||= Settings.send(:build_gitlab_url)
193
Settings.gitlab['user']       ||= 'git'
194 195 196 197 198
Settings.gitlab['user_home']  ||= begin
  Etc.getpwnam(Settings.gitlab['user']).dir
rescue ArgumentError # no user configured
  '/home/' + Settings.gitlab['user']
end
199
Settings.gitlab['time_zone']  ||= nil
200
Settings.gitlab['signup_enabled'] ||= true if Settings.gitlab['signup_enabled'].nil?
201
Settings.gitlab['signin_enabled'] ||= true if Settings.gitlab['signin_enabled'].nil?
202
Settings.gitlab['restricted_visibility_levels'] = Settings.send(:verify_constant_array, Gitlab::VisibilityLevel, Settings.gitlab['restricted_visibility_levels'], [])
203
Settings.gitlab['username_changing_enabled'] = true if Settings.gitlab['username_changing_enabled'].nil?
Jacob Schatz committed
204
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?
205
Settings.gitlab['default_projects_features'] ||= {}
206
Settings.gitlab['webhook_timeout'] ||= 10
207
Settings.gitlab['max_attachment_size'] ||= 10
208
Settings.gitlab['session_expire_delay'] ||= 10080
209 210 211
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?
212
Settings.gitlab.default_projects_features['snippets']       = false if Settings.gitlab.default_projects_features['snippets'].nil?
213
Settings.gitlab.default_projects_features['builds']         = true if Settings.gitlab.default_projects_features['builds'].nil?
214
Settings.gitlab.default_projects_features['visibility_level']    = Settings.send(:verify_constant, Gitlab::VisibilityLevel, Settings.gitlab.default_projects_features['visibility_level'], Gitlab::VisibilityLevel::PRIVATE)
215
Settings.gitlab['repository_downloads_path'] = File.join(Settings.shared['path'], 'cache/archive') if Settings.gitlab['repository_downloads_path'].nil?
216
Settings.gitlab['restricted_signup_domains'] ||= []
Jared Szechy committed
217
Settings.gitlab['import_sources'] ||= ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git']
218
Settings.gitlab['trusted_proxies'] ||= []
219

Valery Sizov committed
220 221 222 223 224

#
# CI
#
Settings['gitlab_ci'] ||= Settingslogic.new({})
225 226 227 228 229
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)
Valery Sizov committed
230

Douwe Maan committed
231 232 233
#
# Reply by email
#
234
Settings['incoming_email'] ||= Settingslogic.new({})
235
Settings.incoming_email['enabled'] = false if Settings.incoming_email['enabled'].nil?
Douwe Maan committed
236

237 238 239 240 241 242 243 244
#
# 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

245 246 247 248
#
# Git LFS
#
Settings['lfs'] ||= Settingslogic.new({})
Marin Jankovski committed
249
Settings.lfs['enabled']      = true if Settings.lfs['enabled'].nil?
250 251
Settings.lfs['storage_path'] = File.expand_path(Settings.lfs['storage_path'] || File.join(Settings.shared['path'], "lfs-objects"), Rails.root)

252 253 254
#
# Gravatar
#
255
Settings['gravatar'] ||= Settingslogic.new({})
256
Settings.gravatar['enabled']      = true if Settings.gravatar['enabled'].nil?
257 258
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'
259
Settings.gravatar['host']         = Settings.host_without_www(Settings.gravatar['plain_url'])
260

261 262 263 264 265 266
#
# 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 * * *'
267
Settings.cron_jobs['stuck_ci_builds_worker']['job_class'] = 'StuckCiBuildsWorker'
268 269
Settings.cron_jobs['repository_check_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['repository_check_worker']['cron'] ||= '20 * * * *'
270
Settings.cron_jobs['repository_check_worker']['job_class'] = 'RepositoryCheck::BatchWorker'
Jacob Vosmaer committed
271
Settings.cron_jobs['admin_email_worker'] ||= Settingslogic.new({})
272
Settings.cron_jobs['admin_email_worker']['cron'] ||= '0 0 * * 0'
Jacob Vosmaer committed
273
Settings.cron_jobs['admin_email_worker']['job_class'] = 'AdminEmailWorker'
274 275 276
Settings.cron_jobs['repository_archive_cache_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['repository_archive_cache_worker']['cron'] ||= '0 * * * *'
Settings.cron_jobs['repository_archive_cache_worker']['job_class'] = 'RepositoryArchiveCacheWorker'
277

278 279 280 281
#
# GitLab Shell
#
Settings['gitlab_shell'] ||= Settingslogic.new({})
282
Settings.gitlab_shell['path']         ||= Settings.gitlab['user_home'] + '/gitlab-shell/'
283
Settings.gitlab_shell['hooks_path']   ||= Settings.gitlab['user_home'] + '/gitlab-shell/hooks/'
284
Settings.gitlab_shell['secret_file'] ||= Rails.root.join('.gitlab_shell_secret')
285 286
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?
287
Settings.gitlab_shell['repos_path']   ||= Settings.gitlab['user_home'] + '/repositories/'
288
Settings.gitlab_shell['ssh_host']     ||= Settings.gitlab.ssh_host
289 290 291 292
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)
293

294 295 296
#
# Backup
#
297
Settings['backup'] ||= Settingslogic.new({})
298
Settings.backup['keep_time']  ||= 0
299
Settings.backup['pg_schema']    = nil
300
Settings.backup['path']         = File.expand_path(Settings.backup['path'] || "tmp/backups/", Rails.root)
301
Settings.backup['archive_permissions']          ||= 0600
302
Settings.backup['upload'] ||= Settingslogic.new({ 'remote_directory' => nil, 'connection' => nil })
303 304 305 306
# 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
307
Settings.backup['upload']['multipart_chunk_size'] ||= 104857600
308
Settings.backup['upload']['encryption'] ||= nil
309

310 311 312
#
# Git
#
313
Settings['git'] ||= Settingslogic.new({})
314
Settings.git['max_size']  ||= 20971520 # 20.megabytes
315
Settings.git['bin_path']  ||= '/usr/bin/git'
316
Settings.git['timeout']   ||= 10
317

318 319 320
# 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
321
Settings['satellites'] ||= Settingslogic.new({})
Riyad Preukschas committed
322
Settings.satellites['path'] = File.expand_path(Settings.satellites['path'] || "tmp/repo_satellites/", Rails.root)
323

324

325 326 327 328
#
# Extra customization
#
Settings['extra'] ||= Settingslogic.new({})
329

330 331 332 333 334
#
# Rack::Attack settings
#
Settings['rack_attack'] ||= Settingslogic.new({})
Settings.rack_attack['git_basic_auth'] ||= Settingslogic.new({})
335
Settings.rack_attack.git_basic_auth['enabled'] = true if Settings.rack_attack.git_basic_auth['enabled'].nil?
336
Settings.rack_attack.git_basic_auth['ip_whitelist'] ||= %w{127.0.0.1}
337 338 339 340
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

341 342 343 344 345
#
# Testing settings
#
if Rails.env.test?
  Settings.gitlab['default_projects_limit']   = 42
346
  Settings.gitlab['default_can_create_group'] = true
347
  Settings.gitlab['default_can_create_team']  = false
Robert Speicher committed
348
end
349 350

# Force a refresh of application settings at startup
351 352 353 354 355 356 357
begin
  ApplicationSetting.expire
  Ci::ApplicationSetting.expire
rescue
  # Gracefully handle when Redis is not available. For example,
  # omnibus may fail here during assets:precompile.
end