Skip to content

Commit f170937

Browse files
authored
Merge pull request #7242 from DavidS/pup-8766-device-load-path
(PUP-8766) device $LOAD_PATH and environment handling
2 parents 6d084ee + e1a810b commit f170937

File tree

3 files changed

+51
-33
lines changed

3 files changed

+51
-33
lines changed

lib/puppet/application/device.rb

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'puppet/application'
2+
require 'puppet/configurer'
23
require 'puppet/util/network_device'
34

45
class Puppet::Application::Device < Puppet::Application
@@ -53,6 +54,10 @@ def preinit
5354
options[:detailed_exitcodes] = true
5455
end
5556

57+
option("--libdir LIBDIR") do |arg|
58+
options[:libdir] = arg
59+
end
60+
5661
option("--apply MANIFEST") do |arg|
5762
options[:apply] = arg.to_s
5863
end
@@ -93,10 +98,11 @@ def help
9398
9499
USAGE
95100
-----
96-
puppet device [-d|--debug] [--detailed-exitcodes] [--deviceconfig <file>]
97-
[-h|--help] [-l|--logdest syslog|<file>|console]
98-
[-v|--verbose] [-w|--waitforcert <seconds>] [-f|--facts]
99-
[-a|--apply <file>] [-r|--resource <type> [name]]
101+
puppet device [-h|--help] [-v|--verbose] [-d|--debug]
102+
[-l|--logdest syslog|<file>|console] [--detailed-exitcodes]
103+
[--deviceconfig <file>] [-w|--waitforcert <seconds>]
104+
[--libdir <directory>]
105+
[-a|--apply <file>] [-f|--facts] [-r|--resource <type> [name]]
100106
[-t|--target <device>] [--user=<user>] [-V|--version]
101107
102108
@@ -135,9 +141,25 @@ def help
135141
long argument. For example, 'server' is a valid configuration parameter, so
136142
you can specify '--server <servername>' as an argument.
137143
138-
* --debug:
144+
* --help, -h:
145+
Print this help message
146+
147+
* --verbose, -v:
148+
Turn on verbose reporting.
149+
150+
* --debug, -d:
139151
Enable full debugging.
140152
153+
* --logdest, -l:
154+
Where to send log messages. Choose between 'syslog' (the POSIX syslog
155+
service), 'console', or the path to a log file. If debugging or verbosity is
156+
enabled, this defaults to 'console'. Otherwise, it defaults to 'syslog'.
157+
158+
A path ending with '.json' will receive structured output in JSON format. The
159+
log file will not have an ending ']' automatically written to it due to the
160+
appending nature of logging. It must be appended manually to make the content
161+
valid JSON.
162+
141163
* --detailed-exitcodes:
142164
Provide transaction information via exit codes. If this is enabled, an exit
143165
code of '1' means at least one device had a compile failure, an exit code of
@@ -149,18 +171,16 @@ def help
149171
Path to the device config file for puppet device.
150172
Default: $confdir/device.conf
151173
152-
* --help:
153-
Print this help message
174+
* --waitforcert, -w:
175+
This option only matters for targets that do not yet have certificates
176+
and it is enabled by default, with a value of 120 (seconds). This causes
177+
+puppet device+ to poll the server every 2 minutes and ask it to sign a
178+
certificate request. This is useful for the initial setup of a target.
179+
You can turn off waiting for certificates by specifying a time of 0.
154180
155-
* --logdest:
156-
Where to send log messages. Choose between 'syslog' (the POSIX syslog
157-
service), 'console', or the path to a log file. If debugging or verbosity is
158-
enabled, this defaults to 'console'. Otherwise, it defaults to 'syslog'.
159-
160-
A path ending with '.json' will receive structured output in JSON format. The
161-
log file will not have an ending ']' automatically written to it due to the
162-
appending nature of logging. It must be appended manually to make the content
163-
valid JSON.
181+
* --libdir:
182+
Override the per-device libdir with a local directory. Specifying a libdir also
183+
disables pluginsync. This is useful for testing.
164184
165185
* --apply:
166186
Apply a manifest against a remote target. Target must be specified.
@@ -183,16 +203,6 @@ def help
183203
* --user:
184204
The user to run as.
185205
186-
* --verbose:
187-
Turn on verbose reporting.
188-
189-
* --waitforcert:
190-
This option only matters for daemons that do not yet have certificates
191-
and it is enabled by default, with a value of 120 (seconds). This causes
192-
+puppet agent+ to connect to the server every 2 minutes and ask it to sign a
193-
certificate request. This is useful for the initial setup of a puppet
194-
client. You can turn off waiting for certificates by specifying a time of 0.
195-
196206
197207
EXAMPLE
198208
-------
@@ -205,7 +215,7 @@ def help
205215
206216
COPYRIGHT
207217
---------
208-
Copyright (c) 2011 Puppet Inc., LLC
218+
Copyright (c) 2011-2018 Puppet Inc., LLC
209219
Licensed under the Apache 2.0 License
210220
HELP
211221
end
@@ -222,11 +232,12 @@ def main
222232
raise _("missing argument: --target is required when using --apply") if options[:target].nil?
223233
raise _("%{file} does not exist, cannot apply") % { file: options[:apply] } unless File.file?(options[:apply])
224234
end
235+
libdir = Puppet[:libdir]
225236
vardir = Puppet[:vardir]
226237
confdir = Puppet[:confdir]
227238
certname = Puppet[:certname]
228239

