Module: Shell

Defined in:
lib/chef/shell.rb,
lib/chef/shell/ext.rb,
lib/chef/shell/model_wrapper.rb,
lib/chef/shell/shell_session.rb

Overview

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.

Defined Under Namespace

Modules: Extensions Classes: ClientSession, DoppelGangerClient, DoppelGangerSession, ModelWrapper, NamedDataBagWrapper, Options, ShellSession, SoloSession, StandAloneSession

Constant Summary collapse

LEADERS =
Hash.new("")

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.client_typeObject

Returns the value of attribute client_type.



43
44
45
# File 'lib/chef/shell.rb', line 43

def client_type
  @client_type
end

.editorObject



181
182
183
# File 'lib/chef/shell.rb', line 181

def self.editor
  @editor || Chef::Config[:editor] || ENV["EDITOR"]
end

.envObject

Returns the value of attribute env.



45
46
47
# File 'lib/chef/shell.rb', line 45

def env
  @env
end

.optionsObject

Returns the value of attribute options.



44
45
46
# File 'lib/chef/shell.rb', line 44

def options
  @options
end

Class Method Details

.configure_irbObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/shell.rb', line 103

def self.configure_irb
  irb_conf[:HISTORY_FILE] = Chef::Util::PathHelper.home(".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)} (#{Chef::VERSION})> "
    conf.prompt_n       = "chef#{leader(m)} ?> "
    conf.prompt_s       = "chef#{leader(m)}%l> "
    conf.use_tracer     = false
  end
end

.fatal!(message, exit_status) ⇒ Object



163
164
165
166
# File 'lib/chef/shell.rb', line 163

def self.fatal!(message, exit_status)
  Chef::Log.fatal(message)
  exit exit_status
end

.greetingObject



150
151
152
153
154
# File 'lib/chef/shell.rb', line 150

def self.greeting
  " #{Etc.getlogin}@#{Shell.session.node["fqdn"]}"
rescue NameError, ArgumentError
  ""
end

.init(main) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chef/shell.rb', line 132

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

.irb_confObject



99
100
101
# File 'lib/chef/shell.rb', line 99

def self.irb_conf
  @irb_conf || IRB.conf
end

.irb_conf=(conf_hash) ⇒ Object

Set the irb_conf object to something other than IRB.conf usful for testing.



95
96
97
# File 'lib/chef/shell.rb', line 95

def self.irb_conf=(conf_hash)
  @irb_conf = conf_hash
end

.leader(main_object) ⇒ Object



119
120
121
122
# File 'lib/chef/shell.rb', line 119

def self.leader(main_object)
  env_string = Shell.env ? " (#{Shell.env})" : ""
  LEADERS[main_object.class] + env_string
end

.parse_jsonObject



156
157
158
159
160
161
# File 'lib/chef/shell.rb', line 156

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

.parse_optsObject



176
177
178
179
# File 'lib/chef/shell.rb', line 176

def self.parse_opts
  @options = Options.new
  @options.parse_opts
end

.running?Boolean

Shell assumes it’s running whenever it is defined

Returns:

  • (Boolean)


89
90
91
# File 'lib/chef/shell.rb', line 89

def self.running?
  true
end

.sessionObject



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

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

.setup_loggerObject



79
80
81
82
83
84
85
86
# File 'lib/chef/shell.rb', line 79

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

.startObject

Start the irb REPL with chef-shell’s customizations



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
# File 'lib/chef/shell.rb', line 50

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