Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a puppet_execution_environment hook method #284

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/kafo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ def params_default_values
execution_env = ExecutionEnvironment.new(self)
KafoConfigure.exit_handler.register_cleanup_path(execution_env.directory)

puppetconf = execution_env.configure_puppet('noop' => true)

dump_manifest = <<EOS
#{includes}
class { '::kafo_configure::dump_values':
Expand All @@ -180,8 +178,8 @@ class { '::kafo_configure::dump_values':
EOS

@logger.info 'Loading default values from puppet modules...'
command = PuppetCommand.new(dump_manifest, [], puppetconf, self).command
stdout, stderr, status = Open3.capture3(*PuppetCommand.format_command(command))
command = execution_env.build_command(dump_manifest, settings: {'noop' => true})
stdout, stderr, status = Open3.capture3(*command)

@logger.debug stdout
@logger.debug stderr
Expand Down
7 changes: 7 additions & 0 deletions lib/kafo/execution_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def configure_puppet(settings = {})
PuppetConfigurer.new(puppet_conf, settings)
end

def build_command(code, options: [], settings: {}, use_answers: false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I might like build_puppet_command to make it clearer to understand what sort of command is being built up. This method signature has the side affect of being able to be read as "build command" a noun rather than build as a verb.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative: would command make sense? You would get execution_env.command(code).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the command be anything I want? or is it specific to Puppet?

store_answers if use_answers
puppetconf = configure_puppet(settings)
command = Kafo::PuppetCommand.new(code, options, puppetconf, @config).command
Kafo::PuppetCommand.format_command(command)
end

private

def environmentpath
Expand Down
9 changes: 9 additions & 0 deletions lib/kafo/hook_context.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'fileutils'
require 'kafo/data_type'
require 'kafo/base_context'

Expand Down Expand Up @@ -134,5 +135,13 @@ def scenario_path
def scenario_data
self.kafo.config.app
end

# Yield a Puppet execution environment that's isolated from the system
def puppet_execution_environment
execution_env = Kafo::ExecutionEnvironment.new(self.kafo.config)
yield execution_env
ensure
FileUtils.rm_rf(execution_env.directory)
end
end
end
10 changes: 5 additions & 5 deletions lib/kafo/kafo_configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,14 @@ def run_installation
execution_env = ExecutionEnvironment.new(config)
self.class.exit_handler.register_cleanup_path(execution_env.directory)

execution_env.store_answers
puppetconf = execution_env.configure_puppet(
code = 'include kafo_configure'
settings = {
'color' => false,
'evaltrace' => !!@progress_bar,
'noop' => !!noop?,
'profile' => !!profile?,
'show_diff' => true,
)
}

exit_code = 0
exit_status = nil
Expand All @@ -442,11 +442,11 @@ def run_installation
'--detailed-exitcodes',
]
begin
command = PuppetCommand.new('include kafo_configure', options, puppetconf).command
command = execution_env.build_command(code, options: options, settings: settings, use_answers: true)
log_parser = PuppetLogParser.new
logger = Logger.new('configure')

PTY.spawn(*PuppetCommand.format_command(command)) do |stdin, stdout, pid|
PTY.spawn(*command) do |stdin, stdout, pid|
begin
stdin.each do |line|
line = normalize_encoding(line)
Expand Down
45 changes: 45 additions & 0 deletions test/kafo/hook_context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,50 @@ module Kafo
assert_equal false, context.has_custom_fact?('not_my_custom_fact')
end
end

describe '#puppet_execution_environment' do
let(:config) { Minitest::Mock.new }
let(:code) { }

before do
kafo.expect :config, config
end

specify 'creates directory' do
context.puppet_execution_environment do |env|
assert File.directory?(env.directory)
end
end

specify 'deletes directory' do
directory = context.puppet_execution_environment do |env|
env.directory
end

refute File.directory?(directory)
end

specify 'builds command' do
def config.app
{}
end
def config.module_dirs
['./modules']
end
def config.kafo_modules_dir
['./kafo_modules']
end
config.expect :scenario_id, 'foobar'
config.expect :config_file, 'foobar.yaml'

context.puppet_execution_environment do |env|
result = env.build_command("notice { 'Hello' }")
assert_kind_of(Array, result)
# TODO: result[0] can either be a Hash or ::ENV which is an Object
assert_match(%{notice { 'Hello' }}, result[1])
assert_equal({unsetenv_others: false}, result[2])
end
end
end
end
end