class ItamaeMitsurin::Resource::AwsEc2Instance

Public Instance Methods

action_create(options) click to toggle source
# File lib/itamae-mitsurin/resource/aws_ec2_instance.rb, line 110
def action_create(options)
  logger = ItamaeMitsurin.logger
  if @flag == nil or @flag == 48
    resp = @ec2.run_instances(@instance_hash)
    instance_id = Array.new(1) {resp[:instances][0][:instance_id]}
    logger.color(:green) {logger.info "created instance #{instance_id}"}

    @ec2.create_tags({
      resources: instance_id,
      tags: [
        {
          key: "Name",
          value: attributes.name,
        },
      ],
    })

    attributes.tags.each do |tag_h|
      @ec2.create_tags({
      resources: instance_id,
      tags: [
        {
          key: tag_h.keys.join,
          value: tag_h.values.join,
        },
      ],
    })
    end

    logger.color(:green) {logger.info "start up to instance #{instance_id}"}
    @ec2.start_instances(instance_ids: instance_id)
    resp = @ec2.wait_until(:instance_running, instance_ids: instance_id) do |w|
      w.interval = 10
      w.max_attempts = 90
      w.before_wait do |attempts, response|
        logger.info "wait initializing..."
      end
    end
    sleep 90
    logger.color(:green) {logger.info "started running instance #{instance_id}"}

    updated!
  end
end
action_start(options) click to toggle source
# File lib/itamae-mitsurin/resource/aws_ec2_instance.rb, line 155
def action_start(options)
  logger = ItamaeMitsurin.logger
  unless @flag == nil or @flag == 48 or @flag == 16
    @ec2.start_instances(instance_ids: @instance_id)
    ItamaeMitsurin.logger.info "start up to instance #{@instance_id}"
    resp = @ec2.wait_until(:instance_running, instance_ids: @instance_id) do |w|
      w.interval = 10
      w.max_attempts = 90
      w.before_wait do |attempts, response|
        logger.info "wait initializing..."
      end
    end
    sleep 90
    logger.color(:green) {logger.info "started running instance #{@instance_id}"}

    updated!
  end
end
pre_action() click to toggle source
# File lib/itamae-mitsurin/resource/aws_ec2_instance.rb, line 46
def pre_action
  logger = ItamaeMitsurin.logger
  @ec2 = ::Aws::EC2::Client.new(region: attributes.region)

  instance = @ec2.describe_instances({
    filters: [
      {
        name: 'tag:Name',
        values: [ attributes.name ],
      },
    ],
  }).reservations

  @flag = nil
  unless instance.empty?
    logger.color(:white) {logger.debug "instance describe status =>\n #{instance.to_s.gsub(/,/, "\n")}"}
    instance[0][:instances][0][:tags].each do |tag|
      st = tag.values.include? attributes.name
    end
    @flag = instance[0][:instances][0][:state].code
    @instance_id = Array.new(1) {instance[0][:instances][0][:instance_id]}
  end

  @instance_hash = {
    dry_run: attributes.dry_run,
    image_id: attributes.image_id,
    min_count: 1,
    max_count: 1,
    key_name: attributes.key_name,
    security_group_ids: attributes.security_group_ids,
    user_data: attributes.user_data,
    instance_type: attributes.instance_type,
    kernel_id: attributes.kernel_id,
    ramdisk_id: attributes.ramdisk_id,
    block_device_mappings: [
      {
        device_name: attributes.device_name,
        ebs: {
          snapshot_id: attributes.snapshot_id,
          volume_size: attributes.volume_size,
          delete_on_termination: attributes.delete_on_termination,
          volume_type: attributes.volume_type,
          iops: attributes.iops,
          encrypted: attributes.encrypted,
        },
      },
    ],
    monitoring: {
      enabled: attributes.monitoring,
    },
    subnet_id: attributes.subnet_id,
    disable_api_termination: attributes.disable_api_termination,
    instance_initiated_shutdown_behavior: attributes.instance_initiated_shutdown_behavior,
    private_ip_address: attributes.private_ip_address,
    client_token: attributes.client_token,
    additional_info: attributes.additional_info,
    iam_instance_profile: {
      name: attributes.iam_instance_profile,
    },
    ebs_optimized: attributes.ebs_optimized,
  }
  logger.debug "created hash =>\n #{@instance_hash.inspect}"
end