From bf5c55ac74aadc1ed357f18e85a09e018c8148b4 Mon Sep 17 00:00:00 2001 From: bbbco Date: Wed, 31 Dec 2014 17:37:11 -0500 Subject: [PATCH 1/3] Make LB options optional --- lib/chef/provisioning/aws_driver/driver.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/chef/provisioning/aws_driver/driver.rb b/lib/chef/provisioning/aws_driver/driver.rb index 6bd1609f..38db21cd 100644 --- a/lib/chef/provisioning/aws_driver/driver.rb +++ b/lib/chef/provisioning/aws_driver/driver.rb @@ -16,7 +16,6 @@ require 'yaml' require 'aws-sdk-v1' - class Chef module Provisioning module AWSDriver @@ -53,17 +52,22 @@ def self.canonicalize_url(driver_url, config) # Load balancer methods def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) - security_group_name = lb_options[:security_group_name] || 'default' + security_group_name = lb_options[:security_group_name] security_group_id = lb_options[:security_group_id] - security_group = if security_group_id.nil? + security_group = if security_group_id.nil? && !security_group_name.nil? ec2.security_groups.filter('group-name', security_group_name).first - else + elsif !security_group_id.nil? ec2.security_groups[security_group_id] end availability_zones = lb_options[:availability_zones] listeners = lb_options[:listeners] + lb_optionals = {} + lb_optionals[:security_groups] = [security_group] if security_group + lb_optionals[:availability_zones] = availability_zones if availability_zones + lb_optionals[:listeners] = listeners + actual_elb = load_balancer_for(lb_spec) if !actual_elb.exists? perform_action = proc { |desc, &block| action_handler.perform_action(desc, &block) } @@ -74,10 +78,7 @@ def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) updates << " with security group #{security_group.name}" if security_group action_handler.perform_action updates do - actual_elb = elb.load_balancers.create(lb_spec.name, - availability_zones: availability_zones, - listeners: listeners, - security_groups: [security_group]) + actual_elb = elb.load_balancers.create(lb_spec.name, lb_optionals) lb_spec.location = { 'driver_url' => driver_url, From d6168993cf139ee65eb63cbe044bd35fdda9d649 Mon Sep 17 00:00:00 2001 From: bbbco Date: Tue, 27 Jan 2015 13:52:40 -0500 Subject: [PATCH 2/3] Fix missed option --- lib/chef/provisioning/aws_driver/driver.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/chef/provisioning/aws_driver/driver.rb b/lib/chef/provisioning/aws_driver/driver.rb index 38db21cd..febd3d84 100644 --- a/lib/chef/provisioning/aws_driver/driver.rb +++ b/lib/chef/provisioning/aws_driver/driver.rb @@ -66,7 +66,7 @@ def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) lb_optionals = {} lb_optionals[:security_groups] = [security_group] if security_group lb_optionals[:availability_zones] = availability_zones if availability_zones - lb_optionals[:listeners] = listeners + lb_optionals[:listeners] = listeners if listeners actual_elb = load_balancer_for(lb_spec) if !actual_elb.exists? @@ -543,6 +543,8 @@ def default_aws_keypair(action_handler, machine_spec) end end + + # Only warn the first time default_warning = 'Using default key, which is not shared between machines! It is recommended to create an AWS key pair with the fog_key_pair resource, and set :bootstrap_options => { :key_name => }' Chef::Log.warn(default_warning) if updated @@ -550,6 +552,10 @@ def default_aws_keypair(action_handler, machine_spec) default_key_name end + def image_for(image_spec) + compute.images.get(image_spec.location['image_id']) + end + end end end From dd76b86a3e5015f440fb8ab514610e7f1f6c2cc4 Mon Sep 17 00:00:00 2001 From: bbbco Date: Tue, 2 Feb 2016 02:23:17 -0500 Subject: [PATCH 3/3] Add support for Classic VPC in ELBs --- lib/chef/provisioning/aws_driver/driver.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/chef/provisioning/aws_driver/driver.rb b/lib/chef/provisioning/aws_driver/driver.rb index a514148a..6caa6692 100644 --- a/lib/chef/provisioning/aws_driver/driver.rb +++ b/lib/chef/provisioning/aws_driver/driver.rb @@ -174,7 +174,13 @@ def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) if !actual_elb || !actual_elb.exists? lb_options[:listeners] ||= get_listeners(:http) if !lb_options[:subnets] && !lb_options[:availability_zones] && machine_specs - lb_options[:subnets] = machine_specs.map { |s| ec2.instances[s.reference['instance_id']].subnet }.uniq + # Use Subnets only if not using Classic VPC (since there is no VPC available) + ec2_instances = machine_spec.map { |s| ec2.instances[s.reference['instance_id']] } + if ec2_instances.any?{ |e| e.vpc } + lb_options[:subnets] = ec2_instances.map{ |s| e.subnet }.uniq + else + lb_options[:availability_zones] = ec2_instances.map { |s| e.availability_zone }.uniq + end end perform_action = proc { |desc, &block| action_handler.perform_action(desc, &block) } @@ -258,7 +264,8 @@ def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) ] default_subnet = ec2.client.describe_subnets(:filters => filters)[:subnet_set] if default_subnet.size != 1 - raise "Could not find default subnet in availability zone #{zone}" + # We could not find default subnet in availability zone, most likely using Classic VPC + next end default_subnet = default_subnet[0] desired_subnets_zones[default_subnet[:subnet_id]] = zone