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