]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/service.rb
Add support for systemd path units
[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 :name, String
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 :restart, String,
49          :is => %w(on-success on-failure on-abnormal on-watchdog on-abort always)
50 property :private_tmp, [TrueClass, FalseClass]
51 property :private_devices, [TrueClass, FalseClass]
52 property :private_network, [TrueClass, FalseClass]
53 property :protect_system, [TrueClass, FalseClass, String]
54 property :protect_home, [TrueClass, FalseClass, String]
55 property :no_new_privileges, [TrueClass, FalseClass]
56 property :timeout_sec, Integer
57 property :pid_file, String
58
59 action :create do
60   service_variables = new_resource.to_hash
61
62   if environment_file.is_a?(Hash)
63     template "/etc/default/#{name}" do
64       cookbook "systemd"
65       source "environment.erb"
66       owner "root"
67       group "root"
68       mode 0o640
69       variables :environment => environment_file
70     end
71
72     service_variables[:environment_file] = "/etc/default/#{name}"
73   end
74
75   template "/etc/systemd/system/#{name}.service" do
76     cookbook "systemd"
77     source "service.erb"
78     owner "root"
79     group "root"
80     mode 0o644
81     variables service_variables
82   end
83
84   execute "systemctl-reload-#{name}.service" do
85     action :nothing
86     command "systemctl daemon-reload"
87     user "root"
88     group "root"
89     subscribes :run, "template[/etc/systemd/system/#{name}.service]"
90   end
91 end
92
93 action :delete do
94   file "/etc/default/#{name}" do
95     action :delete
96     only_if { environment_file.is_a?(Hash) }
97   end
98
99   file "/etc/systemd/system/#{name}.service" do
100     action :delete
101   end
102
103   execute "systemctl-reload-#{name}.service" do
104     action :nothing
105     command "systemctl daemon-reload"
106     user "root"
107     group "root"
108     subscribes :run, "file[/etc/systemd/system/#{name}.service]"
109   end
110 end