Skip to content

Commit dec8f94

Browse files
balasankarcClemens Beckpursultani
committed
Merge branch '8285-support-external-consul-2' into 'master'
Support external consul binary Closes #8332 See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7278 Merged-by: Balasankar 'Balu' C <[email protected]> Reviewed-by: Balasankar 'Balu' C <[email protected]> Co-authored-by: Clemens Beck <[email protected]> Co-authored-by: Hossein Pursultani <[email protected]>
2 parents e23ac18 + 5424583 commit dec8f94

File tree

14 files changed

+218
-8
lines changed

14 files changed

+218
-8
lines changed

config/software/consul.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
name 'consul'
1818

19+
# This version should be keep in sync with consul versions in
20+
# consul_download.rb and consul_helper.rb.
1921
version = Gitlab::Version.new('consul', 'v1.16.3')
2022
default_version version.print(false)
2123

deps.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ dependencies:
7979
- filename: config/software/consul.rb
8080
pattern: "version = Gitlab::Version.new\\('consul', 'v(\\S+)'\\)"
8181
range: '< 1.17.0'
82+
- filename: files/gitlab-ctl-commands-ee/lib/consul_download.rb
83+
pattern: "DEFAULT_VERSION = '(\\S+)'"
84+
range: '< 1.17.0'
85+
- filename: files/gitlab-cookbooks/consul/libraries/consul_helper.rb
86+
pattern: "SUPPORTED_MINOR = '(\\S+)'"
87+
tag_filter:
88+
output_as: '$1.$2'
8289
# go-crond
8390
https://github.com/webdevops/go-crond.git:
8491
replace_in_files:

files/gitlab-config-template/gitlab.rb.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3234,6 +3234,7 @@ external_url 'GENERATED_EXTERNAL_URL'
32343234
# Consul (EE only)
32353235
################################################################################
32363236
# consul['enable'] = false
3237+
# consul['binary_path'] = '/opt/gitlab/embedded/bin/consul'
32373238
# consul['dir'] = '/var/opt/gitlab/consul'
32383239
# consul['username'] = 'gitlab-consul'
32393240
# consul['group'] = 'gitlab-consul'

files/gitlab-cookbooks/consul/attributes/default.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
default['consul']['enable'] = false
2+
default['consul']['binary_path'] = '/opt/gitlab/embedded/bin/consul'
23
default['consul']['dir'] = '/var/opt/gitlab/consul'
34
default['consul']['username'] = 'gitlab-consul'
45
default['consul']['group'] = 'gitlab-consul'

files/gitlab-cookbooks/consul/libraries/consul_helper.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class ConsulHelper
99
# provide an `enable_service_#{service_name}` and `disable_service_#{service_name}` recipe
1010
SERVICES = %w(postgresql).freeze
1111

