Skip to content

Commit

Permalink
Update init scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
RiANOl committed Dec 1, 2016
1 parent 1404900 commit 1b0a1ee
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 170 deletions.
43 changes: 22 additions & 21 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
$purge = true,
) {

if $nomad::init_style {
if $::nomad::init_style != 'unmanaged' {

case $nomad::init_style {
case $::nomad::init_style {
'upstart' : {
file { '/etc/init/nomad.conf':
mode => '0444',
Expand All @@ -34,12 +34,6 @@
}
}
'systemd' : {
file { '/etc/sysconfig/nomad':
ensure => directory,
mode => '0644',
owner => 'root',
group => 'root',
}->
file { '/lib/systemd/system/nomad.service':
mode => '0644',
owner => 'root',
Expand All @@ -52,12 +46,12 @@
refreshonly => true,
}
}
'sysv' : {
'init','redhat' : {
file { '/etc/init.d/nomad':
mode => '0555',
owner => 'root',
group => 'root',
content => template('nomad/nomad.sysv.erb')
content => template('nomad/nomad.init.erb')
}
}
'debian' : {
Expand All @@ -84,26 +78,33 @@
content => template('nomad/nomad.launchd.erb')
}
}
'freebsd': {
file { '/etc/rc.conf.d/nomad':
mode => '0444',
owner => 'root',
group => 'wheel',
content => template('nomad/nomad.freebsd.erb')
}
}
default : {
fail("I don't know how to create an init script for style ${nomad::init_style}")
fail("I don't know how to create an init script for style ${::nomad::init_style}")
}
}
}

file { $nomad::config_dir:
ensure => 'directory',
owner => $nomad::user,
group => $nomad::group,
file { $::nomad::config_dir:
ensure => directory,
owner => $::nomad::user,
group => $::nomad::group,
purge => $purge,
recurse => $purge,
} ->
file { 'nomad config.json':
ensure => present,
path => "${nomad::config_dir}/config.json",
owner => $nomad::user,
group => $nomad::group,
mode => $nomad::config_mode,
content => nomad_sorted_json($config_hash, $nomad::pretty_config, $nomad::pretty_config_indent),
path => "${::nomad::config_dir}/config.json",
owner => $::nomad::user,
group => $::nomad::group,
mode => $::nomad::config_mode,
content => nomad_sorted_json($config_hash, $::nomad::pretty_config, $::nomad::pretty_config_indent),
}

}
14 changes: 7 additions & 7 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
case $::architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
'i386': { $arch = '386' }
/^arm.*/: { $arch = 'arm' }
default: {
fail("Unsupported kernel architecture: ${::architecture}")
}
Expand All @@ -53,13 +54,13 @@
}
} elsif $::operatingsystem =~ /Scientific|CentOS|RedHat|OracleLinux/ {
if versioncmp($::operatingsystemrelease, '7.0') < 0 {
$init_style = 'sysv'
$init_style = 'redhat'
} else {
$init_style = 'systemd'
}
} elsif $::operatingsystem == 'Fedora' {
if versioncmp($::operatingsystemrelease, '12') < 0 {
$init_style = 'sysv'
$init_style = 'init'
} else {
$init_style = 'systemd'
}
Expand All @@ -82,11 +83,10 @@
} elsif $::operatingsystem == 'Darwin' {
$init_style = 'launchd'
} elsif $::operatingsystem == 'Amazon' {
$init_style = 'sysv'
$init_style = 'redhat'
} elsif $::operatingsystem == 'FreeBSD' {
$init_style = 'freebsd'
} else {
$init_style = undef
}
if $init_style == undef {
fail('Unsupported OS')
fail('Cannot determine init_style, unsupported OS')
}
}
97 changes: 7 additions & 90 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,51 +249,14 @@
)}
end

context "When using sysv" do
context "When using init" do
let (:params) {{
:init_style => 'sysv'
:init_style => 'init'
}}
let (:facts) {{
:ipaddress_lo => '127.0.0.1'
}}
it { should contain_class('nomad').with_init_style('sysv') }
it {
should contain_file('/etc/init.d/nomad').
with_content(/-rpc-addr=127.0.0.1:8400/)
}
end

