module Shell

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.

Constants

LEADERS

Attributes

editor[W]
env[RW]
options[RW]

Public Class Methods

client_type() click to toggle source
# File lib/chef/shell.rb, line 195
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() click to toggle source
# File lib/chef/shell.rb, line 128
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       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)} > "
    conf.return_format  = " => %s \n"
    conf.prompt_i       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)} (#{Chef::VERSION})> "
    conf.prompt_n       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)} ?> "
    conf.prompt_s       = "#{ChefUtils::Dist::Infra::EXEC}#{leader(m)}%l> "
    conf.use_tracer     = false
    conf.instance_variable_set(:@use_multiline, false)
    conf.instance_variable_set(:@use_singleline, false)
  end
end
editor() click to toggle source
# File lib/chef/shell.rb, line 209
def self.editor
  @editor || Chef::Config[:editor] || ENV["EDITOR"]
end
fatal!(message, exit_status) click to toggle source
# File lib/chef/shell.rb, line 190
def self.fatal!(message, exit_status)
  Chef::Log.fatal(message)
  exit exit_status
end
greeting() click to toggle source
# File lib/chef/shell.rb, line 177
def self.greeting
  "#{Etc.getlogin}@#{Shell.session.node["fqdn"]}"
rescue NameError, ArgumentError
  ""
end
init(main) click to toggle source
# File lib/chef/shell.rb, line 160
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() click to toggle source
# File lib/chef/shell.rb, line 124
def self.irb_conf
  @irb_conf || IRB.conf
end
irb_conf=(conf_hash) click to toggle source

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

# File lib/chef/shell.rb, line 120
def self.irb_conf=(conf_hash)
  @irb_conf = conf_hash
end
leader(main_object) click to toggle source
# File lib/chef/shell.rb, line 146
def self.leader(main_object)
  env_string = Shell.env ? " (#{Shell.env})" : ""
  LEADERS[main_object.class] + env_string
end
parse_json() click to toggle source
# File lib/chef/shell.rb, line 183
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() click to toggle source
# File lib/chef/shell.rb, line 204
def self.parse_opts
  @options = Options.new
  @options.parse_opts
end
running?() click to toggle source

Shell assumes it's running whenever it is defined

# File lib/chef/shell.rb, line 114
def self.running?
  true
end
session() click to toggle source
# File lib/chef/shell.rb, line 151
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() click to toggle source
# File lib/chef/shell.rb, line 104
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?() click to toggle source
# File lib/chef/shell.rb, line 100
def self.solo_mode?
  Chef::Config[:solo]
end
start() click to toggle source

Start the irb REPL with chef-shell's customizations

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