1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright (c) 2009 Daniel DeLeo
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'singleton'
require 'pp'
require 'etc'
require 'mixlib/cli'
require 'chef'
require 'chef/version'
require 'chef/client'
require 'chef/config'
require 'chef/config_fetcher'
require 'chef/shell/shell_session'
require 'chef/shell/ext'
require 'chef/json_compat'
# = Shell
# Shell is Chef in an IRB session. Shell can interact with a Chef server via the
# REST API, and run and debug recipes interactively.
module Shell
LEADERS = Hash.new("")
LEADERS[Chef::Recipe] = ":recipe"
LEADERS[Chef::Node] = ":attributes"
class << self
attr_accessor :client_type
attr_accessor :options
attr_accessor :env
attr_writer :editor
end
# Start the irb REPL with chef-shell's customizations
def self.start
setup_logger
# FUGLY HACK: irb gives us no other choice.
irb_help = [:help, :irb_help, IRB::ExtendCommandBundle::NO_OVERRIDE]
IRB::ExtendCommandBundle.instance_variable_get(:@ALIASES).delete(irb_help)
parse_opts
Chef::Config[:shell_config] = options.config
# HACK: this duplicates the functions of IRB.start, but we have to do it
# to get access to the main object before irb starts.
::IRB.setup(nil)
irb = IRB::Irb.new
init(irb.context.main)
irb_conf[:IRB_RC].call(irb.context) if irb_conf[:IRB_RC]
irb_conf[:MAIN_CONTEXT] = irb.context
trap("SIGINT") do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
end
def self.setup_logger
Chef::Config[:log_level] ||= :warn
# If log_level is auto, change it to warn
Chef::Config[:log_level] = :warn if Chef::Config[:log_level] == :auto
Chef::Log.init(STDERR)
Mixlib::Authentication::Log.logger = Ohai::Log.logger = Chef::Log.logger
Chef::Log.level = Chef::Config[:log_level] || :warn
end
# Shell assumes it's running whenever it is defined
def self.running?
true
end
# Set the irb_conf object to something other than IRB.conf
# usful for testing.
def self.irb_conf=(conf_hash)
@irb_conf = conf_hash
end
def self.irb_conf
@irb_conf || IRB.conf
end
def self.configure_irb
irb_conf[:HISTORY_FILE] = "~/.chef/chef_shell_history"
irb_conf[:SAVE_HISTORY] = 1000
irb_conf[:IRB_RC] = lambda do |conf|
m = conf.main
conf.prompt_c = "chef#{leader(m)} > "
conf.return_format = " => %s \n"
conf.prompt_i = "chef#{leader(m)} > "
conf.prompt_n = "chef#{leader(m)} ?> "
conf.prompt_s = "chef#{leader(m)}%l> "
conf.use_tracer = false
end
end
def self.leader(main_object)
env_string = Shell.env ? " (#{Shell.env})" : ""
LEADERS[main_object.class] + env_string
end
def self.session
unless client_type.instance.node_built?
puts "Session type: #{client_type.session_type}"
client_type.instance.reset!
end
client_type.instance
end
def self.init(main)
parse_json
configure_irb
session # trigger ohai run + session load
session.node.consume_attributes(@json_attribs)
Extensions.extend_context_object(main)
main.version
puts
puts "run `help' for help, `exit' or ^D to quit."
puts
puts "Ohai2u#{greeting}!"
end
def self.greeting
" #{Etc.getlogin}@#{Shell.session.node.fqdn}"
rescue NameError, ArgumentError
""
end
def self.parse_json
if Chef::Config[:json_attribs]
config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
@json_attribs = config_fetcher.fetch_json
end
end
def self.fatal!(message, exit_status)
Chef::Log.fatal(message)
exit exit_status
end
def self.client_type
type = Shell::StandAloneSession
type = Shell::SoloSession if Chef::Config[:shell_solo]
type = Shell::ClientSession if Chef::Config[:client]
type = Shell::DoppelGangerSession if Chef::Config[:doppelganger]
type
end
def self.parse_opts
@options = Options.new
@options.parse_opts
end
def self.editor
@editor || Chef::Config[:editor] || ENV['EDITOR']
end
class Options
include Mixlib::CLI
def self.footer(text=nil)
@footer = text if text
@footer
end
banner("chef-shell #{Chef::VERSION}\n\nUsage: chef-shell [NAMED_CONF] (OPTIONS)")
footer(<<-FOOTER)
When no CONFIG is specified, chef-shell attempts to load a default configuration file:
* If a NAMED_CONF is given, chef-shell will load ~/.chef/NAMED_CONF/chef_shell.rb
* If no NAMED_CONF is given chef-shell will load ~/.chef/chef_shell.rb if it exists
* chef-shell falls back to loading /etc/chef/client.rb or /etc/chef/solo.rb if -z or
-s options are given and no chef_shell.rb can be found.
FOOTER
option :config_file,
:short => "-c CONFIG",
:long => "--config CONFIG",
:description => "The configuration file to use"
option :help,
:short => "-h",
:long => "--help",
:description => "Show this message",
:on => :tail,
:boolean => true,
:proc => proc { print_help }
option :log_level,
:short => "-l LOG_LEVEL",
:long => '--log-level LOG_LEVEL',
:description => "Set the logging level",
:proc => proc { |level| Chef::Config.log_level = level.to_sym; Shell.setup_logger }
option :standalone,
:short => "-a",
:long => "--standalone",
:description => "standalone session",
:default => true,
:boolean => true
option :shell_solo,
:short => "-s",
:long => "--solo",
:description => "chef-solo session",
:boolean => true,
:proc => proc {Chef::Config[:solo] = true}
option :client,
:short => "-z",
:long => "--client",
:description => "chef-client session",
:boolean => true
option :json_attribs,
:short => "-j JSON_ATTRIBS",
:long => "--json-attributes JSON_ATTRIBS",
:description => "Load attributes from a JSON file or URL",
:proc => nil
option :chef_server_url,
:short => "-S CHEFSERVERURL",
:long => "--server CHEFSERVERURL",
:description => "The chef server URL",
:proc => nil
option :version,
:short => "-v",
:long => "--version",
:description => "Show chef version",
:boolean => true,
:proc => lambda {|v| puts "Chef: #{::Chef::VERSION}"},
:exit => 0
option :override_runlist,
:short => "-o RunlistItem,RunlistItem...",
:long => "--override-runlist RunlistItem,RunlistItem...",
:description => "Replace current run list with specified items",
:proc => lambda { |items| items.split(',').map { |item| Chef::RunList::RunListItem.new(item) }}
def self.print_help
instance = new
instance.parse_options([])
puts instance.opt_parser
puts
puts footer
puts
exit 1
end
def self.setup!
self.new.parse_opts
end
def parse_opts
remainder = parse_options
environment = remainder.first
# We have to nuke ARGV to make sure irb's option parser never sees it.
# otherwise, IRB complains about command line switches it doesn't recognize.
ARGV.clear
config[:config_file] = config_file_for_shell_mode(environment)
config_msg = config[:config_file] || "none (standalone session)"
puts "loading configuration: #{config_msg}"
Chef::Config.from_file(config[:config_file]) if !config[:config_file].nil? && File.exists?(config[:config_file]) && File.readable?(config[:config_file])
Chef::Config.merge!(config)
end
private
def config_file_for_shell_mode(environment)
if config[:config_file]
config[:config_file]
elsif environment && ENV['HOME']
Shell.env = environment
config_file_to_try = ::File.join(ENV['HOME'], '.chef', environment, 'chef_shell.rb')
unless ::File.exist?(config_file_to_try)
puts "could not find chef-shell config for environment #{environment} at #{config_file_to_try}"
exit 1
end
config_file_to_try
elsif ENV['HOME'] && ::File.exist?(File.join(ENV['HOME'], '.chef', 'chef_shell.rb'))
File.join(ENV['HOME'], '.chef', 'chef_shell.rb')
elsif config[:solo]
if Chef::Platform.windows?
"C:\\chef\\solo.rb"
else
"/etc/chef/solo.rb"
end
elsif config[:client]
if Chef::Platform.windows?
"C:\\chef\\client.rb"
else
"/etc/chef/client.rb"
end
else
nil
end
end
end
end
|