1_settings.rb 12.9 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

V
Valery Sizov 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
    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 已提交
34

35
    def build_gitlab_shell_ssh_path_prefix
36 37
      user_host = "#{gitlab_shell.ssh_user}@#{gitlab_shell.ssh_host}"

38
      if gitlab_shell.ssh_port != 22
39
        "ssh://#{user_host}:#{gitlab_shell.ssh_port}/"
R
Riyad Preukschas 已提交
40
      else
41
        if gitlab_shell.ssh_host.include? ':'
42
          "[#{user_host}]:"
43
        else
44
          "#{user_host}:"
45
        end
R
Riyad Preukschas 已提交
46 47 48
      end
    end

49 50 51 52
    def build_base_gitlab_url
      base_gitlab_url.join('')
    end

R
Riyad Preukschas 已提交
53
    def build_gitlab_url
54
      (base_gitlab_url + [gitlab.relative_url_root]).join('')
R
Riyad Preukschas 已提交
55
    end
D
Dmitriy Zaporozhets 已提交
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    # 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
79 80 81 82 83 84 85 86 87 88 89

    private

    def base_gitlab_url
      custom_port = gitlab_on_standard_port? ? nil : ":#{gitlab.port}"
      [ gitlab.protocol,
        "://",
        gitlab.host,
        custom_port
      ]
    end
90 91
  end
end
R
Riyad Preukschas 已提交
92 93 94 95


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

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

109
  Settings.ldap['servers'].each do |key, server|
110
    server['label'] ||= 'LDAP'
111
    server['block_auto_created_users'] = false if server['block_auto_created_users'].nil?
112 113
    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 已提交
114
    server['attributes'] = {} if server['attributes'].nil?
115
    server['provider_name'] ||= "ldap#{key}".downcase
116 117 118
    server['provider_class'] = OmniAuth::Utils.camelize(server['provider_name'])
  end
end
R
Riyad Preukschas 已提交
119

V
Valery Sizov 已提交
120

R
Riyad Preukschas 已提交
121
Settings['omniauth'] ||= Settingslogic.new({})
122
Settings.omniauth['enabled']      = false if Settings.omniauth['enabled'].nil?
123
Settings.omniauth['auto_sign_in_with_provider'] = false if Settings.omniauth['auto_sign_in_with_provider'].nil?
124 125 126
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?
127

R
Riyad Preukschas 已提交
128 129
Settings.omniauth['providers']  ||= []

130 131 132
Settings['shared'] ||= Settingslogic.new({})
Settings.shared['path'] = File.expand_path(Settings.shared['path'] || "shared", Rails.root)

133 134
Settings['issues_tracker']  ||= {}

135 136 137
#
# GitLab
#
R
Riyad Preukschas 已提交
138
Settings['gitlab'] ||= Settingslogic.new({})
139
Settings.gitlab['default_projects_limit'] ||= 10
140
Settings.gitlab['default_branch_protection'] ||= 2
141
Settings.gitlab['default_can_create_group'] = true if Settings.gitlab['default_can_create_group'].nil?
142
Settings.gitlab['default_theme'] = Gitlab::Themes::APPLICATION_DEFAULT if Settings.gitlab['default_theme'].nil?
D
Dmitriy Zaporozhets 已提交
143
Settings.gitlab['host']       ||= 'localhost'
D
Dmitriy Zaporozhets 已提交
144
Settings.gitlab['ssh_host']   ||= Settings.gitlab.host
145
Settings.gitlab['https']        = false if Settings.gitlab['https'].nil?
R
Riyad Preukschas 已提交
146
Settings.gitlab['port']       ||= Settings.gitlab.https ? 443 : 80
147
Settings.gitlab['relative_url_root'] ||= ENV['RAILS_RELATIVE_URL_ROOT'] || ''
R
Riyad Preukschas 已提交
148
Settings.gitlab['protocol']   ||= Settings.gitlab.https ? "https" : "http"
149 150 151 152
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}"
153
Settings.gitlab['base_url']   ||= Settings.send(:build_base_gitlab_url)
D
Dmitriy Zaporozhets 已提交
154
Settings.gitlab['url']        ||= Settings.send(:build_gitlab_url)
D
Dmitriy Zaporozhets 已提交
155
Settings.gitlab['user']       ||= 'git'
156 157 158 159 160
Settings.gitlab['user_home']  ||= begin
  Etc.getpwnam(Settings.gitlab['user']).dir