context "When overriding default rpc port on sysv" do
let (:params) {{
:init_style => 'sysv',
:config_hash => {
'ports' => {
'rpc' => '9999'
},
'addresses' => {
'rpc' => 'nomad.example.com'
}
}
}}
it { should contain_class('nomad').with_init_style('sysv') }
it {
should contain_file('/etc/init.d/nomad').
with_content(/-rpc-addr=nomad.example.com:9999/)
}
end

context "When rpc_addr defaults to client_addr on sysv" do
let (:params) {{
:init_style => 'sysv',
:config_hash => {
'client_addr' => '192.168.34.56',
}
}}
it { should contain_class('nomad').with_init_style('sysv') }
it {
should contain_file('/etc/init.d/nomad').
with_content(/-rpc-addr=192.168.34.56:8400/)
}
it { should contain_class('nomad').with_init_style('init') }
end

context "When using debian" do
Expand All @@ -304,29 +267,6 @@
:ipaddress_lo => '127.0.0.1'
}}
it { should contain_class('nomad').with_init_style('debian') }
it {
should contain_file('/etc/init.d/nomad').
with_content(/-rpc-addr=127.0.0.1:8400/)
}
end

context "When overriding default rpc port on debian" do
let (:params) {{
:init_style => 'debian',
:config_hash => {
'ports' => {
'rpc' => '9999'
},
'addresses' => {
'rpc' => 'nomad.example.com'
}
}
}}
it { should contain_class('nomad').with_init_style('debian') }
it {
should contain_file('/etc/init.d/nomad').
with_content(/-rpc-addr=nomad.example.com:9999/)
}
end

context "When using upstart" do
Expand All @@ -337,29 +277,6 @@
:ipaddress_lo => '127.0.0.1'
}}
it { should contain_class('nomad').with_init_style('upstart') }
it {
should contain_file('/etc/init/nomad.conf').
with_content(/-rpc-addr=127.0.0.1:8400/)
}
end

context "When overriding default rpc port on upstart" do
let (:params) {{
:init_style => 'upstart',
:config_hash => {
'ports' => {
'rpc' => '9999'
},
'addresses' => {
'rpc' => 'nomad.example.com'
}
}
}}
it { should contain_class('nomad').with_init_style('upstart') }
it {
should contain_file('/etc/init/nomad.conf').
with_content(/-rpc-addr=nomad.example.com:9999/)
}
end

context "On a redhat 6 based OS" do
Expand All @@ -368,7 +285,7 @@
:operatingsystemrelease => '6.5'
}}

it { should contain_class('nomad').with_init_style('sysv') }
it { should contain_class('nomad').with_init_style('redhat') }
it { should contain_file('/etc/init.d/nomad').with_content(/daemon --user=nomad/) }
end

Expand All @@ -387,7 +304,7 @@
:operatingsystemrelease => '3.10.34-37.137.amzn1.x86_64'
}}

it { should contain_class('nomad').with_init_style('sysv') }
it { should contain_class('nomad').with_init_style('redhat') }
it { should contain_file('/etc/init.d/nomad').with_content(/daemon --user=nomad/) }
end

Expand Down Expand Up @@ -437,8 +354,8 @@
end

