Update poise-archive and poise-service cookbooks

This commit is contained in:
Greg Karékinian
2018-04-17 12:24:17 +02:00
parent 90bb872305
commit ff2f424ddb
16 changed files with 150 additions and 47 deletions

View File

@@ -167,6 +167,7 @@ module PoiseService
def service_resource
@service_resource ||= PoiseService::Resources::PoiseService::Resource.new(new_resource.name, run_context).tap do |r|
# Set some defaults.
r.declared_type = :poise_service
r.enclosing_provider = self
r.source_line = new_resource.source_line
r.service_name(new_resource.service_name)

View File

@@ -70,12 +70,14 @@ module PoiseService
end
def action_start
return if options['never_start']
notify_if_service do
service_resource.run_action(:start)
end
end
def action_stop
return if options['never_stop']
notify_if_service do
service_resource.run_action(:stop)
end
@@ -139,6 +141,7 @@ module PoiseService
# restart actions.
def service_resource
@service_resource ||= Chef::Resource::Service.new(new_resource.service_name, run_context).tap do |r|
r.declared_type = :service
r.enclosing_provider = self
r.source_line = new_resource.source_line
r.supports(status: true, restart: true, reload: true)

View File

@@ -25,7 +25,16 @@ module PoiseService
class Dummy < Base
provides(:dummy)
# @api private
def self.default_inversion_options(node, resource)
super.merge({
# Time to wait between stop and start.
restart_delay: 1,
})
end
def action_start
return if options['never_start']
return if pid
Chef::Log.debug("[#{new_resource}] Starting #{new_resource.command}")
# Clear the pid file if it exists.
@@ -45,39 +54,62 @@ module PoiseService
end
else
# :nocov:
Chef::Log.debug("[#{new_resource}] Forked")
# First child, daemonize and go to town. This handles multi-fork,
# setsid, and shutting down stdin/out/err.
Process.daemon(true)
Chef::Log.debug("[#{new_resource}] Daemonized")
# Daemonized, set up process environment.
Dir.chdir(new_resource.directory)
Chef::Log.debug("[#{new_resource}] Directory changed to #{new_resource.directory}")
ENV['HOME'] = Dir.home(new_resource.user)
new_resource.environment.each do |key, val|
ENV[key.to_s] = val.to_s
begin
Chef::Log.debug("[#{new_resource}] Forked")
# First child, daemonize and go to town. This handles multi-fork,
# setsid, and shutting down stdin/out/err.
Process.daemon(true)
Chef::Log.debug("[#{new_resource}] Daemonized")
# Daemonized, set up process environment.
Dir.chdir(new_resource.directory)
Chef::Log.debug("[#{new_resource}] Directory changed to #{new_resource.directory}")
ENV['HOME'] = Dir.home(new_resource.user)
new_resource.environment.each do |key, val|
ENV[key.to_s] = val.to_s
end
Chef::Log.debug("[#{new_resource}] Process environment configured")
# Make sure to open the output file and write the pid file before we
# drop privs.
output = ::File.open(output_file, 'ab')
IO.write(pid_file, Process.pid)
Chef::Log.debug("[#{new_resource}] PID #{Process.pid} written to #{pid_file}")
ent = Etc.getpwnam(new_resource.user)
if Process.euid != ent.uid || Process.egid != ent.gid
Process.initgroups(ent.name, ent.gid)
Process::GID.change_privilege(ent.gid) if Process.egid != ent.gid
Process::UID.change_privilege(ent.uid) if Process.euid != ent.uid
Chef::Log.debug("[#{new_resource}] Changed privs to #{new_resource.user} (#{ent.uid}:#{ent.gid})")
end
# Log the command. Happens before ouput redirect or this ends up in the file.
Chef::Log.debug("[#{new_resource}] Execing #{new_resource.command}")
# Set up output logging.
Chef::Log.debug("[#{new_resource}] Logging output to #{output_file}")
$stdout.reopen(output)
$stdout.sync = true
$stderr.reopen(output)
$stderr.sync = true
$stdout.write("#{Time.now} Starting #{new_resource.command}")
# Split the command so we don't get an extra sh -c.
Kernel.exec(*Shellwords.split(new_resource.command))
# Just in case, bail out.
$stdout.reopen(STDOUT)
$stderr.reopen(STDERR)
Chef::Log.debug("[#{new_resource}] Exec failed, bailing out.")
exit!
rescue Exception => e
# Welp, we tried.
$stdout.reopen(STDOUT)
$stderr.reopen(STDERR)
Chef::Log.error("[#{new_resource}] Error during process spawn: #{e}")
exit!
end
Chef::Log.debug("[#{new_resource}] Process environment configured")
IO.write(pid_file, Process.pid)
Chef::Log.debug("[#{new_resource}] PID written to #{pid_file}")
ent = Etc.getpwnam(new_resource.user)
if Process.euid != ent.uid || Process.egid != ent.gid
Process.initgroups(ent.name, ent.gid)
Process::GID.change_privilege(ent.gid) if Process.egid != ent.gid
Process::UID.change_privilege(ent.uid) if Process.euid != ent.uid
end
Chef::Log.debug("[#{new_resource}] Changed privs to #{new_resource.user} (#{ent.uid}:#{ent.gid})")
# Split the command so we don't get an extra sh -c.
Chef::Log.debug("[#{new_resource}] Execing #{new_resource.command}")
Kernel.exec(*Shellwords.split(new_resource.command))
# Just in case, bail out.
exit!
# :nocov:
end
Chef::Log.debug("[#{new_resource}] Started.")
end
def action_stop
return if options['never_stop']
return unless pid
Chef::Log.debug("[#{new_resource}] Stopping with #{new_resource.stop_signal}. Current PID is #{pid.inspect}.")
Process.kill(new_resource.stop_signal, pid)
@@ -87,6 +119,8 @@ module PoiseService
def action_restart
return if options['never_restart']
action_stop
# Give things a moment to stop before we try starting again.
sleep(options['restart_delay'])
action_start
end
@@ -151,6 +185,11 @@ module PoiseService
"/var/run/#{new_resource.service_name}.pid"
end
# Path to the output file.
def output_file
"/var/run/#{new_resource.service_name}.out"
end
end
end
end

