]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/timer.rb
systemd: add Fixed Random Delay support to timer
[chef.git] / cookbooks / systemd / resources / timer.rb
1 #
2 # Cookbook:: systemd
3 # Resource:: systemd_timer
4 #
5 # Copyright:: 2019, 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 :timer, String, :name_property => true
25 property :dropin, String
26 property :description, String
27 property :after, [String, Array]
28 property :wants, [String, Array]
29 property :on_active_sec, [Integer, String]
30 property :on_boot_sec, [Integer, String]
31 property :on_startup_sec, [Integer, String]
32 property :on_unit_active_sec, [Integer, String]
33 property :on_unit_inactive_sec, [Integer, String]
34 property :on_calendar, String
35 property :accuracy_sec, [Integer, String]
36 property :randomized_delay_sec, [Integer, String]
37 property :fixed_random_delay, [true, false]
38 property :unit, String
39 property :persistent, [true, false]
40 property :wake_system, [true, false]
41 property :remain_after_elapse, [true, false]
42
43 action :create do
44   timer_variables = new_resource.to_hash
45
46   if new_resource.dropin
47     directory dropin_directory do
48       owner "root"
49       group "root"
50       mode "755"
51     end
52   end
53
54   template config_name do
55     cookbook "systemd"
56     source "timer.erb"
57     owner "root"
58     group "root"
59     mode "644"
60     variables timer_variables
61     notifies :run, "execute[systemctl-reload]"
62   end
63
64   execute "systemctl-reload" do
65     action :nothing
66     command "systemctl daemon-reload"
67     user "root"
68     group "root"
69   end
70 end
71
72 action :delete do
73   file config_name do
74     action :delete
75   end
76
77   execute "systemctl-reload-#{new_resource.timer}.timer" do
78     action :nothing
79     command "systemctl daemon-reload"
80     user "root"
81     group "root"
82     subscribes :run, "file[/etc/systemd/system/#{new_resource.timer}.timer]"
83   end
84 end
85
86 action_class do
87   def dropin_directory
88     "/etc/systemd/system/#{new_resource.timer}.timer.d"
89   end
90
91   def config_name
92     if new_resource.dropin
93       "#{dropin_directory}/#{new_resource.dropin}.conf"
94     else
95       "/etc/systemd/system/#{new_resource.timer}.timer"
96     end
97   end
98 end