Class: Cumulus::ELB::Manager

Inherits:
Common::Manager show all
Includes:
LoadBalancerChange
Defined in:
lib/elb/manager/Manager.rb

Constant Summary

Constants included from LoadBalancerChange

LoadBalancerChange::BACKEND, LoadBalancerChange::CROSS, LoadBalancerChange::DRAINING, LoadBalancerChange::HEALTH, LoadBalancerChange::IDLE, LoadBalancerChange::INSTANCES, LoadBalancerChange::INTERNAL, LoadBalancerChange::LISTENERS, LoadBalancerChange::LOG, LoadBalancerChange::SECURITY, LoadBalancerChange::SUBNETS, LoadBalancerChange::TAGS

Constants included from Common::DiffChange

Common::DiffChange::ADD, Common::DiffChange::MODIFIED, Common::DiffChange::UNMANAGED

Instance Method Summary collapse

Methods included from Common::DiffChange

next_change_id

Methods inherited from Common::Manager

#diff, #diff_one, #filter_local, #list, #sync, #sync_one

Constructor Details

#initializeManager

Returns a new instance of Manager.



21
22
23
24
# File 'lib/elb/manager/Manager.rb', line 21

def initialize
  super()
  @elb = Aws::ElasticLoadBalancing::Client.new(Configuration.instance.client)
end

Instance Method Details

#added_diff(local) ⇒ Object



42
43
44
# File 'lib/elb/manager/Manager.rb', line 42

def added_diff(local)
  LoadBalancerDiff.added(local)
end

#aws_resourcesObject



34
35
36
# File 'lib/elb/manager/Manager.rb', line 34

def aws_resources
  @aws_resources ||= ELB::elbs
end

#create(local) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/elb/manager/Manager.rb', line 173

def create(local)

  # Create the load balancer with listeners, subnets, security groups, scheme, and tags
  @elb.create_load_balancer({
    load_balancer_name: local.name,
    listeners: local.listeners.map(&:to_aws),
    subnets: local.subnets.map { |subnet| subnet.subnet_id },
    security_groups: local.security_groups.map do |sg_name|
      SecurityGroups::vpc_security_groups[local.vpc_id][sg_name].group_id
    end,
    scheme: if local.internal then "internal" end,
    tags: if !local.tags.empty?
      local.tags.map do |tagKey, tagValue|
        {
          key: tagKey,
          value: tagValue
        }
      end
    end
  })

  # Set the policies for the attached listeners
  local.listeners.each do |listener|
    update_listener_policies(local.name, listener.load_balancer_port, listener.policies)
  end

  # Set the health check config
  update_health_check(local.name, local.health_check)

  # Set the attributes
  update_attributes(local)

  # Update the instances if they are managed
  if local.manage_instances != false
    update_instances(local.name, local.manage_instances)
  end

  # Set the backend policies
  backend_added = local.backend_policies.map do |backend_policy|
    LoadBalancerDiff::BackendChange.new(backend_policy.instance_port, nil, backend_policy.policy_names)
  end
  update_backend_policies(local.name, backend_added)

end

#diff_resource(local, aws) ⇒ Object



46
47
48
# File 'lib/elb/manager/Manager.rb', line 46

def diff_resource(local, aws)
  local.diff(aws)
end

#local_resourcesObject



30
31
32
# File 'lib/elb/manager/Manager.rb', line 30

def local_resources
  @local_resources ||= Hash[Loader.elbs.map { |local| [local.name, local] }]
end

#migrate_default_policiesObject

Migrates all of the default load balancer policies to Cumulus versions



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/elb/manager/Manager.rb', line 51

def migrate_default_policies
  policies_dir = "#{@migration_root}/elb-default-policies"

  if !Dir.exists?(@migration_root)
    Dir.mkdir(@migration_root)
  end
  if !Dir.exists?(policies_dir)
    Dir.mkdir(policies_dir)
  end

  default_policies = ELB::default_policies
  default_policies.map do |policy_name, policy|
    puts "Processing #{policy_name}"

    cumulus_name = "Cumulus-#{policy_name}"
    json = JSON.pretty_generate(policy.to_cumulus_hash)

    File.open("#{policies_dir}/#{cumulus_name}.json", "w") { |f| f.write(json) }
  end
