Class: Chef::Application::Apply

Inherits:
Chef::Application show all
Defined in:
lib/chef/application/apply.rb

Instance Method Summary collapse

Methods inherited from Chef::Application

#auto_log_level?, #configure_chef, #configure_encoding, #configure_logging, #configure_proxy_environment_variables, #configure_stdout_logger, debug_stacktrace, exit!, fatal!, #load_config_file, #resolve_log_level, #run_chef_client, #setup_application, #setup_signal_handlers, #using_output_formatter?, #want_additional_logger?

Constructor Details

#initializeApply

Returns a new instance of Apply.



82
83
84
# File 'lib/chef/application/apply.rb', line 82

def initialize
  super
end

Instance Method Details

#get_recipe_and_run_contextObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/application/apply.rb', line 107

def get_recipe_and_run_context
  Chef::Config[:solo] = true
  @chef_client = Chef::Client.new
  @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("(chef-apply cookbook)", "(chef-apply recipe)", run_context)
  [recipe, run_context]
end

#read_recipe_file(file_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/application/apply.rb', line 93

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

#reconfigureObject



86
87
88
89
90
91
# File 'lib/chef/application/apply.rb', line 86

def reconfigure
  parse_options
  Chef::Config.merge!(config)
  configure_logging
  configure_proxy_environment_variables
end

#runObject

Get this party started



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

def run
  reconfigure
  run_application
end

#run_applicationObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chef/application/apply.rb', line 156

def run_application
  begin
    parse_options
    run_chef_recipe
    Chef::Application.exit! "Exiting", 0
  rescue SystemExit => e
    raise
  rescue Exception => e
    Chef::Application.debug_stacktrace(e)
    Chef::Application.fatal!("#{e.class}: #{e.message}", 1)
  end
end

#run_chef_recipeObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/application/apply.rb', line 131

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
    if !ARGV[0]
      puts opt_parser
      Chef::Application.exit! "No recipe file provided", 1
    end
    @recipe_filename = ARGV[0]
    @recipe_text,@recipe_fh = read_recipe_file @recipe_filename
  end
  recipe,run_context = get_recipe_and_run_context
  recipe.instance_eval(@recipe_text, @recipe_filename, 1)
  runner = Chef::Runner.new(run_context)
  begin
    runner.converge
  ensure
    @recipe_fh.close
  end
end

#temp_recipe_fileObject

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



124
125
126
127
128
129
# File 'lib/chef/application/apply.rb', line 124

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