summaryrefslogtreecommitdiff
path: root/lib/chef/shell/ext.rb
blob: 30d841e4cdee665f4faa7a360b0a68f42305de9e (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#--
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright (c) 2009 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 'tempfile'
require 'chef/recipe'
require 'fileutils'
require 'chef/dsl/platform_introspection'
require 'chef/version'
require 'chef/shell/shell_session'
require 'chef/shell/model_wrapper'
require 'chef/shell/shell_rest'
require 'chef/json_compat'

module Shell
  module Extensions

    Help = Struct.new(:cmd, :desc, :explanation)

    # Extensions to be included in every 'main' object in chef-shell.
    # These objects are extended with this module.
    module ObjectCoreExtensions

      def ensure_session_select_defined
        # irb breaks if you prematurely define IRB::JobMangager
        # so these methods need to be defined at the latest possible time.
        unless jobs.respond_to?(:select_session_by_context)
          def jobs.select_session_by_context(&block)
            @jobs.select { |job| block.call(job[1].context.main)}
          end
        end

        unless jobs.respond_to?(:session_select)
          def jobs.select_shell_session(target_context)
            session = if target_context.kind_of?(Class)
              select_session_by_context { |main| main.kind_of?(target_context) }
            else
              select_session_by_context { |main| main.equal?(target_context) }
            end
            Array(session.first)[1]
          end
        end
      end

      def find_or_create_session_for(context_obj)
        ensure_session_select_defined
        if subsession = jobs.select_shell_session(context_obj)
          jobs.switch(subsession)
        else
          irb(context_obj)
        end
      end

      def help_banner
        banner = []
        banner << ""
        banner << "chef-shell Help"
        banner << "".ljust(80, "=")
        banner << "| " + "Command".ljust(25) + "| " + "Description"
        banner << "".ljust(80, "=")

        self.all_help_descriptions.each do |help_text|
          banner << "| " + help_text.cmd.ljust(25) + "| " + help_text.desc
        end
        banner << "".ljust(80, "=")
        banner << "\n"
        banner << "Use help(:command) to get detailed help with individual commands"
        banner << "\n"
        banner.join("\n")
      end

      def explain_command(method_name)
        help = self.all_help_descriptions.find { |h| h.cmd.to_s == method_name.to_s }
        if help
          puts ""
          puts "Command: #{method_name}"
          puts "".ljust(80, "=")
          puts help.explanation || help.desc
          puts "".ljust(80, "=")
          puts ""
        else
          puts ""
          puts "command #{method_name} not found or no help available"
          puts ""
        end
      end

      # helpfully returns +:on+ so we can have sugary syntax like `tracing on'
      def on
        :on
      end

      # returns +:off+ so you can just do `tracing off'
      def off
        :off
      end

      def help_descriptions
        @help_descriptions ||= []
      end

      def all_help_descriptions
        help_descriptions
      end

      def desc(help_text)
        @desc = help_text
      end

      def explain(explain_text)
        @explain = explain_text
      end

      def subcommands(subcommand_help={})
        @subcommand_help = subcommand_help
      end

      def singleton_method_added(mname)
        if @desc
          help_descriptions << Help.new(mname.to_s, @desc.to_s, @explain)
          @desc, @explain = nil, nil
        end
        if @subcommand_help
          @subcommand_help.each do |subcommand, text|
            help_descriptions << Help.new("#{mname}.#{subcommand}", text.to_s, nil)
          end
        end
        @subcommand_help = {}
      end

    end

    module String
      def on_off_to_bool
        case self
        when "on"
          true
        when "off"
          false
        else
          self
        end
      end
    end

    module Symbol
      def on_off_to_bool
        self.to_s.on_off_to_bool
      end
    end

    module TrueClass
      def to_on_off_str
        "on"
      end

      def on_off_to_bool
        self
      end
    end

    module FalseClass
      def to_on_off_str
        "off"
      end

      def on_off_to_bool
        self
      end
    end

    # Methods that have associated help text need to be dynamically added
    # to the main irb objects, so we define them in a proc and later
    # instance_eval the proc in the object.
    ObjectUIExtensions = Proc.new do
      extend Shell::Extensions::ObjectCoreExtensions

      desc "prints this help message"
      explain(<<-E)
## SUMMARY ##
  When called with no argument, +help+ prints a table of all
  chef-shell commands. When called with an argument COMMAND, +help+
  prints a detailed explanation of the command if available, or the
  description if no explanation is available.
E
      def help(commmand=nil)
        if commmand
          explain_command(commmand)
        else
          puts help_banner
        end
        :ucanhaz_halp
      end
      alias :halp :help

      desc "prints information about chef"
      def version
        puts  "This is the chef-shell.\n" +
              " Chef Version: #{::Chef::VERSION}\n" +
              " http://www.opscode.com/chef\n" +
              " http://wiki.opscode.com/display/chef/Home"
        :ucanhaz_automation
      end
      alias :shell :version

      desc "switch to recipe mode"
      def recipe_mode
        find_or_create_session_for Shell.session.recipe
        :recipe
      end

      desc "switch to attributes mode"
      def attributes_mode
        find_or_create_session_for Shell.session.node
        :attributes
      end

      desc "run chef using the current recipe"
      def run_chef
        Chef::Log.level = :debug
        session = Shell.session
        runrun = Chef::Runner.new(session.run_context).converge
        Chef::Log.level = :info
        runrun
      end

      desc "returns an object to control a paused chef run"
      subcommands :resume       => "resume the chef run",
                  :step         => "run only the next resource",
                  :skip_back    => "move back in the run list",
                  :skip_forward => "move forward in the run list"
      def chef_run
        Shell.session.resource_collection.iterator
      end

      desc "resets the current recipe"
      def reset
        Shell.session.reset!
      end

      desc "assume the identity of another node."
      def become_node(node_name)
        Shell::DoppelGangerSession.instance.assume_identity(node_name)
        :doppelganger
      end
      alias :doppelganger :become_node

      desc "turns printout of return values on or off"
      def echo(on_or_off)
        conf.echo = on_or_off.on_off_to_bool
      end

      desc "says if echo is on or off"
      def echo?
        puts "echo is #{conf.echo.to_on_off_str}"
      end

      desc "turns on or off tracing of execution. *verbose*"
      def tracing(on_or_off)
        conf.use_tracer = on_or_off.on_off_to_bool
        tracing?
      end
      alias :trace :tracing

      desc "says if tracing is on or off"
      def tracing?
        puts "tracing is #{conf.use_tracer.to_on_off_str}"
      end
      alias :trace? :tracing?

      desc "simple ls style command"
      def ls(directory)
        Dir.entries(directory)
      end
    end

    MainContextExtensions = Proc.new do
      desc "returns the current node (i.e., this host)"
      def node
        Shell.session.node
      end

      desc "pretty print the node's attributes"
      def ohai(key=nil)
        pp(key ? node.attribute[key] : node.attribute)
      end
    end

    RESTApiExtensions = Proc.new do
      desc "edit an object in your EDITOR"
      explain(<<-E)
## SUMMARY ##
  +edit(object)+ allows you to edit any object that can be converted to JSON.
  When finished editing, this method will return the edited object:

      new_node = edit(existing_node)

## EDITOR SELECTION ##
  chef-shell looks for an editor using the following logic
  1. Looks for an EDITOR set by Shell.editor = "EDITOR"
  2. Looks for an EDITOR configured in your chef-shell config file
  3. Uses the value of the EDITOR environment variable
E
      def edit(object)
        unless Shell.editor
          puts "Please set your editor with Shell.editor = \"vim|emacs|mate|ed\""
          return :failburger
        end

        filename = "chef-shell-edit-#{object.class.name}-"
        if object.respond_to?(:name)
          filename += object.name
        elsif object.respond_to?(:id)
          filename += object.id
        end

        edited_data = Tempfile.open([filename, ".js"]) do |tempfile|
          tempfile.sync = true
          tempfile.puts Chef::JSONCompat.to_json(object)
          system("#{Shell.editor.to_s} #{tempfile.path}")
          tempfile.rewind
          tempfile.read
        end

        Chef::JSONCompat.from_json(edited_data)
      end

      desc "Find and edit API clients"
      explain(<<-E)
## SUMMARY ##
  +clients+ allows you to query you chef server for information about your api
  clients.

## LIST ALL CLIENTS ##
  To see all clients on the system, use

      clients.all #=> [<Chef::ApiClient...>, ...]

  If the output from all is too verbose, or you're only interested in a specific
  value from each of the objects, you can give a code block to +all+:

      clients.all { |client| client.name } #=> [CLIENT1_NAME, CLIENT2_NAME, ...]

## SHOW ONE CLIENT ##
  To see a specific client, use

      clients.show(CLIENT_NAME)

## SEARCH FOR CLIENTS ##
  You can also search for clients using +find+ or +search+. You can use the
  familiar string search syntax:

      clients.search("KEY:VALUE")

  Just as the +all+ subcommand, the +search+ subcommand can use a code block to
  filter or transform the information returned from the search:

      clients.search("KEY:VALUE") { |c| c.name }

  You can also use a Hash based syntax, multiple search conditions will be
  joined with AND.

      clients.find :KEY => :VALUE, :KEY2 => :VALUE2, ...

## BULK-EDIT CLIENTS ##
                    **BE CAREFUL, THIS IS DESTRUCTIVE**
  You can bulk edit API Clients using the +transform+ subcommand, which requires
  a code block. Each client will be saved after the code block is run. If the
  code block returns +nil+ or +false+, that client will be skipped:

      clients.transform("*:*") do |client|
        if client.name =~ /borat/i
          client.admin(false)
          true
        else
          nil
        end
      end

  This will strip the admin privileges from any client named after borat.
E
      subcommands :all        => "list all api clients",
                  :show       => "load an api client by name",
                  :search     => "search for API clients",
                  :transform  => "edit all api clients via a code block and save them"
      def clients
        @clients ||= Shell::ModelWrapper.new(Chef::ApiClient, :client)
      end

      desc "Find and edit cookbooks"
      subcommands :all        => "list all cookbooks",
                  :show       => "load a cookbook by name",
                  :transform  => "edit all cookbooks via a code block and save them"
      def cookbooks
        @cookbooks ||= Shell::ModelWrapper.new(Chef::CookbookVersion)
      end

      desc "Find and edit nodes via the API"
      explain(<<-E)
## SUMMARY ##
  +nodes+ Allows you to query your chef server for information about your nodes.

## LIST ALL NODES ##
  You can list all nodes using +all+ or +list+

      nodes.all #=> [<Chef::Node...>, <Chef::Node...>, ...]

  To limit the information returned for each node, pass a code block to the +all+
  subcommand:

      nodes.all { |node| node.name } #=> [NODE1_NAME, NODE2_NAME, ...]

## SHOW ONE NODE ##
  You can show the data for a single node using the +show+ subcommand:

      nodes.show("NODE_NAME") => <Chef::Node @name="NODE_NAME" ...>

## SEARCH FOR NODES ##
  You can search for nodes using the +search+ or +find+ subcommands:

      nodes.find(:name => "app*") #=> [<Chef::Node @name="app1.example.com" ...>, ...]

  Similarly to +all+, you can pass a code block to limit or transform the
  information returned:

      nodes.find(:name => "app#") { |node| node.ec2 }

## BULK EDIT NODES ##
              **BE CAREFUL, THIS OPERATION IS DESTRUCTIVE**

  Bulk edit nodes by passing a code block to the +transform+ or +bulk_edit+
  subcommand. The block will be applied to each matching node, and then the node
  will be saved. If the block returns +nil+ or +false+, that node will be
  skipped.

      nodes.transform do |node|
        if node.fqdn =~ /.*\\.preprod\\.example\\.com/
          node.set[:environment] = "preprod"
        end
      end

  This will assign the attribute to every node with a FQDN matching the regex.
E
      subcommands :all        => "list all nodes",
                  :show       => "load a node by name",
                  :search     => "search for nodes",
                  :transform  => "edit all nodes via a code block and save them"
      def nodes
        @nodes ||= Shell::ModelWrapper.new(Chef::Node)
      end

      desc "Find and edit roles via the API"
      explain(<<-E)
## SUMMARY ##
  +roles+ allows you to query and edit roles on your Chef server.

## SUBCOMMANDS ##
  * all       (list)
  * show      (load)
  * search    (find)
  * transform (bulk_edit)

## SEE ALSO ##
  See the help for +nodes+ for more information about the subcommands.
E
      subcommands :all        => "list all roles",
                  :show       => "load a role by name",
                  :search     => "search for roles",
                  :transform  => "edit all roles via a code block and save them"
      def roles
        @roles ||= Shell::ModelWrapper.new(Chef::Role)
      end

      desc "Find and edit +databag_name+ via the api"
      explain(<<-E)
## SUMMARY ##
  +databags(DATABAG_NAME)+ allows you to query and edit data bag items on your
  Chef server. Unlike other commands for working with data on the server,
  +databags+ requires the databag name as an argument, for example:
    databags(:users).all

## SUBCOMMANDS ##
  * all       (list)
  * show      (load)
  * search    (find)
  * transform (bulk_edit)

## SEE ALSO ##
  See the help for +nodes+ for more information about the subcommands.

E
      subcommands :all        => "list all items in the data bag",
                  :show       => "load a data bag item by id",
                  :search     => "search for items in the data bag",
                  :transform  => "edit all items via a code block and save them"
      def databags(databag_name)
        @named_databags_wrappers ||= {}
        @named_databags_wrappers[databag_name] ||= Shell::NamedDataBagWrapper.new(databag_name)
      end

      desc "Find and edit environments via the API"
      explain(<<-E)
## SUMMARY ##
  +environments+ allows you to query and edit environments on your Chef server.

## SUBCOMMANDS ##
  * all       (list)
  * show      (load)
  * search    (find)
  * transform (bulk_edit)

## SEE ALSO ##
  See the help for +nodes+ for more information about the subcommands.
E
      subcommands :all        => "list all environments",
                  :show       => "load an environment by name",
                  :search     => "search for environments",
                  :transform  => "edit all environments via a code block and save them"
      def environments
        @environments ||= Shell::ModelWrapper.new(Chef::Environment)
      end

      desc "A REST Client configured to authenticate with the API"
      def api
        @rest = Shell::ShellREST.new(Chef::Config[:chef_server_url])
      end

    end

    RecipeUIExtensions = Proc.new do
      alias :original_resources :resources

      desc "list all the resources on the current recipe"
      def resources(*args)
        if args.empty?
          pp run_context.resource_collection.instance_variable_get(:@resources_by_name).keys
        else
          pp resources = original_resources(*args)
          resources
        end
      end
    end

    def self.extend_context_object(obj)
      obj.instance_eval(&ObjectUIExtensions)
      obj.instance_eval(&MainContextExtensions)
      obj.instance_eval(&RESTApiExtensions)
      obj.extend(FileUtils)
      obj.extend(Chef::DSL::PlatformIntrospection)
      obj.extend(Chef::DSL::DataQuery)
    end

    def self.extend_context_node(node_obj)
      node_obj.instance_eval(&ObjectUIExtensions)
    end

    def self.extend_context_recipe(recipe_obj)
      recipe_obj.instance_eval(&ObjectUIExtensions)
      recipe_obj.instance_eval(&RecipeUIExtensions)
    end

  end
end

class String
  include Shell::Extensions::String
end

class Symbol
  include Shell::Extensions::Symbol
end

class TrueClass
  include Shell::Extensions::TrueClass
end

class FalseClass
  include Shell::Extensions::FalseClass
end