Class: Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudformer/stack.rb

Constant Summary collapse

SUCESS_STATES =
["CREATE_COMPLETE", "UPDATE_COMPLETE"]
FAILURE_STATES =
["CREATE_FAILED", "DELETE_FAILED", "UPDATE_ROLLBACK_FAILED", "ROLLBACK_FAILED", "ROLLBACK_COMPLETE","ROLLBACK_FAILED","UPDATE_ROLLBACK_COMPLETE","UPDATE_ROLLBACK_FAILED"]
END_STATES =
SUCESS_STATES + FAILURE_STATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_name) ⇒ Stack

WAITING_STATES = [“CREATE_IN_PROGRESS”,“DELETE_IN_PROGRESS”,“ROLLBACK_IN_PROGRESS”,“UPDATE_COMPLETE_CLEANUP_IN_PROGRESS”,“UPDATE_IN_PROGRESS”,“UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS”,“UPDATE_ROLLBACK_IN_PROGRESS”]



12
13
14
15
16
17
# File 'lib/cloudformer/stack.rb', line 12

def initialize(stack_name)
  @name = stack_name
  @cf = AWS::CloudFormation.new
  @stack = @cf.stacks[name]
  @ec2 = AWS::EC2.new
end

Instance Attribute Details

#deployedObject

Returns the value of attribute deployed.



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

def deployed
  @deployed
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#stackObject

Returns the value of attribute stack.



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

def stack
  @stack
end

Instance Method Details

#apply(template_file, parameters, disable_rollback = false, capabilities = [], notify = [], tags = []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudformer/stack.rb', line 23

def apply(template_file, parameters, disable_rollback=false, capabilities=[], notify=[], tags=[])
  if ( template_file =~ /^https:\/\/s3\S+\.amazonaws\.com\/(.*)/ )
    template = template_file
  elsif ( template_file =~ /^http.*(.json)$/ )
    begin
      response = HTTParty.get(template_file)
      template = response.body
    rescue => e
      puts "Unable to retieve json file for template from #{template_file} - #{e.class}, #{e}"
      return :Failed
    end
  else
    template = File.read(template_file)
  end
  validation = validate(template)
  unless validation["valid"]
    puts "Unable to update - #{validation["response"][:code]} - #{validation["response"][:message]}"
    return :Failed
  end
  pending_operations = false
  begin
    if deployed
      pending_operations = update(template, parameters, capabilities)
    else
      pending_operations = create(template, parameters, disable_rollback, capabilities, notify, tags)
    end
  rescue ::AWS::CloudFormation::Errors::ValidationError => e
    puts e.message
    return (if e.message == "No updates are to be performed." then :NoUpdates else :Failed end)
  end
  wait_until_end if pending_operations
  return (if deploy_succeded? then :Succeeded else :Failed end)
end

#deleteObject



71
72
73
74
75
76
77
78
# File 'lib/cloudformer/stack.rb', line 71

def delete
  with_highlight do
    puts "Attempting to delete stack - #{name}"
    stack.delete
    wait_until_end
    return deploy_succeded?
  end
end

#deploy_succeded?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/cloudformer/stack.rb', line 57

def deploy_succeded?
  return true unless FAILURE_STATES.include?(stack.status)
  puts "Unable to deploy template. Check log for more information."
  false
end

#events(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cloudformer/stack.rb', line 90

def events(options = {})
  with_highlight do
    if !deployed
      puts "Stack not up."
      return
    end
    stack.events.sort_by {|a| a.timestamp}.each do |event|
      puts "#{event.timestamp} - #{event.physical_resource_id.to_s} - #{event.logical_resource_id} - #{event.resource_type} - #{event.resource_status} - #{event.resource_status_reason.to_s}"
    end
  end
end

#outputsObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cloudformer/stack.rb', line 102

def outputs
  with_highlight do
  if !deployed
    puts "Stack not up."
    return 1
  end
    stack.outputs.each do |output|
      puts "#{output.key} - #{output.description} - #{output.value}"
    end
  end
  return 0
end

#start_instancesObject



67
68
69
# File 'lib/cloudformer/stack.rb', line 67

def start_instances
  update_instances("start")
end

#statusObject



80
81
82
83
84
85
86
87
88
# File 'lib/cloudformer/stack.rb', line 80

def status
  with_highlight do
    if deployed
      puts "#{stack.name} - #{stack.status} - #{stack.status_reason}"
    else
      puts "#{name} - Not Deployed"
    end
  end
end

#stop_instancesObject



63
64
65
# File 'lib/cloudformer/stack.rb', line 63

def stop_instances
 update_instances("stop")
end