Skip to content

Commit c322a22

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 f333cd0 commit c322a22

File tree

10 files changed

+51
-48
lines changed

10 files changed

+51
-48
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ $ cat ~/.fog
103103
##### Setting up a new machine
104104

105105
```ruby
106-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::abs --targets localhost action=provision platform=ubuntu-1604-x86_64 inventory=/Users/tp/workspace/git/provision
106+
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::abs --targets localhost action=provision platform=ubuntu-1604-x86_64 inventory=/Users/tp/workspace/git/provision/inventory.yaml
107107

108108
Started on localhost...
109109
Finished on localhost:
@@ -118,7 +118,7 @@ Ran on 1 node in 1.44 seconds
118118
##### Tearing down a finished machine
119119

120120
```ruby
121-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::abs --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision node_name=yh6f4djvz7o3te6.delivery.puppetlabs.net
121+
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::abs --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision/inventory.yaml node_name=yh6f4djvz7o3te6.delivery.puppetlabs.net
122122

123123
Started on localhost...
124124
Finished on localhost:
@@ -139,7 +139,7 @@ Containers by default will be managed in the current [docker context](https://do
139139
#### Provision
140140

141141
```ruby
142-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision
142+
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision/inventory.yaml
143143

144144
Started on localhost...
145145
Finished on localhost:
@@ -166,13 +166,13 @@ These defaults can be overriden by passing the flags with different values i.e.
166166
```
167167

168168
```ruby
169-
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision vars='{ "docker_run_opts": ["-p 8086:8086", "-p 3000:3000"]}'
169+
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision/inventory.yaml vars='{ "docker_run_opts": ["-p 8086:8086", "-p 3000:3000"]}'
170170
```
171171

172172
#### Tear down
173173

174174
```ruby
175-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision node_name=localhost:2222
175+
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision/inventory.yaml node_name=localhost:2222
176176

177177
Started on localhost...
178178
Finished on localhost:
@@ -197,7 +197,7 @@ Tested with vagrant images:
197197
provision
198198

199199
```ruby
200-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=provision platform=ubuntu/xenial64 inventory=/Users/tp/workspace/git/provision
200+
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=provision platform=ubuntu/xenial64 inventory=/Users/tp/workspace/git/provision/inventory.yaml
201201

202202
Started on localhost...
203203
Finished on localhost:
@@ -235,7 +235,7 @@ Ran on 1 target in 0.84 sec
235235
tear_down
236236

237237
```ruby
238-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision node_name=127.0.0.1:2222
238+
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision/inventory.yaml node_name=127.0.0.1:2222
239239

240240
Started on localhost...
241241
Finished on localhost:
@@ -290,7 +290,7 @@ In the provision step you can invoke bundle exec rake 'litmus:provision_list[tes
290290
Manual invocation of the provision service task from a workflow can be done using:
291291

292292
```ruby
293-
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::provision_service --targets localhost action=provision platform=centos-7-v20200813 inventory=/Users/tp/workspace/git/provision vars='role: puppetserver'
293+
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::provision_service --targets localhost action=provision platform=centos-7-v20200813 inventory=/Users/tp/workspace/git/provision/inventory.yaml vars='role: puppetserver'
294294
```
295295

296296
Or using Litmus:
@@ -326,7 +326,7 @@ provision
326326

327327
```powershell
328328
PS> $env:LITMUS_HYPERV_VSWITCH = 'internal_nat'
329-
PS> bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=provision platform=centos/7 inventory=/Users/tp/workspace/git/provision hyperv_smb_username=tp hyperv_smb_password=notMyrealPassword
329+
PS> bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=provision platform=centos/7 inventory=/Users/tp/workspace/git/provision/inventory.yaml hyperv_smb_username=tp hyperv_smb_password=notMyrealPassword
330330
331331
Started on localhost...
332332
Finished on localhost:
@@ -359,7 +359,7 @@ Testing/development/debugging it is better to use ruby directly, you will need t
359359
Testing using bolt, the second step
360360

361361
```ruby
362-
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision
362+
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision/inventory.yaml
363363
```
364364

365365
## License

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
@@ -118,8 +118,7 @@ def random_ssh_forwarding_port(start_port = 52_222, end_port = 52_999)
118118

119119
def provision(docker_platform, inventory_location, vars)
120120
include PuppetLitmus::InventoryManipulation
121-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
122-
inventory_hash = get_inventory_hash(inventory_full_path)
121+
inventory_hash = get_inventory_hash(inventory_location)
123122
os_release_facts = docker_image_os_release_facts(docker_platform)
124123
distro = os_release_facts['ID']
125124
version = os_release_facts['VERSION_ID']
@@ -183,7 +182,7 @@ def provision(docker_platform, inventory_location, vars)
183182
inventory_node['facts']['container_id'] = container_id
184183

185184
add_node_to_group(inventory_hash, inventory_node, 'ssh_nodes')
186-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
185+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
187186

188187
{ status: 'ok', node_name: inventory_node['name'], node: inventory_node }
189188
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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@ 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)
130-
vagrant_dirs = Dir.glob("#{File.join(inventory_location, '.vagrant')}/*/").map { |d| File.basename(d) }
131-
@vagrant_env = File.expand_path(File.join(inventory_location, '.vagrant', get_vagrant_dir(platform, vagrant_dirs)))
128+
inventory_hash = get_inventory_hash(inventory_location)
129+
vagrant_dirs = Dir.glob("#{File.join(File.dirname(inventory_location), '.vagrant')}/*/").map { |d| File.basename(d) }
130+
@vagrant_env = File.expand_path(File.join(File.dirname(inventory_location), '.vagrant', get_vagrant_dir(platform, vagrant_dirs)))
132131
FileUtils.mkdir_p @vagrant_env
133132
generate_vagrantfile(File.join(@vagrant_env, 'Vagrantfile'), platform, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url)
134133
command = "vagrant up --provider #{provider}"
@@ -192,23 +191,22 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
192191
node['vars'] = var_hash
193192
end
194193
add_node_to_group(inventory_hash, node, group_name)
195-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
194+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
196195
{ status: 'ok', node_name: node_name, node: node }
197196
end
198197

199198
def tear_down(node_name, inventory_location)
200199
include PuppetLitmus
201200
command = 'vagrant destroy -f'
202-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
203-
if File.file?(inventory_full_path)
204-
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
201+
if File.file?(inventory_location)
202+
inventory_hash = inventory_hash_from_inventory_file(inventory_location)
205203
vagrant_env = facts_from_node(inventory_hash, node_name)['vagrant_env']
206204
run_local_command(command, vagrant_env)
207205
remove_node(inventory_hash, node_name)
208206
FileUtils.rm_r(vagrant_env)
209207
end
210208
warn "Removed #{node_name}"
211-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
209+
File.open(inventory_location, 'w') { |f| f.write inventory_hash.to_yaml }
212210
{ status: 'ok' }
213211
end
214212

0 commit comments

Comments
 (0)