Sha256: bcf4ca4c979957d02d2796a35250c448576465a9714d5fa9759817b200c359f6

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

require 'yaml'
require 'active_support/core_ext/hash'

module AwsEc2
  class Create < Base
    autoload :Params, "aws_ec2/create/params"
    include AwsServices

    def run
      puts "Creating EC2 instance #{@options[:name]}..."
      display_info
      if @options[:noop]
        puts "NOOP mode enabled. EC2 instance not created."
        return
      end

      Hook.run(:before_run_instances, @options)
      resp = ec2.run_instances(params)
      puts "EC2 instance #{@options[:name]} created! 🎉"
      puts "Visit https://console.aws.amazon.com/ec2/home to check on the status"
    end

    # params are main derived from profile files
    def params
      @params ||= Params.new(@options).generate
    end

    def display_info
      puts "Using the following parameters:"
      pretty_display(params)

      display_launch_template
    end

    def display_launch_template
      launch_template = params[:launch_template]
      return unless launch_template

      resp = ec2.describe_launch_template_versions(
        launch_template_id: launch_template[:launch_template_id],
        launch_template_name: launch_template[:launch_template_name],
      )
      versions = resp.launch_template_versions
      launch_template_data = {} # combined launch_template_data
      versions.sort_by { |v| v[:version_number] }.each do |v|
        launch_template_data.merge!(v[:launch_template_data])
      end
      puts "launch template data (versions combined):"
      pretty_display(launch_template_data)
    rescue Aws::EC2::Errors::InvalidLaunchTemplateNameNotFoundException => e
      puts "ERROR: The specified launched template #{launch_template.inspect} was not found."
      puts "Please double check that it exists."
      exit
    end

    def pretty_display(data)
      data = data.deep_stringify_keys

      if data["user_data"]
        message = "base64-encoded: cat /tmp/aws-ec2/user-data.txt to view"
        data["user_data"] = message
      end

      puts YAML.dump(data)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aws-ec2-0.5.2 lib/aws_ec2/create.rb
aws-ec2-0.5.1 lib/aws_ec2/create.rb