Class: Kitchen::Driver::Cloudformation
- Inherits:
-
Base
- Object
- Base
- Kitchen::Driver::Cloudformation
- Defined in:
- lib/kitchen/driver/cloudformation.rb
Overview
Amazon Cloudformation driver for Test Kitchen.
Instance Method Summary collapse
-
#cf ⇒ Object
rubocop:enable Lint/RescueWithoutErrorClass.
- #change_set_exists ⇒ Object
-
#copy_deprecated_configs(state) ⇒ Object
This copies transport config from the current config object into the state.
-
#create(state) ⇒ Object
rubocop:disable Lint/RescueWithoutErrorClass.
- #create_change_set ⇒ Object
- #create_stack ⇒ Object
- #debug_stack_events(stack_name) ⇒ Object
- #destroy(state) ⇒ Object
- #display_stack_events(stack_name) ⇒ Object
- #execute_change_set ⇒ Object
- #stack_exists ⇒ Object
- #stack_generator ⇒ Object
- #update(state) ⇒ Object
- #update_stack ⇒ Object
Instance Method Details
#cf ⇒ Object
rubocop:enable Lint/RescueWithoutErrorClass
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/kitchen/driver/cloudformation.rb', line 179 def cf @cf ||= Aws::CfClient.new( config[:region], config[:shared_credentials_profile], config[:aws_access_key_id], config[:aws_secret_access_key], config[:aws_session_token], config[:http_proxy], config[:retry_limit], config[:ssl_verify_peer] ) end |
#change_set_exists ⇒ Object
225 226 227 228 229 230 231 232 233 |
# File 'lib/kitchen/driver/cloudformation.rb', line 225 def change_set_exists s = cf.describe_change_set(config[:stack_name], config[:change_set_name]) if s.nil? || s.status == 'DELETE_COMPLETE' false else info("Change Set #{s[:change_set_name]} already exists so not creating") true end end |
#copy_deprecated_configs(state) ⇒ Object
This copies transport config from the current config object into the state. This relies on logic in the transport that merges the transport config with the current state object, so its a bad coupling. But we can get rid of this when we get rid of these deprecated configs!
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/kitchen/driver/cloudformation.rb', line 200 def copy_deprecated_configs(state) state[:connection_timeout] = config[:ssh_timeout] if config[:ssh_timeout] state[:connection_retries] = config[:ssh_retries] if config[:ssh_retries] state[:username] = config[:username] if config[:username] # elsif instance.transport[:username] == instance.transport.class.defaults[:username] # If the transport has the default username, copy it from amis.json # This duplicated old behavior but I hate amis.json # ami_username = amis["usernames"][instance.platform.name] # state[:username] = ami_username if ami_username # end state[:ssh_key] = config[:ssh_key] if config[:ssh_key] end |
#create(state) ⇒ Object
rubocop:disable Lint/RescueWithoutErrorClass
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 |
# File 'lib/kitchen/driver/cloudformation.rb', line 63 def create(state) copy_deprecated_configs(state) # return if state[:stack_name] info(Kitchen::Util.outdent!(<<-TEXT)) Creating CloudFormation Stack <#{config[:stack_name]}>... If you are not using an account that qualifies under the AWS free-tier, you may be charged to run these suites. The charge should be minimal, but neither Test Kitchen nor its maintainers are responsible for your incurred costs. TEXT unless stack_exists begin stack = create_stack rescue error("CloudFormation #{$ERROR_INFO}.") # e.message return end state[:stack_name] = stack.stack_name unless state[:stack_name] info("Stack <#{state[:stack_name]}> requested.") # tag_stack(stack) s = cf.get_stack(state[:stack_name]) while s.stack_status == 'CREATE_IN_PROGRESS' debug_stack_events(state[:stack_name]) info("CloudFormation waiting for stack <#{state[:stack_name]}> to be created.....") sleep(30) s = cf.get_stack(state[:stack_name]) end display_stack_events(state[:stack_name]) if s.stack_status == 'CREATE_COMPLETE' outputs = Hash[*s.outputs.map do |o| [o[:output_key], o[:output_value]] end.flatten] state[:hostname] = config[:hostname].gsub(/\${([^}]+)}/) { outputs[Regexp.last_match(1)] || '' } if config[:hostname] info("CloudFormation stack <#{state[:stack_name]}> created.") else error("CloudFormation stack <#{stack.stack_name}> failed to create....attempting to delete") destroy(state) end end return if change_set_exists begin create_change_set rescue error("CloudFormation #{$ERROR_INFO}.") # e.message return end state[:stack_name] = config[:stack_name] unless state[:stack_name] end |
#create_change_set ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/kitchen/driver/cloudformation.rb', line 235 def create_change_set stack_data = stack_generator.cf_stack_data stack_data[:change_set_name] = config[:change_set_name] if config[:change_set_name] stack_data[:change_set_type] = config[:change_set_type] if config[:change_set_type] # accepts CREATE, UPDATE info("Creating Change Set for CloudFormation Stack #{stack_data[:stack_name]}") stack_data[:template_url] = config[:change_set_template_url] if config[:change_set_template_file] if config[:change_set_template_file] stack_data[:template_body] = File.open(config[:change_set_template_file], 'rb') { |file| file.read } end cf.create_change_set(stack_data) end |
#create_stack ⇒ Object
213 214 215 216 217 |
# File 'lib/kitchen/driver/cloudformation.rb', line 213 def create_stack stack_data = stack_generator.cf_stack_data info("Creating CloudFormation Stack #{stack_data[:stack_name]}") cf.create_stack(stack_data) end |
#debug_stack_events(stack_name) ⇒ Object
261 262 263 264 265 266 267 |
# File 'lib/kitchen/driver/cloudformation.rb', line 261 def debug_stack_events(stack_name) return unless logger.debug? response = cf.get_stack_events(stack_name) response[:stack_events].each do |r| debug("#{r[:timestamp]} #{r[:resource_type]} #{r[:logical_resource_id]} #{r[:resource_status]} #{r[:resource_status_reason]}") end end |
#destroy(state) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/kitchen/driver/cloudformation.rb', line 154 def destroy(state) stack = cf.get_stack(state[:stack_name]) if stack.nil? state.delete(:stack_name) else cf.delete_stack(state[:stack_name]) begin stack = cf.get_stack(state[:stack_name]) while stack.stack_status == 'DELETE_IN_PROGRESS' debug_stack_events(state[:stack_name]) info("CloudFormation waiting for stack <#{state[:stack_name]}> to be deleted.....") sleep(30) stack = cf.get_stack(state[:stack_name]) end rescue info("CloudFormation stack <#{state[:stack_name]}> deleted.") state.delete(:stack_name) return end display_stack_events(state[:stack_name]) error("CloudFormation stack <#{stack.stack_name}> failed to deleted.") end end |
#display_stack_events(stack_name) ⇒ Object
269 270 271 272 273 274 |
# File 'lib/kitchen/driver/cloudformation.rb', line 269 def display_stack_events(stack_name) response = cf.get_stack_events(stack_name) response[:stack_events].each do |r| info("#{r[:timestamp]} #{r[:resource_type]} #{r[:logical_resource_id]} #{r[:resource_status]} #{r[:resource_status_reason]}") end end |
#execute_change_set ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/kitchen/driver/cloudformation.rb', line 247 def execute_change_set stack_data = {} stack_data[:stack_name] = config[:stack_name] stack_data[:change_set_name] = config[:change_set_name] info("Execute Change Set #{stack_data[:change_set_name]} for CloudFormation Stack #{stack_data[:stack_name]}") cf.execute_change_set(stack_data) end |
#stack_exists ⇒ Object
219 220 221 222 223 |
# File 'lib/kitchen/driver/cloudformation.rb', line 219 def stack_exists s = cf.get_stack(config[:stack_name]) info("Stack #{s.stack_name} already exists so not creating") if s.exists? s.exists? end |
#stack_generator ⇒ Object
192 193 194 |
# File 'lib/kitchen/driver/cloudformation.rb', line 192 def stack_generator @stack_generator ||= Aws::StackGenerator.new(config, cf) end |
#update(state) ⇒ Object
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 |
# File 'lib/kitchen/driver/cloudformation.rb', line 114 def update(state) info("Stack <#{state[:stack_name]}> Execute Change Set requested to update stack.") stack = cf.get_stack(state[:stack_name]) if stack.nil? info("CloudFormation stack <#{state[:stack_name]}> doesn't exist.") return end unless change_set_exists info("CloudFormation change set <#{config[:change_set_name]}> doesn't exist for stack <#{state[:stack_name]}>.") return end begin stack = execute_change_set rescue error("CloudFormation #{$ERROR_INFO}.") # e.message return end state[:stack_name] = stack.stack_name unless state[:stack_name] info("Stack <#{state[:stack_name]}> requested.") # tag_stack(stack) s = cf.get_stack(state[:stack_name]) while s.stack_status == 'UPDATE_IN_PROGRESS' debug_stack_events(state[:stack_name]) info("CloudFormation waiting for stack <#{state[:stack_name]}> to be updated.....") sleep(20) s = cf.get_stack(state[:stack_name]) end display_stack_events(state[:stack_name]) if s.stack_status == 'UPDATE_COMPLETE' outputs = Hash[*s.outputs.map do |o| [o[:output_key], o[:output_value]] end.flatten] state[:hostname] = config[:hostname].gsub(/\${([^}]+)}/) { outputs[Regexp.last_match(1)] || '' } if config[:hostname] info("CloudFormation stack <#{state[:stack_name]}> updated.") else error("CloudFormation stack <#{stack.stack_name}> failed to update.") end end |
#update_stack ⇒ Object
255 256 257 258 259 |
# File 'lib/kitchen/driver/cloudformation.rb', line 255 def update_stack stack_data = stack_generator.cf_stack_data info("Updating CloudFormation Stack #{stack_data[:stack_name]}") cf.update_stack(stack_data) end |