diff --git a/manifests/config.pp b/manifests/config.pp index a433816..f734243 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -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', @@ -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', @@ -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' : { @@ -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), } } diff --git a/manifests/params.pp b/manifests/params.pp index da5467b..1976a8f 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -36,6 +36,7 @@ case $::architecture { 'x86_64', 'amd64': { $arch = 'amd64' } 'i386': { $arch = '386' } + /^arm.*/: { $arch = 'arm' } default: { fail("Unsupported kernel architecture: ${::architecture}") } @@ -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' } @@ -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') } } diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index 2825fc8..3e50263 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/templates/nomad.debian.erb b/templates/nomad.debian.erb index 9a4bac4..f09f762 100644 --- a/templates/nomad.debian.erb +++ b/templates/nomad.debian.erb @@ -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 @@ -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 @@ -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 diff --git a/templates/nomad.freebsd.erb b/templates/nomad.freebsd.erb new file mode 100644 index 0000000..c24b7dd --- /dev/null +++ b/templates/nomad.freebsd.erb @@ -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 %> diff --git a/templates/nomad.sysv.erb b/templates/nomad.init.erb similarity index 64% rename from templates/nomad.sysv.erb rename to templates/nomad.init.erb index e7b4cf8..0f5402a 100644 --- a/templates/nomad.sysv.erb +++ b/templates/nomad.init.erb @@ -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 @@ -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=$? @@ -108,7 +80,7 @@ case "$1" in stop ;; status) - "$NOMAD" info ${RPC_ADDR} + status -p $PID_FILE ;; restart) stop diff --git a/templates/nomad.systemd.erb b/templates/nomad.systemd.erb index 139d1d7..2b8a1ee 100644 --- a/templates/nomad.systemd.erb +++ b/templates/nomad.systemd.erb @@ -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 \ No newline at end of file +WantedBy=multi-user.target diff --git a/templates/nomad.upstart.erb b/templates/nomad.upstart.erb index 4a23297..9ad7e0d 100644 --- a/templates/nomad.upstart.erb +++ b/templates/nomad.upstart.erb @@ -10,7 +10,6 @@ env GROUP=<%= scope.lookupvar('nomad::group') %> env DEFAULTS=/etc/default/nomad env RUNDIR=/var/run/nomad env PID_FILE=/var/run/nomad/nomad.pid -env RPC_ADDR=-rpc-addr=<%= scope.lookupvar('nomad::rpc_addr') %>:<%= scope.lookupvar('nomad::rpc_port') %> pre-start script [ -e $DEFAULTS ] && . $DEFAULTS @@ -28,13 +27,6 @@ script exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $NOMAD -S -- agent -config $CONFIG <%= scope.lookupvar('nomad::extra_options') %> end script -pre-stop script - # Only leave the cluster if running as an agent - if ("${NOMAD}" info ${RPC_ADDR} 2>/dev/null | grep -q 'server = false' 2>/dev/null) ; then - exec "$NOMAD" leave ${RPC_ADDR} - fi -end script - respawn respawn limit 10 10 kill timeout 10