Skip to content

Commit

Permalink
[#46311371] Refactoring aws configuration template to a class for AWS…
Browse files Browse the repository at this point in the history
… bootstrap
  • Loading branch information
Chris Brown and Vinicius Fuentes committed Apr 6, 2013
1 parent 3548b05 commit 720c948
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 73 deletions.
46 changes: 24 additions & 22 deletions bosh_aws_bootstrap/lib/bosh/cli/commands/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def create_bat_manifest(vpc_receipt_file, route53_receipt_file, stemcell_version
desc "snapshot all EBS volumes in all deployments"
def snapshot_deployments(config_file)
auth_required
config = load_yaml_file(config_file)
config = load_config(config_file)

say("Creating snapshots for director `#{target_name}'")
ec2 = Bosh::Aws::EC2.new(config["aws"])
Expand Down Expand Up @@ -140,7 +140,6 @@ def snapshot_deployments(config_file)
desc "create everything in config file"
option "--trace", "print all HTTP traffic"
def create(config_file = nil)
config_file ||= default_config_file
if !!options[:trace]
require 'logger'
::AWS.config(:logger => Logger.new($stdout), :http_wire_trace => true)
Expand All @@ -155,7 +154,6 @@ def create(config_file = nil)
usage "aws destroy"
desc "destroy everything in an AWS account"
def destroy(config_file = nil)
config_file ||= default_config_file
delete_all_elbs(config_file)
delete_all_ec2(config_file)
delete_all_ebs(config_file)
Expand All @@ -171,7 +169,7 @@ def destroy(config_file = nil)
usage "aws create vpc"
desc "create vpc"
def create_vpc(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)

ec2 = Bosh::Aws::EC2.new(config["aws"])
@output_state["aws"] = config["aws"]
Expand Down Expand Up @@ -272,7 +270,7 @@ def delete_vpc(details_file)
usage "aws delete_all vpcs"
desc "delete all VPCs in an AWS account"
def delete_all_vpcs(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)

ec2 = Bosh::Aws::EC2.new(config["aws"])
vpc_ids = ec2.vpcs.map { |vpc| vpc.id }
Expand Down Expand Up @@ -306,7 +304,7 @@ def delete_all_vpcs(config_file)
usage "aws delete key_pairs"
desc "delete key pairs"
def delete_all_key_pairs(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
ec2 = Bosh::Aws::EC2.new(config["aws"])

if confirmed?("Are you sure you want to delete all SSH Keypairs?")
Expand All @@ -318,7 +316,7 @@ def delete_all_key_pairs(config_file)
usage "aws delete elastic_ips"
desc "delete elastic ips"
def delete_all_elastic_ips(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
ec2 = Bosh::Aws::EC2.new(config["aws"])

if confirmed?("Are you sure you want to delete all Elastic IPs?")
Expand All @@ -330,7 +328,7 @@ def delete_all_elastic_ips(config_file)
usage "aws create key_pairs"
desc "create key pairs"
def create_key_pairs(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
ec2 = Bosh::Aws::EC2.new(config["aws"])

say "allocating #{config["key_pairs"].length} KeyPair(s)"
Expand All @@ -342,7 +340,7 @@ def create_key_pairs(config_file)
usage "aws create s3"
desc "create s3 buckets"
def create_s3(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)

if !config["s3"]
say "s3 not set in config. Skipping"
Expand All @@ -360,9 +358,8 @@ def create_s3(config_file)

usage "aws delete_all s3"
desc "delete all s3 buckets"

def delete_all_s3(config_file)
config = load_yaml_file config_file
config = load_config(config_file)

check_instance_count(config)

Expand All @@ -381,9 +378,8 @@ def delete_all_s3(config_file)

usage "aws delete_all ec2"
desc "terminates all EC2 instances and attached EBS volumes"

def delete_all_ec2(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
credentials = config["aws"]
check_instance_count(config)
ec2 = Bosh::Aws::EC2.new(credentials)
Expand All @@ -407,7 +403,7 @@ def delete_all_ec2(config_file)
usage "aws delete_all elbs"
desc "terminates all Elastic Load Balancers in the account"
def delete_all_elbs(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
credentials = config["aws"]
elb = Bosh::Aws::ELB.new(credentials)
elb_names = elb.names
Expand All @@ -419,7 +415,7 @@ def delete_all_elbs(config_file)
usage "aws create rds"
desc "create all RDS database instances"
def create_rds_dbs(config_file, receipt_file = nil)
config = load_yaml_file(config_file)
config = load_config(config_file)

if !config["rds"]
say "rds not set in config. Skipping"
Expand Down Expand Up @@ -478,7 +474,7 @@ def create_rds_dbs(config_file, receipt_file = nil)
usage "aws delete_all rds"
desc "delete all RDS database instances"
def delete_all_rds_dbs(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
credentials = config["aws"]
check_instance_count(config)
rds = Bosh::Aws::RDS.new(credentials)
Expand All @@ -500,7 +496,7 @@ def delete_all_rds_dbs(config_file)
usage "aws delete_all rds subnet_groups"
desc "delete all RDS subnet groups"
def delete_all_rds_subnet_groups(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
credentials = config["aws"]
rds = Bosh::Aws::RDS.new(credentials)
rds.delete_subnet_groups
Expand All @@ -509,7 +505,7 @@ def delete_all_rds_subnet_groups(config_file)
usage "aws delete_all rds security_groups"
desc "delete all RDS security groups"
def delete_all_rds_security_groups(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
credentials = config["aws"]
rds = Bosh::Aws::RDS.new(credentials)
rds.delete_security_groups
Expand All @@ -518,7 +514,7 @@ def delete_all_rds_security_groups(config_file)
usage "aws delete_all volumes"
desc "delete all EBS volumes"
def delete_all_ebs(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
credentials = config["aws"]
ec2 = Bosh::Aws::EC2.new(credentials)
check_volume_count(config)
Expand All @@ -536,7 +532,7 @@ def delete_all_ebs(config_file)
usage "aws delete_all security_groups"
desc "delete all Security Groups"
def delete_all_security_groups(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
ec2 = Bosh::Aws::EC2.new(config["aws"])

if confirmed?('Are you sure you want to delete all security groups?')
Expand All @@ -552,7 +548,7 @@ def delete_all_security_groups(config_file)
usage "aws create route53 records"
desc "creates requested instances, allocates IPs, and creates A records"
def create_route53_records(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
elastic_ip_specs = config["elastic_ips"]

if elastic_ip_specs
Expand Down Expand Up @@ -597,7 +593,7 @@ def create_route53_records(config_file)
desc "delete all Route 53 records except NS and SOA"
option "--omit_types CNAME,A,TXT...", Array, "override default omissions (NS and SOA)"
def delete_all_route53_records(config_file)
config = load_yaml_file(config_file)
config = load_config(config_file)
route53 = Bosh::Aws::Route53.new(config["aws"])

say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".red)
Expand Down Expand Up @@ -706,6 +702,12 @@ def default_config_file
))
end

def load_config(config_file=nil)
config_file ||= default_config_file

Bosh::Aws::AwsConfig.new(config_file).configuration
end

def aws_retry_wait_time; 10; end
end
end
1 change: 1 addition & 0 deletions bosh_aws_bootstrap/lib/bosh_aws_bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require "bosh_aws_bootstrap/elb"
require "bosh_aws_bootstrap/bosh_bootstrap"
require "bosh_aws_bootstrap/micro_bosh_bootstrap"
require "bosh_aws_bootstrap/aws_config"
require "bosh/cli/commands/aws"
require "bosh/cli/commands/micro"
require "bosh_aws_bootstrap/microbosh_manifest"
Expand Down
90 changes: 90 additions & 0 deletions bosh_aws_bootstrap/lib/bosh_aws_bootstrap/aws_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'yaml'

module Bosh
module Aws
class ConfigurationInvalid < RuntimeError; end

class AwsConfig
def initialize(filename, env = ENV)
@filename = filename
@env = env
end

def configuration
load_configuration(File.read(@filename))
end

def fetch_from_env(key)
@env.fetch(key) {
raise(ConfigurationInvalid, "Missing ENV variable #{key}")
}
end

def aws_secret_access_key
fetch_from_env("BOSH_AWS_SECRET_ACCESS_KEY")
end

def aws_access_key_id
fetch_from_env("BOSH_AWS_ACCESS_KEY_ID")
end

def vpc_subdomain
fetch_from_env("BOSH_VPC_SUBDOMAIN")
end

def vpc_primary_az
fetch_from_env("BOSH_VPC_PRIMARY_AZ")
end

def vpc_secondary_az
fetch_from_env("BOSH_VPC_SECONDARY_AZ")
end

def key_pair_name
@env.fetch("BOSH_KEY_PAIR_NAME", "bosh")
end

def key_pair_path
@env.fetch("BOSH_KEY_PATH", "#{@env['HOME']}/.ssh/id_rsa_bosh")
end

def ssl_key_file
@env.fetch("BOSH_SSL_KEY", "elb-cfrouter.key")
end

def ssl_cert_file
@env.fetch("BOSH_SSL_CERT", "elb-cfrouter.pem")
end

def ssl_cert_chain_file
@env["BOSH_SSL_CHAIN"]
end

def has_package_cache_configuration?
cache_access_key_id
end

def cache_access_key_id
@env["BOSH_CACHE_ACCESS_KEY_ID"]
end

def cache_secret_access_key
unless has_package_cache_configuration?
raise ConfigurationInvalid, "Missing ENV variable BOSH_CACHE_ACCESS_KEY_ID"
end

fetch_from_env("BOSH_CACHE_SECRET_ACCESS_KEY")
end

def cache_bucket_name
@env.fetch("BOSH_CACHE_BUCKET_NAME", "bosh-global-package-cache")
end

def load_configuration(yaml)
renderer = ERB.new(yaml)

YAML.load(renderer.result(binding))
end
end
end
end
58 changes: 27 additions & 31 deletions bosh_aws_bootstrap/spec/functional/aws_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def stub_required_environment_variables
it "should default the configuration file when not passed in" do
stub_required_environment_variables
File.exist?(default_config_filename).should == true
aws.should_receive(:create_key_pairs).with(default_config_filename)
aws.should_receive(:create_vpc).with(default_config_filename)
aws.should_receive(:create_route53_records).with(default_config_filename)
aws.should_receive(:create_rds_dbs).with(default_config_filename)
aws.should_receive(:create_s3).with(default_config_filename)
aws.should_receive(:create_key_pairs)
aws.should_receive(:create_vpc)
aws.should_receive(:create_route53_records)
aws.should_receive(:create_rds_dbs)
aws.should_receive(:create_s3)
aws.create
end
end
Expand All @@ -89,30 +89,30 @@ def stub_required_environment_variables
let(:config_file) { asset "config.yml" }

it "should destroy the specified VPCs, RDS DBs, and S3 Volumes" do
aws.should_receive(:delete_all_ec2).with(config_file)
aws.should_receive(:delete_all_ebs).with(config_file)
aws.should_receive(:delete_all_rds_dbs).with(config_file)
aws.should_receive(:delete_all_s3).with(config_file)
aws.should_receive(:delete_all_vpcs).with(config_file)
aws.should_receive(:delete_all_key_pairs).with(config_file)
aws.should_receive(:delete_all_elastic_ips).with(config_file)
aws.should_receive(:delete_all_security_groups).with(config_file)
aws.should_receive(:delete_all_route53_records).with(config_file)
aws.should_receive(:delete_all_elbs).with(config_file)
aws.should_receive(:delete_all_ec2)
aws.should_receive(:delete_all_ebs)
aws.should_receive(:delete_all_rds_dbs)
aws.should_receive(:delete_all_s3)
aws.should_receive(:delete_all_vpcs)
aws.should_receive(:delete_all_key_pairs)
aws.should_receive(:delete_all_elastic_ips)
aws.should_receive(:delete_all_security_groups)
aws.should_receive(:delete_all_route53_records)
aws.should_receive(:delete_all_elbs)
aws.destroy config_file
end

it "should use a default config file when none is provided" do
aws.should_receive(:delete_all_ec2).with(default_config_filename)
aws.should_receive(:delete_all_ebs).with(default_config_filename)
aws.should_receive(:delete_all_rds_dbs).with(default_config_filename)
aws.should_receive(:delete_all_s3).with(default_config_filename)
aws.should_receive(:delete_all_vpcs).with(default_config_filename)
aws.should_receive(:delete_all_key_pairs).with(default_config_filename)
aws.should_receive(:delete_all_elastic_ips).with(default_config_filename)
aws.should_receive(:delete_all_security_groups).with(default_config_filename)
aws.should_receive(:delete_all_route53_records).with(default_config_filename)
aws.should_receive(:delete_all_elbs).with(default_config_filename)
aws.should_receive(:delete_all_ec2)
aws.should_receive(:delete_all_ebs)
aws.should_receive(:delete_all_rds_dbs)
aws.should_receive(:delete_all_s3)
aws.should_receive(:delete_all_vpcs)
aws.should_receive(:delete_all_key_pairs)
aws.should_receive(:delete_all_elastic_ips)
aws.should_receive(:delete_all_security_groups)
aws.should_receive(:delete_all_route53_records)
aws.should_receive(:delete_all_elbs)
aws.destroy
end
end
Expand Down Expand Up @@ -415,7 +415,7 @@ def make_fake_vpc!(overrides = {})
end

it "should do nothing if s3 config is empty" do
aws.stub(:load_yaml_file).and_return({})
aws.stub(:load_config).and_return({})

aws.should_receive(:say).with("s3 not set in config. Skipping")
fake_s3.should_not_receive(:create_bucket)
Expand Down Expand Up @@ -796,7 +796,7 @@ def make_fake_rds!(opts = {})
end

it "should do nothing if rds config is empty" do
aws.stub(:load_yaml_file).and_return({})
aws.stub(:load_config).and_return({})

aws.should_receive(:say).with("rds not set in config. Skipping")

Expand Down Expand Up @@ -1066,9 +1066,5 @@ def make_fake_rds!(opts = {})
aws.delete_all_elbs(config_file)
end
end

describe "aws create elb" do

end
end
end
Loading

0 comments on commit 720c948

Please sign in to comment.