Skip to content

Commit

Permalink
(maint) tasks require path to inventory yaml
Browse files Browse the repository at this point in the history
provision tasks document the inventory parameter as the "location to inventory
file", so make it behave as such.
  • Loading branch information
h0tw1r3 committed Apr 24, 2024
1 parent 9435bc4 commit a58dde7
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
15 changes: 13 additions & 2 deletions lib/task_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# frozen_string_literal: true

def sanitise_inventory_location(location)
# Inventory location is an optional task parameter. If not specified use the current directory
location.nil? ? Dir.pwd : location
# Inventory location is an optional task parameter.
location = location.nil? ? Dir.pwd : location
# If not specified use the current directory + inventory.yaml
if File.exist?(location) && File.directory?(location)
# DEPRECATED: puppet_litmus <= 1.3.0 support
if Gem.loaded_specs['puppet_litmus'].version <= Gem::Version.new('1.3.0')
File.join(location, 'spec', 'fixtures', 'litmus_inventory.yaml')
else
File.join(location, 'inventory.yaml')
end
else
location
end
end

def get_inventory_hash(inventory_full_path)
Expand Down
6 changes: 3 additions & 3 deletions plans/provisioner.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
) {
# bolt command run 'touch ./inventory.yaml'
# provision server machine, set role
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-7-x86_64', inventory => './', vars => 'role: pe')
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-7-x86_64', vars => 'role: pe')
# provision agents agent_linux agent_windows
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-6-x86_64', inventory => './', vars => 'role: agent_linux')
run_task('provision::abs', 'localhost', action => 'provision', platform => 'win-2016-x86_64', inventory => './', vars => 'role: agent_windows')
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-6-x86_64', vars => 'role: agent_linux')
run_task('provision::abs', 'localhost', action => 'provision', platform => 'win-2016-x86_64', vars => 'role: agent_windows')
}
2 changes: 1 addition & 1 deletion plans/teardown.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
$all_nodes = get_targets('*')
$all_node_names = $all_nodes.map |$n| { $n.name }
$all_node_names.each |$node_name| {
run_task('provision::abs', 'localhost', action=> 'tear_down', node_name=> $node_name, inventory => './')
run_task('provision::abs', 'localhost', action=> 'tear_down', node_name=> $node_name)
}
}
4 changes: 2 additions & 2 deletions spec/tasks/abs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def with_env(env_vars)
{
action: 'provision',
platform: 'redhat-8-x86_64',
inventory: tmpdir
inventory: inventory_file,
}
end
let(:response_body) do
Expand Down Expand Up @@ -139,7 +139,7 @@ def with_env(env_vars)
{
action: 'tear_down',
node_name: 'foo-bar.test',
inventory: tmpdir
inventory: inventory_file
}
end
let(:inventory_yaml) do
Expand Down
12 changes: 5 additions & 7 deletions tasks/abs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def provision(platform, inventory_location, vars)

raise "Timeout: unable to get a 200 response in #{poll_duration} seconds" if reply.code != '200'

inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
inventory_hash = get_inventory_hash(inventory_full_path)
inventory_hash = get_inventory_hash(inventory_location)
data = JSON.parse(reply.body)
data.each do |host|
if platform_uses_ssh(host['type'])
Expand All @@ -113,14 +112,13 @@ def provision(platform, inventory_location, vars)
add_node_to_group(inventory_hash, node, group_name)
end

File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
{ status: 'ok', nodes: data.length }
end

def tear_down(node_name, inventory_location)
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
if File.file?(inventory_full_path)
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
if File.file?(inventory_location)
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
facts = facts_from_node(inventory_hash, node_name)
platform = facts['platform']
job_id = facts['job_id']
Expand All @@ -147,7 +145,7 @@ def tear_down(node_name, inventory_location)
targets_to_remove.each do |target|
remove_node(inventory_hash, target)
end
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
{ status: 'ok', removed: targets_to_remove }
end

Expand Down
5 changes: 2 additions & 3 deletions tasks/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ def random_ssh_forwarding_port(start_port = 52_222, end_port = 52_999)

