Module: PrimaAwsClient

Defined in:
lib/prima_aws_client.rb

Instance Method Summary collapse

Instance Method Details

#asg_clientObject



9
10
11
# File 'lib/prima_aws_client.rb', line 9

def asg_client
  @asg ||= Aws::AutoScaling::Client.new
end

#cf_clientObject



5
6
7
# File 'lib/prima_aws_client.rb', line 5

def cf_client
  @cf ||= Aws::CloudFormation::Client.new
end

#describe_auto_scaling_groups(auto_scaling_group_names, max_records) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/prima_aws_client.rb', line 136

def describe_auto_scaling_groups(auto_scaling_group_names, max_records)
  resp = asg_client.describe_auto_scaling_groups({
                                                   auto_scaling_group_names: auto_scaling_group_names,
                                                   max_records: max_records
                                                 })
rescue Aws::CloudFormation::Errors::Throttling => e
  output 'Throttling, retrying in 15 seconds'.red
  sleep 15
  resp = describe_auto_scaling_groups(auto_scaling_group_names, max_records)
end

#get_autoscaling_capacity(asg_name) ⇒ Object



147
148
149
150
# File 'lib/prima_aws_client.rb', line 147

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_stack_outputs(name) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/prima_aws_client.rb', line 111

def get_stack_outputs(name)
  resp = cf_client.describe_stacks(stack_name: name)
rescue Aws::CloudFormation::Errors::Throttling => e
  output 'Throttling, retrying in 15 seconds'.red
  sleep 15
  get_stack_outputs(name)
else
  resp.stacks[0].outputs
end

#get_stack_parameters(name) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/prima_aws_client.rb', line 101

def get_stack_parameters(name)
  resp = cf_client.describe_stacks(stack_name: name)
rescue Aws::CloudFormation::Errors::Throttling => e
  output 'Throttling, retrying in 15 seconds'.red
  sleep 15
  get_stack_parameters(name)
else
  resp.stacks[0].parameters
end

#list_exportsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prima_aws_client.rb', line 13

def list_exports
  exports = []
  next_token = ''
  loop do
    print '.'.yellow; STDOUT.flush
    options = next_token != '' ? { next_token: next_token } : {}
    begin
      resp = cf_client.list_exports(options)
    rescue Aws::CloudFormation::Errors::Throttling => e
      output 'Throttling, retrying in 15 seconds'.red
      sleep 15
      resp = cf_client.list_exports(options)
    end
    exports += resp.exports
    break unless resp.next_token

    next_token = resp.next_token
  end
  puts '.'.yellow; STDOUT.flush
  exports
end

#stack_ready?(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS])) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/prima_aws_client.rb', line 121

def stack_ready?(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS])
  begin
    resp = cf_client.describe_stacks(
      stack_name: stack_name
    )
    stack_status = resp.stacks[0].stack_status
  rescue Aws::CloudFormation::Errors::Throttling => e
    print 'Throttling'.red; STDOUT.flush
    return false
  end
  raise "The stack #{stack_name} errored out" if failed_statuses.include? stack_status

  %w[CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE ROLLBACK_COMPLETE].include? stack_status
end

#update_stack(stack_name, template_body, parameters = [], tags = [], role = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/prima_aws_client.rb', line 35

def update_stack(stack_name, template_body, parameters = [], tags = [], role = nil)
  cf_args = {
    stack_name: stack_name,
    template_body: template_body,
    parameters: parameters,
    tags: tags,
    capabilities: ['CAPABILITY_IAM']
  }

  cf_args.merge!(role_arn: role) unless role.nil?

  begin
    cf_client.update_stack(cf_args)
  rescue Aws::CloudFormation::Errors::Throttling => e
    output 'Throttling, retrying in 15 seconds'.red
    sleep 15
    update_stack(stack_name, template_body, parameters = [], tags = [])
  rescue Aws::CloudFormation::Errors::ValidationError => e
    raise e
  else
    output "CloudFormation stack #{stack_name} update started".green
  end
end

#update_stack_reuse_template(stack_name, parameters = [], tags = [], role = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/prima_aws_client.rb', line 59

def update_stack_reuse_template(stack_name, parameters = [], tags = [], role = nil)
  cf_args = {
    stack_name: stack_name,
    use_previous_template: true,
    parameters: parameters,
    tags: tags,
    capabilities: ['CAPABILITY_IAM']
  }

  cf_args.merge!(role_arn: role) unless role.nil?

  begin
    cf_client.update_stack(cf_args)
  rescue Aws::CloudFormation::Errors::Throttling => e
    output 'Throttling, retrying in 15 seconds'.red
    sleep 15
    update_stack(stack_name, template_body, parameters = [], tags = [])
  rescue Aws::CloudFormation::Errors::ValidationError => e
    raise e
  else
    output "CloudFormation stack #{stack_name} update started".green
  end
end

#wait_for_stack_ready(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS])) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/prima_aws_client.rb', line 83

def wait_for_stack_ready(stack_name, failed_statuses = %w[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 = 13
  output "Waiting for stack #{stack_name}...\n".yellow
  until 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} ready!\n".green
end