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, SoloLegacySession, SoloSession, StandAloneSession

Constant Summary collapse

LEADERS =
Hash.new("")

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.editor ⇒ Object



227
228
229
# File 'lib/chef/shell.rb', line 227

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

.env ⇒ Object

Returns the value of attribute env.



54
55
56
# File 'lib/chef/shell.rb', line 54

def env
  @env
end

.options ⇒ Object

Returns the value of attribute options.



53
54
55
# File 'lib/chef/shell.rb', line 53

def options
  @options
end

Class Method Details

.client_type ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/chef/shell.rb', line 213

def self.client_type
  type = Shell::StandAloneSession
  type = Shell::SoloSession         if solo_mode?
  type = Shell::SoloLegacySession   if Chef::Config[:solo_legacy_shell]
  type = Shell::ClientSession       if Chef::Config[:client]
  type = Shell::DoppelGangerSession if Chef::Config[:doppelganger]
  type
end

.configure_irb ⇒ Object



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

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

    # remove this clause for any Chef version that has upgraded to ruby >= 3.3.0
    if RUBY_VERSION >= "3.3.0"
      conf.prompt_c       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)} > "
      # prompt_n deprecation warnings as of 3.4.x
      conf.prompt_s       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)}%l> "
    else
      # there's a bug if you use a left arrow and the alternative prompts
      # ... looks like 3.1.0 < version < 3.3.0 have it, from my manual testing
      conf.prompt_c       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)} (#{Chef::VERSION})> "
      conf.prompt_n       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)}(#{Chef::VERSION})?> "
      conf.prompt_s       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)}(#{Chef::VERSION})%l> "
    end
    conf.prompt_i       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)} (#{Chef::VERSION})> "
    conf.return_format  = " => %s \n"
    conf.use_tracer     = false
    conf.instance_variable_set(:@use_multiline, false)
    conf.instance_variable_set(:@use_singleline, false)
  end
end

.fatal!(message, exit_status) ⇒ Object



208
209
210
211
# File 'lib/chef/shell.rb', line 208

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

.greeting ⇒ Object



195
196
197
198
199
# File 'lib/chef/shell.rb', line 195

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

.init(main) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef/shell.rb', line 178

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
end

.irb_conf ⇒ Object



133
134
135
# File 'lib/chef/shell.rb', line 133

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 useful for testing.



129
130
131
# File 'lib/chef/shell.rb', line 129

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

.leader(main_object) ⇒ Object



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

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

.parse_json ⇒ Object



201
202
203
204
205
206
# File 'lib/chef/shell.rb', line 201

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_opts ⇒ Object



222
223
224
225
# File 'lib/chef/shell.rb', line 222

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

.running? ⇒ Boolean

Shell assumes it's running whenever it is defined

Returns:

  • (Boolean)


123
124
125
# File 'lib/chef/shell.rb', line 123

def self.running?
  true
end

.session ⇒ Object



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

def self.session
  unless client_type.instance.node_built?
    puts "Session type: #{client_type.session_type}"
    client_type.instance.json_configuration = @json_attribs
    client_type.instance.reset!
  end
  client_type.instance
end

.setup_logger ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/chef/shell.rb', line 113

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

.solo_mode? ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/chef/shell.rb', line 109

def self.solo_mode?
  Chef::Config[:solo]
end

.start ⇒ Object

Start the irb REPL with chef-shell's customizations



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

def self.start
  setup_logger

  IRB::Command.register(
    :help,
    proc { Shell::Options.print_help }
  )

  parse_opts
  Chef::Config[:shell_config] = options.config

  if ChefUtils::Dist::Infra::SHELL == "chef-shell"
    Chef::Licensing.fetch_and_persist
    Chef::Licensing.check_software_entitlement!
  end

  # 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_conf[:USE_COLORIZE] = options.config[:use_colorize]
  irb_conf[:USE_SINGLELINE] = options.config[:use_singleline]
  irb_conf[:USE_MULTILINE] = options.config[:use_multiline]
  pp irb_conf[:USE_MULTILINE]

  irb = IRB::Irb.new

  if solo_mode?
    # Setup the mocked ChefServer
    Chef::Config.local_mode = true
    Chef::LocalMode.setup_server_connectivity
  end

  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
ensure
  # We destroy the mocked ChefServer
  Chef::LocalMode.destroy_server_connectivity if solo_mode?
end