Skip to content

Commit 775430e

Browse files
Merge pull request #260 from h0tw1r3/uncouple
Uncouple from the puppet_litmus gem
2 parents 29bc65b + f677336 commit 775430e

23 files changed

+388
-286
lines changed

README.md

Lines changed: 12 additions & 12 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:
@@ -212,7 +212,7 @@ Ran on 1 node in 51.98 seconds
212212
For multi-node provisioning, you can assign arbitrary tags to the nodes you deploy, by passing an optional YAML-string 'vars' to the bolt task. In the example below we are assigning the role of `k8s-controller` to the provisioned node.
213213

214214
```ruby
215-
$ 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 vars='role: k8s-controller'
215+
$ bundle exec bolt task run provision::vagrant --targets localhost action=provision platform=ubuntu/xenial64 inventory=/Users/tp/workspace/git/provision vars='role: k8s-controller'
216216
```
217217

218218
sudo secure_path fix
@@ -223,7 +223,7 @@ This leads to errors when anything tries to execute `puppet` commands on the tes
223223
To add the Puppet agent binary path to the *secure_path* please run the `provision::fix_secure_path` Bolt task:
224224

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

228228
Started on 127.0.0.1:2222...
229229
Finished on 127.0.0.1:2222:
@@ -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/inventory.yaml node_name=127.0.0.1:2222
238+
$ bundle exec bolt task run provision::vagrant --targets localhost action=tear_down 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/inventory.yaml vars='role: puppetserver'
293+
bundle exec bolt 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/inventory.yaml hyperv_smb_username=tp hyperv_smb_password=notMyrealPassword
329+
PS> bundle exec bolt task run provision::vagrant --targets localhost action=provision platform=centos/7 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/inventory.yaml
362+
bundle exec bolt task run provision::docker --targets localhost action=provision platform=ubuntu:14.04
363363
```
364364

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

lib/task_helper.rb

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +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.4.0 support
9-
if Gem.loaded_specs['puppet_litmus'].version <= Gem::Version.new('1.4.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-
{
24-
'version' => 2,
25-
'groups' => [
26-
{ 'name' => 'docker_nodes', 'targets' => [] },
27-
{ 'name' => 'lxd_nodes', 'targets' => [] },
28-
{ 'name' => 'ssh_nodes', 'targets' => [] },
29-
{ 'name' => 'winrm_nodes', 'targets' => [] },
30-
]
31-
}
32-
end
33-
end
34-
353
def run_local_command(command, dir = Dir.pwd)
364
require 'open3'
375
stdout, stderr, status = Open3.capture3(command, chdir: dir)

spec/spec_helper_local.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'fileutils'
2+
3+
RSpec.configure do |rspec|
4+
rspec.expect_with :rspec do |c|
5+
c.max_formatted_output_length = nil
6+
end
7+
end
8+
9+
RSpec.shared_context('with tmpdir') do
10+
let(:tmpdir) { @tmpdir } # rubocop:disable RSpec/InstanceVariable
11+
12+
around(:each) do |example|
13+
Dir.mktmpdir('rspec-provision_test') do |t|
14+
FileUtils.mkdir_p(File.join(t, 'spec', 'fixtures'))
15+
@tmpdir = t
16+
example.run
17+
end
18+
end
19+
end

spec/tasks/abs_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55
require_relative '../../tasks/abs'
66
require 'yaml'
77

8-
RSpec.shared_context('with tmpdir') do
9-
let(:tmpdir) { @tmpdir } # rubocop:disable RSpec/InstanceVariable
10-
11-
around(:each) do |example|
12-
Dir.mktmpdir('rspec-provision_test') do |t|
13-
@tmpdir = t
14-
example.run
15-
end
16-
end
17-
end
18-
198
describe 'provision::abs' do
209
let(:abs) { ABSProvision.new }
2110
let(:inventory_dir) { "#{tmpdir}/spec/fixtures" }

0 commit comments

Comments
 (0)