rescue ArgumentError # no user configured
  '/home/' + Settings.gitlab['user']
end
161
Settings.gitlab['time_zone']  ||= nil
D
Dmitriy Zaporozhets 已提交
162
Settings.gitlab['signup_enabled'] ||= true if Settings.gitlab['signup_enabled'].nil?
163
Settings.gitlab['signin_enabled'] ||= true if Settings.gitlab['signin_enabled'].nil?
164
Settings.gitlab['twitter_sharing_enabled'] ||= true if Settings.gitlab['twitter_sharing_enabled'].nil?
165
Settings.gitlab['restricted_visibility_levels'] = Settings.send(:verify_constant_array, Gitlab::VisibilityLevel, Settings.gitlab['restricted_visibility_levels'], [])
166
Settings.gitlab['username_changing_enabled'] = true if Settings.gitlab['username_changing_enabled'].nil?
167
Settings.gitlab['issue_closing_pattern'] = '((?:[Cc]los(?:e[sd]?|ing)|[Ff]ix(?:e[sd]|ing)?|[Rr]esolv(?:e[sd]?|ing)) +(?:(?:issues? +)?%{issue_ref}(?:(?:, *| +and +)?))+)' if Settings.gitlab['issue_closing_pattern'].nil?
168
Settings.gitlab['default_projects_features'] ||= {}
169
Settings.gitlab['webhook_timeout'] ||= 10
170
Settings.gitlab['max_attachment_size'] ||= 10
171
Settings.gitlab['session_expire_delay'] ||= 10080
172 173 174
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?
175
Settings.gitlab.default_projects_features['snippets']       = false if Settings.gitlab.default_projects_features['snippets'].nil?
176
Settings.gitlab.default_projects_features['builds']         = true if Settings.gitlab.default_projects_features['builds'].nil?
177
Settings.gitlab.default_projects_features['visibility_level']    = Settings.send(:verify_constant, Gitlab::VisibilityLevel, Settings.gitlab.default_projects_features['visibility_level'], Gitlab::VisibilityLevel::PRIVATE)
178
Settings.gitlab['repository_downloads_path'] = File.join(Settings.shared['path'], 'cache/archive') if Settings.gitlab['repository_downloads_path'].nil?
179
Settings.gitlab['restricted_signup_domains'] ||= []
J
Jared Szechy 已提交
180
Settings.gitlab['import_sources'] ||= ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git']
R
Riyad Preukschas 已提交
181

V
Valery Sizov 已提交
182 183 184 185 186

#
# CI
#
Settings['gitlab_ci'] ||= Settingslogic.new({})
187 188 189 190 191
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 已提交
192

D
Douwe Maan 已提交
193 194 195
#
# Reply by email
#
196
Settings['incoming_email'] ||= Settingslogic.new({})
197 198
Settings.incoming_email['enabled']    = false if Settings.incoming_email['enabled'].nil?
Settings.incoming_email['port']       = 143 if Settings.incoming_email['port'].nil?
D
Douwe Maan 已提交
199 200
Settings.incoming_email['ssl']        = false if Settings.incoming_email['ssl'].nil?
Settings.incoming_email['start_tls']  = false if Settings.incoming_email['start_tls'].nil?
201
Settings.incoming_email['mailbox']    = "inbox" if Settings.incoming_email['mailbox'].nil?
D
Douwe Maan 已提交
202

K
Kamil Trzcinski 已提交
203 204 205 206 207 208 209 210
#
# 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 已提交
211 212 213 214
#
# Git LFS
#
Settings['lfs'] ||= Settingslogic.new({})
M
Marin Jankovski 已提交
215
Settings.lfs['enabled']      = true if Settings.lfs['enabled'].nil?
M
Marin Jankovski 已提交
216 217
Settings.lfs['storage_path'] = File.expand_path(Settings.lfs['storage_path'] || File.join(Settings.shared['path'], "lfs-objects"), Rails.root)

