Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using poise_service instead of manual service creation #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ Style/StringLiterals:
Style/TrailingCommaInLiteral:
Exclude:
- 'attributes/checksums.rb'

# Offense count: 1
Metrics/BlockLength:
Enabled: false
18 changes: 10 additions & 8 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
default['consul_template']['config'] = Hash.new

# Windows only
default['consul_template']['nssm_params'] = {
'AppDirectory' => data_path,
'AppStdout' => join_path(config_prefix_path, 'stdout.log'),
'AppStderr' => join_path(config_prefix_path, 'error.log'),
'AppRotateFiles' => 1,
'AppRotateOnline' => 1,
'AppRotateBytes' => 20_000_000
}
if node['platform'] == 'windows'
default['consul_template']['nssm_params'] = {
'AppDirectory' => data_path,
'AppStdout' => join_path(config_prefix_path, 'stdout.log'),
'AppStderr' => join_path(config_prefix_path, 'error.log'),
'AppRotateFiles' => 1,
'AppRotateOnline' => 1,
'AppRotateBytes' => 20_000_000
}
end
2 changes: 2 additions & 0 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
depends 'golang', '~> 1.4'
depends 'runit'
depends 'nssm', '~> 1.2'
depends 'poise-service'
depends 'poise-service-runit'

issues_url 'https://github.com/adamkrone/chef-consul-template/issues' if respond_to?(:issues_url)
source_url 'https://github.com/adamkrone/chef-consul-template' if respond_to?(:source_url)
119 changes: 17 additions & 102 deletions recipes/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,123 +13,38 @@
consul_template_directories = []
consul_template_directories << node['consul_template']['config_dir']

# Select service user & group
case node['consul_template']['init_style']
when 'runit'
include_recipe 'runit::default'

consul_template_user = node['consul_template']['service_user']
consul_template_group = node['consul_template']['service_group']
consul_template_directories << '/var/log/consul-template'
when 'systemd', 'upstart'
consul_template_user = node['consul_template']['service_user']
consul_template_group = node['consul_template']['service_group']
else
consul_template_user = 'root'
consul_template_group = 'root'
end

# Create service user
user consul_template_user do
not_if { consul_template_user == 'root' }
home '/dev/null'
shell '/bin/false'
comment 'consul-template service user'
end

# Create service group
group consul_template_group do
not_if { consul_template_group == 'root' }
members consul_template_user
append true
# Defince service parameters
poise_service_user node['consul_template']['service_user'] do
group node['consul_template']['service_group']
end
cmd = "#{node['consul_template']['install_dir']}/consul-template"
opt = "-config #{node['consul_template']['config_dir']}"

# Create service directories
consul_template_directories.each do |dirname|
directory dirname do
owner consul_template_user
group consul_template_group
owner node['consul_template']['service_user']
group node['consul_template']['service_group']
mode 0o755
end
end

# Define global options here. use consul_template lwrp to register new
# templates
file File.join(node['consul_template']['config_dir'], 'default.json') do
user consul_template_user
group consul_template_group
user node['consul_template']['service_user']
group node['consul_template']['service_group']
mode node['consul_template']['template_mode']
action :create
content JSON.pretty_generate(node['consul_template']['config'], quirks_mode: true)
if node['consul_template']['init_style'] == 'runit'
notifies :restart, 'runit_service[consul-template]', :delayed
else
notifies :restart, 'service[consul-template]', :delayed
end
notifies :restart, 'poise_service[consul-template]', :delayed
end

command = "#{node['consul_template']['install_dir']}/consul-template"
options = "-config #{node['consul_template']['config_dir']}"

case node['consul_template']['init_style']
when 'init', 'upstart'
if node['consul_template']['init_style'] == 'upstart'
is_upstart = true
init_file = '/etc/init/consul-template.conf'
init_tmpl = 'consul-template-conf.erb'
else
init_file = '/etc/init.d/consul-template'
init_tmpl = 'consul-template-init.erb'
end

template init_file do
source init_tmpl
mode 0o755
variables(
command: command,
options: options,
loglevel: node['consul_template']['log_level'],
service_user: consul_template_user,
service_group: consul_template_group
)
notifies :restart, 'service[consul-template]', :immediately
end

service 'consul-template' do
provider Chef::Provider::Service::Upstart if is_upstart
supports status: true, restart: true, reload: true
action [:enable, :start]
subscribes :restart, "libarchive_file[#{ConsulTemplateHelpers.install_file(node)}]", :delayed
end

