Class: CfnFlow::CLI
- Inherits:
-
Thor
- Object
- Thor
- CfnFlow::CLI
- Defined in:
- lib/cfn_flow/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #delete(name) ⇒ Object
- #deploy(environment) ⇒ Object
- #events(name) ⇒ Object
- #list(environment = nil) ⇒ Object
- #publish(*templates) ⇒ Object
- #show(name) ⇒ Object
- #update(environment, name) ⇒ Object
- #validate(*templates) ⇒ Object
-
#version ⇒ Object
Version command.
Class Method Details
.exit_on_failure? ⇒ Boolean
4 5 6 |
# File 'lib/cfn_flow/cli.rb', line 4 def self.exit_on_failure? CfnFlow.exit_on_failure? end |
Instance Method Details
#delete(name) ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/cfn_flow/cli.rb', line 194 def delete(name) stack = find_stack_in_service(name) if [:force] || yes?("Are you sure you want to shut down #{name}?", :red) stack.delete say "Deleted stack #{name}" say "Polling for events..." invoke :events, [stack.name], ['--poll'] end end |
#deploy(environment) ⇒ Object
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 |
# File 'lib/cfn_flow/cli.rb', line 54 def deploy(environment) # Export environment as an env var so it can be interpolated in config ENV['CFN_FLOW_ENVIRONMENT'] = environment begin params = CfnFlow.stack_params(environment) stack = CfnFlow.cfn_resource.create_stack(params) rescue Aws::CloudFormation::Errors::ValidationError => e raise Thor::Error.new(e.) end say "Launching stack #{stack.name}" # Invoke events say "Polling for events..." invoke :events, [stack.name], ['--poll'] say "Stack Outputs:" invoke :show, [stack.name], ['--format=outputs-table'] # Optionally cleanup other stacks in this environment if [:cleanup] puts "Finding stacks to clean up" list_stacks_in_service.select {|s| s.name != stack.name && \ s..any? {|tag| tag.key == 'CfnFlowEnvironment' && tag.value == environment } }.map(&:name).each do |name| delete(name) end end end |
#events(name) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cfn_flow/cli.rb', line 171 def events(name) stack = find_stack_in_service(name) say EventPresenter.header unless ['no-header'] EventPresenter.present(stack.events) {|p| say p } if [:poll] # Display events until we're COMPLETE/FAILED delay = (ENV['CFN_FLOW_EVENT_POLL_INTERVAL'] || 2).to_i begin stack.wait_until(max_attempts: -1, delay: delay) do |s| EventPresenter.present(s.events) {|p| say p } # Wait until the stack status ends with _FAILED or _COMPLETE s.stack_status.match(/_(FAILED|COMPLETE)$/) end rescue Aws::CloudFormation::Errors::ValidationError # The stack was deleted. Keep on trucking. end end end |
#list(environment = nil) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cfn_flow/cli.rb', line 123 def list(environment=nil) stacks = list_stacks_in_service if environment stacks.select! do |stack| stack..any? {|tag| tag.key == 'CfnFlowEnvironment' && tag.value == environment } end end return if stacks.empty? table_header = ['no-header'] ? [] : [['NAME', 'ENVIRONMENT', 'STATUS', 'CREATED']] table_data = stacks.map do |s| env_tag = s..detect {|tag| tag.key == 'CfnFlowEnvironment'} env = env_tag ? env_tag.value : 'NONE' [ s.name, env, s.stack_status, s.creation_time ] end print_table(table_header + table_data) end |
#publish(*templates) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cfn_flow/cli.rb', line 33 def publish(*templates) if templates.empty? raise Thor::RequiredArgumentMissingError.new('You must specify a template to publish') end validate(*templates) release = publish_release templates.each do |path| t = Template.new(path) say "Publishing #{t.local_path} to #{t.url(release)}" t.upload(release) end end |
#show(name) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cfn_flow/cli.rb', line 146 def show(name) formatters = { 'json' => ->(stack) { say MultiJson.dump(stack.data.to_hash, pretty: true) }, 'yaml' => ->(stack) { say stack.data.to_hash.to_yaml }, 'outputs-table' => ->(stack) do outputs = stack.outputs.to_a if outputs.any? table_header = [['KEY', 'VALUE', 'DESCRIPTION']] table_data = outputs.map do |s| [ s.output_key, s.output_value, s.description ] end print_table(table_header + table_data) else say "No stack outputs to show." end end } stack = find_stack_in_service(name) formatters[[:format]].call(stack) end |
#update(environment, name) ⇒ Object
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 |
# File 'lib/cfn_flow/cli.rb', line 87 def update(environment, name) # Export environment as an env var so it can be interpolated in config ENV['CFN_FLOW_ENVIRONMENT'] = environment stack = find_stack_in_service(name) # Check that environment matches unless stack..any?{|tag| tag.key == 'CfnFlowEnvironment' && tag.value == environment } raise Thor::Error.new "Stack #{name} is not tagged for environment #{environment}" end begin params = CfnFlow.stack_params(environment) params.delete(:tags) # No allowed for Stack#update stack.update(params) rescue Aws::CloudFormation::Errors::ValidationError => e raise Thor::Error.new(e.) end say "Updating stack #{stack.name}" # NB: there's a potential race condition where polling for events would # see the last complete state before the stack has a chance to begin updating. # Consider putting a sleep, wait_for an UPDATE_IN_PROGRESS state beforehand, # or look for events newer than the last event before updating. # Invoke events say "Polling for events..." invoke :events, [stack.name], ['--poll'] say "Stack Outputs:" invoke :show, [stack.name], ['--format=outputs-table'] end |
#validate(*templates) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cfn_flow/cli.rb', line 12 def validate(*templates) if templates.empty? raise Thor::RequiredArgumentMissingError.new('You must specify a template to validate') end templates.map{|path| Template.new(path) }.each do |template| say "Validating #{template.local_path}... " template.validate! say 'valid.', :green end rescue Aws::CloudFormation::Errors::ValidationError => e raise Thor::Error.new("Invalid template. Message: #{e.}") rescue CfnFlow::Template::Error => e raise Thor::Error.new("Error loading template. (#{e.class}) Message: #{e.}") end |
#version ⇒ Object
Version command
207 |
# File 'lib/cfn_flow/cli.rb', line 207 desc "version", "Prints the version information" |