summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavide Cavalca <dcavalca@fb.com>2015-12-14 12:37:04 -0800
committerDavide Cavalca <dcavalca@fb.com>2016-03-03 09:39:01 -0800
commit0475cf75df5a186e3ef8fa62a9212f775aae30b3 (patch)
tree6d2926eff802711cb17166757572b67ac5573016 /lib
parent383e681013bdd188f208753f9be60b4b9580aa52 (diff)
downloadchef-0475cf75df5a186e3ef8fa62a9212f775aae30b3.tar.gz
Extend service resource to support systemd user services
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/provider/service/systemd.rb51
-rw-r--r--lib/chef/resource/service.rb9
2 files changed, 49 insertions, 11 deletions
diff --git a/lib/chef/provider/service/systemd.rb b/lib/chef/provider/service/systemd.rb
index 7f90172edb..a6189e4332 100644
--- a/lib/chef/provider/service/systemd.rb
+++ b/lib/chef/provider/service/systemd.rb
@@ -70,6 +70,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 +95,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 +108,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 +118,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 +128,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 +137,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 }