View File

@@ -27,8 +27,6 @@ module PoiseService
# @api private
def self.provides_auto?(node, resource)
# Don't allow systemd under docker, it won't work in most cases.
return false if node['virtualization'] && %w{docker lxc}.include?(node['virtualization']['system'])
service_resource_hints.include?(:systemd)
end
@@ -75,8 +73,10 @@ module PoiseService
end
def destroy_service
reloader = systemctl_daemon_reload
file "/etc/systemd/system/#{new_resource.service_name}.service" do
action :delete
notifies :run, reloader, :immediately if options['auto_reload']
end
end

View File

@@ -37,7 +37,7 @@ module PoiseService
r.provider(case node['platform_family']
when 'debian'
Chef::Provider::Service::Debian
when 'rhel'
when 'rhel', 'amazon'
Chef::Provider::Service::Redhat
else
# Better than nothing I guess? Will fail on enable I think.

View File

@@ -30,11 +30,17 @@ module PoiseService
provides(:upstart)
def self.provides_auto?(node, resource)
# Don't allow upstart under docker, it won't work.
return false if node['virtualization'] && %w{docker lxc}.include?(node['virtualization']['system'])
service_resource_hints.include?(:upstart)
end
# @api private
def self.default_inversion_options(node, resource)
super.merge({
# Time to wait between stop and start.
restart_delay: 1,
})
end
# True restart in Upstart preserves the original config data, we want the
# more obvious behavior like everything else in the world that restart
# would re-read the updated config file. Use stop+start to get this
@@ -42,6 +48,8 @@ module PoiseService
def action_restart
return if options['never_restart']
action_stop
# Give things a moment to stop before we try starting again.
sleep(options['restart_delay'])
action_start
end

View File

@@ -16,5 +16,5 @@
module PoiseService
VERSION = '1.4.2'
VERSION = '1.5.2'
end