218 219 220
#
# Gravatar
#
R
Riyad Preukschas 已提交
221
Settings['gravatar'] ||= Settingslogic.new({})
222
Settings.gravatar['enabled']      = true if Settings.gravatar['enabled'].nil?
223 224
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'
225
Settings.gravatar['host']         = Settings.get_host_without_www(Settings.gravatar['plain_url'])
R
Riyad Preukschas 已提交
226

227 228 229 230
#
# GitLab Shell
#
Settings['gitlab_shell'] ||= Settingslogic.new({})
231
Settings.gitlab_shell['path']         ||= Settings.gitlab['user_home'] + '/gitlab-shell/'
232
Settings.gitlab_shell['hooks_path']   ||= Settings.gitlab['user_home'] + '/gitlab-shell/hooks/'
233
Settings.gitlab_shell['secret_file'] ||= Rails.root.join('.gitlab_shell_secret')
234 235
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?
236
Settings.gitlab_shell['repos_path']   ||= Settings.gitlab['user_home'] + '/repositories/'
D
Dmitriy Zaporozhets 已提交
237
Settings.gitlab_shell['ssh_host']     ||= Settings.gitlab.ssh_host
238 239 240 241
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 已提交
242

243 244 245
#
# Backup
#
R
Riyad Preukschas 已提交
246
Settings['backup'] ||= Settingslogic.new({})
D
Dmitriy Zaporozhets 已提交
247
Settings.backup['keep_time']  ||= 0
V
Valery Sizov 已提交
248
Settings.backup['pg_schema']    = nil
D
Dmitriy Zaporozhets 已提交
249
Settings.backup['path']         = File.expand_path(Settings.backup['path'] || "tmp/backups/", Rails.root)
250
Settings.backup['archive_permissions']          ||= 0600
251
Settings.backup['upload'] ||= Settingslogic.new({ 'remote_directory' => nil, 'connection' => nil })
252 253 254 255
# 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
256
Settings.backup['upload']['multipart_chunk_size'] ||= 104857600
257
Settings.backup['upload']['encryption'] ||= nil
R
Riyad Preukschas 已提交
258

259 260 261
#
# Git
#
R
Riyad Preukschas 已提交
262
Settings['git'] ||= Settingslogic.new({})
263
Settings.git['max_size']  ||= 20971520 # 20.megabytes
D
Dmitriy Zaporozhets 已提交
264
Settings.git['bin_path']  ||= '/usr/bin/git'
265
Settings.git['timeout']   ||= 10
266

267 268 269
# 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 已提交
270
Settings['satellites'] ||= Settingslogic.new({})
R
Riyad Preukschas 已提交
271
Settings.satellites['path'] = File.expand_path(Settings.satellites['path'] || "tmp/repo_satellites/", Rails.root)
272

273

274 275 276 277
#
# Extra customization
#
Settings['extra'] ||= Settingslogic.new({})
278

279 280 281 282 283
#
# Rack::Attack settings
#
Settings['rack_attack'] ||= Settingslogic.new({})
Settings.rack_attack['git_basic_auth'] ||= Settingslogic.new({})
284
Settings.rack_attack.git_basic_auth['enabled'] = true if Settings.rack_attack.git_basic_auth['enabled'].nil?
285
Settings.rack_attack.git_basic_auth['ip_whitelist'] ||= %w{127.0.0.1}
286 287 288 289
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

290 291 292 293 294
#
# Testing settings
#
if Rails.env.test?
  Settings.gitlab['default_projects_limit']   = 42
295
  Settings.gitlab['default_can_create_group'] = true
296
  Settings.gitlab['default_can_create_team']  = false
R
Robert Speicher 已提交
297
end
298 299

# Force a refresh of application settings at startup
300 301 302 303 304 305 306
begin
  ApplicationSetting.expire
  Ci::ApplicationSetting.expire
rescue
  # Gracefully handle when Redis is not available. For example,
  # omnibus may fail here during assets:precompile.
end