Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sensu::backend_upgrade task #1265

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,10 @@ sensu_bonsai_asset { 'sensu/sensu-pagerduty-handler':

The following Bolt tasks are provided by this Module:

**sensu::backend\_upgrade**: Perform backend upgrade via `sensu-backend upgrade` command.

Example: `bolt task run sensu::backend_upgrade --targets sensu_backend`

**sensu::agent\_event**: Create a Sensu Go agent event via the agent API

Example: `bolt task run sensu::agent_event name=bolttest status=1 output=test --targets sensu_agent`
Expand Down
33 changes: 33 additions & 0 deletions spec/acceptance/sensu_bolt_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,36 @@ class { 'sensu::agent':
end
end
end

describe 'sensu backend_upgrade task', if: RSpec.configuration.sensu_mode == 'full' do
backend = hosts_as('sensu-backend')[0]
context 'setup' do
it 'is successful' do
on backend, 'yum remove -y sensu-go\*'
on backend, 'rm -rf /var/lib/sensu/sensu-backend/etcd /root/.config'
pp = <<-EOS
class { 'sensu':
version => '5.21.0-14262',
}
include sensu::backend
EOS
apply_manifest_on(backend, pp, :catch_failures => true)
upgrade_pp = <<-EOS
class { 'sensu':
version => 'latest',
}
include sensu::backend
EOS
apply_manifest_on(backend, upgrade_pp, :catch_failures => true)
end
end

context 'peforms upgrade' do
describe command('bolt task run sensu::backend_upgrade --targets sensu-backend'), :node => backend do
its(:exit_status) { should eq 0 }
end
describe command('sensu-backend upgrade --skip-confirm 2>&1'), :node => backend do
its(:stdout) { should match /up to date/ }
end
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/tasks/backend_upgrade/logs.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"component":"store","level":"warning","msg":"migrating etcd database to a new version","time":"2020-07-11T15:07:50Z"}
{"component":"store","database_version":1,"level":"info","msg":"successfully upgraded database","time":"2020-07-11T15:07:50Z"}
{"component":"store","database_version":2,"level":"info","msg":"successfully upgraded database","time":"2020-07-11T15:07:50Z"}
8 changes: 3 additions & 5 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
c.add_setting :sensu_mode, default: 'base'
c.add_setting :sensu_enterprise_file, default: nil
c.add_setting :sensu_test_enterprise, default: false
c.add_setting :add_ci_repo, default: false
c.add_setting :sensu_manage_repo, default: true
c.add_setting :sensu_use_agent, default: false
c.add_setting :examples_dir, default: nil
Expand All @@ -37,10 +38,7 @@
secrets = File.join(project_dir, 'tests/secrets')
if File.exists?(secrets) && (ENV['BEAKER_sensu_ci_build'] == 'yes' || ENV['BEAKER_sensu_ci_build'] == 'true')
c.sensu_manage_repo = false
add_ci_repo = true
else
c.sensu_manage_repo = true
add_ci_repo = false
c.add_ci_repo = true
end

c.examples_dir = File.join(project_dir, 'examples')
Expand Down Expand Up @@ -80,7 +78,7 @@
on host, "puppet config set --section main certname #{host.name}"
end

if add_ci_repo
if c.add_ci_repo
scp_to(hosts, ci_build, '/tmp/ci_build.sh')
scp_to(hosts, secrets, '/tmp/secrets')
on hosts, '/tmp/ci_build.sh'
Expand Down
32 changes: 32 additions & 0 deletions spec/tasks/backend_upgrade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require_relative '../../tasks/backend_upgrade.rb'

describe SensuBackendUpgrade do
let(:output) { my_fixture_read('logs.out') }
let(:logs) do
[
{"component" => "store","level" => "warning","msg" => "migrating etcd database to a new version","time" => "2020-07-11T15:07:50Z"},
{"component" => "store","database_version" => 1,"level" => "info","msg" => "successfully upgraded database","time" => "2020-07-11T15:07:50Z"},
{"component" => "store","database_version" => 2,"level" => "info","msg" => "successfully upgraded database","time" => "2020-07-11T15:07:50Z"},
]
end

it 'executes upgrade with no arguments' do
expect(Open3).to receive(:capture3).with('sensu-backend upgrade --skip-confirm').and_return(["", output, 0])
ret = described_class.upgrade({})
expect(ret).to eq(logs)
end

it 'handles arguments' do
expected_cmd = 'sensu-backend upgrade --skip-confirm --config-file /etc/foo.yml --timeout 10 --etcd-client-cert-auth --etcd-cipher-suites foo bar'
params = {
'_task': 'sensu::backend_upgrade',
'config_file': '/etc/foo.yml',
'timeout': 10,
'etcd_client_cert_auth': true,
'etcd_cipher_suites': ['foo','bar'],
}
expect(Open3).to receive(:capture3).with(expected_cmd).and_return(["", output, 0])
described_class.upgrade(params)
end
end
46 changes: 46 additions & 0 deletions tasks/backend_upgrade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"description": "Execute Sensu Go backend upgrade",
"input_method": "stdin",
"parameters": {
"config_file": {
"description": "Path to backend.yml",
"type": "Optional[String[1]]"
},
"timeout": {
"description": "Timeout for connecting to etcd",
"type": "Optional[Integer]"
},
"etcd_advertise_client_urls": {
"description": "list of this member's client URLs to advertise to clients",
"type": "Optional[Array]"
},
"etcd_cert_file": {
"description": "path to the client server TLS cert file",
"type": "Optional[String[1]]"
},
"etcd_cipher_suites": {
"description": "list of ciphers to use for etcd TLS configuration",
"type": "Optional[Array]"
},
"etcd_client_cert_auth": {
"description": "enable client cert authentication",
"type": "Optional[Boolean]"
},
"etcd_client_urls": {
"description": "client URLs to use when operating as an etcd client",
"type": "Optional[Array]"
},
"etcd_key_file": {
"description": "path to the client server TLS key file",
"type": "Optional[String[1]]"
},
"etcd_max_request_bytes": {
"description": "maximum etcd request size in bytes (use with caution)",
"type": "Optional[Integer]"
},
"etcd_trusted_ca_file": {
"description": "path to the client server TLS trusted CA cert file",
"type": "Optional[String[1]]"
}
}
}
48 changes: 48 additions & 0 deletions tasks/backend_upgrade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/opt/puppetlabs/puppet/bin/ruby
require 'resolv'
require 'json'
require 'open3'