229-
env = Puppet.lookup(:environments).get(Puppet[:environment])
240+
env = Puppet::Node::Environment.remote(Puppet[:environment])
230241
returns = Puppet.override(:current_environment => env, :loaders => Puppet::Pops::Loaders.new(env)) do
231242
# find device list
232243
require 'puppet/util/network_device/config'
@@ -251,9 +262,13 @@ def main
251262

252263
# override local $vardir and $certname
253264
Puppet[:confdir] = ::File.join(Puppet[:devicedir], device.name)
265+
Puppet[:libdir] = options[:libdir] || ::File.join(Puppet[:devicedir], device.name, 'lib')
254266
Puppet[:vardir] = ::File.join(Puppet[:devicedir], device.name)
255267
Puppet[:certname] = device.name
256268

269+
unless options[:resource] || options[:facts] || options[:apply] || options[:libdir]
270+
Puppet::Configurer::PluginHandler.new.download_plugins(env)
271+
end
257272
# this init the device singleton, so that the facts terminus
258273
# and the various network_device provider can use it
259274
Puppet::Util::NetworkDevice.init(device)
@@ -309,13 +324,14 @@ def main
309324

310325
require 'puppet/configurer'
311326
configurer = Puppet::Configurer.new
312-
configurer.run(:network_device => true, :pluginsync => Puppet::Configurer.should_pluginsync?)
327+
configurer.run(:network_device => true, :pluginsync => Puppet::Configurer.should_pluginsync? && !options[:libdir])
313328
end
314329
rescue => detail
315330
Puppet.log_exception(detail)
316331
# If we rescued an error, then we return 1 as the exit code
317332
1
318333
ensure
334+
Puppet[:libdir] = libdir
319335
Puppet[:vardir] = vardir
320336
Puppet[:confdir] = confdir
321337
Puppet[:certname] = certname

lib/puppet/util/command_line.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def initialize(subcommand_name, command_line)
106106
def run
107107
# For most applications, we want to be able to load code from the modulepath,
108108
# such as apply, describe, resource, and faces.
109-
# For agent, we only want to load pluginsync'ed code from libdir.
109+
# For agent and device, we only want to load pluginsync'ed code from libdir.
110110
# For master, we shouldn't ever be loading per-environment code into the master's
111111
# ruby process, but that requires fixing (#17210, #12173, #8750). So for now
112112
# we try to restrict to only code that can be autoloaded from the node's
@@ -116,8 +116,7 @@ def run
116116
# have an appropriate application-wide current_environment set.
117117
# If we cannot find the configured environment, which may not exist,
118118
# we do not attempt to add plugin directories to the load path.
119-
#
120-
if @subcommand_name != 'master' and @subcommand_name != 'agent'
119+
unless ['master', 'agent', 'device'].include? @subcommand_name
121120
if configured_environment = Puppet.lookup(:environments).get(Puppet[:environment])
122121
configured_environment.each_plugin_directory do |dir|
123122
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)

spec/unit/application/device_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
@device.options.stubs(:[]).with(:facts).returns(false)
300300
@device.options.stubs(:[]).with(:resource).returns(false)
301301
@device.options.stubs(:[]).with(:to_yaml).returns(false)
302+
@device.options.stubs(:[]).with(:libdir).returns(nil)
302303
@device.options.stubs(:[]).with(:client)
303304
@device.command_line.stubs(:args).returns([])
304305
Puppet::Util::NetworkDevice::Config.stubs(:devices).returns({})
@@ -504,7 +505,9 @@
504505
expect { @device.main }.to exit_with 1
505506
end
506507

507-
it "should print the device url scheme, host, and port" do
508+
it "should retrieve plugins and print the device url scheme, host, and port" do
509+
Puppet.stubs(:info)
510+
Puppet.expects(:info).with "Retrieving pluginfacts"
508511
Puppet.expects(:info).with "starting applying configuration to device1 at ssh://testhost"
509512
Puppet.expects(:info).with "starting applying configuration to device2 at https://testhost:443/some/path"
510513
expect { @device.main }.to exit_with 1

0 commit comments

Comments
 (0)