Class: Chef::Application::Apply

Inherits:
Chef::Application show all
Includes:
ChefLicensing::CLIFlags::MixlibCLI, LicenseAcceptance::CLIFlags::MixlibCLI
Defined in:
lib/chef/application/apply.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chef::Application

#apply_extra_config_options, #check_license_acceptance, #chef_config, #chef_configfetcher, #configure_chef, #configure_encoding, #configure_log_location, #configure_logging, debug_stacktrace, #emit_warnings, exit!, fatal!, #force_force_logger, #load_config_file, logger, #logger, normalize_exit_code, #resolve_log_level, #run_chef_client, #set_specific_recipes, #setup_application, #setup_signal_handlers, use_separate_defaults?, #using_output_formatter?

Constructor Details

#initializeApply

Returns a new instance of Apply.



165
166
167
# File 'lib/chef/application/apply.rb', line 165

def initialize
  super
end

Instance Attribute Details

#json_attribsObject (readonly)

Returns the value of attribute json_attribs.



155
156
157
# File 'lib/chef/application/apply.rb', line 155

def json_attribs
  @json_attribs
end

Class Method Details



157
158
159
160
161
162
163
# File 'lib/chef/application/apply.rb', line 157

def self.print_help
  instance = new
  instance.parse_options([])
  puts instance.opt_parser
  puts Chef::Licensing.licensing_help if ChefUtils::Dist::Apply::EXEC == "chef-apply"
  exit 0
end

Instance Method Details

#get_recipe_and_run_contextObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef/application/apply.rb', line 199

def get_recipe_and_run_context
  Chef::Config[:solo_legacy_mode] = true
  @chef_client = Chef::Client.new(@json_attribs)
  @chef_client.run_ohai
  @chef_client.load_node
  @chef_client.build_node
  run_context = if @chef_client.events.nil?
                  Chef::RunContext.new(@chef_client.node, {})
                else
                  Chef::RunContext.new(@chef_client.node, {}, @chef_client.events)
                end
  recipe = Chef::Recipe.new("(#{ChefUtils::Dist::Apply::EXEC} cookbook)", "(#{ChefUtils::Dist::Apply::EXEC} recipe)", run_context)
  [recipe, run_context]
end

#parse_jsonObject



178
179
180
181
182
183
# File 'lib/chef/application/apply.rb', line 178

def parse_json
  if Chef::Config[:json_attribs]
    config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
    @json_attribs = config_fetcher.fetch_json
  end
end

#read_recipe_file(file_name) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chef/application/apply.rb', line 185

def read_recipe_file(file_name)
  if file_name.nil?
    Chef::Application.fatal!("No recipe file was provided", Chef::Exceptions::RecipeNotFound.new)
  else
    recipe_path = File.expand_path(file_name)
    unless File.exist?(recipe_path)
      Chef::Application.fatal!("No file exists at #{recipe_path}", Chef::Exceptions::RecipeNotFound.new)
    end
    recipe_fh = open(recipe_path)
    recipe_text = recipe_fh.read
    [recipe_text, recipe_fh]
  end
end

#reconfigureObject



169
170
171
172
173
174
175
176
# File 'lib/chef/application/apply.rb', line 169

def reconfigure
  parse_options
  Chef::Config.merge!(config)
  configure_logging
  Chef::Config.export_proxies
  Chef::Config.init_openssl
  parse_json
end

#run(enforce_license: false) ⇒ Object

Get this party started



269
270
271
272
273
274
# File 'lib/chef/application/apply.rb', line 269

def run(enforce_license: false)
  reconfigure
  check_license_acceptance if enforce_license
  Chef::Licensing.fetch_and_persist if ChefUtils::Dist::Apply::EXEC == "chef-apply"
  run_application
end

#run_applicationObject



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/chef/application/apply.rb', line 256

def run_application
  Chef::Licensing.check_software_entitlement! if ChefUtils::Dist::Apply::EXEC == "chef-apply"
  parse_options
  run_chef_recipe
  Chef::Application.exit! "Exiting", 0
rescue SystemExit
  raise
rescue Exception => e
  Chef::Application.debug_stacktrace(e)
  Chef::Application.fatal!("#{e.class}: #{e.message}", e)
end

#run_chef_recipeObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/chef/application/apply.rb', line 223

def run_chef_recipe
  if config[:execute]
    @recipe_text = config[:execute]
    temp_recipe_file
  elsif config[:stdin]
    @recipe_text = STDIN.read
    temp_recipe_file
  else
    unless ARGV[0]
      puts opt_parser
      Chef::Application.exit! "No recipe file provided", Chef::Exceptions::RecipeNotFound.new
    end
    @recipe_filename = ARGV[0]
    @recipe_text, @recipe_fh = read_recipe_file @recipe_filename
  end
  recipe, run_context = get_recipe_and_run_context
  if config[:yaml] || File.extname(@recipe_filename) == ".yml"
    logger.info "Parsing recipe as YAML"
    recipe.from_yaml(@recipe_text)
  else
    recipe.instance_eval(@recipe_text, @recipe_filename, 1)
  end
  runner = Chef::Runner.new(run_context)
  catch(:end_client_run_early) do

    runner.converge
  ensure
    @recipe_fh.close

  end
  Chef::Platform::Rebooter.reboot_if_needed!(runner)
end

#temp_recipe_fileObject

write recipe to temp file, so in case of error, user gets error w/ context



216
217
218
219
220
221
# File 'lib/chef/application/apply.rb', line 216

def temp_recipe_file
  @recipe_fh = Tempfile.open("recipe-temporary-file")
  @recipe_fh.write(@recipe_text)
  @recipe_fh.rewind
  @recipe_filename = @recipe_fh.path
end