]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/service.rb
Use multipackage installs throughout
[chef.git] / cookbooks / systemd / resources / service.rb
1 #
2 # Cookbook Name:: 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 # http://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 :description, String, :required => true
24 property :after, [String, Array]
25 property :wants, [String, Array]
26 property :type, String,
27          :default => "simple",
28          :is => %w[simple forking oneshot dbus notify idle]
29 property :limit_nofile, Integer
30 property :environment, Hash, :default => {}
31 property :environment_file, [String, Hash]
32 property :user, String
33 property :group, String
34 property :working_directory, String
35 property :exec_start_pre, String
36 property :exec_start, String, :required => true
37 property :exec_start_post, String
38 property :exec_stop, String
39 property :exec_reload, String
40 property :runtime_directory, String
41 property :runtime_directory_mode, Integer
42 property :standard_input, String,
43          :is => %w[null tty tty-force tty-fail socket]
44 property :standard_output, String,
45          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
46 property :standard_error, String,
47          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
48 property :success_exit_status, [Integer, String, Array]
49 property :restart, String,
50          :is => %w[on-success on-failure on-abnormal on-watchdog on-abort always]
51 property :private_tmp, [TrueClass, FalseClass]
52 property :private_devices, [TrueClass, FalseClass]
53 property :private_network, [TrueClass, FalseClass]
54 property :protect_system, [TrueClass, FalseClass, String]
55 property :protect_home, [TrueClass, FalseClass, String]
56 property :no_new_privileges, [TrueClass, FalseClass]
57 property :timeout_sec, Integer
58 property :pid_file, String
59
60 action :create do
61   service_variables = new_resource.to_hash
62
63   if environment_file.is_a?(Hash)
64     template "/etc/default/#{service}" do
65       cookbook "systemd"
66       source "environment.erb"
67       owner "root"
68       group "root"
69       mode 0o640
70       variables :environment => environment_file
71     end
72
73     service_variables[:environment_file] = "/etc/default/#{service}"
74   end
75
76   template "/etc/systemd/system/#{service}.service" do
77     cookbook "systemd"
78     source "service.erb"
79     owner "root"
80     group "root"
81     mode 0o644
82     variables service_variables
83   end
84
85   execute "systemctl-reload-#{service}.service" do
86     action :nothing
87     command "systemctl daemon-reload"
88     user "root"
89     group "root"
90     subscribes :run, "template[/etc/systemd/system/#{service}.service]"
91   end
92 end
93
94 action :delete do
95   file "/etc/default/#{service}" do
96     action :delete
97     only_if { environment_file.is_a?(Hash) }
98   end
99
100   file "/etc/systemd/system/#{service}.service" do
101     action :delete
102   end
103
104   execute "systemctl-reload-#{service}.service" do
105     action :nothing
106     command "systemctl daemon-reload"
107     user "root"
108     group "root"
109     subscribes :run, "file[/etc/systemd/system/#{service}.service]"
110   end
111 end