Skip to content

Commit 3f2b0e7

Browse files
Robert Marshallbalasankarc
andcommitted
Merge branch 'redis-version-information-new-redis-helper' into 'master'
Move Redis server information to NewRedisHelper See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7465 Merged-by: Robert Marshall <[email protected]> Approved-by: Dustin Collins <[email protected]> Approved-by: Andrew Patterson <[email protected]> Approved-by: Robert Marshall <[email protected]> Co-authored-by: Balasankar 'Balu' C <[email protected]>
2 parents ef895d0 + be58ec8 commit 3f2b0e7

File tree

7 files changed

+242
-224
lines changed

7 files changed

+242
-224
lines changed

files/gitlab-cookbooks/gitlab/libraries/redis_helper.rb

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -116,90 +116,6 @@ def redis_sentinel_urls(sentinels_key)
116116
end
117117
end
118118

119-
def running_version
120-
return unless OmnibusHelper.new(@node).service_up?('redis')
121-
122-
commands = ['/opt/gitlab/embedded/bin/redis-cli']
123-
commands << redis_cli_connect_options
124-
commands << "INFO"
125-
command = commands.join(" ")
126-
127-
command_output = VersionHelper.version(command)
128-
raise "Execution of the command `#{command}` failed" unless command_output
129-
130-
version_match = command_output.match(/redis_version:(?<redis_version>\d*\.\d*\.\d*)/)
131-
raise "Execution of the command `#{command}` generated unexpected output `#{command_output.strip}`" unless version_match
132-
133-
version_match['redis_version']
134-
end
135-
136-
def installed_version
137-
return unless OmnibusHelper.new(@node).service_up?('redis')
138-
139-
command = '/opt/gitlab/embedded/bin/redis-server --version'
140-
141-
command_output = VersionHelper.version(command)
142-
raise "Execution of the command `#{command}` failed" unless command_output
143-
144-
version_match = command_output.match(/Redis server v=(?<redis_version>\d*\.\d*\.\d*)/)
145-
raise "Execution of the command `#{command}` generated unexpected output `#{command_output.strip}`" unless version_match
146-
147-
version_match['redis_version']
148-
end
149-
150-
def redis_cli_connect_options
151-
args = []
152-
if RedisHelper::Checks.is_redis_tcp?
153-
args = redis_cli_tcp_connect_options(args)
154-
else
155-
args << "-s #{redis['unixsocket']}"
156-
end
157-
158-
args << "-a '#{Gitlab['redis']['password']}'" if Gitlab['redis']['password']
159-
160-
args
161-
end
162-
163-
def redis_cli_tcp_connect_options(args)
164-
args << ["-h #{redis['bind']}"]
165-
port = redis['port'].to_i
166-
167-
if port.zero?
168-
args = redis_cli_tls_options(args)
169-
else
170-
args << "-p #{port}"
171-
end
172-
173-
args
174-
end
175-
176-
def redis_cli_tls_options(args)
177-
tls_port = redis['tls_port'].to_i
178-
179-
args << "--tls"
180-
args << "-p #{tls_port}"
181-
args << "--cacert '#{redis['tls_ca_cert_file']}'" if redis['tls_ca_cert_file']
182-
args << "--cacertdir '#{redis['tls_ca_cert_dir']}'" if redis['tls_ca_cert_dir']
183-
184-
return args unless client_certs_required?
185-
186-
raise "Redis TLS client authentication requires redis['tls_cert_file'] and redis['tls_key_file'] options" unless client_cert_and_key_available?
187-
188-
args << "--cert '#{redis['tls_cert_file']}'"
189-
args << "--key '#{redis['tls_key_file']}'"
190-
191-
args
192-
end
193-
194-
def client_certs_required?
195-
redis['tls_auth_clients'] == 'yes'
196-
end
197-
198-
def client_cert_and_key_available?
199-
redis['tls_cert_file'] && !redis['tls_cert_file'].empty? &&
200-
redis['tls_key_file'] && !redis['tls_key_file'].empty?
201-
end
202-
203119
class Checks
204120
class << self
205121
def is_redis_tcp?
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../version_helper'
4+
5+
module NewRedisHelper
6+
class Server < NewRedisHelper::Base
7+
def installed_version
8+
return unless OmnibusHelper.new(@node).service_up?('redis')
9+
10+
command = '/opt/gitlab/embedded/bin/redis-server --version'
11+
12+
command_output = VersionHelper.version(command)
13+
raise "Execution of the command `#{command}` failed" unless command_output
14+
15+
version_match = command_output.match(/Redis server v=(?<redis_version>\d*\.\d*\.\d*)/)
16+
raise "Execution of the command `#{command}` generated unexpected output `#{command_output.strip}`" unless version_match
17+
18+
version_match['redis_version']
19+
end
20+
21+
def running_version
22+
return unless OmnibusHelper.new(@node).service_up?('redis')
23+
24+
commands = ['/opt/gitlab/embedded/bin/redis-cli']
25+
commands << redis_cli_connect_options
26+
commands << "INFO"
27+
command = commands.join(" ")
28+
29+
command_output = VersionHelper.version(command)
30+
raise "Execution of the command `#{command}` failed" unless command_output
31+
32+
version_match = command_output.match(/redis_version:(?<redis_version>\d*\.\d*\.\d*)/)
33+
raise "Execution of the command `#{command}` generated unexpected output `#{command_output.strip}`" unless version_match
34+
35+
version_match['redis_version']
36+
end
37+
38+
private
39+
40+
def redis_cli_connect_options
41+
args = []
42+
if redis_server_over_tcp?
43+
args = redis_cli_tcp_connect_options(args)
44+
else
45+
args << "-s #{redis['unixsocket']}"
46+
end
47+
48+
args << "-a '#{redis['password']}'" if redis['password']
49+
50+
args
51+
end
52+
53+
def redis_cli_tcp_connect_options(args)
54+
args << ["-h #{redis['bind']}"]
55+
port = redis['port'].to_i
56+
57+
if port.zero?
58+
args = redis_cli_tls_options(args)
59+
else
60+
args << "-p #{port}"
61+
end
62+
63+
args
64+
end
65+
66+
def redis_cli_tls_options(args)
67+
tls_port = redis['tls_port'].to_i
68+
69+
args << "--tls"
70+
args << "-p #{tls_port}"
71+
args << "--cacert '#{redis['tls_ca_cert_file']}'" if redis['tls_ca_cert_file']
72+
args << "--cacertdir '#{redis['tls_ca_cert_dir']}'" if redis['tls_ca_cert_dir']
73+
74+
return args unless client_certs_required?
75+
76+
raise "Redis TLS client authentication requires redis['tls_cert_file'] and redis['tls_key_file'] options" unless client_cert_and_key_available?
77+
78+
args << "--cert '#{redis['tls_cert_file']}'"
79+
args << "--key '#{redis['tls_key_file']}'"
80+
81+
args
82+
end
83+
84+
def client_certs_required?
85+
redis['tls_auth_clients'] == 'yes'
86+
end
87+
88+
def client_cert_and_key_available?
89+
redis['tls_cert_file'] && !redis['tls_cert_file'].empty? &&
90+
redis['tls_key_file'] && !redis['tls_key_file'].empty?
91+
end
92+
end
93+
end

