Skip to content

Commit

Permalink
(PE-19049) Add method to create or update a meep node.conf file
Browse files Browse the repository at this point in the history
This is necessary if we need to adjust parameters for a specific node
rather than for all infrastructure via pe.conf.
  • Loading branch information
jpartlow committed Feb 23, 2017
1 parent d4bab31 commit bb4094a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 31 deletions.
20 changes: 19 additions & 1 deletion lib/beaker-pe/install/pe_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module PEUtils
DEFAULT_MEEP_CLASSIFICATION = false
MEEP_DATA_DIR = '/etc/puppetlabs/enterprise'
PE_CONF_FILE = "#{MEEP_DATA_DIR}/conf.d/pe.conf"
NODE_CONF_PATH = "#{MEEP_DATA_DIR}/conf.d/nodes"

# @!macro [new] common_opts
# @param [Hash{Symbol=>String}] opts Options to alter execution.
Expand Down Expand Up @@ -1227,7 +1228,7 @@ def update_pe_conf(parameters, pe_conf_file = PE_CONF_FILE)
end

updated = case value
when String
when String
pe_conf.set_value(hocon_key, hocon_value)
when nil
pe_conf.remove_value(hocon_key)
Expand All @@ -1244,6 +1245,23 @@ def update_pe_conf(parameters, pe_conf_file = PE_CONF_FILE)
on(master, 'cat /etc/puppetlabs/enterprise/conf.d/pe.conf')
end
end

def create_or_update_node_conf(host, parameters, node_conf_path = NODE_CONF_PATH)
node_conf_file = "#{node_conf_path}/#{host.puppet['certname']}.conf"
step "Create or Update #{node_conf_file} with #{parameters}" do
if !master.file_exist?(node_conf_file)
if !master.file_exist?(node_conf_path)
# potentially create the nodes directory
on(master, "mkdir #{node_conf_path}")
end
# The hocon gem will create a list of comma separated parameters
# on the same line unless we start with something in the file.
create_remote_file(master, node_conf_file, %Q|{\n}\n|)
on(master, "chown pe-puppet #{node_conf_file}")
end
update_pe_conf(parameters, node_conf_file)
end
end
end
end
end
Expand Down
121 changes: 91 additions & 30 deletions spec/beaker-pe/install/pe_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,25 @@ def test_setup(mock_values)
end
end

def assert_meep_conf_edit(input, output, path, &test)
# mock reading pe.conf
expect(master).to receive(:exec).with(
have_attributes(:command => match(%r{cat #{path}})),
anything
).and_return(
double('result', :stdout => input)
)

# mock writing pe.conf and check for parameters
expect(subject).to receive(:create_remote_file).with(
master,
path,
output
)

yield
end

describe 'configure_puppet_agent_service' do
let(:pe_version) { '2017.1.0' }
let(:master) { hosts[0] }
Expand Down Expand Up @@ -1656,27 +1675,14 @@ def test_setup(mock_values)
end

it "modifies the agent puppet service settings in pe.conf" do
# mock reading pe.conf
expect(master).to receive(:exec).with(
have_attributes(:command => match(%r{cat .*/pe\.conf})),
anything
).and_return(
double('result', :stdout => pe_conf)
)

# mock hitting the console
dispatcher = double('dispatcher').as_null_object
expect(subject).to receive(:get_console_dispatcher_for_beaker_pe)
.and_return(dispatcher)

# mock writing pe.conf and check for parameters
expect(subject).to receive(:create_remote_file).with(
master,
pe_conf_path,
gold_pe_conf
)

subject.configure_puppet_agent_service(:ensure => 'stopped', :enabled => false)
assert_meep_conf_edit(pe_conf, gold_pe_conf, pe_conf_path) do
subject.configure_puppet_agent_service(:ensure => 'stopped', :enabled => false)
end
end
end
end
Expand Down Expand Up @@ -1713,31 +1719,86 @@ def test_setup(mock_values)
"namespace::changed": "new"
"namespace::add": "hi"
"namespace::add2": "other"
EOF
end

it "adds, changes and removes hocon parameters from pe.conf" do
# mock reading pe.conf
expect(master).to receive(:exec).with(
have_attributes(:command => match(%r{cat .*/pe\.conf})),
anything
).and_return(
double('result', :stdout => pe_conf)
)
assert_meep_conf_edit(pe_conf, gold_pe_conf, pe_conf_path) do
subject.update_pe_conf(
{
"namespace::add" => "hi",
"namespace::changed" => "new",
"namespace::removed" => nil,
"namespace::add2" => "other",
}
)
end
end
end
end

describe 'create_or_update_node_conf' do
let(:pe_version) { '2017.1.0' }
let(:master) { hosts[0] }
let(:node) { hosts[1] }
let(:node_conf_path) { "/etc/puppetlabs/enterprise/conf.d/nodes/vm2.conf" }
let(:node_conf) do
<<-EOF
"namespace::removed": "bye"
"namespace::changed": "old"
EOF
end
let(:updated_node_conf) do
<<-EOF
"namespace::changed": "new"
"namespace::add": "hi"
EOF
end
let(:created_node_conf) do
<<-EOF
{
"namespace::one": "red"
"namespace::two": "blue"
}
EOF
end

# mock writing pe.conf and check for parameters
expect(subject).to receive(:create_remote_file).with(
master,
pe_conf_path,
gold_pe_conf
before(:each) do
hosts.each { |h| h[:pe_ver] = pe_version }
allow( subject ).to receive( :hosts ).and_return( hosts )
end

it 'requires parameters' do
expect { subject.update_pe_conf}.to raise_error(ArgumentError, /wrong number/)
end

it 'creates a node file that did not exist' do
expect(master).to receive(:file_exist?).with(node_conf_path).and_return(false)
expect(master).to receive(:file_exist?).with("/etc/puppetlabs/enterprise/conf.d/nodes").and_return(false)
expect(subject).to receive(:create_remote_file).with(master, node_conf_path, %Q|{\n}\n|)

assert_meep_conf_edit(%Q|{\n}\n|, created_node_conf, node_conf_path) do
subject.create_or_update_node_conf(
node,
{
"namespace::one" => "red",
"namespace::two" => "blue",
},
)
end
end

subject.update_pe_conf(
it 'updates a node file that did exist' do
assert_meep_conf_edit(node_conf, updated_node_conf, node_conf_path) do
subject.create_or_update_node_conf(
node,
{
"namespace::add" => "hi",
"namespace::changed" => "new",
"namespace::removed" => nil,
}
},
)
end
end
Expand Down

0 comments on commit bb4094a

Please sign in to comment.