Class: Jackal::Cfn::AmiManager

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

Overview

Manage AMI Resources

Expected resource:

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

Required configuration:

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

Constant Summary collapse

PHYSICAL_ID_JOINER =
'__-__'

Constants inherited from Resource

Resource::VALID_RESOURCE_STATUS

Instance Method Summary collapse

Methods inherited from Resource

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

Methods included from Utils::Http

#response_endpoint

Methods included from 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



69
70
71
72
73
74
75
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 69

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)


102
103
104
105
106
107
108
109
110
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 102

def compute_api(region)
  ::Fog::Compute.new(
    {:provider => :aws}.merge(
      config.get(: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)


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

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 "Failed to remove AMI: #{e.class}: #{e}"
    response['Reason'] = "Failed to remove AMI resource: #{e}. Ignoring."
  rescue => e
    error "Unexpected error removing 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)


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

def execute(message)
  failure_wrap(message) 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].to_sym
    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}"
      cfn_response['Status'] = 'FAILED'
      cfn_response['Reason'] = 'Unknown request type received'
    end
    respond_to_stack(cfn_response, cfn_resource[:response_url])
    job_completed(:jackal_cfn, payload, message)
  end
end

#setup(*_) ⇒ Object

Ensure fog library is loaded



35
36
37
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 35

def setup(*_)
  require 'fog'
end