files/gitlab-cookbooks/redis/resources/service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
property :dir, String, default: lazy { node['redis']['dir'] }
55
property :account_helper, default: lazy { AccountHelper.new(node) }, sensitive: true
66
property :omnibus_helper, default: lazy { OmnibusHelper.new(node) }, sensitive: true
7-
property :redis_helper, default: lazy { RedisHelper.new(node) }, sensitive: true
7+
property :redis_helper, default: lazy { NewRedisHelper::Server.new(node) }, sensitive: true
88
property :runit_sv_timeout, [Integer, nil], default: lazy { node['redis']['runit_sv_timeout'] }
99
property :logfiles_helper, default: lazy { LogfilesHelper.new(node) }, sensitive: true
1010

spec/chef/cookbooks/gitlab/libraries/redis_helper_spec.rb

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -233,139 +233,6 @@
233233
end
234234
end
235235

236-
describe '#running_version' do
237-
let(:redis_cli_output) do
238-
<<~MSG
239-
# Server
240-
redis_version:3.2.12
241-
redis_git_sha1:00000000
242-
redis_git_dirty:0
243-
redis_build_id:e16da30f4a0a7845
244-
redis_mode:standalone
245-
os:Linux 4.15.0-58-generic x86_64
246-
MSG
247-
end
248-
249-
before do
250-
# Un-doing the stub added in chef_helper
251-
allow_any_instance_of(described_class).to receive(:running_version).and_call_original
252-
allow(Gitlab).to receive(:[]).and_call_original
253-
allow(VersionHelper).to receive(:version).with(/redis-cli.*INFO/).and_return(redis_cli_output)
254-
end
255-
256-
context 'when redis is not running' do
257-
it 'returns nil' do
258-
allow_any_instance_of(OmnibusHelper).to receive(:service_up?).with('redis').and_return(false)
259-
260-
expect(subject.running_version).to be_nil
261-
end
262-
end
263-
264-
context 'when redis is running' do
265-
before do
266-
allow_any_instance_of(OmnibusHelper).to receive(:service_up?).with('redis').and_return(true)
267-
end
268-
context 'over socket' do
269-
it 'calls VersionHelper.version with correct arguments' do
270-
expect(VersionHelper).to receive(:version).with('/opt/gitlab/embedded/bin/redis-cli -s /var/opt/gitlab/redis/redis.socket INFO')
271-
272-
subject.running_version
273-
end
274-
end
275-
276-
context 'over TCP' do
277-
context 'on non-TLS port' do
278-
before do
279-
stub_gitlab_rb(
280-
redis: {
281-
bind: '0.0.0.0',
282-
port: 6379
283-
}
284-
)
285-
end
286-
287-
it 'calls VersionHelper.version with correct arguments' do
288-
expect(VersionHelper).to receive(:version).with('/opt/gitlab/embedded/bin/redis-cli -h 0.0.0.0 -p 6379 INFO')
289-
290-
subject.running_version
291-
end
292-
end
293-
294-
context 'on TLS port' do
295-
before do
296-
stub_gitlab_rb(
297-
redis: {
298-
bind: '0.0.0.0',
299-
tls_port: 6380,
300-
tls_cert_file: '/tmp/self_signed.crt',
301-
tls_key_file: '/tmp/self_signed.key',
302-
tls_auth_clients: 'yes'
303-
}
304-
)
305-
end
306-
307-
it 'calls VersionHelper.version with correct arguments' do
308-
expected_args = "-h 0.0.0.0 --tls -p 6380 --cacert '/opt/gitlab/embedded/ssl/certs/cacert.pem' --cacertdir '/opt/gitlab/embedded/ssl/certs/' --cert '/tmp/self_signed.crt' --key '/tmp/self_signed.key'"
309-
expect(VersionHelper).to receive(:version).with("/opt/gitlab/embedded/bin/redis-cli #{expected_args} INFO")
310-
311-
subject.running_version
312-
end
313-
end
314-
end
315-
316-
context 'with a Redis password specified' do
317-
before do
318-
stub_gitlab_rb(
319-
redis: {
320-
bind: '0.0.0.0',
321-
port: 6379,
322-
password: 'toomanysecrets'
323-
}
324-
)
325-
end
326-
327-
it 'it passes password to the command' do
328-
expect(VersionHelper).to receive(:version).with("/opt/gitlab/embedded/bin/redis-cli -h 0.0.0.0 -p 6379 -a 'toomanysecrets' INFO")
329-
330-
subject.running_version
331-
end
332-
end
333-
334-
it 'parses version from redis-cli output properly' do
335-
expect(subject.running_version).to eq('3.2.12')
336-
end
337-
end
338-
end
339-
340-
describe '#installed_version' do
341-
let(:redis_server_output) { 'Redis server v=3.2.12 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=e16da30f4a0a7845' }
342-
343-
before do
344-
# Un-doing the stub added in chef_helper
345-
allow_any_instance_of(described_class).to receive(:installed_version).and_call_original
346-
allow(Gitlab).to receive(:[]).and_call_original
347-
allow(VersionHelper).to receive(:version).with(/redis-server --version/).and_return(redis_server_output)
348-
end
349-
350-
context 'when redis is not running' do
351-
it 'returns nil' do
352-
allow_any_instance_of(OmnibusHelper).to receive(:service_up?).with('redis').and_return(false)
353-
354-
expect(subject.installed_version).to be_nil
355-
end
356-
end
357-
358-
context 'when redis is running' do
359-
before do
360-
allow_any_instance_of(OmnibusHelper).to receive(:service_up?).with('redis').and_return(true)
361-
end
362-
363-
it 'parses redis-server output properly' do
364-
expect(subject.installed_version).to eq('3.2.12')
365-
end
366-
end
367-
end
368-
369236
describe '#validate_instance_shard_config' do
370237
before { allow(Gitlab).to receive(:[]).and_call_original }
371238

0 commit comments

Comments
 (0)