12+
# This version should be keep in sync with consul versions in
13+
# software/consul.rb and consul_download.rb.
14+
SUPPORTED_MINOR = '1.16'.freeze
15+
1216
def initialize(node)
1317
@node = node
1418
@default_configuration = {
@@ -176,7 +180,7 @@ def disabled_services
176180
def installed_version
177181
return unless OmnibusHelper.new(@node).service_up?('consul')
178182

179-
command = '/opt/gitlab/embedded/bin/consul version'
183+
command = "#{@node['consul']['binary_path']} version"
180184
command_output = VersionHelper.version(command)
181185
raise "Execution of the command `#{command}` failed" unless command_output
182186

@@ -195,6 +199,16 @@ def running_version
195199
info[:Config][:Version] unless info.empty?
196200
end
197201

202+
def installed_is_supported?
203+
installed = installed_version
204+
return true if installed.nil?
205+
206+
major_installed, minor_installed = installed.split('.')[0..1]
207+
major_supported, minor_supported = SUPPORTED_MINOR.split('.')
208+
209+
major_installed == major_supported && minor_installed == minor_supported
210+
end
211+
198212
private
199213

200214
def verify_incoming?

files/gitlab-cookbooks/consul/recipes/enable_daemon.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
runit_service 'consul' do
2222
options({
23+
binary_path: node['consul']['binary_path'],
2324
config_dir: node['consul']['config_dir'],
2425
custom_config_dir: node['consul']['custom_config_dir'],
2526
config_file: node['consul']['config_file'],
@@ -57,3 +58,17 @@
5758
end
5859
only_if { consul_helper.running_version != consul_helper.installed_version }
5960
end
61+
62+
ruby_block 'warn consul version mismatch' do
63+
block do
64+
message = <<~MESSAGE
65+
The version of the installed consul service is different than what is expected.
66+
Please run `gitlab-ctl consul-download --force` and restart consul to start
67+
the new version:
68+
69+
https://docs.gitlab.com/ee/administration/consul.html#restart-consul
70+
MESSAGE
71+
LoggingHelper.warning(message)
72+
end
73+
not_if { consul_helper.installed_is_supported? }
74+
end

files/gitlab-cookbooks/consul/templates/default/sv-consul-run.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ cd <%= @options[:dir] %>
99
exec chpst -P -e <%= @options[:env_dir] %> \
1010
-U <%= @options[:user] %>:<%= @options[:groupname] %> \
1111
-u <%= @options[:user] %>:<%= @options[:groupname] %> \
12-
/opt/gitlab/embedded/bin/consul agent -config-file <%= @options[:config_file] %> \
12+
<%= @options[:binary_path] %> \
13+
agent \
14+
-config-file <%= @options[:config_file] %> \
1315
-config-dir <%= @options[:config_dir] %> \
1416
<% unless @options[:custom_config_dir].nil? -%>
1517
-config-dir <%= @options[:custom_config_dir] %> \

files/gitlab-ctl-commands-ee/consul.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
#
1616

1717
require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/consul"
18+
require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/consul_download"
1819

1920
add_command_under_category('consul', 'consul', 'Interact with the gitlab-consul cluster', 2) do
2021
consul = ConsulHandler.new(ARGV, $stdin.gets)
2122
consul.execute
2223
end
24+
25+
add_command_under_category('consul-download', 'consul', 'Download consul for the gitlab-consul cluster', 2) do
26+
ConsulDownloadCommand.new(ARGV).run
27+
end

files/gitlab-ctl-commands-ee/lib/consul.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
require 'mixlib/shellout'
2+
# For testing purposes, if the first path cannot be found load the second
3+
begin
4+
require_relative '../../omnibus-ctl/lib/gitlab_ctl'
5+
rescue LoadError
6+
require_relative '../../gitlab-ctl-commands/lib/gitlab_ctl'
7+
end
28

39
class ConsulHandler
410
WatcherError = Class.new(StandardError)
@@ -17,7 +23,7 @@ def execute
1723

1824
class << self
1925
def run_consul(cmd)
20-
command = Mixlib::ShellOut.new("/opt/gitlab/embedded/bin/consul #{cmd}")
26+
command = Mixlib::ShellOut.new("#{consul_binary} #{cmd}")
2127
command.run_command
2228
begin
2329
command.error!
@@ -28,6 +34,12 @@ def run_consul(cmd)
2834
end
2935
command.stdout
3036
end
37+
38+
private
39+
40+
def consul_binary
41+
GitlabCtl::Util.get_node_attributes.dig('consul', 'binary_path')
42+
end
3143
end
3244

3345
class Kv
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
require 'net/http'
2+
require 'zip'
3+
4+
class ConsulDownloadCommand
5+
# This version should be keep in sync with consul versions in
6+
# software/consul.rb and consul_helper.rb.
7+
DEFAULT_VERSION = '1.16.3'.freeze
8+
9+
def initialize(args)
10+
@args = args
11+
@options = {
12+
version: DEFAULT_VERSION,
13+
architecture: default_architecture,
14+
output: '/usr/local/bin/consul',
15+
force: false,
16+
}
17+
18+
parse_options!
19+
end
20+
21+
def run
22+
if !@options[:force] && File.exist?(@options[:output])
23+
warn("Consul binary already exists! Use --force to overwrite #{@options[:output]}.")
24+
return
25+
end
26+
27+
response = Net::HTTP.get_response(download_url)
28+
29+
if response.code != '200'
30+
warn("Error downloading consul from #{download_url}: #{resp.code}")
31+
return
32+
end
33+
34+
Zip::File.open_buffer(StringIO.new(response.body)) do |zipio|
35+
zipio.each do |entry|
36+
next unless entry.file? && entry.name == 'consul'
37+
38+
File.write(@options[:output], entry.get_input_stream.read)
39+
File.chmod(0755, @options[:output])
40+
puts "Successfully downloaded Consul #{@options[:version]} to #{@options[:output]}."
41+
puts "Start using the external Consul by setting consul['binary_path'] = '#{@options[:output]}' in your `gitlab.rb` and run `gitlab-ctl reconfigure`"
42+
end
43+
end
44+
end
45+
46+
private
47+
48+
def parse_options!
49+
opts_parser = OptionParser.new do |opts|
50+
opts.banner = 'Usage: gitlab-ctl consul download [options]'
51+
52+
opts.on('-v', '--version=VERSION', 'Version of the consul binary to be downloaded') do |v|
53+
@options[:version] = v
54+
end
55+
56+
opts.on('-a', '--arch=ARCHITECTURE', 'Architecture of the consul binary to be downloaded') do |v|
57+
@options[:architecture] = v
58+
end
59+
60+
opts.on('-o', '--output=PATH', 'Path to write the consul binary to') do |v|
61+
@options[:output] = v
62+
end
63+
64+
opts.on('-f', '--force', 'Overwrite existing consul binary') do |v|
65+
@options[:force] = v
66+
end
67+
68+
opts.on_tail('-h', '--help', 'Show this message') do
69+
puts opts
70+
exit
71+
end
72+
end
73+
74+
opts_parser.parse!(@args.dup)
75+
end
76+
77+
def download_url
78+
URI.parse("https://releases.hashicorp.com/consul/#{@options[:version]}/consul_#{@options[:version]}_linux_#{@options[:architecture]}.zip")
79+
end
80+
81+
def default_architecture
82+
GitlabCtl::Util.get_node_attributes.dig('kernel', 'machine') || 'amd64'
83+
end
84+
end

0 commit comments

Comments
 (0)