Skip to content

Commit

Permalink
(maint) uncouple from the puppet_litmus gem
Browse files Browse the repository at this point in the history
  • Loading branch information
h0tw1r3 committed May 5, 2024
1 parent a134735 commit b47e388
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 170 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ $ cat ~/.fog
##### Setting up a new machine

```ruby
$ 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
$ bundle exec bolt task run provision::abs --targets localhost action=provision platform=ubuntu-1604-x86_64

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

```ruby
$ 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
$ bundle exec bolt task run provision::abs --targets localhost action=tear_down node_name=yh6f4djvz7o3te6.delivery.puppetlabs.net

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

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

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

```ruby
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"]}'
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"]}'
```

#### Tear down

```ruby
$ 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
$ bundle exec bolt task run provision::docker --targets localhost action=tear_down node_name=localhost:2222

Started on localhost...
Finished on localhost:
Expand All @@ -197,7 +197,7 @@ Tested with vagrant images:
provision

```ruby
$ 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
$ bundle exec bolt task run provision::vagrant --targets localhost action=provision platform=ubuntu/xenial64

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

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

Started on 127.0.0.1:2222...
Finished on 127.0.0.1:2222:
Expand All @@ -229,7 +229,7 @@ Ran on 1 target in 0.84 sec
tear_down

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

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

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

Or using Litmus:
Expand Down Expand Up @@ -320,7 +320,7 @@ provision

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

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

## License
Expand Down
20 changes: 5 additions & 15 deletions lib/docker_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# frozen_string_literal: true

require 'json'
require 'puppet_litmus'

def docker_exec(container, command)
run_local_command("docker exec #{container} #{command}")
def docker_exec(container_id, command)
run_local_command("docker exec #{container_id} #{command}")
end

def docker_image_os_release_facts(image)
Expand Down Expand Up @@ -34,18 +33,9 @@ def docker_image_os_release_facts(image)
os_release_facts
end

def docker_tear_down(node_name, inventory_location)
extend PuppetLitmus::InventoryManipulation
inventory_full_path = File.join(inventory_location, '/spec/fixtures/litmus_inventory.yaml')
raise "Unable to find '#{inventory_full_path}'" unless File.file?(inventory_full_path)

inventory_hash = inventory_hash_from_inventory_file(inventory_full_path)
node_facts = facts_from_node(inventory_hash, node_name)
remove_docker = "docker rm -f #{node_facts['container_id']}"
run_local_command(remove_docker)
remove_node(inventory_hash, node_name)
puts "Removed #{node_name}"
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
def docker_tear_down(container_id)
run_local_command("docker rm -f #{container_id}")
puts "Removed #{container_id}"
{ status: 'ok' }
end

Expand Down
108 changes: 108 additions & 0 deletions lib/inventory_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# frozen_string_literal: true

require 'yaml'
require 'delegate'

# simple bolt inventory manipulator
class InventoryHelper < SimpleDelegator
def initialize(location)
@location = location
super(refresh)
end

# Load inventory from location in YAML format
# or generate a default structure
#
# @return [Hash]
def refresh
x = YAML.load_file(@location) if File.file?(@location)
{ 'version' => 2, 'groups' => [] }.merge(x || {})
end

# Save inventory to location in yaml format
def save
File.open(@location, 'wb+') { |f| f.write(to_yaml) }
end

# Adds a node to a group specified, if group_name exists in inventory hash.
#
# @param node [Hash] node to add to the group
# @param group [String] group of nodes to limit the search for the node_name in
# @return [Hash] inventory_hash with node added to group if group_name exists in inventory hash.
def add(node, group)
# check if group exists
if self['groups'].any? { |g| g['name'] == group }
self['groups'].each do |g|
g['targets'].push(node) if g['name'] == group
end
else
# add new group
self['groups'].push({ 'name' => group, 'targets' => [node] })
end

self
end

# Lookup a node
#
# @param uri [String] uri of node to find
# @param name [String] name of node to find
# @param group [String] limit search to group
# @return [Hash] inventory target
def lookup(uri = nil, name: nil, group: nil)
value = uri || name
key = uri.nil? ? 'name' : 'uri'

