From a58dde7d7b8ac624078a27b0bb11030a10052b08 Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Tue, 23 Apr 2024 22:42:37 -0500 Subject: [PATCH] (maint) tasks require path to inventory yaml provision tasks document the inventory parameter as the "location to inventory file", so make it behave as such. --- lib/task_helper.rb | 15 +++++++++++++-- plans/provisioner.pp | 6 +++--- plans/teardown.pp | 2 +- spec/tasks/abs_spec.rb | 4 ++-- tasks/abs.rb | 12 +++++------- tasks/docker.rb | 5 ++--- tasks/docker_exp.rb | 5 ++--- tasks/provision_service.rb | 14 ++++++-------- tasks/vagrant.rb | 12 +++++------- 9 files changed, 39 insertions(+), 36 deletions(-) diff --git a/lib/task_helper.rb b/lib/task_helper.rb index 7cc21d0..af1aafe 100644 --- a/lib/task_helper.rb +++ b/lib/task_helper.rb @@ -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) diff --git a/plans/provisioner.pp b/plans/provisioner.pp index a0667d9..19296ac 100644 --- a/plans/provisioner.pp +++ b/plans/provisioner.pp @@ -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') } diff --git a/plans/teardown.pp b/plans/teardown.pp index 5dde856..446cb17 100644 --- a/plans/teardown.pp +++ b/plans/teardown.pp @@ -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) } } diff --git a/spec/tasks/abs_spec.rb b/spec/tasks/abs_spec.rb index a191aab..b4d3742 100644 --- a/spec/tasks/abs_spec.rb +++ b/spec/tasks/abs_spec.rb @@ -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 @@ -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 diff --git a/tasks/abs.rb b/tasks/abs.rb index 3562f84..122e895 100755 --- a/tasks/abs.rb +++ b/tasks/abs.rb @@ -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']) @@ -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'] @@ -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 diff --git a/tasks/docker.rb b/tasks/docker.rb index 385243f..a7d4ca5 100755 --- a/tasks/docker.rb +++ b/tasks/docker.rb @@ -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'] @@ -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 diff --git a/tasks/docker_exp.rb b/tasks/docker_exp.rb index b37aa25..f7888fc 100755 --- a/tasks/docker_exp.rb +++ b/tasks/docker_exp.rb @@ -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 = { @@ -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 diff --git a/tasks/provision_service.rb b/tasks/provision_service.rb index b0df15b..b5690da 100755 --- a/tasks/provision_service.rb +++ b/tasks/provision_service.rb @@ -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) @@ -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 @@ -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) diff --git a/tasks/vagrant.rb b/tasks/vagrant.rb index 07c81e6..cdc02cb 100755 --- a/tasks/vagrant.rb +++ b/tasks/vagrant.rb @@ -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 @@ -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