class Puppet::Application::Resource
Attributes
extra_params[RW]
host[RW]
Public Instance Methods
help()
click to toggle source
# File lib/puppet/application/resource.rb 38 def help 39 <<-HELP 40 41 puppet-resource(8) -- #{summary} 42 ======== 43 44 SYNOPSIS 45 -------- 46 Uses the Puppet RAL to directly interact with the system. 47 48 49 USAGE 50 ----- 51 puppet resource [-h|--help] [-d|--debug] [-v|--verbose] [-e|--edit] 52 [-p|--param <parameter>] [-t|--types] [-y|--to_yaml] <type> 53 [<name>] [<attribute>=<value> ...] 54 55 56 DESCRIPTION 57 ----------- 58 This command provides simple facilities for converting current system 59 state into Puppet code, along with some ability to modify the current 60 state using Puppet's RAL. 61 62 By default, you must at least provide a type to list, in which case 63 puppet resource will tell you everything it knows about all resources of 64 that type. You can optionally specify an instance name, and puppet 65 resource will only describe that single instance. 66 67 If given a type, a name, and a series of <attribute>=<value> pairs, 68 puppet resource will modify the state of the specified resource. 69 Alternately, if given a type, a name, and the '--edit' flag, puppet 70 resource will write its output to a file, open that file in an editor, 71 and then apply the saved file as a Puppet transaction. 72 73 74 OPTIONS 75 ------- 76 Note that any setting that's valid in the configuration 77 file is also a valid long argument. For example, 'ssldir' is a valid 78 setting, so you can specify '--ssldir <directory>' as an 79 argument. 80 81 See the configuration file documentation at 82 https://puppet.com/docs/puppet/latest/configuration.html for the 83 full list of acceptable parameters. A commented list of all 84 configuration options can also be generated by running puppet with 85 '--genconfig'. 86 87 * --debug: 88 Enable full debugging. 89 90 * --edit: 91 Write the results of the query to a file, open the file in an editor, 92 and read the file back in as an executable Puppet manifest. 93 94 * --help: 95 Print this help message. 96 97 * --param: 98 Add more parameters to be outputted from queries. 99 100 * --types: 101 List all available types. 102 103 * --verbose: 104 Print extra information. 105 106 * --to_yaml: 107 Output found resources in yaml format, suitable to use with Hiera and 108 create_resources. 109 110 EXAMPLE 111 ------- 112 This example uses `puppet resource` to return a Puppet configuration for 113 the user `luke`: 114 115 $ puppet resource user luke 116 user { 'luke': 117 home => '/home/luke', 118 uid => '100', 119 ensure => 'present', 120 comment => 'Luke Kanies,,,', 121 gid => '1000', 122 shell => '/bin/bash', 123 groups => ['sysadmin','audio','video','puppet'] 124 } 125 126 127 AUTHOR 128 ------ 129 Luke Kanies 130 131 132 COPYRIGHT 133 --------- 134 Copyright (c) 2011 Puppet Inc., LLC Licensed under the Apache 2.0 License 135 136 HELP 137 end
main()
click to toggle source
# File lib/puppet/application/resource.rb 139 def main 140 # If the specified environment does not exist locally, fall back to the default (production) environment 141 env = Puppet.lookup(:environments).get(Puppet[:environment]) || create_default_environment 142 143 Puppet.override(:current_environment => env, :loaders => Puppet::Pops::Loaders.new(env)) do 144 type, name, params = parse_args(command_line.args) 145 146 raise _("Editing with Yaml output is not supported") if options[:edit] and options[:to_yaml] 147 148 resources = find_or_save_resources(type, name, params) 149 150 if options[:to_yaml] 151 data = resources.map do |resource| 152 resource.prune_parameters(:parameters_to_include => @extra_params).to_hiera_hash 153 end.inject(:merge!) 154 text = YAML.dump(type.downcase => data) 155 else 156 text = resources.map do |resource| 157 resource.prune_parameters(:parameters_to_include => @extra_params).to_manifest.force_encoding(Encoding.default_external) 158 end.join("\n") 159 end 160 161 options[:edit] ? 162 handle_editing(text) : 163 (puts text) 164 end 165 end
preinit()
click to toggle source
# File lib/puppet/application/resource.rb 9 def preinit 10 @extra_params = [:provider] 11 end
setup()
click to toggle source
# File lib/puppet/application/resource.rb 167 def setup 168 Puppet::Util::Log.newdestination(:console) 169 set_log_level 170 end
summary()
click to toggle source
# File lib/puppet/application/resource.rb 34 def summary 35 _("The resource abstraction layer shell") 36 end
Private Instance Methods
create_default_environment()
click to toggle source
# File lib/puppet/application/resource.rb 217 def create_default_environment 218 Puppet.debug("Specified environment '#{Puppet[:environment]}' does not exist on the filesystem, defaulting to 'production'") 219 Puppet[:environment] = :production 220 basemodulepath = Puppet::Node::Environment.split_path(Puppet[:basemodulepath]) 221 modulepath = Puppet[:modulepath] 222 modulepath = (modulepath.nil? || modulepath.empty?) ? basemodulepath : Puppet::Node::Environment.split_path(modulepath) 223 Puppet::Node::Environment.create(Puppet[:environment], modulepath, Puppet::Node::Environment::NO_MANIFEST) 224 end
find_or_save_resources(type, name, params)
click to toggle source
# File lib/puppet/application/resource.rb 226 def find_or_save_resources(type, name, params) 227 key = local_key(type, name) 228 229 Puppet.override(stringify_rich: true) do 230 if name 231 if params.empty? 232 [ Puppet::Resource.indirection.find( key ) ] 233 else 234 resource = Puppet::Resource.new( type, name, :parameters => params ) 235 236 # save returns [resource that was saved, transaction log from applying the resource] 237 save_result = Puppet::Resource.indirection.save(resource, key) 238 [ save_result.first ] 239 end 240 else 241 if type == "file" 242 raise _("Listing all file instances is not supported. Please specify a file or directory, e.g. puppet resource file /etc") 243 end 244 Puppet::Resource.indirection.search( key, {} ) 245 end 246 end 247 end
handle_editing(text)
click to toggle source
# File lib/puppet/application/resource.rb 178 def handle_editing(text) 179 require 'tempfile' 180 # Prefer the current directory, which is more likely to be secure 181 # and, in the case of interactive use, accessible to the user. 182 tmpfile = Tempfile.new('x2puppet', Dir.pwd, :encoding => Encoding::UTF_8) 183 begin 184 # sync write, so nothing buffers before we invoke the editor. 185 tmpfile.sync = true 186 tmpfile.puts text 187 188 # edit the content 189 system(ENV["EDITOR"] || 'vi', tmpfile.path) 190 191 # ...and, now, pass that file to puppet to apply. Because 192 # many editors rename or replace the original file we need to 193 # feed the pathname, not the file content itself, to puppet. 194 system('puppet apply -v ' + tmpfile.path) 195 ensure 196 # The temporary file will be safely removed. 197 tmpfile.close(true) 198 end 199 end
local_key(type, name)
click to toggle source
# File lib/puppet/application/resource.rb 174 def local_key(type, name) 175 [type, name].join('/') 176 end
parse_args(args)
click to toggle source
# File lib/puppet/application/resource.rb 201 def parse_args(args) 202 type = args.shift or raise _("You must specify the type to display") 203 Puppet::Type.type(type) or raise _("Could not find type %{type}") % { type: type } 204 name = args.shift 205 params = {} 206 args.each do |setting| 207 if setting =~ /^(\w+)=(.+)$/ 208 params[$1] = $2 209 else 210 raise _("Invalid parameter setting %{setting}") % { setting: setting } 211 end 212 end 213 214 [type, name, params] 215 end