Skip to content

Commit a58dde7

Browse files
committed
(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.
1 parent 9435bc4 commit a58dde7

File tree

9 files changed

+39
-36
lines changed

9 files changed

+39
-36
lines changed

lib/task_helper.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
# frozen_string_literal: true
22

33
def sanitise_inventory_location(location)
4-
# Inventory location is an optional task parameter. If not specified use the current directory
5-
location.nil? ? Dir.pwd : location
4+
# Inventory location is an optional task parameter.
5+
location = location.nil? ? Dir.pwd : location
6+
# If not specified use the current directory + inventory.yaml
7+
if File.exist?(location) && File.directory?(location)
8+
# DEPRECATED: puppet_litmus <= 1.3.0 support
9+
if Gem.loaded_specs['puppet_litmus'].version <= Gem::Version.new('1.3.0')
10+
File.join(location, 'spec', 'fixtures', 'litmus_inventory.yaml')
11+
else
12+
File.join(location, 'inventory.yaml')
13+
end
14+
else
15+
location
16+
end
617
end
718

819
def get_inventory_hash(inventory_full_path)

plans/provisioner.pp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
) {
33
# bolt command run 'touch ./inventory.yaml'
44
# provision server machine, set role
5-
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-7-x86_64', inventory => './', vars => 'role: pe')
5+
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-7-x86_64', vars => 'role: pe')
66
# provision agents agent_linux agent_windows
7-
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-6-x86_64', inventory => './', vars => 'role: agent_linux')
8-
run_task('provision::abs', 'localhost', action => 'provision', platform => 'win-2016-x86_64', inventory => './', vars => 'role: agent_windows')
7+
run_task('provision::abs', 'localhost', action => 'provision', platform => 'centos-6-x86_64', vars => 'role: agent_linux')
8+
run_task('provision::abs', 'localhost', action => 'provision', platform => 'win-2016-x86_64', vars => 'role: agent_windows')
99
}

plans/teardown.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
$all_nodes = get_targets('*')
44
$all_node_names = $all_nodes.map |$n| { $n.name }
55
$all_node_names.each |$node_name| {
6-
run_task('provision::abs', 'localhost', action=> 'tear_down', node_name=> $node_name, inventory => './')
6+
run_task('provision::abs', 'localhost', action=> 'tear_down', node_name=> $node_name)
77
}
88
}

spec/tasks/abs_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def with_env(env_vars)
8989
{
9090
action: 'provision',
9191
platform: 'redhat-8-x86_64',
92-
inventory: tmpdir
92+
inventory: inventory_file,
9393
}
9494
end
9595
let(:response_body) do
@@ -139,7 +139,7 @@ def with_env(env_vars)
139139
{
140140
action: 'tear_down',
141141
node_name: 'foo-bar.test',
142-
inventory: tmpdir
142+
inventory: inventory_file
143143
}
144144
end
145145
let(:inventory_yaml) do

tasks/abs.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ def provision(platform, inventory_location, vars)
8585

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

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

116-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
115+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
117116
{ status: 'ok', nodes: data.length }
118117
end
119118

120119
def tear_down(node_name, inventory_location)
121-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
122-
if File.file?(inventory_full_path)
123-
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
120+
if File.file?(inventory_location)
121+
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
124122
facts = facts_from_node(inventory_hash, node_name)
125123
platform = facts['platform']
126124
job_id = facts['job_id']
@@ -147,7 +145,7 @@ def tear_down(node_name, inventory_location)
147145
targets_to_remove.each do |target|
148146
remove_node(inventory_hash, target)
149147
end
150-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
148+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
151149
{ status: 'ok', removed: targets_to_remove }
152150
end
153151

tasks/docker.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ def random_ssh_forwarding_port(start_port = 52_222, end_port = 52_999)
121121

122122
def provision(docker_platform, inventory_location, vars)
123123
include PuppetLitmus::InventoryManipulation
124-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
125-
inventory_hash = get_inventory_hash(inventory_full_path)
124+
inventory_hash = get_inventory_hash(inventory_location)
126125
os_release_facts = docker_image_os_release_facts(docker_platform)
127126
distro = os_release_facts['ID']
128127
version = os_release_facts['VERSION_ID']
@@ -186,7 +185,7 @@ def provision(docker_platform, inventory_location, vars)
186185
inventory_node['facts']['container_id'] = container_id
187186

