class Puppet::Application::Resource

Attributes

extra_params[RW]
host[RW]

Public Instance Methods

help() click to toggle source
    # File lib/puppet/application/resource.rb
 37   def help
 38     <<-HELP
 39 
 40 puppet-resource(8) -- #{summary}
 41 ========
 42 
 43 SYNOPSIS
 44 --------
 45 Uses the Puppet RAL to directly interact with the system.
 46 
 47 
 48 USAGE
 49 -----
 50 puppet resource [-h|--help] [-d|--debug] [-v|--verbose] [-e|--edit]
 51   [-p|--param <parameter>] [-t|--types] [-y|--to_yaml] <type>
 52   [<name>] [<attribute>=<value> ...]
 53 
 54 
 55 DESCRIPTION
 56 -----------
 57 This command provides simple facilities for converting current system
 58 state into Puppet code, along with some ability to modify the current
 59 state using Puppet's RAL.
 60 
 61 By default, you must at least provide a type to list, in which case
 62 puppet resource will tell you everything it knows about all resources of
 63 that type. You can optionally specify an instance name, and puppet
 64 resource will only describe that single instance.
 65 
 66 If given a type, a name, and a series of <attribute>=<value> pairs,
 67 puppet resource will modify the state of the specified resource.
 68 Alternately, if given a type, a name, and the '--edit' flag, puppet
 69 resource will write its output to a file, open that file in an editor,
 70 and then apply the saved file as a Puppet transaction.
 71 
 72 
 73 OPTIONS
 74 -------
 75 Note that any setting that's valid in the configuration
 76 file is also a valid long argument. For example, 'ssldir' is a valid
 77 setting, so you can specify '--ssldir <directory>' as an
 78 argument.
 79 
 80 See the configuration file documentation at
 81 https://puppet.com/docs/puppet/latest/configuration.html for the
 82 full list of acceptable parameters. A commented list of all
 83 configuration options can also be generated by running puppet with
 84 '--genconfig'.
 85 
 86 * --debug:
 87   Enable full debugging.
 88 
 89 * --edit:
 90   Write the results of the query to a file, open the file in an editor,
 91   and read the file back in as an executable Puppet manifest.
 92 
 93 * --help:
 94   Print this help message.
 95 
 96 * --param:
 97   Add more parameters to be outputted from queries.
 98 
 99 * --types:
100   List all available types.
101 
102 * --verbose:
103   Print extra information.
104 
105 * --to_yaml:
106   Output found resources in yaml format, suitable to use with Hiera and
107   create_resources.
108 
109 EXAMPLE
110 -------
111 This example uses `puppet resource` to return a Puppet configuration for
112 the user `luke`:
113 
114     $ puppet resource user luke
115     user { 'luke':
116      home => '/home/luke',
117      uid => '100',
118      ensure => 'present',
119      comment => 'Luke Kanies,,,',
120      gid => '1000',
121      shell => '/bin/bash',
122      groups => ['sysadmin','audio','video','puppet']
123     }
124 
125 
126 AUTHOR
127 ------
128 Luke Kanies
129 
130 
131 COPYRIGHT
132 ---------
133 Copyright (c) 2011 Puppet Inc., LLC Licensed under the Apache 2.0 License
134 
135     HELP
136   end
main() click to toggle source
    # File lib/puppet/application/resource.rb
138 def main
139   # If the specified environment does not exist locally, fall back to the default (production) environment
140   env = Puppet.lookup(:environments).get(Puppet[:environment]) || create_default_environment
141 
142   Puppet.override(:current_environment => env, :loaders => Puppet::Pops::Loaders.new(env)) do
143     type, name, params = parse_args(command_line.args)
144 
145     raise _("Editing with Yaml output is not supported") if options[:edit] and options[:to_yaml]
146 
147     resources = find_or_save_resources(type, name, params)
148 
149   if options[:to_yaml]
150     data = resources.map do |resource|
151       resource.prune_parameters(:parameters_to_include => @extra_params).to_hiera_hash
152     end.inject(:merge!)
153     text = YAML.dump(type.downcase => data)
154   else
155     text = resources.map do |resource|
156       resource.prune_parameters(:parameters_to_include => @extra_params).to_manifest.force_encoding(Encoding.default_external)
157     end.join("\n")
158   end
159 
160     options[:edit] ?
161       handle_editing(text) :
162       (puts text)
163   end
164 end
preinit() click to toggle source
   # File lib/puppet/application/resource.rb
 8 def preinit
 9   @extra_params = [:provider]
