Class: Jackal::CfnTools::AmiManager

Inherits:
Jackal::Cfn::Resource show all
Defined in:
lib/jackal-cfn/resource/ami_manager.rb

Overview

Manage AMI Resources

Expected resource:

{
  "Type": "Custom::AmiManager",
  "Properties": {
    "Parameters": {
      "InstanceId": "",
      "AmiId": "",
      "Action": "",
      "Region": ""
    }
  }
}

Required configuration:

{
  "config": {
    "ami": {
      "credentials": {
        "compute": {
          FOG_CREDENTIALS
        }
      }
    }
  }
}

Constant Summary collapse

PHYSICAL_ID_JOINER =
'__-__'

Constants inherited from Jackal::Cfn::Resource

Jackal::Cfn::Resource::VALID_RESOURCE_STATUS

Instance Method Summary collapse

Methods inherited from Jackal::Cfn::Resource

#build_response, inherited, #physical_resource_id, #respond_to_stack, #setup, #unpack, #valid?

Methods included from Jackal::Cfn::Utils::Http

#response_endpoint

Methods included from Jackal::Cfn::Utils

#snakecase, #transform_parameters

Instance Method Details

#ami_response_update(response, parameters) ⇒ Hash

Update the physical resource id to include the ami id

Parameters:

  • response (Hash)

    cfn response

  • parameters (Hash)

    resource parameters

Returns:

  • (Hash)

    updated response hash



66
67
68
69
70
71
72
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 66

def ami_response_update(response, parameters)
  response['PhysicalResourceId'] = [
    response['PhysicalResourceId'],
    parameters[:ami_id]
  ].join(PHYSICAL_ID_JOINER)
  response
end

#compute_api(region) ⇒ Fog::Compute

Build new compute api connection

Parameters:

  • region (String)

    AWS region ami exists

Returns:

  • (Fog::Compute)


99
100
101
102
103
104
105
106
107
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 99

def compute_api(region)
  Fog::Compute.new(
    {:provider => :aws}.merge(
      config.fetch(:ami, :credentials, :compute).merge(
        :region => region
      )
    )
  )
end

#destroy_ami(response, payload, parameters) ⇒ TrueClass

Destroy the AMI referenced by the resource

Parameters:

  • response (Hash)

    cfn response

  • payload (Hash)

    message payload

  • parameters (Hash)

    resource parameters

Returns:

  • (TrueClass)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 80

def destroy_ami(response, payload, parameters)
  ami_id = payload[:physical_resource_id].split(PHYSICAL_ID_JOINER).last
  begin
    compute_api(parameters[:region]).deregister_image(ami_id)
  rescue Fog::Compute::AWS::Error => e
    warn "Non-fatal error encountered on AMI removal: #{e.class}: #{e}"
    response['Reason'] = e.message
  rescue => e
    error "Failed to remove AMI: #{e.class}: #{e}"
    response['Status'] = 'FAILED'
    response['Reason'] = e.message
  end
  true
end

#execute(message) ⇒ Object

Process message, send value back to CFN

Parameters:

  • message (Carnivore::Message)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 39

def execute(message)
  failure_wrap do |payload|
    cfn_resource = rekey_hash(payload.get(:data, :cfn_resource))
    properties = rekey_hash(cfn_resource[:resource_properties])
    parameters = rekey_hash(properties[:parameters])
    cfn_response = build_response(cfn_resource)
    case cfn_resource[:request_type]
    when :create
      ami_response_update(cfn_response, parameters)
    when :delete
      destroy_ami(cfn_response, cfn_resource, parameters)
    when :update
    else
      error "Unknown request type received: #{cfn_resource[:request_type].inspect}"
      response['Status'] = 'FAILED'
      response['Reason'] = 'Unknown request type received'
    end
    respond_to_stack(cfn_response, cfn_resource[:response_url])
    completed(payload, message)
  end
end