Class: Convection::Control::Cloud

Inherits:
Object
  • Object
show all
Defined in:
lib/convection/control/cloud.rb

Overview

Control tour Clouds

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cloudfileObject

Returns the value of attribute cloudfile.



10
11
12
# File 'lib/convection/control/cloud.rb', line 10

def cloudfile
  @cloudfile
end

Instance Method Details

#configure(cloudfile) ⇒ Object



11
12
13
# File 'lib/convection/control/cloud.rb', line 11

def configure(cloudfile)
  @cloudfile = Model::Cloudfile.new(cloudfile)
end

#converge(to_stack, options = {}, &block) ⇒ Object



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
# File 'lib/convection/control/cloud.rb', line 67

def converge(to_stack, options = {}, &block)
  if to_stack && !stacks.include?(to_stack)
    block.call(Model::Event.new(:error, "Undefined Stack #{ to_stack }", :error)) if block
    return
  end

  exit 1 if stack_initialization_errors?(&block)

  filter_deck(options, &block).each_value do |stack|
    block.call(Model::Event.new(:converge, "Stack #{ stack.name }", :info)) if block
    stack.apply(&block)

    emit_credential_error_and_exit!(stack, &block) if stack.credential_error?
    if stack.error?
      block.call(Model::Event.new(:error, "Error converging stack #{ stack.name }", :error), stack.errors) if block
      break
    end

    ## Stop on converge error
    break unless stack.success?

    ## Stop here
    break if !to_stack.nil? && stack.name == to_stack
    sleep rand @cloudfile.splay || 2
  end
end

#delete(stacks, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/convection/control/cloud.rb', line 94

def delete(stacks, &block)
  exit 1 if stack_initialization_errors?(&block)

  stacks.each do |stack|
    unless stack.exist?
      block.call(Model::Event.new(:delete_skipped, "Stack #{ stack.cloud_name } does not exist remotely", :warn))
      next
    end

    block.call(Model::Event.new(:delete, "Delete remote stack #{ stack.cloud_name }", :info)) if block
    stack.delete(&block)

    if stack.error?
      block.call(Model::Event.new(:error, "Error deleting stack #{ stack.name }", :error), stack.errors) if block
      break
    end

    break unless stack.delete_success?

    sleep rand @cloudfile.splay || 2
  end
end

#diff(to_stack, options = {}, &block) ⇒ Object



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
# File 'lib/convection/control/cloud.rb', line 117

def diff(to_stack, options = {}, &block)
  if to_stack && !stacks.include?(to_stack)
    block.call(Model::Event.new(:error, "Undefined Stack #{ to_stack }", :error)) if block
    return
  end

  exit 1 if stack_initialization_errors?(&block)

  filter_deck(options, &block).each_value do |stack|
    block.call(Model::Event.new(:compare, "Compare local state of stack #{ stack.name } (#{ stack.cloud_name }) with remote template", :info))

    difference = stack.diff
    # Find errors during diff
    emit_credential_error_and_exit!(stack, &block) if stack.credential_error?
    if stack.error?
      errors = stack.errors.collect { |x| x.exception.message }
      errors = errors.uniq.flatten
      block.call(Model::Event.new(:error, "Error diffing stack #{ stack.name} Error(s): #{errors.join(', ')}", :error), stack.errors) if block
      break
    end

    if difference.empty?
      difference << Model::Event.new(:unchanged, "Stack #{ stack.cloud_name } has no changes", :info)
    end

    difference.each { |diff| block.call(diff) }

    break if !to_stack.nil? && stack.name == to_stack
  end
end

#emit_credential_error_and_exit!(stack, &block) ⇒ Object



165
166
167
168
169
170
# File 'lib/convection/control/cloud.rb', line 165

def emit_credential_error_and_exit!(stack, &block)
  event = Model::Event.new(:error, "Credentials expired while converging #{ stack.name }. " \
                           'Visit the AWS console to track progress of the stack being created/updated.', :error)
  block.call(event, stack.errors) if block
  exit 1
end

#filter_deck(options = {}, &block) ⇒ Object



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
# File 'lib/convection/control/cloud.rb', line 24

def filter_deck(options = {}, &block)
  # throw an error if the user specifies both a stack group and list of stacks
  if options[:stack_group] && options[:stacks]
    block.call(Model::Event.new(:error, 'Cannot specify --stack-group and --stack-list at the same time', :error)) if block
    return {}
  end

  # throw an error if the user specifies a nonexistent stack groups
  if options[:stack_group] && !stack_groups.key?(options[:stack_group])
    block.call(Model::Event.new(:error, "Unknown stack group: #{options[:stack_group]}", :error)) if block
    return {}
  end

  # throw an error if the user specifies nonexistent stacks
  if Array(options[:stacks]).any? { |name| !@cloudfile.stacks.key?(name) }
    bad_stack_names = options[:stacks].reject { |name| @cloudfile.stacks.key?(name) }
    block.call(Model::Event.new(:error, "Undefined Stack(s) #{bad_stack_names.join(', ')}", :error)) if block
    return {}
  end

  filter = Array(stack_groups[options[:stack_group]] || options[:stacks])

  # if no filter is specified, return the entire deck
  return stacks if filter.empty?
  filter.reduce({}) do |result, stack_name|
    result.merge(stack_name => @cloudfile.stacks[stack_name])
  end
end

#stack_groupsObject



20
21
22
# File 'lib/convection/control/cloud.rb', line 20

def stack_groups
  @cloudfile.stack_groups
end

#stack_initialization_errors?(&block) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/convection/control/cloud.rb', line 148

def stack_initialization_errors?(&block)
  errors = []
  # Find errors during stack init
  stacks.each_value do |stack|
    if stack.error?
      errors << stack.errors.collect { |x| x.exception.message }
    end
  end

  unless errors.empty?
    errors = errors.uniq.flatten
    block.call(Model::Event.new(:error, "Error(s) during stack initialization #{errors.join(', ')}", :error), errors) if block
    return true
  end
  false
end

#stacksObject



16
17
18
# File 'lib/convection/control/cloud.rb', line 16

def stacks
  @cloudfile.stacks
end

#stacks_until(last_stack_name, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/convection/control/cloud.rb', line 53

def stacks_until(last_stack_name, options = {}, &block)
  stacks = filter_deck(options, &block).values
  return stacks if last_stack_name.nil?

  last_stack = stacks.find { |stack| stack.name == last_stack_name }
  unless last_stack
    block.call(Model::Event.new(:error, "Stacks filter did not include last stack #{ last_stack }", :error)) if block
    return []
  end

  last_stack_index = stacks.index(last_stack)
  stacks[0..last_stack_index]
end