Skip to content

Commit

Permalink
Initial try to use augeas for defaults file management
Browse files Browse the repository at this point in the history
  • Loading branch information
electrical committed Feb 28, 2014
1 parent 772184b commit 862d677
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 40 deletions.
24 changes: 9 additions & 15 deletions manifests/service/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,24 @@

# defaults file content. Either from a hash or file
if ($elasticsearch::init_defaults_file != undef) {
$defaults_content = undef
$defaults_source = $elasticsearch::init_defaults_file
} elsif ($elasticsearch::init_defaults != undef and is_hash($elasticsearch::init_defaults) ) {
$defaults_content = template("${module_name}/etc/sysconfig/defaults.erb")
$defaults_source = undef
} else {
$defaults_content = undef
$defaults_source = undef
}

# Check if we are going to manage the defaults file.
if ( $defaults_content != undef or $defaults_source != undef ) {

file { "${elasticsearch::params::defaults_location}/${name}":
ensure => $elasticsearch::ensure,
source => $defaults_source,
content => $defaults_content,
source => $elasticsearch::init_defaults_file,
owner => 'root',
group => 'root',
mode => '0644',
before => Service[$name],
notify => $notify_service
}

} elsif ($elasticsearch::init_defaults != undef and is_hash($elasticsearch::init_defaults) ) {
augeas { "defaults_${name}":
context => "/files${elasticsearch::params::defaults_location}/${name}",
changes => template("${module_name}/etc/sysconfig/defaults.erb"),
before => Service[$name],
notify => $notify_service
}

}

# init file from template
Expand Down
27 changes: 11 additions & 16 deletions manifests/service/systemd.pp
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,26 @@

# defaults file content. Either from a hash or file
if ($elasticsearch::init_defaults_file != undef) {
$defaults_content = undef
$defaults_source = $elasticsearch::init_defaults_file
} elsif ($elasticsearch::init_defaults != undef and is_hash($elasticsearch::init_defaults) ) {
$defaults_content = template("${module_name}/etc/sysconfig/defaults.erb")
$defaults_source = undef
} else {
$defaults_content = undef
$defaults_source = undef
}

# Check if we are going to manage the defaults file.
if ( $defaults_content != undef or $defaults_source != undef ) {

file { "${elasticsearch::params::defaults_location}/${name}":
ensure => $elasticsearch::ensure,
source => $defaults_source,
content => $defaults_content,
source => $elasticsearch::init_defaults_file,
owner => 'root',
group => 'root',
mode => '0644',
before => Service['elasticsearch'],
before => Service[$name],
notify => $notify_service
}

} elsif ($elasticsearch::init_defaults != undef and is_hash($elasticsearch::init_defaults) ) {
augeas { "defaults_${name}":
context => "/files${elasticsearch::params::defaults_location}/${name}",
changes => template("${module_name}/etc/sysconfig/defaults.erb"),
before => Service[$name],
notify => $notify_service
}

}

}

# action
Expand Down
39 changes: 39 additions & 0 deletions spec/acceptance/011_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper_acceptance'

describe "Service tests:" do

case fact('osfamily')
when 'RedHat'
defaults_file = '/etc/sysconfig/elasticsearch'
when 'Debian'
defaults_file = '/etc/default/elasticsearch'
when 'Suse'
defaults_file = '/etc/sysconfig/elasticsearch'
end


describe "Make sure we can manage the defaults file" do

context "Change the defaults file" do
it 'should run successfully' do
pp = "class { 'elasticsearch': manage_repo => true, repo_version => '1.0', java_install => true, config => { 'node.name' => 'elasticsearch001' }, init_defaults => { 'USER' => 'root' } }"

# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
sleep 10
expect(apply_manifest(pp, :catch_failures => true).exit_code).to be_zero
end
end

context "Make sure we have USER=root" do

describe file(defaults_file) do
it { should contain 'USER=root' }
it { should_not contain 'USER=elasticsearch' }
end

end

end

