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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/convection/control/cloud.rb', line 84

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(retain: options[:retain], &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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/convection/control/cloud.rb', line 111

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/convection/control/cloud.rb', line 134

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(retain: options[:retain])
    # 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



182
183
184
185
186
187
# File 'lib/convection/control/cloud.rb', line 182

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/convection/control/cloud.rb', line 24

def filter_deck(options = {}, &block)
  # throw an error if the user specifies more than one (stack group, list of stacks, exclusion list of stacks)
  if (options[:stack_group] && options[:stacks]) ||
     (options[:stack_group] && options[:exclude_stacks]) ||
     (options[:stacks] && options[:exclude_stacks])
    block.call(Model::Event.new(:error, 'Cannot specify --stack-group , --stack-list, or --exclude-stacks at the same time as each other', :error)) if block
    return {}
  end

  if options[:stack_group] || options[:stacks]
    # 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])
    filter.reduce({}) do |result, stack_name|
      result.merge(stack_name => @cloudfile.stacks[stack_name])
    end

  elsif options[:exclude_stacks]
    # throw an error if the user specifies nonexistent excluded stacks
    if Array(options[:exclude_stacks]).any? { |name| !@cloudfile.stacks.key?(name) }
      bad_stack_names = options[:exclude_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(options[:exclude_stacks])
    return stacks.reject { |stack_name| filter.include? stack_name }

  else
    # if no filter is specified, return the entire deck
    return stacks

  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)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/convection/control/cloud.rb', line 165

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/convection/control/cloud.rb', line 70

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