diff --git a/recipes/_service.rb b/recipes/_service.rb index cc7a00aa..a94dc410 100644 --- a/recipes/_service.rb +++ b/recipes/_service.rb @@ -133,13 +133,22 @@ case node['consul']['init_style'] when 'init' + if platform?("ubuntu") + init_file = '/etc/init/consul.conf' + init_tmpl = 'consul.conf.erb' + else + init_file = '/etc/init.d/consul' + init_tmpl = 'consul-init.erb' + end + template node['consul']['etc_config_dir'] do source 'consul-sysconfig.erb' mode 0755 - notifies :create, 'template[/etc/init.d/consul]', :immediately + notifies :create, "template[#{init_file}]", :immediately end - template '/etc/init.d/consul' do - source 'consul-init.erb' + + template init_file do + source init_tmpl mode 0755 variables( consul_binary: "#{node['consul']['install_dir']}/consul", @@ -149,6 +158,7 @@ end service 'consul' do + provider Chef::Provider::Service::Upstart if platform?("ubuntu") supports status: true, restart: true, reload: true action [:enable, :start] subscribes :restart, "file[#{consul_config_filename}", :delayed diff --git a/spec/unit/recipes/_service_spec.rb b/spec/unit/recipes/_service_spec.rb index a9fcd992..85531384 100644 --- a/spec/unit/recipes/_service_spec.rb +++ b/spec/unit/recipes/_service_spec.rb @@ -18,11 +18,6 @@ .with(group: 'root') .with(mode: 0600) end - it do - expect(chef_run).to create_template('/etc/init.d/consul') - .with(source: 'consul-init.erb') - .with(mode: 0755) - end it do expect(chef_run).to enable_service('consul') .with(supports: {status: true, restart: true, reload: true}) @@ -30,10 +25,15 @@ end end - context 'config on centos' do + context 'init on centos' do let(:chef_run) do ChefSpec::Runner.new(platform: 'centos', version: '6.3').converge(described_recipe) end + it do + expect(chef_run).to create_template('/etc/init.d/consul') + .with(source: 'consul-init.erb') + .with(mode: 0755) + end it do expect(chef_run).to create_template('/etc/sysconfig/consul') .with(source: 'consul-sysconfig.erb') @@ -44,10 +44,15 @@ end end - context 'config on ubuntu' do + context 'init on ubuntu' do let(:chef_run) do ChefSpec::Runner.new(platform: 'ubuntu', version: '14.04').converge(described_recipe) end + it do + expect(chef_run).to create_template('/etc/init/consul.conf') + .with(source: 'consul.conf.erb') + .with(mode: 0755) + end it do expect(chef_run).to create_template('/etc/default/consul') .with(source: 'consul-sysconfig.erb') diff --git a/templates/default/consul.conf.erb b/templates/default/consul.conf.erb new file mode 100644 index 00000000..0ccf237b --- /dev/null +++ b/templates/default/consul.conf.erb @@ -0,0 +1,20 @@ +description "Consul Service Discovery Platform" + +emits consul-up + +start on runlevel [2345] +stop on runlevel [!2345] + +script + if [ -f <%= node['consul']['etc_config_dir'] %> ]; then + . <%= node['consul']['etc_config_dir'] %> + fi + export GOMAXPROCS=${GOMAXPROCS} + CMD="<%= @consul_binary %> agent -config-dir <%= @config_dir %>" + LOGFILE="/var/log/consul.log" + exec $CMD >> "$LOGFILE" +end script + +post-start exec initctl emit consul-up + +kill signal INT