Skip to content

Commit b47e388

Browse files
committed
(maint) uncouple from the puppet_litmus gem
1 parent a134735 commit b47e388

16 files changed

+213
-170
lines changed

README.md

Lines changed: 11 additions & 11 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/inventory.yaml
106+
$ bundle exec bolt task run provision::abs --targets localhost action=provision platform=ubuntu-1604-x86_64
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/inventory.yaml node_name=yh6f4djvz7o3te6.delivery.puppetlabs.net
121+
$ bundle exec bolt task run provision::abs --targets localhost action=tear_down 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/inventory.yaml
142+
$ bundle exec bolt task run provision::docker --targets localhost action=provision platform=ubuntu:14.04
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/inventory.yaml vars='{ "docker_run_opts": ["-p 8086:8086", "-p 3000:3000"]}'
169+
bundle exec bolt task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 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/inventory.yaml node_name=localhost:2222
175+
$ bundle exec bolt task run provision::docker --targets localhost action=tear_down 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/inventory.yaml
200+
$ bundle exec bolt task run provision::vagrant --targets localhost action=provision platform=ubuntu/xenial64
201201

202202
Started on localhost...
203203
Finished on localhost:
@@ -217,7 +217,7 @@ This leads to errors when anything tries to execute `puppet` commands on the tes
217217
To add the Puppet agent binary path to the *secure_path* please run the `provision::fix_secure_path` Bolt task:
218218

219219
```ruby
220-
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::fix_secure_path path=/opt/puppetlabs/bin -i inventory.yaml -t ssh_nodes
220+
$ bundle exec bolt task run provision::fix_secure_path path=/opt/puppetlabs/bin -i inventory.yaml -t ssh_nodes
221221

222222
Started on 127.0.0.1:2222...
223223
Finished on 127.0.0.1:2222:
@@ -229,7 +229,7 @@ Ran on 1 target in 0.84 sec
229229
tear_down
230230

231231
```ruby
232-
$ 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
232+
$ bundle exec bolt task run provision::vagrant --targets localhost action=tear_down node_name=127.0.0.1:2222
233233

234234
Started on localhost...
235235
Finished on localhost:
@@ -284,7 +284,7 @@ In the provision step you can invoke bundle exec rake 'litmus:provision_list[tes
284284
Manual invocation of the provision service task from a workflow can be done using:
285285

286286
```ruby
287-
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
287+
bolt task run provision::provision_service --targets localhost action=provision platform=centos-7-v20200813
288288
```
289289

290290
Or using Litmus:
@@ -320,7 +320,7 @@ provision
320320

