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