Class: BBC::Cosmos::Tools::Commands::Stack
- Defined in:
- lib/bbc/cosmos/tools/commands/stack.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
Attributes inherited from Base
Instance Method Summary collapse
- #create(component = nil, main_stack = "true") ⇒ Object
- #delete(component) ⇒ Object
- #diff(component = nil) ⇒ Object
- #events(component = nil) ⇒ Object
- #generate(component) ⇒ Object
-
#initialize(args = [], local_options = {}, thor_config = {}) ⇒ Stack
constructor
A new instance of Stack.
- #list(component = nil) ⇒ Object
- #update(component = nil) ⇒ Object
Constructor Details
#initialize(args = [], local_options = {}, thor_config = {}) ⇒ Stack
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 19 def initialize(args = [], = {}, thor_config = {}) super(args, , thor_config) @l = -> data, component, environment do if data["Parameters"] && data["Parameters"]["ImageId"] data["Parameters"]["ImageId"]["Default"] = latest_ami_for(component, environment) end end @api = BBC::Cosmos::Tools::API.new(config.app["api"], [:key_path]) end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
14 15 16 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 14 def api @api end |
Instance Method Details
#create(component = nil, main_stack = "true") ⇒ Object
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 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 79 def create(component = nil, main_stack = "true") say "#{banner}\n" components(, component).each do |id| say get_key_value("\nComponent", id) type = Tools::Types::Factory.create(id, , config) data = type.generate_data stack_data = Tools::Cloudformation::Generator.component_stack_details(config.resources, , id) post_data = { "name_suffix" => [:stack], "service_stack" => !main_stack.match(/(true|t|yes|y|1)$/i).nil?, "template" => data, "parameters" => Tools::Cloudformation::Generator.parameters(data, main_stack) } post_data["aws_account_id"] = stack_data["account_id"] unless stack_data["account_id"].nil? response = api.post sprintf(config.app["stack_create_endpoint"], [:env], id), JSON.pretty_generate(post_data) if response.success? say "Stack creation for component '#{component}' successfully started", :green else api_error(response, false) end end rescue Exception => e error e. end |
#delete(component) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 198 def delete(component) say "#{banner(component)}\n" reply = yes?("Are you sure you want to delete the above stack?", :yellow) if reply || reply.nil? stack_name = "#{options[:env]}-#{component}-#{options[:stack]}" response = api.post sprintf(config.app["stack_delete"], [:env], component, stack_name), JSON.pretty_generate({}) if response.success? say "Stack delete for component '#{component}' successfully started", :green else api_error response end else error "Action cancelled" end end |
#diff(component = nil) ⇒ Object
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 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 156 def diff(component = nil) components(, component).each do |id| stack_name = "#{options[:env]}-#{id}-#{options[:stack]}" response = api.get sprintf(config.app["stack_template"], [:env], id, stack_name) ### Copy-pasted: extract this logic out type = Tools::Types::Factory.create(id, , config) data = type.generate_data.tap do |d| @l.call(d, id, [:env]) end = stack_data(component, stack_name, [:env]) if data["Parameters"] data["Parameters"] = deep_merge( stack_parameters(["parameters"], data["Parameters"]), data["Parameters"] ) params = Tools::Cloudformation::Generator.parameters(data, false) end post_data = { "template" => data, "parameters" => params || {} } ### if response.success? original_template = JSON.parse(response.body) removed, added = original_template.easy_diff(data) say JSON.pretty_generate(removed), :red say JSON.pretty_generate(added), :green else api_error(response, false) end end end |
#events(component = nil) ⇒ Object
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 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 222 def events(component = nil) say "#{banner}\n" components(, component).each do |id| say get_key_value("\nComponent", id) stack_name = "#{options[:env]}-#{id}-#{options[:stack]}" response = api.get sprintf(config.app["stack_events"], [:env], id, stack_name) if response.success? events = JSON.parse(response.body) data = events.slice(0, ([:limit])).map do |event| event.split(" ") end print_table( build_table( ["Event type", "CF type", "Name", "Status"], data ) ) else api_error(response, false) end end end |
#generate(component) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 67 def generate(component) say "#{banner(component)}\n" type = Tools::Types::Factory.create(component, , config) data = type.generate_data true say data, :white end |
#list(component = nil) ⇒ Object
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 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 33 def list(component = nil) say "#{banner}\n" components(, component).each do |id| response = api.get sprintf(config.app["stacks"], [:env], id) say get_key_value("\nComponent", id) if response.success? data = JSON.parse(response.body).map do |stack| [ stack["name"].split("-").last, stack["main_stack"], stack["updated_at"], stack["status"] ] end print_table( build_table( ["Name", "Main stack", "Updated at", "Status"], data ) ) else api_error(response, false) end end end |
#update(component = nil) ⇒ Object
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 |
# File 'lib/bbc/cosmos/tools/commands/stack.rb', line 113 def update(component = nil) say "#{banner}\n" components(, component).each do |id| say get_key_value("\nComponent", id) stack_name = "#{options[:env]}-#{id}-#{options[:stack]}" type = Tools::Types::Factory.create(id, , config) data = type.generate_data.tap do |d| @l.call(d, id, [:env]) end = stack_data(component, stack_name, [:env]) if data["Parameters"] data["Parameters"] = deep_merge( stack_parameters(["parameters"], data["Parameters"]), data["Parameters"] ) params = Tools::Cloudformation::Generator.parameters(data, false) end post_data = { "template" => data, "parameters" => params || {} } response = api.post sprintf(config.app["stack_update_endpoint"], [:env], id, stack_name), JSON.pretty_generate(post_data) if response.success? say "Stack update for component '#{id}' successfully started", :green else api_error(response, false) end end end |