188187
add_node_to_group(inventory_hash, inventory_node, 'ssh_nodes')
189-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
188+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
190189

191190
{ status: 'ok', node_name: inventory_node['name'], node: inventory_node }
192191
end

tasks/docker_exp.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
def provision(docker_platform, inventory_location, vars)
1414
include PuppetLitmus::InventoryManipulation
15-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
16-
inventory_hash = get_inventory_hash(inventory_full_path)
15+
inventory_hash = get_inventory_hash(inventory_location)
1716
os_release_facts = docker_image_os_release_facts(docker_platform)
1817

1918
inventory_node = {
@@ -55,7 +54,7 @@ def provision(docker_platform, inventory_location, vars)
5554
inventory_node['facts']['container_id'] = container_id
5655

5756
add_node_to_group(inventory_hash, inventory_node, 'docker_nodes')
58-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
57+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
5958

6059
{ status: 'ok', node_name: inventory_node['name'], node: inventory_node }
6160
end

tasks/provision_service.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ def provision(platform, inventory_location, vars, retry_attempts)
101101
data = JSON.parse(vars.tr(';', ','))
102102
job_url = data['job_url']
103103
end
104-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
105104
currnet_retry_count = 0
106105
begin
107106
params = platform_to_cloud_request_parameters(platform, cloud, region, zone)
@@ -127,17 +126,17 @@ def provision(platform, inventory_location, vars, retry_attempts)
127126
end
128127
end
129128

130-
if File.file?(inventory_full_path)
131-
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
129+
if File.file?(inventory_location)
130+
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
132131
inventory_hash['groups'].each do |g|
133132
response_hash['groups'].each do |bg|
134133
g['targets'] = g['targets'] + bg['targets'] if g['name'] == bg['name']
135134
end
136135
end
137-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
136+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
138137
else
139138
FileUtils.mkdir_p(File.join(Dir.pwd, '/spec/fixtures'))
140-
File.open(inventory_full_path, 'wb') do |f|
139+
File.open(inventory_location, 'wb') do |f|
141140
f.write(YAML.dump(response_hash))
142141
end
143142
end
@@ -153,10 +152,9 @@ def tear_down(platform, inventory_location, _vars, retry_attempts)
153152
# remove all provisioned resources
154153
uri = URI.parse(ENV['SERVICE_URL'] || default_uri)
155154

156-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
157155
# rubocop:disable Style/GuardClause
158-
if File.file?(inventory_full_path)
159-
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
156+
if File.file?(inventory_location)
157+
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
160158
facts = facts_from_node(inventory_hash, platform)
161159
job_id = facts['uuid']
162160
response = invoke_cloud_request(job_id, uri, '', 'delete', retry_attempts)

tasks/vagrant.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
125125
end
126126

127127
include PuppetLitmus
128-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
129-
inventory_hash = get_inventory_hash(inventory_full_path)
128+
inventory_hash = get_inventory_hash(inventory_location)
130129
vagrant_dirs = Dir.glob("#{File.join(inventory_location, '.vagrant')}/*/").map { |d| File.basename(d) }
131130
@vagrant_env = File.expand_path(File.join(inventory_location, '.vagrant', get_vagrant_dir(platform, vagrant_dirs)))
132131
FileUtils.mkdir_p @vagrant_env
@@ -187,23 +186,22 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
187186
group_name = 'winrm_nodes'
188187
end
189188
add_node_to_group(inventory_hash, node, group_name)
190-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
189+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
191190
{ status: 'ok', node_name: node_name, node: node }
192191
end
193192

194193
def tear_down(node_name, inventory_location)
195194
include PuppetLitmus
196195
command = 'vagrant destroy -f'
197-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
198-
if File.file?(inventory_full_path)
199-
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
196+
if File.file?(inventory_location)
197+
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
200198
vagrant_env = facts_from_node(inventory_hash, node_name)['vagrant_env']
201199
run_local_command(command, vagrant_env)
202200
remove_node(inventory_hash, node_name)
203201
FileUtils.rm_r(vagrant_env)
204202
end
205203
warn "Removed #{node_name}"
206-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
204+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
207205
{ status: 'ok' }
208206
end
209207

0 commit comments

Comments
 (0)