]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/service.rb
Enable unified mode for custom resources
[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 :type, String, :is => %w[simple forking oneshot dbus notify idle]
33 property :limit_nofile, Integer
34 property :limit_as, [Integer, String]
35 property :limit_cpu, [Integer, String]
36 property :memory_low, [Integer, String]
37 property :memory_high, [Integer, String]
38 property :memory_max, [Integer, String]
39 property :environment, Hash, :default => {}
40 property :environment_file, [String, Hash]
41 property :user, String
42 property :group, String
43 property :working_directory, String
44 property :exec_start_pre, String
45 property :exec_start, String
46 property :exec_start_post, String
47 property :exec_stop, String
48 property :exec_reload, String
49 property :runtime_directory, String
50 property :runtime_directory_mode, Integer
51 property :standard_input, String,
52          :is => %w[null tty tty-force tty-fail socket]
53 property :standard_output, String,
54          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
55 property :standard_error, String,
56          :is => %w[inherit null tty journal syslog kmsg journal+console syslog+console kmsg+console socket]
57 property :success_exit_status, [Integer, String, Array]
58 property :restart, String,
59          :is => %w[on-success on-failure on-abnormal on-watchdog on-abort always]
60 property :private_tmp, [true, false]
61 property :private_devices, [true, false]
62 property :private_network, [true, false]
63 property :protect_system, [TrueClass, FalseClass, String]
64 property :protect_home, [TrueClass, FalseClass, String]
65 property :read_write_paths, [String, Array]
66 property :read_only_paths, [String, Array]
67 property :inaccessible_paths, [String, Array]
68 property :restrict_address_families, [String, Array]
69 property :no_new_privileges, [true, false]
70 property :tasks_max, Integer
71 property :timeout_sec, Integer
72 property :pid_file, String
73 property :nice, Integer
74 property :io_scheduling_class, [Integer, String]
75 property :io_scheduling_priority, Integer
76 property :kill_mode, String,
77          :is => %w[control-group process mixed none]
78
79 action :create do
80   service_variables = new_resource.to_hash
81
82   unless new_resource.dropin
83     service_variables[:type] ||= "simple"
84   end
85
86   if new_resource.environment_file.is_a?(Hash)
87     template "/etc/default/#{new_resource.service}" do
88       cookbook "systemd"
89       source "environment.erb"
90       owner "root"
91       group "root"
92       mode "640"
93       variables :environment => new_resource.environment_file
94     end
95
96     service_variables[:environment_file] = "/etc/default/#{new_resource.service}"
97   end
98
99   if new_resource.dropin
100     directory dropin_directory do
101       owner "root"
102       group "root"
103       mode "755"
104     end
105   end
106
107   template config_name do
108     cookbook "systemd"
109     source "service.erb"
110     owner "root"
111     group "root"
112     mode "644"
113     variables service_variables
114     notifies :run, "execute[systemctl-reload]"
115   end
116
117   execute "systemctl-reload" do
118     action :nothing
119     command "systemctl daemon-reload"
120     user "root"
121     group "root"
122   end
123 end
124
125 action :delete do
126   file "/etc/default/#{new_resource.service}" do
127     action :delete
128     only_if { new_resource.environment_file.is_a?(Hash) }
129   end
130
131   file config_name do
132     action :delete
133     notifies :run, "execute[systemctl-reload]"
134   end
135
136   execute "systemctl-reload" do
137     action :nothing
138     command "systemctl daemon-reload"
139     user "root"
140     group "root"
141   end
142 end
143
144 action_class do
145   def dropin_directory
146     "/etc/systemd/system/#{new_resource.service}.service.d"
147   end
148
149   def config_name
150     if new_resource.dropin
151       "#{dropin_directory}/#{new_resource.dropin}.conf"
152     else
153       "/etc/systemd/system/#{new_resource.service}.service"
154     end
155   end
156 end