]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/service.rb
Allow path accessibility to be configured for systemd services
[chef.git] / cookbooks / systemd / resources / service.rb
1 #
2 # Cookbook:: systemd
3 # Resource:: systemd_service
4 #
5 # Copyright:: 2016, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 default_action :create
21
22 property :service, String, :name_property => true
23 property :dropin, String
24 property :description, String
25 property :after, [String, Array]
26 property :wants, [String, Array]
27 property :type, String, :is => %w[simple forking oneshot dbus notify idle]
28 property :limit_nofile, Integer
29 property :limit_as, [Integer, String]
30 property :limit_cpu, [Integer, String]
31 property :memory_low, [Integer, String]
32 property :memory_high, [Integer, String]
33 property :memory_max, [Integer, String]
34 property :environment, Hash, :default => {}
35 property :environment_file, [String, Hash]
36 property :user, String
37 property :group, String
38 property :working_directory, String
39 property :exec_start_pre, String
40 property :exec_start, String
41 property :exec_start_post, String
42 property :exec_stop, String
43 property :exec_reload, String
44 property :runtime_directory, String
45 property :runtime_directory_mode, Integer
46 property :standard_input, String,
47          :is => %w[null tty tty-force tty-fail socket]
48 property :standard_output, String,
49          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
50 property :standard_error, String,
51          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
52 property :success_exit_status, [Integer, String, Array]
53 property :restart, String,
54          :is => %w[on-success on-failure on-abnormal on-watchdog on-abort always]
55 property :private_tmp, [true, false]
56 property :private_devices, [true, false]
57 property :private_network, [true, false]
58 property :protect_system, [TrueClass, FalseClass, String]
59 property :protect_home, [TrueClass, FalseClass, String]
60 property :read_write_paths, [String, Array]
61 property :read_only_paths, [String, Array]
62 property :inaccessible_paths, [String, Array]
63 property :restrict_address_families, [String, Array]
64 property :no_new_privileges, [true, false]
65 property :tasks_max, Integer
66 property :timeout_sec, Integer
67 property :pid_file, String
68 property :nice, Integer
69 property :io_scheduling_class, [Integer, String]
70 property :io_scheduling_priority, Integer
71 property :kill_mode, String,
72          :is => %w[control-group process mixed none]
73
74 action :create do
75   service_variables = new_resource.to_hash
76
77   unless new_resource.dropin
78     service_variables[:type] ||= "simple"
79   end
80
81   if new_resource.environment_file.is_a?(Hash)
82     template "/etc/default/#{new_resource.service}" do
83       cookbook "systemd"
84       source "environment.erb"
85       owner "root"
86       group "root"
87       mode "640"
88       variables :environment => new_resource.environment_file
89     end
90
91     service_variables[:environment_file] = "/etc/default/#{new_resource.service}"
92   end
93
94   if new_resource.dropin
95     directory dropin_directory do
96       owner "root"
97       group "root"
98       mode "755"
99     end
100   end
101
102   template config_name do
103     cookbook "systemd"
104     source "service.erb"
105     owner "root"
106     group "root"
107     mode "644"
108     variables service_variables
109     notifies :run, "execute[systemctl-reload]"
110   end
111
112   execute "systemctl-reload" do
113     action :nothing
114     command "systemctl daemon-reload"
115     user "root"
116     group "root"
117   end
118 end
119
120 action :delete do
121   file "/etc/default/#{new_resource.service}" do
122     action :delete
123     only_if { new_resource.environment_file.is_a?(Hash) }
124   end
125
126   file config_name do
127     action :delete
128     notifies :run, "execute[systemctl-reload]"
129   end
130
131   execute "systemctl-reload" do
132     action :nothing
133     command "systemctl daemon-reload"
134     user "root"
135     group "root"
136   end
137 end
138
139 action_class do
140   def dropin_directory
141     "/etc/systemd/system/#{new_resource.service}.service.d"
142   end
143
144   def config_name
145     if new_resource.dropin
146       "#{dropin_directory}/#{new_resource.dropin}.conf"
147     else
148       "/etc/systemd/system/#{new_resource.service}.service"
149     end
150   end
151 end