diff --git a/.gitignore b/.gitignore index b7c5ea6..b48a3fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .*.sw? pkg -spec/fixtures +spec/fixtures/modules .rspec_system Gemfile.lock .bundle diff --git a/manifests/init.pp b/manifests/init.pp index 5016b67..5a921e9 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -22,6 +22,7 @@ $user = $synapse::params::user, $group = $synapse::params::group, $stats_socket = $synapse::params::stats_socket, + $working_dir = '/run/synapse', $haproxy_daemon = true, $haproxy_reload_command = $synapse::params::haproxy_reload_command, $haproxy_bind_address = 'localhost', @@ -62,12 +63,20 @@ } if str2bool($service_manage) { - class { 'synapse::system_service': - subscribe => [ + initscript { 'synapse': + ulimit => { + 'nofile' => '65535', + }, + before_command => [ + ['mkdir', '-p', "${working_dir}/sockets", "${working_dir}/services"], + ['chown', '-R', $user, $working_dir], + ], + command => ['setuidgid', $user, '/usr/bin/synapse', '--config', $config_file], + service_ensure => $service_ensure, + subscribe => [ Class['synapse::install'], Class['synapse::config'], ], } } - } diff --git a/manifests/service.pp b/manifests/service.pp index b4b29ea..4dd2419 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -70,7 +70,7 @@ } if str2bool($synapse::service_manage) { - File[$target] ~> Service['synapse'] + File[$target] ~> Initscript['synapse'] } } diff --git a/manifests/system_service.pp b/manifests/system_service.pp deleted file mode 100644 index afc468f..0000000 --- a/manifests/system_service.pp +++ /dev/null @@ -1,46 +0,0 @@ -# == Class synapse::service -# -# This class is meant to be called from synapse -# It ensure the service is running -# -class synapse::system_service { - $user = $synapse::user - - # TODO: This assumes upstart. Be more compatible someday - - $config_file = $synapse::config_file - file { '/etc/init/synapse.conf': - owner => 'root', - group => 'root', - mode => '0444', - content => template('synapse/synapse.conf.upstart.erb'), - } - - if $::osfamily == 'RedHat' and $::operatingsystemmajrelease == 6 { - service { 'synapse': - ensure => $synapse::service_ensure, - enable => false, - hasstatus => true, - start => '/sbin/initctl start synapse', - stop => '/sbin/initctl stop synapse', - status => '/sbin/initctl status synapse | grep "/running" 1>/dev/null 2>&1', - subscribe => File['/etc/init/synapse.conf'], - } - } else { - service { 'synapse': - ensure => $::synapse::service_ensure, - enable => str2bool($::synapse::service_enable), - hasstatus => true, - subscribe => File['/etc/init/synapse.conf'], - } - } - - $log_file = $synapse::log_file - file { $log_file: - ensure => file, - owner => $user, - group => $synapse::group, - mode => '0640', - } - -} diff --git a/metadata.json b/metadata.json index b022bd1..18a4dae 100644 --- a/metadata.json +++ b/metadata.json @@ -2,7 +2,7 @@ "name": "KyleAnderson-synapse", "version": "0.0.5", "author": "Kyle Anderson ", - "summary": "Puppet configuration for Airbnbs system for service discovery. Configures a dynamic HAproxy based on zookeeper entries", + "summary": "Puppet configuration for Airbnb's system for service discovery. Configures a dynamic HAproxy based on zookeeper entries", "license": "Apache-2.0", "source": "https://github.com/solarkennedy/puppet-synapse", "project_page": "https://github.com/solarkennedy/puppet-synapse", @@ -11,6 +11,9 @@ { "name": "puppetlabs/stdlib", "version_requirement": ">= 3.0.0" + }, + { + "name": "EvanKrall/puppet-initscript" } ] } diff --git a/spec/classes/synapse_spec.rb b/spec/classes/synapse_spec.rb index 0d99c81..ef0de0c 100644 --- a/spec/classes/synapse_spec.rb +++ b/spec/classes/synapse_spec.rb @@ -11,7 +11,6 @@ it { should contain_class('synapse::params') } it { should contain_class('synapse::install') } it { should contain_class('synapse::config') } - it { should contain_class('synapse::system_service') } end end end @@ -48,7 +47,7 @@ :name => 'rubygem-synapse' ) } end - + # Config stuff context 'config by default' do let(:params) {{ }} @@ -103,8 +102,8 @@ context 'when requested not to run' do let(:params) {{ :service_ensure => 'stopped' }} let(:facts) {{ :osfamily => 'Debian' }} - it { should contain_service('synapse').with( - :ensure => 'stopped' + it { should contain_initscript('synapse').with( + :service_ensure => 'stopped' ) } end diff --git a/spec/defines/synapse_service_spec.rb b/spec/defines/synapse_service_spec.rb index efad51d..fcdbe97 100644 --- a/spec/defines/synapse_service_spec.rb +++ b/spec/defines/synapse_service_spec.rb @@ -1,5 +1,12 @@ require 'spec_helper' describe 'synapse::service', :type => :define do + let(:facts) {{ :osfamily => 'Debian' }} + let(:pre_condition) do + global_pre_condition + + [ + "class { 'synapse': }", + ] + end describe 'With an example service' do let :title do @@ -36,7 +43,7 @@ ) } it { should contain_file('/etc/synapse/conf.d/example_service.json').with_content(/\"default_servers\":/) } - + it { should contain_file('/etc/synapse/conf.d/example_service.json').with_content(/\"discovery\": \{\n \"method\": \"zookeeper\",\n \"path\": \"\/airbnb\/service\/service2\",\n \"hosts\": \[\n \"zk0\.airbnb\.com:2181\",/) } it { should contain_file('/etc/synapse/conf.d/example_service.json').with_content(/\"haproxy\": \{\n \"port\": \"3214\",\n \"server_options\": \"check inter 2s rise 3 fall 2\",\n \"listen\": \[\n \"mode http\",\n \"option httpchk \/health\"\n \]\n\}\n\}/ ) } diff --git a/spec/fixtures/manifests/site.pp b/spec/fixtures/manifests/site.pp new file mode 100644 index 0000000..7275684 --- /dev/null +++ b/spec/fixtures/manifests/site.pp @@ -0,0 +1,18 @@ +define initscript( + $command, + $manage_service = true, + $user = undef, + $group = undef, + $service_ensure = 'running', + $service_enable = true, + $has_reload = true, + $reload_command = undef, + $launchd_name = undef, + $description = '', + $short_description = '', + $init_style = undef, + $source_default_file = false, + $default_file_path = undef, + $before_command = [], + $ulimit = {} +) {} diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2c6f566..c4e5fc2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1 +1,19 @@ +require 'rspec/core/shared_context' require 'puppetlabs_spec_helper/module_spec_helper' + +module GlobalHelper + extend RSpec::Core::SharedContext + + def global_pre_condition + if ENV['FUTURE_PARSER'] == 'yes' or not Puppet.version =~ /^3/ + [(File.read('spec/fixtures/manifests/site.pp') rescue ''), + 'Package { provider => "apt" }'] + else [] end + end + + let(:pre_condition) { global_pre_condition } +end + +RSpec.configure do |c| + c.include GlobalHelper +end diff --git a/templates/synapse.conf.upstart.erb b/templates/synapse.conf.upstart.erb deleted file mode 100644 index 98f23b4..0000000 --- a/templates/synapse.conf.upstart.erb +++ /dev/null @@ -1,23 +0,0 @@ -description "synapse" - -respawn -respawn limit 10 5 - -script - set -e - - # When we no longer need to maintain compatibility with OSes != Ubuntu - # Trusty, we can remove this, as Trusty provides logging and log - # rotation for free. - exec >>"<%= @log_file %>" - - # Allow HAProxy to use lots of fds - ulimit -n 65535 - - # Create synapse working directory if necessary - SYNAPSE_WORKING_DIR=/var/run/synapse - mkdir -p "$SYNAPSE_WORKING_DIR" - chown -R <%= @user %> "$SYNAPSE_WORKING_DIR" - - exec setuidgid <%= @user %> synapse --config <%= @config_file %> 2>&1 -end script