BigW Consortium Gitlab

current_settings.rb 2.63 KB
Newer Older
1 2 3
module Gitlab
  module CurrentSettings
    def current_application_settings
4 5 6 7 8 9
      if RequestStore.active?
        RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
      else
        ensure_application_settings!
      end
    end
10

11
    def ensure_application_settings!
12 13
      if connect_to_db?
        begin
14
          settings = ::ApplicationSetting.current
15 16 17 18
        # In case Redis isn't running or the Redis UNIX socket file is not available
        rescue ::Redis::BaseError, ::Errno::ENOENT
          settings = ::ApplicationSetting.last
        end
19

20
        settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
21
      end
22 23

      settings || fake_application_settings
24
    end
25 26 27 28

    def fake_application_settings
      OpenStruct.new(
        default_projects_limit: Settings.gitlab['default_projects_limit'],
29
        default_branch_protection: Settings.gitlab['default_branch_protection'],
30 31 32
        signup_enabled: Settings.gitlab['signup_enabled'],
        signin_enabled: Settings.gitlab['signin_enabled'],
        gravatar_enabled: Settings.gravatar['enabled'],
33 34 35 36
        sign_in_text: nil,
        after_sign_up_text: nil,
        help_page_text: nil,
        shared_runners_text: nil,
37
        restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
38
        max_attachment_size: Settings.gitlab['max_attachment_size'],
39
        session_expire_delay: Settings.gitlab['session_expire_delay'],
40 41 42
        default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
        default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
        restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
43
        import_sources: %w[github bitbucket gitlab gitorious google_code fogbugz git gitlab_project],
44
        shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
45
        max_artifacts_size: Settings.artifacts['max_size'],
46
        require_two_factor_authentication: false,
47
        two_factor_grace_period: 48,
48 49
        akismet_enabled: false,
        repository_checks_enabled: true,
50
        container_registry_token_expire_delay: 5,
51
        user_default_external: false,
52 53
      )
    end
54 55 56 57

    private

    def connect_to_db?
58 59 60
      # When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
      active_db_connection = ActiveRecord::Base.connection.active? rescue false

61
      ENV['USE_DB'] != 'false' &&
62
      active_db_connection &&
63
      ActiveRecord::Base.connection.table_exists?('application_settings')
64 65 66

    rescue ActiveRecord::NoDatabaseError
      false
67
    end
68 69
  end
end