end
4 changes: 2 additions & 2 deletions spec/classes/001_elasticsearch_init_debian_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
:init_defaults => { 'SERVICE_USER' => 'root', 'SERVICE_GROUP' => 'root' }
} end

it { should contain_file('/etc/default/elasticsearch').with(:content => "### MANAGED BY PUPPET ###\n\nSERVICE_GROUP=root\nSERVICE_USER=root\n", :notify => 'Service[elasticsearch]') }
it { should contain_augeas('defaults_elasticsearch').with(:notify => 'Service[elasticsearch]', :context => '/files/etc/default/elasticsearch', :changes => "set SERVICE_GROUP root\nset SERVICE_USER root\n") }

end

Expand All @@ -174,7 +174,7 @@
:restart_on_change => false
} end

it { should contain_file('/etc/default/elasticsearch').with(:content => "### MANAGED BY PUPPET ###\n\nSERVICE_GROUP=root\nSERVICE_USER=root\n").without_notify }
it { should contain_augeas('defaults_elasticsearch').with(:context => '/files/etc/default/elasticsearch', :changes => "set SERVICE_GROUP root\nset SERVICE_USER root\n").without_notify }

end

Expand Down
4 changes: 2 additions & 2 deletions spec/classes/002_elasticsearch_init_redhat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
:init_defaults => { 'SERVICE_USER' => 'root', 'SERVICE_GROUP' => 'root' }
} end

it { should contain_file('/etc/sysconfig/elasticsearch').with(:content => "### MANAGED BY PUPPET ###\n\nSERVICE_GROUP=root\nSERVICE_USER=root\n", :notify => 'Service[elasticsearch]') }
it { should contain_augeas('defaults_elasticsearch').with(:notify => 'Service[elasticsearch]', :context => '/files/etc/sysconfig/elasticsearch', :changes => "set SERVICE_GROUP root\nset SERVICE_USER root\n") }

end

Expand All @@ -174,7 +174,7 @@
:restart_on_change => false
} end

it { should contain_file('/etc/sysconfig/elasticsearch').with(:content => "### MANAGED BY PUPPET ###\n\nSERVICE_GROUP=root\nSERVICE_USER=root\n").without_notify }
it { should contain_augeas('defaults_elasticsearch').with(:context => '/files/etc/sysconfig/elasticsearch', :changes => "set SERVICE_GROUP root\nset SERVICE_USER root\n").without_notify }

end

Expand Down
4 changes: 2 additions & 2 deletions spec/classes/003_elasticsearch_init_opensuse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
:init_defaults => { 'SERVICE_USER' => 'root', 'SERVICE_GROUP' => 'root' }
} end

it { should contain_file('/etc/sysconfig/elasticsearch').with(:content => "### MANAGED BY PUPPET ###\n\nSERVICE_GROUP=root\nSERVICE_USER=root\n", :notify => 'Service[elasticsearch]') }
it { should contain_augeas('defaults_elasticsearch').with(:notify => 'Service[elasticsearch]', :context => '/files/etc/sysconfig/elasticsearch', :changes => "set SERVICE_GROUP root\nset SERVICE_USER root\n") }

end

Expand All @@ -174,7 +174,7 @@
:restart_on_change => false
} end

it { should contain_file('/etc/sysconfig/elasticsearch').with(:content => "### MANAGED BY PUPPET ###\n\nSERVICE_GROUP=root\nSERVICE_USER=root\n").without_notify }
it { should contain_augeas('defaults_elasticsearch').with(:context => '/files/etc/sysconfig/elasticsearch', :changes => "set SERVICE_GROUP root\nset SERVICE_USER root\n").without_notify }

end

Expand Down
4 changes: 1 addition & 3 deletions templates/etc/sysconfig/defaults.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
### MANAGED BY PUPPET ###

<% scope.lookupvar('elasticsearch::init_defaults').sort.map do |key, value| -%>
<%= key %>=<%= value %>
set <%= key %> <%= value %>
<% end -%>

0 comments on commit 862d677

Please sign in to comment.