Class: StackMaster::CLI

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/stack_master/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
# File 'lib/stack_master/cli.rb', line 7

def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel)
  @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
  Commander::Runner.instance_variable_set('@singleton', Commander::Runner.new(argv))
  StackMaster.stdout = @stdout
  StackMaster.stderr = @stderr
  TablePrint::Config.io = StackMaster.stdout
end

Instance Method Details

#execute!Object



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

def execute!
  program :name, 'StackMaster'
  program :version, '0.0.1'
  program :description, 'AWS Stack Management'

  global_option '-c', '--config FILE', 'Config file to use'

  command :apply do |c|
    c.syntax = 'stack_master apply [region_or_alias] [stack_name]'
    c.summary = 'Creates or updates a stack'
    c.description = "Creates or updates a stack. Shows a diff of the proposed stack's template and parameters. Tails stack events until CloudFormation has completed."
    c.example 'update a stack named myapp-vpc in us-east-1', 'stack_master apply us-east-1 myapp-vpc'
    c.action do |args, options|
      execute_stack_command(StackMaster::Commands::Apply, args, options)
    end
  end

  command :outputs do |c|
    c.syntax = 'stack_master outputs [region_or_alias] [stack_name]'
    c.summary = 'Displays outputs for a stack'
    c.description = "Displays outputs for a stack"
    c.action do |args, options|
      execute_stack_command(StackMaster::Commands::Outputs, args, options)
    end
  end

  command :init do |c|
    c.syntax = 'stack_master init [region_or_alias] [stack_name]'
    c.summary = 'Initialises the expected directory structure and stack_master.yml file'
    c.description = 'Initialises the expected directory structure and stack_master.yml file'
    c.option('--overwrite', 'Overwrite existing files')
    c.action do |args, options|
      unless args.size == 2
        say "Invalid arguments. stack_master init [region] [stack_name]"
      else
        StackMaster::Commands::Init.perform(options.overwrite, *args)
      end
    end
  end

  command :diff do |c|
    c.syntax = 'stack_master diff [region_or_alias] [stack_name]'
    c.summary = "Shows a diff of the proposed stack's template and parameters"
    c.description = "Shows a diff of the proposed stack's template and parameters"
    c.example 'diff a stack named myapp-vpc in us-east-1', 'stack_master diff us-east-1 myapp-vpc'
    c.action do |args, options|
      execute_stack_command(StackMaster::Commands::Diff, args, options)
    end
  end

  command :events do |c|
    c.syntax = 'stack_master events [region_or_alias] [stack_name]'
    c.summary = "Shows events for a stack"
    c.description = "Shows events for a stack"
    c.example 'show events for myapp-vpc in us-east-1', 'stack_master events us-east-1 myapp-vpc'
    c.option '--number Integer', Integer, 'Number of recent events to show'
    c.option '--all', 'Show all events'
    c.option '--tail', 'Tail events'
    c.action do |args, options|
      execute_stack_command(StackMaster::Commands::Events, args, options)
    end
  end

  command :resources do |c|
    c.syntax = 'stack_master resources [region] [stack_name]'
    c.summary = "Shows stack resources"
    c.description = "Shows stack resources"
    c.action do |args, options|
      execute_stack_command(StackMaster::Commands::Resources, args, options)
    end
  end

  command :list do |c|
    c.syntax = 'stack_master list'
    c.summary = 'List stack definitions'
    c.description = 'List stack definitions'
    c.action do |args, options|
      say "Invalid arguments." if args.size > 0
      config = load_config(options.config)
      StackMaster::Commands::ListStacks.perform(config)
    end
  end

  command :validate do |c|
    c.syntax = 'stack_master validate [region_or_alias] [stack_name]'
    c.summary = 'Validate a template'
    c.description = 'Validate a template'
    c.example 'validate a stack named myapp-vpc in us-east-1', 'stack_master validate us-east-1 myapp-vpc'
    c.action do |args, options|
      execute_stack_command(StackMaster::Commands::Validate, args, options)
    end
  end

  command :status do |c|
    c.syntax = 'stack_master status'
    c.summary = 'Check the current status stacks.'
    c.description = 'Checks the status of all stacks defined in the stack_master.yml file. Warning this operation can be somewhat slow.'
    c.example 'description', 'Check the status of all stack definitions'
    c.action do |args, options|
      say "Invalid arguments. stack_master status" and return unless args.size == 0
      config = load_config(options.config)
      StackMaster::Commands::Status.perform(config)
    end
  end

  command :delete do |c|
    c.syntax = 'stack_master delete [region] [stack_name]'
    c.summary = 'Delete an existing stack'
    c.description = 'Deletes a stack. The stack does not necessarily have to appear in the stack_master.yml file.'
    c.example 'description', 'Delete a stack'
    c.action do |args, options|
      unless args.size == 2
        say "Invalid arguments. stack_master delete [region] [stack_name]"
        return
      end
    # Because delete can work without a stack_master.yml
    if options.config and File.file?(options.config)
        config = load_config(options.config)
        region = Utils.underscore_to_hyphen(config.unalias_region(args[0]))
    else
 region = args[0]
    end
      StackMaster.cloud_formation_driver.set_region(region)
      StackMaster::Commands::Delete.perform(region, args[1])
    end
  end

  run!
end

#execute_stack_command(command, args, options) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/stack_master/cli.rb', line 153

def execute_stack_command(command, args, options)
  unless args.size == 2
    say "Invalid arguments. stack_master #{command.name.split('::').last.downcase} [region] [stack_name]"
    return
  end
  config = load_config(options.config)
  aliased_region, stack_name = args
  region = Utils.underscore_to_hyphen(config.unalias_region(aliased_region))
  stack_name = Utils.underscore_to_hyphen(stack_name)
  StackMaster.cloud_formation_driver.set_region(region)
  stack_definition ||= config.find_stack(region, stack_name)
  if stack_definition.nil?
    say "Could not find stack definition #{stack_name} in region #{region}"
    return
  end
  command.perform(config, stack_definition, options)
end

#load_config(file) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/stack_master/cli.rb', line 145

def load_config(file)
  stack_file = file || 'stack_master.yml'
  StackMaster::Config.load!(stack_file)
rescue Errno::ENOENT => e
  say "Failed to load config file #{stack_file}"
  exit 1
end