]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/service.rb
Merge remote-tracking branch 'github/pull/740'
[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 unified_mode true
21
22 default_action :create
23
24 property :service, String, :name_property => true
25 property :dropin, String
26 property :description, String
27 property :condition_path_exists, [String, Array]
28 property :condition_path_exists_glob, [String, Array]
29 property :after, [String, Array]
30 property :conflicts, [String, Array]
31 property :wants, [String, Array]
32 property :requires, [String, Array]
33 property :joins_namespace_of, [String, Array]
34 property :type, String, :is => %w[simple forking oneshot dbus notify idle]
35 property :notify_access, String, :is => %w[none main exec all]
36 property :limit_nofile, Integer
37 property :limit_as, [Integer, String]
38 property :limit_cpu, [Integer, String]
39 property :limit_core, [Integer, String]
40 property :memory_low, [Integer, String]
41 property :memory_high, [Integer, String]
42 property :memory_max, [Integer, String]
43 property :environment, Hash, :default => {}
44 property :environment_file, [String, Hash]
45 property :user, String
46 property :group, String
47 property :dynamic_user, [true, false]
48 property :working_directory, String
49 property :umask, String
50 property :exec_start_pre, [String, Array]
51 property :exec_start, [String, Array]
52 property :exec_start_post, [String, Array]
53 property :exec_stop, [String, Array]
54 property :exec_stop_post, [String, Array]
55 property :exec_reload, String
56 property :runtime_max_sec, Integer
57 property :runtime_directory, String
58 property :runtime_directory_mode, Integer
59 property :runtime_directory_preserve, [true, false, String]
60 property :state_directory, String
61 property :state_directory_mode, Integer
62 property :cache_directory, String
63 property :cache_directory_mode, Integer
64 property :logs_directory, String
65 property :logs_directory_mode, Integer
66 property :configuration_directory, String
67 property :configuration_directory_mode, Integer
68 property :standard_input, String,
69          :is => %w[null tty tty-force tty-fail socket]
70 property :standard_output, String,
71          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
72 property :standard_error, String,
73          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
74 property :success_exit_status, [Integer, String, Array]
75 property :restart, String,
76          :is => %w[on-success on-failure on-abnormal on-watchdog on-abort always]
77 property :restart_sec, [Integer, String]
78 property :protect_proc, String,
79          :is => %w[noaccess invisible ptraceable default]
80 property :proc_subset, String,
81          :is => %w[all pid]
82 property :bind_paths, [String, Array]
83 property :bind_read_only_paths, [String, Array]
84 property :capability_bounding_set, [String, Array]
85 property :ambient_capabilities, [String, Array]
86 property :no_new_privileges, [true, false]
87 property :protect_system, [true, false, String]
88 property :protect_home, [true, false, String]
89 property :read_write_paths, [String, Array]
90 property :read_only_paths, [String, Array]
91 property :inaccessible_paths, [String, Array]
92 property :private_tmp, [true, false]
93 property :private_devices, [true, false]
94 property :private_network, [true, false]
95 property :private_ipc, [true, false]
96 property :private_users, [true, false]
97 property :protect_hostname, [true, false]
98 property :protect_clock, [true, false]
99 property :protect_kernel_tunables, [true, false]
100 property :protect_kernel_modules, [true, false]
101 property :protect_kernel_logs, [true, false]
102 property :protect_control_groups, [true, false]
103 property :restrict_address_families, [String, Array]
104 property :restrict_namespaces, [true, false, String, Array]
105 property :lock_personality, [true, false]
106 property :memory_deny_write_execute, [true, false]
107 property :restrict_realtime, [true, false]
108 property :restrict_suid_sgid, [true, false]
109 property :remove_ipc, [true, false]
110 property :stop_when_unneeded, [true, false]
111 property :system_call_filter, [String, Array]
112 property :system_call_architectures, [String, Array]
113 property :tasks_max, Integer
114 property :timeout_start_sec, Integer
115 property :timeout_stop_sec, Integer
116 property :timeout_abort_sec, Integer
117 property :timeout_sec, Integer
118 property :pid_file, String
119 property :nice, Integer
120 property :io_scheduling_class, [Integer, String]
121 property :io_scheduling_priority, Integer
122 property :kill_mode, String,
123          :is => %w[control-group process mixed none]
124 property :sandbox, [true, false, Hash]
125
126 action :create do
127   service_variables = new_resource.to_hash
128
129   unless new_resource.dropin
130     service_variables[:type] ||= "simple"
131   end
132
133   if new_resource.sandbox
134     service_variables[:protect_proc] = "invisible" unless property_is_set?(:protect_proc)
135     service_variables[:proc_subset] = "pid" unless property_is_set?(:proc_subset)
136     service_variables[:capability_bounding_set] = [] unless property_is_set?(:capability_bounding_set)
137     service_variables[:ambient_capabilities] = [] unless property_is_set?(:ambient_capabilities)
138     service_variables[:no_new_privileges] = true unless property_is_set?(:no_new_privileges)
139     service_variables[:protect_system] = "strict" unless property_is_set?(:protect_system)
140     service_variables[:protect_home] = true unless property_is_set?(:protect_home)
141     service_variables[:private_tmp] = true unless property_is_set?(:private_tmp)
142     service_variables[:private_devices] = true unless property_is_set?(:private_devices)
143     service_variables[:private_network] = true unless property_is_set?(:private_network)
144     service_variables[:private_ipc] = true unless property_is_set?(:private_ipc)
145     service_variables[:private_users] = true unless property_is_set?(:private_users)
146     service_variables[:protect_hostname] = true unless property_is_set?(:protect_hostname)
147     service_variables[:protect_clock] = true unless property_is_set?(:protect_clock)
148     service_variables[:protect_kernel_tunables] = true unless property_is_set?(:protect_kernel_tunables)
149     service_variables[:protect_kernel_modules] = true unless property_is_set?(:protect_kernel_modules)
150     service_variables[:protect_kernel_logs] = true unless property_is_set?(:protect_kernel_logs)
151     service_variables[:protect_control_groups] = true unless property_is_set?(:protect_control_groups)
152     service_variables[:restrict_address_families] = [] unless property_is_set?(:restrict_address_families)
153     service_variables[:restrict_namespaces] = true unless property_is_set?(:restrict_namespaces)
154     service_variables[:lock_personality] = true unless property_is_set?(:lock_personality)
155     service_variables[:memory_deny_write_execute] = true unless property_is_set?(:memory_deny_write_execute)
156     service_variables[:restrict_realtime] = true unless property_is_set?(:restrict_realtime)
157     service_variables[:restrict_suid_sgid] = true unless property_is_set?(:restrict_suid_sgid)
158     service_variables[:remove_ipc] = true unless property_is_set?(:remove_ipc)
159     service_variables[:system_call_filter] = "@system-service" unless property_is_set?(:system_call_filter)
160     service_variables[:system_call_architectures] = "native" unless property_is_set?(:system_call_architectures)
161
162     if sandbox_option(:enable_network)
163       service_variables[:private_network] = false
164       service_variables[:restrict_address_families] = Array(service_variables[:restrict_address_families]).append("AF_INET", "AF_INET6").reject { |f| f == "none" }
165     end
166   end
167
168   if new_resource.environment_file.is_a?(Hash)
169     template "/etc/default/#{new_resource.service}" do
170       cookbook "systemd"
171       source "environment.erb"
172       owner "root"
173       group "root"
174       mode "640"
175       variables :environment => new_resource.environment_file
176     end
177
178     service_variables[:environment_file] = "/etc/default/#{new_resource.service}"
179   end
180
181   if new_resource.dropin
182     directory dropin_directory do
183       owner "root"
184       group "root"
185       mode "755"
186     end
187   end
188
189   template config_name do
190     cookbook "systemd"
191     source "service.erb"
192     owner "root"
193     group "root"
194     mode "644"
195     variables service_variables
196     notifies :run, "execute[systemctl-reload]"
197   end
198
199   execute "systemctl-reload" do
200     action :nothing
201     command "systemctl daemon-reload"
202     user "root"
203     group "root"
204   end
205 end
206
207 action :delete do
208   file "/etc/default/#{new_resource.service}" do
209     action :delete
210     only_if { new_resource.environment_file.is_a?(Hash) }
211   end
212
213   file config_name do
214     action :delete
215     notifies :run, "execute[systemctl-reload]"
216   end
217
218   execute "systemctl-reload" do
219     action :nothing
220     command "systemctl daemon-reload"
221     user "root"
222     group "root"
223   end
224 end
225
226 action_class do
227   def sandbox_option(option)
228     new_resource.sandbox[option] if new_resource.sandbox.is_a?(Hash)
229   end
230
231   def dropin_directory
232     "/etc/systemd/system/#{new_resource.service}.service.d"
233   end
234
235   def config_name
236     if new_resource.dropin
237       "#{dropin_directory}/#{new_resource.dropin}.conf"
238     else
239       "/etc/systemd/system/#{new_resource.service}.service"
240     end
241   end
242 end