summaryrefslogtreecommitdiff
path: root/lib/chef/shell.rb
blob: fbee4c093b9099215ea44de0af14fbf012a6ca77 (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright 2009-2016, 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" unless defined?(Singleton)
require "pp" unless defined?(PP)
require "etc" unless defined?(Etc)
require "mixlib/cli" unless defined?(Mixlib::CLI)

require_relative "../chef"
require_relative "version"
require_relative "client"
require_relative "config"
require_relative "config_fetcher"
require_relative "dist"

require_relative "shell/shell_session"
require_relative "workstation_config_loader"
require_relative "shell/ext"
require_relative "json_compat"
require_relative "util/path_helper"

# = 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 :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_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

  def self.solo_mode?
    Chef::Config[:solo]
  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
  # useful 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::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::Dist::EXEC}#{leader(m)} > "
      conf.return_format  = " => %s \n"
      conf.prompt_i       = "#{Chef::Dist::EXEC}#{leader(m)} (#{Chef::VERSION})> "
      conf.prompt_n       = "#{Chef::Dist::EXEC}#{leader(m)} ?> "
      conf.prompt_s       = "#{Chef::Dist::EXEC}#{leader(m)}%l> "
      conf.use_tracer     = false
      conf.instance_variable_set(:@use_multiline, false)
      conf.instance_variable_set(:@use_singleline, 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.json_configuration = @json_attribs
      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
  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 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

  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::Dist::SHELL} #{Chef::VERSION}\n\nUsage: #{Chef::Dist::SHELL} [NAMED_CONF] (OPTIONS)")

    footer(<<~FOOTER)
      When no CONFIG is specified, #{Chef::Dist::SHELL} attempts to load a default configuration file:
      * If a NAMED_CONF is given, #{Chef::Dist::SHELL} will load ~/#{Chef::Dist::USER_CONF_DIR}/NAMED_CONF/#{Chef::Dist::SHELL_CONF}
      * If no NAMED_CONF is given #{Chef::Dist::SHELL} will load ~/#{Chef::Dist::USER_CONF_DIR}/#{Chef::Dist::SHELL_CONF} if it exists
      * If no #{Chef::Dist::SHELL_CONF} can be found, #{Chef::Dist::SHELL} falls back to load:
            #{Chef::Dist::CONF_DIR}/client.rb if -z option is given.
            #{Chef::Dist::CONF_DIR}/solo.rb   if --solo-legacy-mode option is given.
            #{Chef::Dist::USER_CONF_DIR}/config.rb     if -s option is given.
            #{Chef::Dist::USER_CONF_DIR}/knife.rb      if -s option is given.
    FOOTER

    option :use_multiline,
      long: "--[no-]multiline",
      default: true,
      description: "[Do not] use multiline editor module"

    option :use_singleline,
      long: "--[no-]singleline",
      default: true,
      description: "[Do not] use singleline editor module"

    option :use_colorize,
      long: "--[no-]colorize",
      default: true,
      description: "[Do not] use colorization"

    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 :solo_shell,
      short: "-s",
      long: "--solo",
      description: "#{Chef::Dist::SOLO} session",
      boolean: true,
      proc: proc { Chef::Config[:solo] = true }

    option :client,
      short: "-z",
      long: "--client",
      description: "#{Chef::Dist::PRODUCT} session",
      boolean: true

    option :solo_legacy_shell,
      long: "--solo-legacy-mode",
      description: "#{Chef::Dist::SOLO} legacy session",
      boolean: true,
      proc: proc { Chef::Config[:solo_legacy_mode] = 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::Dist::SERVER_PRODUCT} URL",
      proc: nil

    option :version,
      short: "-v",
      long: "--version",
      description: "Show #{Chef::Dist::PRODUCT} version",
      boolean: true,
      proc: lambda { |v| puts "#{Chef::Dist::PRODUCT}: #{::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) } }

    option :skip_cookbook_sync,
      long: "--[no-]skip-cookbook-sync",
      description: "Use cached cookbooks without overwriting local differences from the server",
      boolean: false

    def self.print_help
      instance = new
      instance.parse_options([])
      puts instance.opt_parser
      puts
      puts footer
      puts
      exit 1
    end

    def self.setup!
      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)
      dot_chef_dir = Chef::Util::PathHelper.home(".chef")
      if config[:config_file]
        config[:config_file]
      elsif environment
        Shell.env = environment
        config_file_to_try = ::File.join(dot_chef_dir, environment, Chef::Dist::SHELL_CONF)
        unless ::File.exist?(config_file_to_try)
          puts "could not find #{Chef::Dist::SHELL} config for environment #{environment} at #{config_file_to_try}"
          exit 1
        end
        config_file_to_try
      elsif dot_chef_dir && ::File.exist?(File.join(dot_chef_dir, Chef::Dist::SHELL_CONF))
        File.join(dot_chef_dir, Chef::Dist::SHELL_CONF)
      elsif config[:solo_legacy_shell]
        Chef::Config.platform_specific_path("#{Chef::Dist::CONF_DIR}/solo.rb")
      elsif config[:client]
        Chef::Config.platform_specific_path("#{Chef::Dist::CONF_DIR}/client.rb")
      elsif config[:solo_shell]
        Chef::WorkstationConfigLoader.new(nil, Chef::Log).config_location
      else
        nil
      end
    end

  end

end