end

#migrate_elbsObject

Migrates all of the current AWS load balancers to Cumulus versions



73
74
75
76
77
78
79
80
81
82
83
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
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/elb/manager/Manager.rb', line 73

def migrate_elbs
  elbs_dir = "#{@migration_root}/elb-load-balancers"
  policies_dir ="#{@migration_root}/elb-policies"

  if !Dir.exists?(@migration_root)
    Dir.mkdir(@migration_root)
  end
  if !Dir.exists?(elbs_dir)
    Dir.mkdir(elbs_dir)
  end
  if !Dir.exists?(policies_dir)
    Dir.mkdir(policies_dir)
  end

  ELB::elbs.map do |elb_name, elb|
    puts "Migrating load balancer #{elb_name}"

    local_elb = LoadBalancerConfig.new(elb_name)
    tags = ELB::elb_tags(elb_name)
    attributes = ELB::elb_attributes(elb_name)
    local_elb.populate!(elb, tags, attributes)

    # Migrate the backend and listener policies if they do not already
    # exist locally and are not the default AWS policies
    listener_policies = local_elb.listeners.flat_map { |l| l.policies }
    backend_policies = local_elb.backend_policies.flat_map { |b| b.policy_names }

    (listener_policies + backend_policies).uniq.map do |policy_name|
      policy_file = "#{policies_dir}/#{policy_name}.json"
      # If it is not already migrated and not a default policy, create it
      if !File.file? policy_file
        if !ELB::default_policies.has_key? policy_name
          # Get the full policy description from the elb policies
          full_policy = ELB::elb_policies(elb_name)[policy_name]

          if full_policy.nil?
            puts "Unable to migrate policy #{policy_name}"
          else
            puts "Migrating policy #{policy_name}"
            json = JSON.pretty_generate(full_policy.to_cumulus_hash)
            File.open(policy_file, "w") { |f| f.write(json) }
          end
        end
      end
    end

    File.open("#{elbs_dir}/#{elb_name}.json", "w") { |f| f.write(local_elb.pretty_json) }
  end

end

#resource_nameObject



26
27
28
# File 'lib/elb/manager/Manager.rb', line 26

def resource_name
  "Elastic Load Balancer"
end

#unmanaged_diff(aws) ⇒ Object



38
39
40
# File 'lib/elb/manager/Manager.rb', line 38

def unmanaged_diff(aws)
  LoadBalancerDiff.unmanaged(aws)
end

#update(local, diffs) ⇒ Object



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/elb/manager/Manager.rb', line 124

def update(local, diffs)
  attributes_changed = false
  diffs.each do |diff|
    case diff.type
    when LISTENERS
      puts "Updating listeners..."
      update_listeners(local.name, diff.local, diff.aws, diff.listeners)
    when SUBNETS
      puts "Updating subnets..."
      update_subnets(local.name, diff.subnets)
    when SECURITY
      puts "Updating security groups..."
      update_security_groups(local.name, local.vpc_id, local.security_groups)
    when INTERNAL
      puts "AWS does not allow changing internal after creation"
    when TAGS
      puts "Updating tags..."
      update_tags(local.name, diff.tags)
    when INSTANCES
      if local.manage_instances != false
        puts "Updating managed instances..."
        update_instances(local.name, diff.instances.added, diff.instances.removed)
      end
    when HEALTH
      puts "Updating health check..."
      update_health_check(local.name, local.health_check)
    when BACKEND
      puts "Updating backend policies"
      update_backend_policies(local.name, diff.backend_policies.added, diff.backend_policies.removed, diff.backend_policies.modified)
    when CROSS
      puts "Updating cross zone load balancing"
      attributes_changed = true
    when LOG
      puts "Updating access log config"
      attributes_changed = true
    when DRAINING
      puts "Updating connection draining"
      attributes_changed = true
    when IDLE
      puts "Updating idle timeout"
      attributes_changed = true
    end
  end

  if attributes_changed
    update_attributes(local)
  end
end