class SensuBackendUpgrade
def self.upgrade(params)
cmd = ['sensu-backend', 'upgrade', '--skip-confirm']
valid_params = [
'config_file', 'timeout', 'etcd_advertise_client_urls', 'etcd_cert_file',
'etcd_cipher_suites', 'etcd_client_cert_auth', 'etcd_client_urls',
'etcd_key_file', 'etcd_max_request_bytes', 'etcd_trusted_ca_file',
]
params.each_pair do |key, value|
next unless valid_params.include?(key.to_s)
cmd << "--#{key.to_s.gsub('_', '-')}"
if [TrueClass, FalseClass].include?(value.class)
next
elsif value.is_a?(Array)
cmd << value.join(' ')
else
cmd << value
end
end
stdout, stderr, status = Open3.capture3(cmd.join(' '))
if status != 0
raise Exception, "Failed to execute #{cmd.join(' ')}: #{stderr}"
end
logs = []
stderr.each_line do |line|
log = JSON.parse(line)
logs << log
end
logs
end

def self.run
params = JSON.parse(STDIN.read)
logs = upgrade(params)
puts({ logs: logs}.to_json)
rescue Exception => e
puts({ _error: e.message }.to_json)
exit 1
end
end

SensuBackendUpgrade.run if $PROGRAM_NAME == __FILE__