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.



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

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



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

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

  global_option '-c', '--config FILE', String, 'Config file to use'
  global_option '--changed', 'filter stack selection to only ones that have changed'
  global_option '-y', '--yes', 'Run in non-interactive mode answering yes to any prompts' do
    StackMaster.non_interactive!
    StackMaster.non_interactive_answer = 'y'
  end
  global_option '-n', '--no', 'Run in non-interactive mode answering no to any prompts' do
    StackMaster.non_interactive!
    StackMaster.non_interactive_answer = 'n'
  end
  global_option '-d', '--debug', 'Run in debug mode' do
    StackMaster.debug!
  end
  global_option '-q', '--quiet', 'Do not output the resulting Stack Events, just return immediately' do
    StackMaster.quiet!
  end
  global_option '--skip-account-check', 'Do not check if command is allowed to execute in account' do
    StackMaster.
  end

  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.option '--on-failure ACTION', String, "Action to take on CREATE_FAILURE. Valid Values: [ DO_NOTHING | ROLLBACK | DELETE ]. Default: ROLLBACK\nNote: You cannot use this option with Serverless Application Model (SAM) templates."
    c.option '--yes-param PARAM_NAME', String, "Auto-approve stack updates when only parameter PARAM_NAME changes"
    c.action do |args, options|
      options.defaults config: default_config_file
      execute_stacks_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|
      options.defaults config: default_config_file
      execute_stacks_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|
      options.defaults config: default_config_file
      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|
      options.defaults config: default_config_file
      execute_stacks_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|
      options.defaults config: default_config_file
      execute_stacks_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|
      options.defaults config: default_config_file
      execute_stacks_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|
      options.defaults config: default_config_file
      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|
      options.defaults config: default_config_file
      execute_stacks_command(StackMaster::Commands::Validate, args, options)
    end
  end

  command :lint do |c|
    c.syntax = 'stack_master lint [region_or_alias] [stack_name]'
    c.summary = "Check the stack definition locally"
    c.description = "Runs cfn-lint on the template which would be sent to AWS on apply"
    c.example 'run cfn-lint on stack myapp-vpc with us-east-1 settings', 'stack_master lint us-east-1 myapp-vpc'
    c.action do |args, options|
      options.defaults config: default_config_file
      execute_stacks_command(StackMaster::Commands::Lint, args, options)
    end
  end

  command :compile do |c|
    c.syntax = 'stack_master compile [region_or_alias] [stack_name]'
    c.summary = "Print the compiled version of a given stack"
    c.description = "Processes the stack and prints out a compiled version - same we'd send to AWS"
    c.example 'print compiled stack myapp-vpc with us-east-1 settings', 'stack_master compile us-east-1 myapp-vpc'
    c.action do |args, options|
      options.defaults config: default_config_file
      execute_stacks_command(StackMaster::Commands::Compile, 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|
      options.defaults config: default_config_file
      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|
      options.default config: default_config_file
      unless args.size == 2
        say "Invalid arguments. stack_master delete [region] [stack_name]"
        return
      end

      stack_name = Utils.underscore_to_hyphen(args[1])
      allowed_accounts = []

      # 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]))
        allowed_accounts = config.find_stack(region, stack_name)&.allowed_accounts
      else
        region = args[0]
      end

      (allowed_accounts) do
        StackMaster.cloud_formation_driver.set_region(region)
        StackMaster::Commands::Delete.perform(region, stack_name)
      end
    end
  end

  run!
end