10 end
setup() click to toggle source
    # File lib/puppet/application/resource.rb
166 def setup
167   Puppet::Util::Log.newdestination(:console)
168   set_log_level
169 end
summary() click to toggle source
   # File lib/puppet/application/resource.rb
33 def summary
34   _("The resource abstraction layer shell")
35 end

Private Instance Methods

create_default_environment() click to toggle source
    # File lib/puppet/application/resource.rb
216 def create_default_environment
217   Puppet.debug("Specified environment '#{Puppet[:environment]}' does not exist on the filesystem, defaulting to 'production'")
218   Puppet[:environment] = :production
219   basemodulepath = Puppet::Node::Environment.split_path(Puppet[:basemodulepath])
220   modulepath = Puppet[:modulepath]
221   modulepath = (modulepath.nil? || modulepath.empty?) ? basemodulepath : Puppet::Node::Environment.split_path(modulepath)
222   Puppet::Node::Environment.create(Puppet[:environment], modulepath, Puppet::Node::Environment::NO_MANIFEST)
223 end
find_or_save_resources(type, name, params) click to toggle source
    # File lib/puppet/application/resource.rb
225 def find_or_save_resources(type, name, params)
226   key = local_key(type, name)
227 
228   if name
229     if params.empty?
230       [ Puppet::Resource.indirection.find( key ) ]
231     else
232       resource = Puppet::Resource.new( type, name, :parameters => params )
233 
234       # save returns [resource that was saved, transaction log from applying the resource]
235       save_result = Puppet::Resource.indirection.save(resource, key)
236       [ save_result.first ]
237     end
238   else
239     if type == "file"
240       raise _("Listing all file instances is not supported.  Please specify a file or directory, e.g. puppet resource file /etc")
241     end
242     Puppet::Resource.indirection.search( key, {} )
243   end
244 end
handle_editing(text) click to toggle source
    # File lib/puppet/application/resource.rb
177 def handle_editing(text)
178   require 'tempfile'
179   # Prefer the current directory, which is more likely to be secure
180   # and, in the case of interactive use, accessible to the user.
181   tmpfile = Tempfile.new('x2puppet', Dir.pwd, :encoding => Encoding::UTF_8)
182   begin
183     # sync write, so nothing buffers before we invoke the editor.
184     tmpfile.sync = true
185     tmpfile.puts text
186 
187     # edit the content
188     system(ENV["EDITOR"] || 'vi', tmpfile.path)
189 
190     # ...and, now, pass that file to puppet to apply.  Because
191     # many editors rename or replace the original file we need to
192     # feed the pathname, not the file content itself, to puppet.
193     system('puppet apply -v ' + tmpfile.path)
194   ensure
195     # The temporary file will be safely removed.
196     tmpfile.close(true)
197   end
198 end
local_key(type, name) click to toggle source
    # File lib/puppet/application/resource.rb
173 def local_key(type, name)
174   [type, name].join('/')
175 end
parse_args(args) click to toggle source
    # File lib/puppet/application/resource.rb
200 def parse_args(args)
201   type = args.shift or raise _("You must specify the type to display")
202   Puppet::Type.type(type) or raise _("Could not find type %{type}") % { type: type }
203   name = args.shift
204   params = {}
205   args.each do |setting|
206     if setting =~ /^(\w+)=(.+)$/
207       params[$1] = $2
208     else
209       raise _("Invalid parameter setting %{setting}") % { setting: setting }
210     end
211   end
212 
213   [type, name, params]
214 end