1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#
# Copyright:: 2020, Chef Software Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative "../resource"
require_relative "../dist"
class Chef
class Resource
class ChefClientSystemdTimer < Chef::Resource
unified_mode true
provides :chef_client_systemd_timer
description "Use the chef_client_systemd_timer resource to setup the #{Chef::Dist::PRODUCT} to run as a systemd timer."
introduced "16.0"
examples <<~DOC
Setup #{Chef::Dist::PRODUCT} to run using the default 30 minute cadence
```ruby
chef_client_systemd_timer "Run chef-client as a systemd timer"
```
Run #{Chef::Dist::PRODUCT} every 1 hour
```ruby
chef_client_systemd_timer "Run chef-client every 1 hour" do
interval "1hr"
end
```
Run #{Chef::Dist::PRODUCT} with extra options passed to the client
```ruby
chef_client_systemd_timer "Run an override recipe" do
daemon_options ["--override-runlist mycorp_base::default"]
end
```
DOC
property :job_name, String,
description: "The name of the system timer to create.",
default: Chef::Dist::CLIENT
property :description, String,
description: "The description to add to the systemd timer. This will be displayed when running `systemctl status` for the timer.",
default: "#{Chef::Dist::PRODUCT} periodic execution"
property :user, String,
description: "The name of the user that #{Chef::Dist::PRODUCT} runs as.",
default: "root"
property :delay_after_boot, String,
description: "The time to wait after booting before the interval starts. This is expressed as a systemd time span such as `300seconds`, `1hr`, or `1m`. See <https://www.freedesktop.org/software/systemd/man/systemd.time.html> for a complete list of allowed time span values.",
default: "1min"
property :interval, String,
description: "The interval to wait between executions. This is expressed as a systemd time span such as `300seconds`, `1hr`, or `1m`. See <https://www.freedesktop.org/software/systemd/man/systemd.time.html> for a complete list of allowed time span values.",
default: "30min"
property :splay, String,
description: "A interval between 0 and X to add to the interval so that all #{Chef::Dist::CLIENT} commands don't execute at the same time. This is expressed as a systemd time span such as `300seconds`, `1hr`, or `1m`. See <https://www.freedesktop.org/software/systemd/man/systemd.time.html> for a complete list of allowed time span values.",
default: "5min"
property :accept_chef_license, [true, false],
description: "Accept the Chef Online Master License and Services Agreement. See https://www.chef.io/online-master-agreement/",
default: false
property :run_on_battery, [true, false],
description: "Run the timer for #{Chef::Dist::PRODUCT} if the system is on battery.",
default: true
property :config_directory, String,
description: "The path of the config directory.",
default: Chef::Dist::CONF_DIR
property :chef_binary_path, String,
description: "The path to the #{Chef::Dist::CLIENT} binary.",
default: "/opt/#{Chef::Dist::DIR_SUFFIX}/bin/#{Chef::Dist::CLIENT}"
property :daemon_options, Array,
description: "An array of options to pass to the #{Chef::Dist::CLIENT} command.",
default: lazy { [] }
property :environment, Hash,
description: "A Hash containing additional arbitrary environment variables under which the systemd timer will be run in the form of ``({'ENV_VARIABLE' => 'VALUE'})``.",
default: lazy { {} }
action :add do
systemd_unit "#{new_resource.job_name}.service" do
content service_content
action :create
end
systemd_unit "#{new_resource.job_name}.timer" do
content timer_content
action %i{create enable start}
end
end
action :remove do
systemd_unit "#{new_resource.job_name}.service" do
action :remove
end
systemd_unit "#{new_resource.job_name}.timer" do
action :remove
end
end
action_class do
#
# The chef-client command to run in the systemd unit.
#
# @return [String]
#
def chef_client_cmd
cmd = "#{new_resource.chef_binary_path}"
cmd << " #{new_resource.daemon_options.join(" ")}" unless new_resource.daemon_options.empty?
cmd << " --chef-license accept" if new_resource.accept_chef_license
cmd << " -c #{::File.join(new_resource.config_directory, "client.rb")}"
cmd
end
#
# The timer content to pass to the systemd_unit
#
# @return [Hash]
#
def timer_content
{
"Unit" => { "Description" => new_resource.description },
"Timer" => {
"OnBootSec" => new_resource.delay_after_boot,
"OnUnitActiveSec" => new_resource.interval,
"RandomizedDelaySec" => new_resource.splay,
},
"Install" => { "WantedBy" => "timers.target" },
}
end
#
# The service content to pass to the systemd_unit
#
# @return [Hash]
#
def service_content
unit = {
"Unit" => {
"Description" => new_resource.description,
"After" => "network.target auditd.service",
},
"Service" => {
"Type" => "oneshot",
"ExecStart" => chef_client_cmd,
"SuccessExitStatus" => [3, 213, 35, 37, 41],
},
"Install" => { "WantedBy" => "multi-user.target" },
}
unit["Service"]["ConditionACPower"] = "true" unless new_resource.run_on_battery
unit["Service"]["Environment"] = new_resource.environment.collect { |k, v| "\"#{k}=#{v}\"" } unless new_resource.environment.empty?
unit
end
end
end
end
end
|