def provision(docker_platform, inventory_location, vars)
include PuppetLitmus::InventoryManipulation
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
inventory_hash = get_inventory_hash(inventory_full_path)
inventory_hash = get_inventory_hash(inventory_location)
os_release_facts = docker_image_os_release_facts(docker_platform)
distro = os_release_facts['ID']
version = os_release_facts['VERSION_ID']
Expand Down Expand Up @@ -186,7 +185,7 @@ def provision(docker_platform, inventory_location, vars)
inventory_node['facts']['container_id'] = container_id

add_node_to_group(inventory_hash, inventory_node, 'ssh_nodes')
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }

{ status: 'ok', node_name: inventory_node['name'], node: inventory_node }
end
Expand Down
5 changes: 2 additions & 3 deletions tasks/docker_exp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

def provision(docker_platform, inventory_location, vars)
include PuppetLitmus::InventoryManipulation
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
inventory_hash = get_inventory_hash(inventory_full_path)
inventory_hash = get_inventory_hash(inventory_location)
os_release_facts = docker_image_os_release_facts(docker_platform)

inventory_node = {
Expand Down Expand Up @@ -55,7 +54,7 @@ def provision(docker_platform, inventory_location, vars)
inventory_node['facts']['container_id'] = container_id

add_node_to_group(inventory_hash, inventory_node, 'docker_nodes')
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }

{ status: 'ok', node_name: inventory_node['name'], node: inventory_node }
end
Expand Down
14 changes: 6 additions & 8 deletions tasks/provision_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def provision(platform, inventory_location, vars, retry_attempts)
data = JSON.parse(vars.tr(';', ','))
job_url = data['job_url']
end
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
currnet_retry_count = 0
begin
params = platform_to_cloud_request_parameters(platform, cloud, region, zone)
Expand All @@ -127,17 +126,17 @@ def provision(platform, inventory_location, vars, retry_attempts)
end
end

if File.file?(inventory_full_path)
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
if File.file?(inventory_location)
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
inventory_hash['groups'].each do |g|
response_hash['groups'].each do |bg|
g['targets'] = g['targets'] + bg['targets'] if g['name'] == bg['name']
end
end
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
else
FileUtils.mkdir_p(File.join(Dir.pwd, '/spec/fixtures'))
File.open(inventory_full_path, 'wb') do |f|
File.open(inventory_location, 'wb') do |f|
f.write(YAML.dump(response_hash))
end
end
Expand All @@ -153,10 +152,9 @@ def tear_down(platform, inventory_location, _vars, retry_attempts)
# remove all provisioned resources
uri = URI.parse(ENV['SERVICE_URL'] || default_uri)

inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
# rubocop:disable Style/GuardClause
if File.file?(inventory_full_path)
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
if File.file?(inventory_location)
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
facts = facts_from_node(inventory_hash, platform)
job_id = facts['uuid']
response = invoke_cloud_request(job_id, uri, '', 'delete', retry_attempts)
Expand Down
12 changes: 5 additions & 7 deletions tasks/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
end

include PuppetLitmus
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
inventory_hash = get_inventory_hash(inventory_full_path)
inventory_hash = get_inventory_hash(inventory_location)
vagrant_dirs = Dir.glob("#{File.join(inventory_location, '.vagrant')}/*/").map { |d| File.basename(d) }
@vagrant_env = File.expand_path(File.join(inventory_location, '.vagrant', get_vagrant_dir(platform, vagrant_dirs)))
FileUtils.mkdir_p @vagrant_env
Expand Down Expand Up @@ -187,23 +186,22 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
group_name = 'winrm_nodes'
end
add_node_to_group(inventory_hash, node, group_name)
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
{ status: 'ok', node_name: node_name, node: node }
end

def tear_down(node_name, inventory_location)
include PuppetLitmus
command = 'vagrant destroy -f'
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
if File.file?(inventory_full_path)
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
if File.file?(inventory_location)
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
vagrant_env = facts_from_node(inventory_hash, node_name)['vagrant_env']
run_local_command(command, vagrant_env)
remove_node(inventory_hash, node_name)
FileUtils.rm_r(vagrant_env)
end
warn "Removed #{node_name}"
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
{ status: 'ok' }
end

Expand Down

0 comments on commit a58dde7

Please sign in to comment.