context "When asked not to manage the init_style" do
let(:params) {{ :init_style => false }}
it { should contain_class('nomad').with_init_style(false) }
let(:params) {{ :init_style => 'unmanaged' }}
it { should contain_class('nomad').with_init_style('unmanaged') }
it { should_not contain_file("/etc/init.d/nomad") }
it { should_not contain_file("/lib/systemd/system/nomad.service") }
end
Expand Down
8 changes: 1 addition & 7 deletions templates/nomad.debian.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ PIDFILE=/var/run/$NAME/$NAME.pid
DAEMON_ARGS="agent -config <%= scope.lookupvar('nomad::config_dir') %> <%= scope.lookupvar('nomad::extra_options') %>"
USER=<%= scope.lookupvar('nomad::user') %>
SCRIPTNAME=/etc/init.d/$NAME
RPC_ADDR=-rpc-addr=<%= scope.lookupvar('nomad::rpc_addr') %>:<%= scope.lookupvar('nomad::rpc_port') %>

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
Expand Down Expand Up @@ -65,8 +64,7 @@ do_start()
RETVAL=2
sleep 1
continue
fi
if "$DAEMON" info ${RPC_ADDR} >/dev/null; then
else
return 0
fi
done
Expand All @@ -79,10 +77,6 @@ do_start()
#
do_stop()
{
# If nomad is not acting as a server, exit gracefully
if ("${DAEMON}" info ${RPC_ADDR} 2>/dev/null | grep -q 'server = false' 2>/dev/null) ; then
"$DAEMON" leave ${RPC_ADDR}
fi
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
Expand Down
15 changes: 15 additions & 0 deletions templates/nomad.freebsd.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Managed by Puppet
<% if scope.lookupvar('nomad::service_enable') %>
nomad_enable="YES"
<% else %>
nomad_enable="NO"
<% end %>
<% if scope.lookupvar('nomad::user') %>
nomad_user="<%= scope.lookupvar('nomad::user') %>"
<% end %>
<% if scope.lookupvar('nomad::group') %>
nomad_group="<%= scope.lookupvar('nomad::group') %>"
<% end %>
<% if scope.lookupvar('nomad::bin_dir') %>
nomad_dir="<%= scope.lookupvar('nomad::bin_dir') %>"
<% end %>
32 changes: 2 additions & 30 deletions templates/nomad.sysv.erb → templates/nomad.init.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
NOMAD=<%= scope.lookupvar('nomad::bin_dir') %>/nomad
CONFIG=<%= scope.lookupvar('nomad::config_dir') %>
PID_FILE=/var/run/nomad/nomad.pid
LOG_FILE=/var/log/nomad
RPC_ADDR=-rpc-addr=<%= scope.lookupvar('nomad::rpc_addr') %>:<%= scope.lookupvar('nomad::rpc_port') %>
LOG_FILE=<%= scope.lookupvar('nomad::log_file') %>

[ -e /etc/sysconfig/nomad ] && . /etc/sysconfig/nomad

Expand Down Expand Up @@ -66,33 +65,6 @@ stop() {
echo -n "Shutting down nomad: "
mkpidfile

# If nomad is not acting as a server, exit gracefully
# Use SIGINT to create a "leave" event, unless the user has explicitly
# changed that behavior in the nomad config.
if ("${NOMAD}" info ${RPC_ADDR} 2>/dev/null | grep -q 'server = false' 2>/dev/null) ; then
nomad_pid=$(cat $PID_FILE)
killproc $KILLPROC_OPT $NOMAD -INT
retcode=$?

# We'll wait if necessary to make sure the leave works, and return
# early if we can. If not, escalate to harsher signals.
try=0
while [ $try -lt $DELAY ]; do
if ! checkpid $nomad_pid ; then
rm -f /var/lock/subsys/nomad
return $retcode
fi
sleep 1
let try+=1
done
fi

# If acting as a server, use a SIGTERM to avoid a leave.
# This behavior is also configurable. Avoid doing a "leave" because
# having servers missing is a bad thing that we want to notice.
#
# A SIGTERM will mark the node as "failed" until it rejoins.
# killproc with no arguments uses TERM, then escalates to KILL.
killproc $KILLPROC_OPT $NOMAD
retcode=$?

Expand All @@ -108,7 +80,7 @@ case "$1" in
stop
;;
status)
"$NOMAD" info ${RPC_ADDR}
status -p $PID_FILE
;;
restart)
stop
Expand Down
15 changes: 8 additions & 7 deletions templates/nomad.systemd.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
[Unit]
Description=Nomad agent
Requires=network-online.target
After=network-online.target
Description=Nomad Agent
Requires=basic.target network-online.target
After=basic.target network-online.target

[Service]
EnvironmentFile=-/etc/sysconfig/nomad
Restart=on-failure
ExecStart=<%= scope.lookupvar('nomad::bin_dir') %>/nomad agent -config=<%= scope.lookupvar('nomad::config_dir') %> <%= scope.lookupvar('nomad::extra_options') %>
User=<%= scope.lookupvar('nomad::user') %>
Group=<%= scope.lookupvar('nomad::group') %>
ExecStart=<%= scope.lookupvar('nomad::bin_dir') %>/nomad agent \
-config=<%= scope.lookupvar('nomad::config_dir') %> <%= scope.lookupvar('nomad::extra_options') %>
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
RestartSec=42s
LimitNOFILE=131072

[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
Loading

0 comments on commit 1b0a1ee

Please sign in to comment.