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