Module: PrimaAwsClient
- Defined in:
- lib/prima_aws_client.rb
Instance Method Summary collapse
- #appscaling_client ⇒ Object
- #artifact_exists?(bucket, path) ⇒ Boolean
- #asg_client ⇒ Object
- #cf_client ⇒ Object
- #create_stack(stack_name, stack_body, parameters = [], tags = []) ⇒ Object
- #delete_stack(stack_name) ⇒ Object
- #get_autoscaling_capacity(asg_name) ⇒ Object
- #get_spotfleet_capacity(fleet_arn) ⇒ Object
- #get_stack_outputs(name) ⇒ Object
- #get_stack_parameters(name) ⇒ Object
- #get_stack_tags(name) ⇒ Object
- #get_stack_template(name) ⇒ Object
- #list_import_stacks(export_name) ⇒ Object
- #s3_client ⇒ Object
- #stack_exists?(stack_name) ⇒ Boolean
- #stack_list ⇒ Object
- #stack_ready?(stack_name, failed_statuses = ['CREATE_FAILED', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'DELETE_COMPLETE', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS']) ⇒ Boolean
- #update_stack(stack_name, template_body, parameters = [], tags = []) ⇒ Object
- #update_stack_url(stack_name, template_url, parameters = [], tags = []) ⇒ Object
- #upload_artifact(source_path, destination_path, bucket_name_override = nil) ⇒ Object
- #wait_for_stack_ready(stack_name, failed_statuses = ['CREATE_FAILED', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'DELETE_COMPLETE', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS']) ⇒ Object
Instance Method Details
#appscaling_client ⇒ Object
17 18 19 |
# File 'lib/prima_aws_client.rb', line 17 def appscaling_client @appscaling ||= Aws::ApplicationAutoScaling::Client.new end |
#artifact_exists?(bucket, path) ⇒ Boolean
137 138 139 140 141 142 143 144 |
# File 'lib/prima_aws_client.rb', line 137 def artifact_exists?(bucket, path) resp = s3_client.list_objects( bucket: bucket, max_keys: 1, prefix: path ) !resp.contents.empty? end |
#asg_client ⇒ Object
13 14 15 |
# File 'lib/prima_aws_client.rb', line 13 def asg_client @asg ||= Aws::AutoScaling::Client.new end |
#cf_client ⇒ Object
9 10 11 |
# File 'lib/prima_aws_client.rb', line 9 def cf_client @cf ||= Aws::CloudFormation::Client.new end |
#create_stack(stack_name, stack_body, parameters = [], tags = []) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/prima_aws_client.rb', line 37 def create_stack(stack_name, stack_body, parameters = [], = []) cf_client.create_stack( stack_name: stack_name, template_body: stack_body, parameters: parameters, tags: , capabilities: ['CAPABILITY_IAM'], on_failure: 'ROLLBACK' ) output "La creazione dello stack #{stack_name} è stata avviata".green end |
#delete_stack(stack_name) ⇒ Object
86 87 88 89 |
# File 'lib/prima_aws_client.rb', line 86 def delete_stack(stack_name) cf_client.delete_stack(stack_name: stack_name) output "Stack #{stack_name} spenta con successo\n".green end |
#get_autoscaling_capacity(asg_name) ⇒ Object
171 172 173 174 |
# File 'lib/prima_aws_client.rb', line 171 def get_autoscaling_capacity(asg_name) resp = asg_client.describe_auto_scaling_groups(auto_scaling_group_names: [asg_name]) resp.auto_scaling_groups[0].desired_capacity end |
#get_spotfleet_capacity(fleet_arn) ⇒ Object
176 177 178 179 |
# File 'lib/prima_aws_client.rb', line 176 def get_spotfleet_capacity(fleet_arn) resp = appscaling_client.describe_scalable_targets(service_namespace: 'ec2', resource_ids: ["spot-fleet-request/#{fleet_arn}"]) resp.scalable_targets[0].min_capacity end |
#get_stack_outputs(name) ⇒ Object
118 119 120 121 |
# File 'lib/prima_aws_client.rb', line 118 def get_stack_outputs(name) resp = cf_client.describe_stacks(stack_name: name) resp.stacks[0].outputs end |
#get_stack_parameters(name) ⇒ Object
113 114 115 116 |
# File 'lib/prima_aws_client.rb', line 113 def get_stack_parameters(name) resp = cf_client.describe_stacks(stack_name: name) resp.stacks[0].parameters end |
#get_stack_tags(name) ⇒ Object
108 109 110 111 |
# File 'lib/prima_aws_client.rb', line 108 def (name) resp = cf_client.describe_stacks(stack_name: name) resp.stacks[0]. end |
#get_stack_template(name) ⇒ Object
123 124 125 126 |
# File 'lib/prima_aws_client.rb', line 123 def get_stack_template(name) resp = cf_client.get_template(stack_name: name) resp.template_body end |
#list_import_stacks(export_name) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/prima_aws_client.rb', line 157 def list_import_stacks(export_name) stacks = [] next_token = '' loop do print '.'.yellow; STDOUT.flush = next_token != '' ? { export_name: export_name, next_token: next_token } : {export_name: export_name} resp = cf_client.list_imports() stacks += resp.imports break unless resp.next_token next_token = resp.next_token end stacks end |
#s3_client ⇒ Object
5 6 7 |
# File 'lib/prima_aws_client.rb', line 5 def s3_client @s3 ||= Aws::S3::Client.new end |
#stack_exists?(stack_name) ⇒ Boolean
77 78 79 80 81 82 83 84 |
# File 'lib/prima_aws_client.rb', line 77 def stack_exists?(stack_name) cf_client.describe_stacks(stack_name: stack_name) rescue Aws::CloudFormation::Errors::ValidationError => e return false if e..include? 'does not exist' raise e else true end |
#stack_list ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/prima_aws_client.rb', line 21 def stack_list stacks = [] next_token = '' loop do print '.'.yellow; STDOUT.flush = next_token != '' ? { next_token: next_token } : {} resp = cf_client.describe_stacks() stacks += resp.stacks break unless resp.next_token next_token = resp.next_token end puts '.'.yellow; STDOUT.flush stacks.keep_if { |stack| stack.stack_name.include? '-qa-' } stacks end |
#stack_ready?(stack_name, failed_statuses = ['CREATE_FAILED', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'DELETE_COMPLETE', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS']) ⇒ Boolean
128 129 130 131 132 133 134 135 |
# File 'lib/prima_aws_client.rb', line 128 def stack_ready?(stack_name, failed_statuses = ['CREATE_FAILED', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'DELETE_COMPLETE', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS']) resp = cf_client.describe_stacks( stack_name: stack_name ) stack_status = resp.stacks[0].stack_status raise "The stack #{stack_name} errored out" if failed_statuses.include? stack_status ['CREATE_COMPLETE', 'UPDATE_COMPLETE', 'UPDATE_ROLLBACK_COMPLETE', 'ROLLBACK_COMPLETE'].include? stack_status end |
#update_stack(stack_name, template_body, parameters = [], tags = []) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/prima_aws_client.rb', line 49 def update_stack(stack_name, template_body, parameters = [], = []) cf_client.update_stack( stack_name: stack_name, template_body: template_body, parameters: parameters, tags: , capabilities: ['CAPABILITY_IAM'] ) rescue Aws::CloudFormation::Errors::ValidationError => e raise e else output "L'update dello stack #{stack_name} è stato avviato".green end |
#update_stack_url(stack_name, template_url, parameters = [], tags = []) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/prima_aws_client.rb', line 63 def update_stack_url(stack_name, template_url, parameters = [], = []) cf_client.update_stack( stack_name: stack_name, template_url: template_url, parameters: parameters, tags: , capabilities: ['CAPABILITY_IAM'] ) rescue Aws::CloudFormation::Errors::ValidationError => e raise e else output "L'update dello stack #{stack_name} è stato avviato".green end |
#upload_artifact(source_path, destination_path, bucket_name_override = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/prima_aws_client.rb', line 146 def upload_artifact(source_path, destination_path, bucket_name_override=nil) output "Upload dell'artifact in corso (#{(File.size(source_path).to_f / 2**20).round(2)} MiB)\n".yellow s3 = Aws::S3::Resource.new s3_bucket = if !bucket_name_override.nil? then bucket_name_override else @s3_bucket end puts s3_bucket obj = s3.bucket(s3_bucket).object(destination_path) obj.upload_file(source_path) output "#{s3_bucket}/#{destination_path} uploadato con successo!\n".green end |
#wait_for_stack_ready(stack_name, failed_statuses = ['CREATE_FAILED', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'DELETE_COMPLETE', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS']) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/prima_aws_client.rb', line 91 def wait_for_stack_ready(stack_name, failed_statuses = ['CREATE_FAILED', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'DELETE_COMPLETE', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS']) ready = false sleep_seconds = 10 output "Attendo che lo stack #{stack_name} finisca di essere inizializzato...\n".yellow while !ready ready = true if stack_ready?(stack_name, failed_statuses) seconds_elapsed = 0 loop do break if seconds_elapsed >= sleep_seconds print '.'.yellow; STDOUT.flush sleep 1 seconds_elapsed += 1 end end output "\nStack #{stack_name} pronto!\n".green end |