321321
```powershell
322322
PS> $env:LITMUS_HYPERV_VSWITCH = 'internal_nat'
323-
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
323+
PS> bundle exec bolt task run provision::vagrant --targets localhost action=provision platform=centos/7 hyperv_smb_username=tp hyperv_smb_password=notMyrealPassword
324324
325325
Started on localhost...
326326
Finished on localhost:
@@ -353,7 +353,7 @@ Testing/development/debugging it is better to use ruby directly, you will need t
353353
Testing using bolt, the second step
354354

355355
```ruby
356-
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
356+
bundle exec bolt task run provision::docker --targets localhost action=provision platform=ubuntu:14.04
357357
```
358358

359359
## License

lib/docker_helper.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# frozen_string_literal: true
22

33
require 'json'
4-
require 'puppet_litmus'
54

6-
def docker_exec(container, command)
7-
run_local_command("docker exec #{container} #{command}")
5+
def docker_exec(container_id, command)
6+
run_local_command("docker exec #{container_id} #{command}")
87
end
98

109
def docker_image_os_release_facts(image)
@@ -34,18 +33,9 @@ def docker_image_os_release_facts(image)
3433
os_release_facts
3534
end
3635

37-
def docker_tear_down(node_name, inventory_location)
38-
extend PuppetLitmus::InventoryManipulation
39-
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
40-
raise "Unable to find '#{inventory_full_path}'" unless File.file?(inventory_full_path)
41-
42-
inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
43-
node_facts = facts_from_node(inventory_hash, node_name)
44-
remove_docker = "docker rm -f #{node_facts['container_id']}"
45-
run_local_command(remove_docker)
46-
remove_node(inventory_hash, node_name)
47-
puts "Removed #{node_name}"
48-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
36+
def docker_tear_down(container_id)
37+
run_local_command("docker rm -f #{container_id}")
38+
puts "Removed #{container_id}"
4939
{ status: 'ok' }
5040
end
5141

lib/inventory_helper.rb

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# frozen_string_literal: true
2+
3+
require 'yaml'
4+
require 'delegate'
5+
6+
# simple bolt inventory manipulator
7+
class InventoryHelper < SimpleDelegator
8+
def initialize(location)
9+
@location = location
10+
super(refresh)
11+
end
12+
13+
# Load inventory from location in YAML format
14+
# or generate a default structure
15+
#
16+
# @return [Hash]
17+
def refresh
18+
x = YAML.load_file(@location) if File.file?(@location)
19+
{ 'version' => 2, 'groups' => [] }.merge(x || {})
20+
end
21+
22+
# Save inventory to location in yaml format
23+
def save
24+
File.open(@location, 'wb+') { |f| f.write(to_yaml) }
25+
end
26+
27+
# Adds a node to a group specified, if group_name exists in inventory hash.
28+
#
29+
# @param node [Hash] node to add to the group
30+
# @param group [String] group of nodes to limit the search for the node_name in
31+
# @return [Hash] inventory_hash with node added to group if group_name exists in inventory hash.
32+
def add(node, group)
33+
# check if group exists
34+
if self['groups'].any? { |g| g['name'] == group }
35+
self['groups'].each do |g|
36+
g['targets'].push(node) if g['name'] == group
37+
end
38+
else
39+
# add new group
40+
self['groups'].push({ 'name' => group, 'targets' => [node] })
41+
end
42+
43+
self
44+
end
45+
46+
# Lookup a node
47+
#
48+
# @param uri [String] uri of node to find
49+
# @param name [String] name of node to find
50+
# @param group [String] limit search to group
51+
# @return [Hash] inventory target
52+
def lookup(uri = nil, name: nil, group: nil)
53+
value = uri || name
54+
key = uri.nil? ? 'name' : 'uri'
55+
56+
self['groups'].each do |g|
57+
next unless (group && group == g['name']) || group.nil?
58+
g['targets'].each do |t|
59+
return t if t[key].eql? value
60+
end
61+
end
62+
63+
# fallback lookup uri by name
64+
return lookup(value, group: group) if uri.nil?
65+
66+
raise "Failed to lookup target for #{key} #{value} in inventory #{inspect}"
67+
end
68+
69+
# Remove node
70+
#
71+
# @param node [Hash]
72+
# @return [Hash] inventory_hash with node of node_name removed.
73+
def remove(node)
74+
self['groups'].map! do |g|
75+
g['targets'].reject! { |target| target == node }
76+
g
77+
end
78+
79+
self
80+
end
81+
82+
class << self
83+
attr_accessor :instances
84+
85+
def open(location = nil)
86+
# Inventory location is an optional task parameter.
87+
location = location.nil? ? Dir.pwd : location
88+
location = if File.directory?(location)
89+
# DEPRECATED: puppet_litmus <= 1.3.0 support
90+
if Gem.loaded_specs['puppet_litmus'] && Gem.loaded_specs['puppet_litmus'].version <= Gem::Version.new('1.3.0')
91+
File.join(location, 'spec', 'fixtures', 'litmus_inventory.yaml')
92+
else
93+
File.join(location, 'inventory.yaml')
94+
end
95+
else
96+
location
97+
end
98+
99+
@instances ||= {}
100+
@instances[location] = new(location) unless @instances.key? location
101+
@instances[location]
102+
end
103+
end
104+
105+
protected
106+
107+
attr_accessor :location
108+
end

lib/task_helper.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
# frozen_string_literal: true
22

3-
def sanitise_inventory_location(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
17-
end
18-
19-
def get_inventory_hash(inventory_full_path)
20-
if File.file?(inventory_full_path)
21-
inventory_hash_from_inventory_file(inventory_full_path)
22-
else
23-
{ 'version' => 2, 'groups' => [{ 'name' => 'docker_nodes', 'targets' => [] }, { 'name' => 'ssh_nodes', 'targets' => [] }, { 'name' => 'winrm_nodes', 'targets' => [] }] }
24-
end
25-
end
26-
273
def run_local_command(command, dir = Dir.pwd)
284
require 'open3'
295
stdout, stderr, status = Open3.capture3(command, chdir: dir)

spec/tasks/provision_service_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
end
6666

6767
describe '#provision' do
68-
let(:inventory_location) { "#{Dir.pwd}/litmus_inventory.yaml" }
68+
let(:inventory) { InventoryHelper.open("#{Dir.pwd}/litmus_inventory.yaml") }
6969
let(:vars) { nil }
7070
let(:platform) { 'centos-8' }
7171
let(:retry_attempts) { 8 }
@@ -94,7 +94,7 @@
9494
},
9595
)
9696
.to_return(status: 200, body: '', headers: {})
97-
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
97+
expect { provision_service.provision(platform, inventory, vars, retry_attempts) }.to raise_error(RuntimeError)
9898
end
9999
end
100100

@@ -112,7 +112,7 @@
112112
},
113113
)
114114
.to_return(status: 200, body: '', headers: {})
115-
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
115+
expect { provision_service.provision(platform, inventory, vars, retry_attempts) }.to raise_error(RuntimeError)
116116
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
117117
.with(
118118
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
@@ -126,11 +126,11 @@
126126
)
127127
.to_return(status: 200, body: response_body.to_json, headers: {})
128128
allow(File).to receive(:open)
129-
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
129+
expect(provision_service.provision(platform, inventory, vars, retry_attempts)[:status]).to eq('ok')
130130
end
131131
end
132132

133-
context 'when response is avlid' do
133+
context 'when response is valid' do
134134
it 'return valid response' do
135135
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
136136
.with(
@@ -146,7 +146,7 @@
146146
.to_return(status: 200, body: response_body.to_json, headers: {})
147147

148148
allow(File).to receive(:open)
149-
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
149+
expect(provision_service.provision(platform, inventory, vars, retry_attempts)[:status]).to eq('ok')
150150
end
151151
end
152152
end

spec/unit/docker_helper_spec.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,10 @@
8181
end
8282

8383
describe '.docker_tear_down' do
84-
it 'expect to raise error if inventory file is not found' do
85-
allow(File).to receive(:file?).and_return(false)
86-
expect { docker_tear_down(container_id, inventory_location) }.to raise_error(RuntimeError, "Unable to find '#{inventory_location}/spec/fixtures/litmus_inventory.yaml'")
87-
end
88-
8984
it 'expect to return status ok' do
90-
allow(File).to receive(:file?).with(full_inventory_location).and_return(true)
91-
allow(File).to receive(:exist?).with(full_inventory_location).and_return(true)
92-
allow(File).to receive(:open).with(full_inventory_location, anything).and_yield(StringIO.new(inventory_yaml.dup))
9385
allow(self).to receive(:run_local_command).with("docker rm -f #{container_id}")
94-
allow(self).to receive(:remove_node).and_return(nil)
9586
expect {
96-
expect(docker_tear_down(container_id, inventory_location)).to eql({ status: 'ok' })
87+
expect(docker_tear_down(container_id)).to eql({ status: 'ok' })
9788
}.to output("Removed #{container_id}\n").to_stdout
9889
end
9990
end

tasks/abs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
}
2727
},
2828
"files": [
29-
"provision/lib/task_helper.rb"
29+
"provision/lib/task_helper.rb",
30+
"provision/lib/inventory_helper.rb"
3031
]
3132
}

0 commit comments

Comments
 (0)