Class: CloudFormationTool::CloudFormation::Stack

Inherits:
Object
  • Object
show all
Includes:
CloudFormationTool, Storable, Enumerable
Defined in:
lib/cloud_formation_tool/cloud_formation/stack.rb

Constant Summary

Constants included from CloudFormationTool

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CloudFormationTool

#aws_config, #awsas, #awscf, #awscreds, #awsec2, #awss3, #cf_bucket_name, #find_profile, #profile, #region, #s3_bucket_name

Methods included from Storable

#make_filename, #upload

Constructor Details

#initialize(name) ⇒ Stack

Returns a new instance of Stack.



11
12
13
14
15
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 11

def initialize(name)
  @name = name
  @seenev = Set.new
  @watch_timeouts = 0
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 9

def name
  @name
end

Instance Method Details

#asgroupsObject



77
78
79
80
81
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 77

def asgroups
  resources.select do |res|
    res.resource_type == 'AWS::AutoScaling::AutoScalingGroup'
  end
end

#create(template, params = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 47

def create(template, params = {})
  tmpl = CloudFormation.parse(template).to_yaml(params)
  url = upload(make_filename('yaml'), tmpl)
  return update(url, template, params) if exist?
  log "Creating stack '#{name}' from '#{template}' params #{params.inspect}"
  resp = awscf.create_stack({
    stack_name: @name,
    template_url: url,
    capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM),
    on_failure: "DO_NOTHING", ##"ROLLBACK",
    parameters: params.collect do |k,v|
      {
        parameter_key: k.to_s,
        parameter_value: v.to_s,
        use_previous_value: false,
      }
    end
  })
  resp.stack_id
end

#deleteObject



17
18
19
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 17

def delete
  awscf.delete_stack  stack_name: @name
end

#eachObject



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
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 114

def each
  token = nil
  sleep(if @_last_poll_time.nil?
    0
    else
      diff = Time.now - @_last_poll_time
      if diff < 1
        diff
      else
        0
      end
    end)
  begin
    resp = awscf.describe_stack_events stack_name: name, next_token: token
    @watch_timeouts = 0
    resp.stack_events.each do |ev|
      yield ev
    end
  rescue Aws::CloudFormation::Errors::Throttling => e
    sleep 1
    retry
  rescue Seahorse::Client::NetworkingError => e # we get this when there's a timeout
    if (@watch_timeouts += 1) > 5
      raise CloudFormationTool::Errors::AppError, "Too many timeouts!"
    else
      retry
    end
  rescue Aws::CloudFormation::Errors::ValidationError => e
    if e.message =~ /does not exist/
      raise CloudFormationTool::Errors::StackDoesNotExistError, "Stack does not exist"
    else
      raise e
    end
  end
end

#exist?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 21

def exist?
  begin
    awscf.describe_stacks stack_name: name
    true
  rescue Aws::CloudFormation::Errors::ValidationError => e
    false
  end
end

#monitor(start_time = nil) ⇒ 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
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 87

def monitor(start_time = nil)
  done = false
  begin
    until done
      reverse_each do |ev|
        next if @seenev.add?(ev.event_id).nil?
        text = "#{ev.timestamp.strftime "%Y-%m-%d %H:%M:%S"}| " + %w(
          resource_type:40
          logical_resource_id:38
          resource_status
        ).collect { |field|
          (name,size) = field.split(":")
          size ||= 1
          ev.send(name.to_sym).ljust(size.to_i, ' ')
        }.join("  ")
        text += " " + ev.resource_status_reason if ev.resource_status =~ /_FAILED/
        if start_time.nil? or start_time < ev.timestamp
          puts text
        end
        done = (ev.resource_type == "AWS::CloudFormation::Stack" and ev.resource_status =~ /(_COMPLETE|_FAILED)$/)
      end
    end
  rescue CloudFormationTool::Errors::StackDoesNotExistError => e
    puts "Stack #{name} does not exist"
  end
end

#resourcesObject



68
69
70
71
72
73
74
75
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 68

def resources
  begin
    resp = awscf.describe_stack_resources  stack_name: @name
    resp.stack_resources
  rescue Aws::CloudFormation::Errors::ValidationError => e
    raise CloudFormationTool::Errors::AppError, "Failed to get resources: #{e.message}"
  end
end

#see_eventsObject



83
84
85
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 83

def see_events
  each { |e| @seenev << e.event_id }
end

#update(url, filepath, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 30

def update(url, filepath, params = {})
  log "Updating existing stack '#{name}' from '#{filepath}' params #{params.inspect}"
  resp = awscf.update_stack({
    stack_name: @name,
    template_url: url,
    capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM),
    parameters: params.collect do |k,v|
      {
        parameter_key: k.to_s,
        parameter_value: v.to_s,
        use_previous_value: false,
      }
    end
  })
  resp.stack_id
end