Commit 6cc5d351 authored by Matthias Pausch's avatar Matthias Pausch
Browse files

Add generic config-file renderer for /etc/krb5.conf

parent bb40950f
module Sys
module Harry
def generate_harry_config(sections, indent=1, flags={})
if flags[:sections].nil?
flags[:sections] = true
end
unless flags[:indentation]
flags[:indentation] = ''
8.times{flags[:indentation] << ' '}
end
flags[:separator] ||= '='
flags[:separator].strip!
if flags[:separator].length > 0
flags[:separator].prepend(' ') << ' '
else
flags[:separator] = ' '
end
# Should not be set by user
flags[:align] = ''
file = ''
sections.each do |name, section|
file << "[#{name}]\n" if flags[:sections]
max_key_length = section.keys.max_by {|e| e.length}.length
section.each do |k, v|
flags[:align] = ''
(max_key_length - k.length).times { flags[:align] << ' '}
render_harry_config(file, k, v, indent, flags)
end
file << "\n"
end
file.strip
end
def render_harry_config(config, key, value, indent, flags)
indentation = ''
indent.times { indentation << flags[:indentation] }
case value
when Array
value.each do |elt|
render_harry_config(config, key, elt, indent, flags)
end
when Hash
max_key_length = value.keys.max_by {|e| e.length}.length
# Strictly speaking, flags[:align] should not be reset, but
# that yields slightly less readable config-files
flags[:align] = ''
config << "#{indentation}#{key}#{flags[:align]}#{flags[:separator]}{\n"
value.each do |k, v|
flags[:align] = ''
(max_key_length - k.length).times { flags[:align] << ' '}
render_harry_config(config, k, v, indent + 1, flags)
end
config << "#{indentation}}\n"
else
config << "#{indentation}#{key}#{flags[:align]}#{flags[:separator]}#{value}\n"
end
end
end
end
......@@ -6,4 +6,4 @@ description 'System Software configuration and maintenance'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
source_url 'https://github.com/GSI-HPC/sys-chef-cookbook'
issues_url 'https://github.com/GSI-HPC/sys-chef-cookbook/issues'
version '1.24.1'
version '1.25.0'
......@@ -27,21 +27,30 @@ unless node['sys']['krb5'].empty?
kstart
).each { |p| package p }
template "/etc/krb5.conf" do
source "etc_krb5.conf.erb"
owner "root"
group "root"
mode "0644"
variables(
:realm => node['sys']['krb5']['realm'].upcase,
:realms => begin node['sys']['krb5']['realms'] || [] rescue [] end,
:admin_server => node['sys']['krb5']['admin_server'],
:servers => [ node['sys']['krb5']['master'], node['sys']['krb5']['slave'] ],
:domain => node['domain'],
:wallet_server => begin node['sys']['krb5']['wallet_server'] rescue nil end,
:use_pkinit => begin node['sys']['krb5']['use_pkinit'] rescue nil end,
:libdefaults => begin node['sys']['krb5']['libdefaults'] rescue nil end
)
if node['sys']['krb5']['krb5.conf']
template "/etc/krb5.conf" do
helpers(Sys::Harry)
source "etc_krb5.conf_generic.erb"
mode "0644"
variables(:sections => node['sys']['krb5']['krb5.conf'])
end
else
template "/etc/krb5.conf" do
source "etc_krb5.conf.erb"
owner "root"
group "root"
mode "0644"
variables(
:realm => node['sys']['krb5']['realm'].upcase,
:realms => begin node['sys']['krb5']['realms'] || [] rescue [] end,
:admin_server => node['sys']['krb5']['admin_server'],
:servers => [ node['sys']['krb5']['master'], node['sys']['krb5']['slave'] ],
:domain => node['domain'],
:wallet_server => begin node['sys']['krb5']['wallet_server'] rescue nil end,
:use_pkinit => begin node['sys']['krb5']['use_pkinit'] rescue nil end,
:libdefaults => begin node['sys']['krb5']['libdefaults'] rescue nil end
)
end
end
package "wallet-client"
......
<%= generate_harry_config(@sections) %>
\ No newline at end of file
......@@ -40,6 +40,7 @@ describe 'sys::krb5' do
:admin_server => "master.example.com",
:servers => [ 'master.example.com', 'slave.example.com'],
:domain => "example.com",
:realms => [],
:wallet_server => nil,
:use_pkinit => nil,
:libdefaults => nil
......@@ -76,6 +77,7 @@ describe 'sys::krb5' do
:servers => [ 'master.example.com', 'slave.example.com'],
:domain => "example.com",
:wallet_server => "wallet.example.com",
:realms => [],
:libdefaults => nil,
:use_pkinit => true
}
......@@ -99,4 +101,48 @@ describe 'sys::krb5' do
expect(chef_run).to deploy_sys_wallet('host/node.example.com')
end
end
context 'sys_harry' do
cached(:chef_run) do
ChefSpec::SoloRunner.new do |node|
node.default['sys']['krb5']['krb5.conf'] = {
:realms => {
'EXAMPLE.COM' => {
'kdc' => [
'kdc1.example.com',
'kdc2.example.com'
],
'admin_server' => 'kdc1.example.com',
}
},
:appdefaults => {
'default_realm' => 'EXAMPLE.COM',
'wallet' => {
'wallet_port' => '4373',
'wallet_server' => 'wallet.example.com'
}
}
}
end.converge(described_recipe)
end
it 'deploys krb5.conf with two sections' do
config = ''
config << "[realms]\n"
config << " EXAMPLE.COM = {\n"
config << " kdc = kdc1.example.com\n"
config << " kdc = kdc2.example.com\n"
config << " admin_server = kdc1.example.com\n"
config << " }\n"
config << "\n"
config << "[appdefaults]\n"
config << " default_realm = EXAMPLE.COM\n"
config << " wallet = {\n"
config << " wallet_port = 4373\n"
config << " wallet_server = wallet.example.com\n"
config << " }"
expect(chef_run).to create_template('/etc/krb5.conf').with_mode('0644')
expect(chef_run).to render_file("/etc/krb5.conf").with_content(config)
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment