summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPhil Dibowitz <phil@ipom.com>2016-03-08 09:25:34 -0800
committerPhil Dibowitz <phil@ipom.com>2016-03-08 09:25:34 -0800
commitc54f2fe21ea9b0119b82bbcf7f313ee8f68f7b38 (patch)
treeef4e99a8a8195427a2a6d523c017841f348d75c3 /lib
parent211ae6a47aedbd7a81042bdd42f577f0d11f8b85 (diff)
parent7f8af07e287282aefdb55bea57f50c05318787f2 (diff)
downloadchef-c54f2fe21ea9b0119b82bbcf7f313ee8f68f7b38.tar.gz
Merge pull request #4661 from davide125/systemd-user
Extend service resource to support systemd user services
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/provider/service.rb9
-rw-r--r--lib/chef/provider/service/solaris.rb5
-rw-r--r--lib/chef/provider/service/systemd.rb55
-rw-r--r--lib/chef/resource/service.rb9
4 files changed, 67 insertions, 11 deletions
diff --git a/lib/chef/provider/service.rb b/lib/chef/provider/service.rb
index b848d33083..e693bd2eed 100644
--- a/lib/chef/provider/service.rb
+++ b/lib/chef/provider/service.rb
@@ -62,7 +62,16 @@ class Chef
end
end
+ # subclasses should override this if they do implement user services
+ def user_services_requirements
+ requirements.assert(:all_actions) do |a|
+ a.assertion { @new_resource.user.nil? }
+ a.failure_message Chef::Exceptions::UnsupportedAction, "#{self} does not support user services"
+ end
+ end
+
def shared_resource_requirements
+ user_services_requirements
end
def define_resource_requirements
diff --git a/lib/chef/provider/service/solaris.rb b/lib/chef/provider/service/solaris.rb
index 0787392094..1e5398eba8 100644
--- a/lib/chef/provider/service/solaris.rb
+++ b/lib/chef/provider/service/solaris.rb
@@ -49,6 +49,11 @@ class Chef
@current_resource
end
+ def define_resource_requirements
+ # FIXME? need reload from service.rb
+ shared_resource_requirements
+ end
+
def enable_service
shell_out!(default_init_command, "clear", @new_resource.service_name) if @maintenance
shell_out!(default_init_command, "enable", "-s", @new_resource.service_name)
diff --git a/lib/chef/provider/service/systemd.rb b/lib/chef/provider/service/systemd.rb
index 7f90172edb..1597d46a3d 100644
--- a/lib/chef/provider/service/systemd.rb
+++ b/lib/chef/provider/service/systemd.rb
@@ -60,6 +60,10 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
current_resource
end
+ # systemd supports user services just fine
+ def user_services_requirements
+ end
+
def define_resource_requirements
shared_resource_requirements
requirements.assert(:all_actions) do |a|
@@ -70,6 +74,24 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
end
end
+ def get_systemctl_options_args
+ if new_resource.user
+ uid = node["etc"]["passwd"][new_resource.user]["uid"]
+ options = {
+ "environment" => {
+ "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/#{uid}/bus",
+ },
+ "user" => new_resource.user,
+ }
+ args = "--user"
+ else
+ options = {}
+ args = "--system"
+ end
+
+ return options, args
+ end
+
def start_service
if current_resource.running
Chef::Log.debug("#{new_resource} already running, not starting")
@@ -77,7 +99,8 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
if new_resource.start_command
super
else
- shell_out_with_systems_locale!("#{systemctl_path} start #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out_with_systems_locale!("#{systemctl_path} #{args} start #{new_resource.service_name}", options)
end
end
end
@@ -89,7 +112,8 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
if new_resource.stop_command
super
else
- shell_out_with_systems_locale!("#{systemctl_path} stop #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out_with_systems_locale!("#{systemctl_path} #{args} stop #{new_resource.service_name}", options)
end
end
end
@@ -98,7 +122,8 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
if new_resource.restart_command
super
else
- shell_out_with_systems_locale!("#{systemctl_path} restart #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out_with_systems_locale!("#{systemctl_path} #{args} restart #{new_resource.service_name}", options)
end
end
@@ -107,7 +132,8 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
super
else
if current_resource.running
- shell_out_with_systems_locale!("#{systemctl_path} reload #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out_with_systems_locale!("#{systemctl_path} #{args} reload #{new_resource.service_name}", options)
else
start_service
end
@@ -115,31 +141,38 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple
end
def enable_service
- shell_out!("#{systemctl_path} enable #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out!("#{systemctl_path} #{args} enable #{new_resource.service_name}", options)
end
def disable_service
- shell_out!("#{systemctl_path} disable #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out!("#{systemctl_path} #{args} disable #{new_resource.service_name}", options)
end
def mask_service
- shell_out!("#{systemctl_path} mask #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out!("#{systemctl_path} #{args} mask #{new_resource.service_name}", options)
end
def unmask_service
- shell_out!("#{systemctl_path} unmask #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ shell_out!("#{systemctl_path} #{args} unmask #{new_resource.service_name}", options)
end
def is_active?
- shell_out("#{systemctl_path} is-active #{new_resource.service_name} --quiet").exitstatus == 0
+ options, args = get_systemctl_options_args
+ shell_out("#{systemctl_path} #{args} is-active #{new_resource.service_name} --quiet", options).exitstatus == 0
end
def is_enabled?
- shell_out("#{systemctl_path} is-enabled #{new_resource.service_name} --quiet").exitstatus == 0
+ options, args = get_systemctl_options_args
+ shell_out("#{systemctl_path} #{args} is-enabled #{new_resource.service_name} --quiet", options).exitstatus == 0
end
def is_masked?
- s = shell_out("#{systemctl_path} is-enabled #{new_resource.service_name}")
+ options, args = get_systemctl_options_args
+ s = shell_out("#{systemctl_path} #{args} is-enabled #{new_resource.service_name}", options)
s.exitstatus != 0 && s.stdout.include?("masked")
end
diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb
index 860d5b05ff..1ca4b84af0 100644
--- a/lib/chef/resource/service.rb
+++ b/lib/chef/resource/service.rb
@@ -47,6 +47,7 @@ class Chef
@priority = nil
@timeout = nil
@run_levels = nil
+ @user = nil
@supports = { :restart => nil, :reload => nil, :status => nil }
end
@@ -193,6 +194,14 @@ class Chef
:kind_of => [ Array ] )
end
+ def user(arg = nil)
+ set_or_return(
+ :user,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
def supports(args = {})
if args.is_a? Array
args.each { |arg| @supports[arg] = true }