]> git.openstreetmap.org Git - chef.git/blob - cookbooks/systemd/resources/timer.rb
blogs: fix build in test
[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 :unit, String
38 property :persistent, [true, false]
39 property :wake_system, [true, false]
40 property :remain_after_elapse, [true, false]
41
42 action :create do
43   timer_variables = new_resource.to_hash
44
45   if new_resource.dropin
46     directory dropin_directory do
47       owner "root"
48       group "root"
49       mode "755"
50     end
51   end
52
53   template config_name do
54     cookbook "systemd"
55     source "timer.erb"
56     owner "root"
57     group "root"
58     mode "644"
59     variables timer_variables
60     notifies :run, "execute[systemctl-reload]"
61   end
62
63   execute "systemctl-reload" do
64     action :nothing
65     command "systemctl daemon-reload"
66     user "root"
67     group "root"
68   end
69 end
70
71 action :delete do
72   file config_name do
73     action :delete
74   end
75
76   execute "systemctl-reload-#{new_resource.timer}.timer" do
77     action :nothing
78     command "systemctl daemon-reload"
79     user "root"
80     group "root"
81     subscribes :run, "file[/etc/systemd/system/#{new_resource.timer}.timer]"
82   end
83 end
84
85 action_class do
86   def dropin_directory
87     "/etc/systemd/system/#{new_resource.timer}.timer.d"
88   end
89
90   def config_name
91     if new_resource.dropin
92       "#{dropin_directory}/#{new_resource.dropin}.conf"
93     else
94       "/etc/systemd/system/#{new_resource.timer}.timer"
95     end
96   end
97 end