Skip to content

Commit d7818b2

Browse files
authored
Merge pull request #632 from bastelfreak/noop
run task/plan: Allow noop and environment option
2 parents 163f5de + f39b4bc commit d7818b2

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

plans/run.pp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Starts a Puppet agent run on the specified targets.
22
# Note: This plan may cause issues when run in Puppet Enterprise.
33
# @param targets The targets to start a Puppet agent run on.
4+
# @param noop if true, all runs will use --noop
5+
# @param environment the desired puppet code environment
46
plan puppet_agent::run (
5-
TargetSpec $targets
7+
TargetSpec $targets,
8+
Boolean $noop = false,
9+
Optional[String[1]] $environment = undef,
610
) {
711
# Check which targets have the agent installed by checking
812
# the version of the agent. No point in trying to run the
@@ -21,8 +25,8 @@
2125
'_error' => {
2226
'msg' => "The task puppet_agent::version failed: ${result.error.message}. Unable to determine if the Puppet agent is installed.",
2327
'kind' => 'puppet_agent/agent-version-error',
24-
'details' => {}
25-
}
28+
'details' => {},
29+
},
2630
}
2731

2832
Result.new($result.target, $err)
@@ -45,19 +49,28 @@
4549
'_error' => {
4650
'msg' => 'Puppet agent is not installed on the target. Run the puppet_agent::install task on these targets to install the Puppet agent.',
4751
'kind' => 'puppet_agent/agent-not-installed',
48-
'details' => {}
49-
}
52+
'details' => {},
53+
},
5054
}
5155

5256
Result.new($result.target, $err)
5357
}
5458

5559
# Run the agent on all targets that have the agent installed.
60+
$arg_env = $environment ? {
61+
Undef => {},
62+
default => { 'environment' => $environment, },
63+
}
64+
$arg_noop = $noop ? {
65+
true => { 'noop' => true, },
66+
default => {},
67+
}
68+
$args = $arg_env + $arg_noop + { '_catch_errors' => true }
5669
$run_results = run_task(
5770
'puppet_agent::run',
5871
$agent_results.targets,
5972
'Run Puppet agent',
60-
'_catch_errors' => true
73+
$args,
6174
)
6275

6376
# Merge all of the results into a single ResultSet so each

tasks/run.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
{
22
"description": "Run the Puppet agent. This task may cause problems if run in Puppet Enterprise.",
3-
"parameters": {},
3+
"parameters": {
4+
"noop": {
5+
"description": "run the puppet agent with --noop",
6+
"type": "Boolean",
7+
"default": false
8+
},
9+
"environment": {
10+
"description": "The desired puppet code environment to use",
11+
"type": "Optional[String[1]]"
12+
}
13+
},
414
"files": ["puppet_agent/files/rb_task_helper.rb"],
515
"private": true
616
}

tasks/run.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,34 @@ def config_print(*keys)
138138
end
139139
end
140140

141+
def noop(params)
142+
params['noop'] == true ? '--noop' : ''
143+
end
144+
145+
def environment(params)
146+
params['environment'].length > 1 ? "--environment=#{params['environment']}" : ''
147+
end
148+
141149
# Attempts to run the Puppet agent, returning the mtime for the last run report
142150
# and the exit code from the Puppet agent run.
143-
def try_run(last_run_report)
151+
def try_run(last_run_report, params)
144152
start_time = get_start_time(last_run_report)
145153

146-
command = [puppet_bin, 'agent', '-t', '--color', 'false']
154+
command = [puppet_bin, 'agent', '-t', '--color', 'false', noop(params), environment(params)]
147155

148156
options = {
149157
failonfail: false,
150158
custom_environment: get_env_fix_up,
151159
override_locale: false
152160
}
153161

154-
run_result = Puppet::Util::Execution.execute(command, options)
162+
run_result = Puppet::Util::Execution.execute(command.reject(&:empty?), options)
155163

156164
[start_time, run_result]
157165
end
158166

159167
# Runs the Puppet agent and returns the last run report.
160-
def run
168+
def run(params)
161169
unless puppet_bin_present?
162170
return error_result(
163171
'puppet_agent/no-puppet-bin-error',
@@ -177,7 +185,7 @@ def run
177185

178186
# Initially ignore the lockfile. It might be out-dated, so we give Puppet a chance
179187
# to clean it up and run.
180-
start_time, run_result = try_run(last_run_report)
188+
start_time, run_result = try_run(last_run_report, params)
181189
if run_result.nil?
182190
return error_result(
183191
'puppet_agent/fail-to-start-error',
@@ -204,7 +212,7 @@ def run
204212
if running?(lockfile)
205213
wait_for_lockfile(lockfile)
206214

207-
start_time, run_result = try_run(last_run_report)
215+
start_time, run_result = try_run(last_run_report, params)
208216
if run_result.nil?
209217
return error_result(
210218
'puppet_agent/fail-to-start-error',
@@ -237,5 +245,5 @@ def run
237245

238246
if __FILE__ == $PROGRAM_NAME
239247
runner = PuppetAgent::Runner.new
240-
puts JSON.dump(runner.run)
248+
puts JSON.dump(runner.run(params))
241249
end

0 commit comments

Comments
 (0)