self['groups'].each do |g|
next unless (group && group == g['name']) || group.nil?
g['targets'].each do |t|
return t if t[key].eql? value
end
end

# fallback lookup uri by name
return lookup(value, group: group) if uri.nil?

raise "Failed to lookup target for #{key} #{value} in inventory #{inspect}"
end

# Remove node
#
# @param node [Hash]
# @return [Hash] inventory_hash with node of node_name removed.
def remove(node)
self['groups'].map! do |g|
g['targets'].reject! { |target| target == node }
g
end

self
end

class << self
attr_accessor :instances

def open(location = nil)
# Inventory location is an optional task parameter.
location = location.nil? ? Dir.pwd : location
location = if File.directory?(location)
# DEPRECATED: puppet_litmus <= 1.3.0 support
if Gem.loaded_specs['puppet_litmus'] && 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

@instances ||= {}
@instances[location] = new(location) unless @instances.key? location
@instances[location]
end
end

protected

attr_accessor :location
end
24 changes: 0 additions & 24 deletions lib/task_helper.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
# frozen_string_literal: true

def sanitise_inventory_location(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)
if File.file?(inventory_full_path)
inventory_hash_from_inventory_file(inventory_full_path)
else
{ 'version' => 2, 'groups' => [{ 'name' => 'docker_nodes', 'targets' => [] }, { 'name' => 'ssh_nodes', 'targets' => [] }, { 'name' => 'winrm_nodes', 'targets' => [] }] }
end
end

def run_local_command(command, dir = Dir.pwd)
require 'open3'
stdout, stderr, status = Open3.capture3(command, chdir: dir)
Expand Down
12 changes: 6 additions & 6 deletions spec/tasks/provision_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
end

describe '#provision' do
let(:inventory_location) { "#{Dir.pwd}/litmus_inventory.yaml" }
let(:inventory) { InventoryHelper.open("#{Dir.pwd}/litmus_inventory.yaml") }
let(:vars) { nil }
let(:platform) { 'centos-8' }
let(:retry_attempts) { 8 }
Expand Down Expand Up @@ -94,7 +94,7 @@
},
)
.to_return(status: 200, body: '', headers: {})
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
expect { provision_service.provision(platform, inventory, vars, retry_attempts) }.to raise_error(RuntimeError)
end
end

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

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

allow(File).to receive(:open)
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
expect(provision_service.provision(platform, inventory, vars, retry_attempts)[:status]).to eq('ok')
end
end
end
Expand Down
11 changes: 1 addition & 10 deletions spec/unit/docker_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,10 @@
end

describe '.docker_tear_down' do
it 'expect to raise error if inventory file is not found' do
allow(File).to receive(:file?).and_return(false)
expect { docker_tear_down(container_id, inventory_location) }.to raise_error(RuntimeError, "Unable to find '#{inventory_location}/spec/fixtures/litmus_inventory.yaml'")
end

it 'expect to return status ok' do
allow(File).to receive(:file?).with(full_inventory_location).and_return(true)
allow(File).to receive(:exist?).with(full_inventory_location).and_return(true)
allow(File).to receive(:open).with(full_inventory_location, anything).and_yield(StringIO.new(inventory_yaml.dup))
allow(self).to receive(:run_local_command).with("docker rm -f #{container_id}")
allow(self).to receive(:remove_node).and_return(nil)
expect {
expect(docker_tear_down(container_id, inventory_location)).to eql({ status: 'ok' })
expect(docker_tear_down(container_id)).to eql({ status: 'ok' })
}.to output("Removed #{container_id}\n").to_stdout
end
end
Expand Down
3 changes: 2 additions & 1 deletion tasks/abs.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}
},
"files": [
"provision/lib/task_helper.rb"
"provision/lib/task_helper.rb",
"provision/lib/inventory_helper.rb"
]
}
Loading

0 comments on commit b47e388

Please sign in to comment.