diff options
author | Douwe Maan <douwe@gitlab.com> | 2018-04-23 15:21:46 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-04-23 15:21:46 +0000 |
commit | 1f062ee0d04175bbed3ea1d23a3e090230d4b9bd (patch) | |
tree | 165998a59d8eadb0ced83e3a7bba3f0a039f4a2e /lib | |
parent | 38b35076575e440860d45625747da882dfc97af7 (diff) | |
parent | 2afe3a1210e6c444f9b92d59a02fcb3f474ae614 (diff) | |
download | gitlab-ce-1f062ee0d04175bbed3ea1d23a3e090230d4b9bd.tar.gz |
Merge branch '44713-fast-spec-helper' into 'master'
Resolve "Skip setup of gitlab-shell, gitaly, ... for specs that don't need it."
Closes #44713
See merge request gitlab-org/gitlab-ce!18074
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab.rb | 13 | ||||
-rw-r--r-- | lib/gitlab/git.rb | 2 | ||||
-rw-r--r-- | lib/settings.rb | 126 |
3 files changed, 140 insertions, 1 deletions
diff --git a/lib/gitlab.rb b/lib/gitlab.rb index f6629982512..0a167104bf4 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -1,9 +1,20 @@ -require_dependency 'gitlab/git' +require_dependency 'settings' +require_dependency 'gitlab/popen' module Gitlab + def self.root + Pathname.new(File.expand_path('..', __dir__)) + end + + def self.config + Settings + end + COM_URL = 'https://gitlab.com'.freeze APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))} SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z} + VERSION = File.read(root.join("VERSION")).strip.freeze + REVISION = Gitlab::Popen.popen(%W(#{config.git.bin_path} log --pretty=format:%h -n 1)).first.chomp.freeze def self.com? # Check `gl_subdomain?` as well to keep parity with gitlab.com diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb index c9abea90d21..e85e87a54af 100644 --- a/lib/gitlab/git.rb +++ b/lib/gitlab/git.rb @@ -1,3 +1,5 @@ +require_dependency 'gitlab/encoding_helper' + module Gitlab module Git # The ID of empty tree. diff --git a/lib/settings.rb b/lib/settings.rb new file mode 100644 index 00000000000..69d637761ea --- /dev/null +++ b/lib/settings.rb @@ -0,0 +1,126 @@ +require 'settingslogic' + +class Settings < Settingslogic + source ENV.fetch('GITLAB_CONFIG') { Pathname.new(File.expand_path('..', __dir__)).join('config/gitlab.yml') } + namespace ENV.fetch('GITLAB_ENV') { Rails.env } + + class << self + def gitlab_on_standard_port? + on_standard_port?(gitlab) + end + + def host_without_www(url) + host(url).sub('www.', '') + end + + def build_gitlab_ci_url + custom_port = + if on_standard_port?(gitlab) + nil + else + ":#{gitlab.port}" + end + + [ + gitlab.protocol, + "://", + gitlab.host, + custom_port, + gitlab.relative_url_root + ].join('') + end + + def build_pages_url + base_url(pages).join('') + end + + def build_gitlab_shell_ssh_path_prefix + user_host = "#{gitlab_shell.ssh_user}@#{gitlab_shell.ssh_host}" + + if gitlab_shell.ssh_port != 22 + "ssh://#{user_host}:#{gitlab_shell.ssh_port}/" + else + if gitlab_shell.ssh_host.include? ':' + "[#{user_host}]:" + else + "#{user_host}:" + end + end + end + + def build_base_gitlab_url + base_url(gitlab).join('') + end + + def build_gitlab_url + (base_url(gitlab) + [gitlab.relative_url_root]).join('') + end + + # check that values in `current` (string or integer) is a contant in `modul`. + def verify_constant_array(modul, current, default) + values = default || [] + unless 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 + + def absolute(path) + File.expand_path(path, Rails.root) + end + + private + + def base_url(config) + custom_port = on_standard_port?(config) ? nil : ":#{config.port}" + + [ + config.protocol, + "://", + config.host, + custom_port + ] + end + + def on_standard_port?(config) + config.port.to_i == (config.https ? 443 : 80) + end + + # 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 + + # Runs every minute in a random ten-minute period on Sundays, to balance the + # load on the server receiving these pings. The usage ping is safe to run + # multiple times because of a 24 hour exclusive lock. + def cron_for_usage_ping + hour = rand(24) + minute = rand(6) + + "#{minute}0-#{minute}9 #{hour} * * 0" + end + end +end |