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