Module: Shef

Defined in:
lib/chef/shef.rb,
lib/chef/shef/ext.rb,
lib/chef/shef/shef_rest.rb,
lib/chef/shef/shef_session.rb,
lib/chef/shef/model_wrapper.rb

Overview

Author

Daniel DeLeo (<[email protected]>)

Copyright

Copyright © 2010 Opscode, Inc.

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.

Defined Under Namespace

Modules: Extensions Classes: ClientSession, DoppelGangerClient, DoppelGangerSession, ModelWrapper, NamedDataBagWrapper, Options, ShefREST, ShefSession, 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.



40
41
42
# File 'lib/chef/shef.rb', line 40

def client_type
  @client_type
end

.editorObject



192
193
194
# File 'lib/chef/shef.rb', line 192

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

.envObject

Returns the value of attribute env.



42
43
44
# File 'lib/chef/shef.rb', line 42

def env
  @env
end

.optionsObject

Returns the value of attribute options.



41
42
43
# File 'lib/chef/shef.rb', line 41

def options
  @options
end

Class Method Details

.configure_irbObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/shef.rb', line 98

def self.configure_irb
  irb_conf[:HISTORY_FILE] = "~/.shef_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> "
  end
end

.fatal!(message, exit_status) ⇒ Object



174
175
176
177
# File 'lib/chef/shef.rb', line 174

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

.greetingObject



144
145
146
147
148
# File 'lib/chef/shef.rb', line 144

def self.greeting
  " #{Etc.getlogin}@#{Shef.session.node.fqdn}"
rescue NameError
  ""
end

.init(main) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/shef.rb', line 126

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



94
95
96
# File 'lib/chef/shef.rb', line 94

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.



90
91
92
# File 'lib/chef/shef.rb', line 90

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

.leader(main_object) ⇒ Object



113
114
115
116
# File 'lib/chef/shef.rb', line 113

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

.parse_jsonObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/chef/shef.rb', line 150

def self.parse_json
  # HACK: copied verbatim from chef/application/client, because it's not
  # reusable as written there :(
  if Chef::Config[:json_attribs]
    begin
      json_io = open(Chef::Config[:json_attribs])
    rescue SocketError => error
      fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
    rescue Errno::ENOENT => error
      fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
    rescue Errno::EACCES => error
      fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
    rescue Exception => error
      fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
    end

    begin
      @json_attribs = Chef::JSONCompat.from_json(json_io.read)
    rescue JSON::ParserError => error
      fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
    end
  end
end

.parse_optsObject



187
188
189
190
# File 'lib/chef/shef.rb', line 187

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

.running?Boolean

Shef assumes it’s running whenever it is defined

Returns:

  • (Boolean)


84
85
86
# File 'lib/chef/shef.rb', line 84

def self.running?
  true
end

.sessionObject



118
119
120
121
122
123
124
# File 'lib/chef/shef.rb', line 118

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



76
77
78
79
80
81
# File 'lib/chef/shef.rb', line 76

def self.setup_logger
  Chef::Config[:log_level] ||= :warn
  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 shef’s customizations



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

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

  # 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