From abb6c87d2c1f1809c10e9feb79c32e33e5e95983 Mon Sep 17 00:00:00 2001 From: Jamie Date: Fri, 17 Jan 2020 14:36:10 +0000 Subject: [PATCH] Support the force registration of tentacles (#153) * feat: support force flag for command calls * Docs and validate option_flag with !.empty? --- README.md | 1 + libraries/shared.rb | 5 +++++ resources/tentacle.rb | 15 ++++++++++++++- spec/unit/lib_shared_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index be4d3ac..f6085a4 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ end - :api_key: Api Key used to register Tentacle to Octopus Server - :roles: Array of roles to apply to Tentacle when registering with Octopus Deploy Server (e.g ["web-server","app-server"]) - :environment: Which environment or environments the Tentacle will become part of when registering with Octopus Deploy Server (Defaults to node.chef_environment). Accepts string or array. +- :forced_registration: Whether the command should include `--force` to support overwriting tentacle references that already exist in Octopus Server - :tenants: Optional array of tenants to add to the Tentacle. Tenant must already exist on Octopus Deploy Server. Requires Octopus 3.4 - :tenant_tags: Optional array of tenant tags to add to the Tentacle. Tags must already exist on Octopus Deploy Server. If tag is part of a tag group, include the group name followed by a slash `/`. e.g ( Priority/VIP, Datacenter/US ).. Requires Octopus 3.4 - :tentacle_name: Optional custom name for Tentacle. Defaults to the Chef node name diff --git a/libraries/shared.rb b/libraries/shared.rb index 9a4b25f..652d1a2 100644 --- a/libraries/shared.rb +++ b/libraries/shared.rb @@ -34,6 +34,11 @@ def option_list(name, options) options.map { |option| option(name, option) }.join(' ') if name && options end + # Includes the named option based on boolean input + def option_flag(name, value) + name && !name.empty? && value ? "--#{name} " : '' + end + def option(name, option) "--#{name} \"#{option}\"" if name && option end diff --git a/resources/tentacle.rb b/resources/tentacle.rb index 79f832b..c82d69d 100644 --- a/resources/tentacle.rb +++ b/resources/tentacle.rb @@ -43,6 +43,7 @@ property :tenants, [Array, nil], default: nil property :tenant_tags, [Array, nil], default: nil property :tentacle_name, String, default: node.name +property :forced_registration, [true, false], default: false property :service_user, [String, nil], default: nil property :service_password, [String, nil], default: nil property :public_dns, String, default: node['fqdn'] @@ -159,7 +160,19 @@ action :run cwd tentacle_install_location code <<-EOH - .\\Tentacle.exe register-with --instance "#{new_resource.instance}" --server "#{new_resource.server}" --name "#{new_resource.tentacle_name}" --publicHostName "#{new_resource.public_dns}" --apiKey "#{new_resource.api_key}" #{register_comm_config(new_resource.polling, port)} #{option_list('environment', environment)} #{option_list('role', new_resource.roles)} #{option_list('tenant', new_resource.tenants)} #{option_list('tenanttag', new_resource.tenant_tags)} #{option('tenanted-deployment-participation', new_resource.tenated_deployment_participation)} --console + .\\Tentacle.exe register-with --instance "#{new_resource.instance}" ` + --server "#{new_resource.server}" ` + --name "#{new_resource.tentacle_name}" ` + --publicHostName "#{new_resource.public_dns}" ` + --apiKey "#{new_resource.api_key}" ` + #{register_comm_config(new_resource.polling, port)} ` + #{option_list('environment', environment)} ` + #{option_list('role', new_resource.roles)} ` + #{option_list('tenant', new_resource.tenants)} ` + #{option_list('tenanttag', new_resource.tenant_tags)} ` + #{option('tenanted-deployment-participation', new_resource.tenated_deployment_participation)} ` + #{option_flag('force', new_resource.forced_registration)} ` + --console #{catch_powershell_error('Registering Tentacle')} EOH # This is sort of a hack, you need to specify the config_path on register if it is not default diff --git a/spec/unit/lib_shared_spec.rb b/spec/unit/lib_shared_spec.rb index fe64601..bd4c7f5 100644 --- a/spec/unit/lib_shared_spec.rb +++ b/spec/unit/lib_shared_spec.rb @@ -32,6 +32,28 @@ end end + describe 'option_flag' do + it 'should return --attr flag if name is valid and value is true' do + name = 'attr' + value = true + expect(shared.option_flag(name, value)).to eq '--attr ' + end + + it 'should return empty string if name is valid and value is false' do + name = 'attr' + value = false + expect(shared.option_flag(name, value)).to eq '' + end + + it 'should return empty string if inputs are invalid' do + expect(shared.option_flag('', false)).to eq '' + expect(shared.option_flag('', true)).to eq '' + expect(shared.option_flag(nil, true)).to eq '' + expect(shared.option_flag(nil, false)).to eq '' + expect(shared.option_flag('attr', nil)).to eq '' + end + end + describe 'option' do it 'should return the command line command for an option' do name = 'name'