lib/ami_spec.rb in ami_spec-1.4.0 vs lib/ami_spec.rb in ami_spec-1.5.0

- old
+ new

@@ -1,7 +1,8 @@ require 'ami_spec/aws_instance' require 'ami_spec/aws_instance_options' +require 'ami_spec/aws_default_vpc' require 'ami_spec/aws_key_pair' require 'ami_spec/aws_security_group' require 'ami_spec/server_spec' require 'ami_spec/server_spec_options' require 'ami_spec/wait_for_ssh' @@ -52,10 +53,17 @@ def self.run(options) logger = Logger.new(STDOUT, formatter: proc { |_sev, _time, _name, message| "#{message}\n" }) ec2 = Aws::EC2::Resource.new(options[:aws_region] ? {region: options[:aws_region]} : {}) + if options[:subnet_id].nil? + default_vpc_subnet = AwsDefaultVpc.find_subnet(ec2: ec2) + raise 'No default VPC subnet found. Please specify a subnet id.' if default_vpc_subnet.nil? + options[:subnet_id] = default_vpc_subnet.id + logger.info("Using subnet #{options[:subnet_id]} from the default VPC") + end + unless options[:key_name] key_pair = AwsKeyPair.create(ec2: ec2, logger: logger) options[:key_name] = key_pair.key_name options[:key_file] = key_pair.key_file end @@ -92,11 +100,11 @@ key_pair.delete if key_pair temporary_security_group.delete if temporary_security_group end def self.stop_instances(instances, debug) - instances.each do |instance| + instances && instances.each do |instance| begin if debug puts "EC2 instance ##{instance.instance_id} has not been stopped due to debug mode." else instance.terminate @@ -114,10 +122,10 @@ opt :role, "The role to test, this should map to a directory in the spec folder", type: :string opt :ami, "The ami ID to run tests against", type: :string opt :role_ami_file, "A file containing comma separated roles and amis. i.e.\nweb_server,ami-id.", type: :string opt :specs, "The directory to find ServerSpecs", type: :string, required: true - opt :subnet_id, "The subnet to start the instance in", type: :string, required: true + opt :subnet_id, "The subnet to start the instance in. If not provided a subnet will be chosen from the default VPC", type: :string opt :key_name, "The SSH key name to assign to instances. If not provided a temporary key pair will be generated in AWS", type: :string opt :key_file, "The SSH private key file associated to the key_name", type: :string opt :ssh_user, "The user to ssh to the instance as", type: :string, required: true opt :aws_region, "The AWS region, defaults to AWS_DEFAULT_REGION environment variable", type: :string