module AMA

Public Instance Methods

create_stack(conf) click to toggle source
# File lib/aws/drupal_aws.rb, line 14
def create_stack conf
  parameters = get_default_network_ids().update conf[:create_params]
  paramlist = parameters.map {|k,v| {parameter_key: k.to_s, parameter_value: v.to_s}}
  create_params = conf[:create].update({parameters: paramlist})
  #puts create_params; return ###
  cfn = Aws::CloudFormation::Client.new
  begin
    cfn.create_stack( create_params )
    descr = cfn.wait_until(:stack_create_complete,
               {stack_name: conf[:create][:stack_name]}, { 
      delay: 6,
      max_attempts: 150, # 15 min
      before_attempt: ->(*x) do 
        print "."
      end  
    })
    puts
  rescue Aws::CloudFormation::Errors::ServiceError, \
         Aws::Waiters::Errors::WaiterFailed, \
         ArgumentError  => e
    puts e
    return
  end
  shape_outputs( descr )
end
delete_stack(stack_name) click to toggle source
# File lib/aws/drupal_aws.rb, line 40
def delete_stack stack_name
  begin
    cfn = Aws::CloudFormation::Client.new
    cfn.delete_stack({stack_name: stack_name})
  rescue Aws::CloudFormation::Errors::ServiceError => e
    puts e
  end
end
get_default_network_ids() click to toggle source
# File lib/aws/drupal_aws.rb, line 88
def get_default_network_ids
  begin
    ec2 = Aws::EC2::Client.new
    descr = ec2.describe_vpcs
    def_vpc = descr.vpcs.select {
        |x| x.is_default } .first.vpc_id
    descr = ec2.describe_subnets({ filters: [
        {name: "vpc-id", values: [def_vpc]}
    ] })
  rescue Aws::EC2::Errors => e
    puts " *** #{e}"
    return
  end
  subnets = descr.subnets.map &:subnet_id
  { VpcId: def_vpc,
    SubnetA: subnets[0],
    SubnetB: subnets[1] }
end
info(stack_name) click to toggle source
# File lib/aws/drupal_aws.rb, line 67
def info stack_name
  begin
    cfn = Aws::CloudFormation::Client.new
    descr = cfn.describe_stacks({stack_name: stack_name})
  rescue Aws::CloudFormation::Errors::ServiceError, ArgumentError => e
    puts e
    return
  end
  shape_outputs( descr )
end
stack_status(stack_name) click to toggle source
# File lib/aws/drupal_aws.rb, line 49
def stack_status stack_name
  begin
    cfn = Aws::CloudFormation::Client.new
    events = cfn.describe_stack_events({stack_name: stack_name})
  rescue Aws::CloudFormation::Errors::ServiceError, ArgumentError => e
    puts e
    return
  end
  last_event = events.stack_events.first
  finished = ("AWS::CloudFormation::Stack" == last_event.resource_type)
  finished &= (stack_name == last_event.logical_resource_id)
  finished &= !("CREATE_IN_PROGRESS" == last_event.resource_status)
  success = ("CREATE_COMPLETE" == last_event.resource_status)
  finished ? 
    (success ? :created : :failed) :
    :ongoing
end
terminate(instance_id) click to toggle source
# File lib/aws/drupal_aws.rb, line 78
def terminate instance_id
  begin
    ec2 = Aws::EC2::Client.new
    ec2.terminate_instances({ instance_ids: [instance_id] })
  rescue Aws::EC2::Errors => e
    puts " *** #{e}"
  end
end

Private Instance Methods

shape_outputs( stacks_description ) click to toggle source
# File lib/aws/drupal_aws.rb, line 108
def shape_outputs( stacks_description )
  stack = stacks_description.stacks.first
  stack.outputs.map {|o| {o.output_key => o.output_value}
    }.reduce {|hash1, hash2| hash1.update hash2} 
end