when 'runit'
runit_service 'consul-template' do
supports status: true, restart: true
action [:enable, :start]
log true
options(
command: command,
options: options
)
env 'CONSUL_TEMPLATE_LOG' => node['consul_template']['log_level']
subscribes :restart, "libarchive_file[#{ConsulTemplateHelpers.install_file(node)}]", :delayed
end

when 'systemd'
template '/etc/systemd/system/consul-template.service' do
source 'consul-template-systemd.erb'
mode 0o755
variables(
command: command,
options: options
)
notifies :restart, 'service[consul-template]', :immediately
end

service 'consul-template' do
supports status: true, restart: true, reload: true
action [:enable, :start]
subscribes :restart, "libarchive_file[#{ConsulTemplateHelpers.install_file(node)}]", :delayed
end

# Create service using poise
poise_service 'consul-template' do
command "#{cmd} #{opt}"
user node['consul_template']['service_user']
options :systemd, after_target: 'network'
options :sysvinit, user: 'root'
restart_on_update true
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
command: 'touch /tmp/consul-template-command-test',
perms: 777
}]
notifies :restart, 'service[consul-template]'
notifies :restart, 'poise_service[consul-template]'
end
68 changes: 20 additions & 48 deletions spec/recipes/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,36 @@
end

it 'should enable the consul-template service' do
expect(chef_run).to enable_service('consul-template')
end

it 'should start the consul-template service' do
expect(chef_run).to start_service('consul-template')
expect(chef_run).to enable_poise_service('consul-template')
end

context 'when using init' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'centos', version: '6.3').converge(described_recipe)
end
it 'should create the consul-template init script' do
expect(chef_run).to create_template('/etc/init.d/consul-template')
end

it 'should not create the consul-template service user (using root)' do
expect(chef_run).to_not create_user('root')
expect(chef_run).to_not create_poise_service_user('root')
.with(group: 'root')
end

it 'should not create the consul-template service group (using root)' do
expect(chef_run).to_not create_group('root')
it 'should create service with the correct command' do
expect(chef_run).to enable_poise_service('consul-template')
end
end

context 'when using upstart' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04').converge(described_recipe)
end
it 'should create the consul-template upstart script' do
expect(chef_run).to create_template('/etc/init/consul-template.conf')
end

it 'should create the consul-template service user' do
expect(chef_run).to create_user('consul-template')
expect(chef_run).to create_poise_service_user('consul-template')
.with(group: 'consul-template')
end

it 'should create the consul-template service group' do
expect(chef_run).to create_group('consul-template')
it 'should create service with the correct command' do
expect(chef_run).to enable_poise_service('consul-template')
.with(user: 'consul-template')
end
end

Expand All @@ -61,23 +53,13 @@
end

it 'should create the consul-template service user' do
expect(chef_run).to create_user('consul-template')
end

it 'should create the consul-template service group' do
expect(chef_run).to create_group('consul-template')
end

it 'should create the consul-template log directory' do
expect(chef_run).to create_directory('/var/log/consul-template')
expect(chef_run).to create_poise_service_user('consul-template')
.with(group: 'consul-template')
end

it 'should enable the consul-template service' do
expect(chef_run).to enable_runit_service('consul-template')
end

it 'should start the consul-template service' do
expect(chef_run).to start_runit_service('consul-template')
it 'should create service with the correct command' do
expect(chef_run).to enable_poise_service('consul-template')
.with(user: 'consul-template')
end
end

Expand All @@ -89,23 +71,13 @@
end

it 'should create the consul-template service user' do
expect(chef_run).to create_user('consul-template')
end

it 'should create the consul-template service group' do
expect(chef_run).to create_group('consul-template')
end

it 'should create the consul-template service config' do
expect(chef_run).to create_template('/etc/systemd/system/consul-template.service')
end

it 'should enable the consul-template service' do
expect(chef_run).to enable_service('consul-template')
expect(chef_run).to create_poise_service_user('consul-template')
.with(group: 'consul-template')
end

it 'should start the consul-template service' do
expect(chef_run).to start_service('consul-template')
it 'should create service with the correct command' do
expect(chef_run).to enable_poise_service('consul-template')
.with(user: 'consul-template')
end
end
end
2 changes: 1 addition & 1 deletion spec/resources/consul_template_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

it 'should notify consul-template to restart' do
consul_template = chef_run.consul_template_config('test')
expect(consul_template).to notify('service[consul-template]').to(:restart).delayed
expect(consul_template).to notify('poise_service[consul-template]').to(:restart).delayed
end
end
end