Class: Provisioner::CLI

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/provisioner/cli.rb

Direct Known Subclasses

Bootstrap, Provision

Defined Under Namespace

Classes: Bootstrap, Provision

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.commandsObject (readonly)

Returns the value of attribute commands.



8
9
10
# File 'lib/provisioner/cli.rb', line 8

def commands
  @commands
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



91
92
93
# File 'lib/provisioner/cli.rb', line 91

def args
  @args
end

Class Method Details

.command(name) ⇒ Object

Returns a command class by it’s name



11
12
13
# File 'lib/provisioner/cli.rb', line 11

def command name
  @commands[name]
end

.inherited(subclass) ⇒ Object



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
# File 'lib/provisioner/cli.rb', line 20

def inherited subclass
  @commands ||= {}
  @commands[subclass.name.split("::").last.downcase] = subclass

  subclass.instance_eval do
    option :config_file,
           short: '-c CONFIG_FILE',
           long: '--config CONFIG_FILE',
           description: 'Path to the config file (YML)',
           required: true

    option :debug,
           short: '-g',
           long: '--debug',
           description: 'Log status to STDOUT',
           boolean: true,
           required: false,
           default: false

    option :template,
           short: '-t TEMPLATE',
           long: '--template TEMPLATE',
           description: 'Template name',
           required: true

    option :number,
           short: '-n NUMBER',
           long: '--number NUMBER',
           description: 'Ruby range or a number for the host, ie 3 or 1..3 or [2,4,6]',
           required: false

    option :ssh_user,
           short: '-l SSH_USERNAME',
           long: '--user SSH_USERNAME',
           description: 'SSH user used to connect to server (overrides yml configuration)',
           required: false

    option :dry,
           long: '--dry-run',
           description: 'Dry runs and prints all commands without executing them',
           boolean: true,
           required: false

    option :help,
           short: '-h',
           long: '--help',
           description: 'Show this message',
           on: :tail,
           boolean: true,
           show_options: true,
           exit: 0
  end

  subclass.class_eval do
    def run(argv = ARGV)
      parse_options argv
      enable_logger if config[:debug]

      if config[:dry]
        provisioner_command.shell_commands.each do |command|
          puts command
        end
      else
        provisioner_command.run
      end
    end

  end
end

.supported_commandsObject

Returns array of registered command names



16
17
18
# File 'lib/provisioner/cli.rb', line 16

def supported_commands
  commands.keys
end

Instance Method Details

#run(args = ARGV) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/provisioner/cli.rb', line 93

def run args = ARGV
  @args = args
  validate!
  command_name = args.shift
  command_class = Provisioner::CLI.command(command_name)
  Provisioner::Exit.with_message("Command '#{command_name}' not found.") if command_class.nil?
  command_class.new.run(args)
end