Class: SparkleFormation::Translation::Heat

Inherits:
SparkleFormation::Translation show all
Defined in:
lib/sparkle_formation/translation/heat.rb

Overview

Translation for Heat (HOT)

Direct Known Subclasses

Rackspace

Constant Summary collapse

MAP =

Heat translation mapping

{
  :resources => {
    'AWS::EC2::Instance' => {
      :name => 'OS::Nova::Server',
      :finalizer => :nova_server_finalizer,
      :properties => {
        'AvailabilityZone' => 'availability_zone',
        'BlockDeviceMappings' => :nova_server_block_device_mapping,
        'ImageId' => 'image',
        'InstanceType' => 'flavor',
        'KeyName' => 'key_name',
        'NetworkInterfaces' => 'networks',
        'SecurityGroups' => 'security_groups',
        'SecurityGroupIds' => 'security_groups',
        'Tags' => 'metadata',
        'UserData' => :nova_server_user_data
      }
    },
    'AWS::AutoScaling::AutoScalingGroup' => {
      :name => 'OS::Heat::AutoScalingGroup',
      :properties => {
        'Cooldown' => 'cooldown',
        'DesiredCapacity' => 'desired_capacity',
        'MaxSize' => 'max_size',
        'MinSize' => 'min_size',
        'LaunchConfigurationName' => :autoscaling_group_launchconfig
      }
    },
    'AWS::AutoScaling::LaunchConfiguration' => :delete,
    'AWS::ElasticLoadBalancing::LoadBalancer' => {
      :name => 'OS::Neutron::LoadBalancer',
      :finalizer => :neutron_loadbalancer_finalizer,
      :properties => {
        'Instances' => 'members',
        'Listeners' => 'listeners',
        'HealthCheck' => 'health_check',
        'Subnets' => 'subnets'
      }
    },
    'AWS::EC2::VPC' => {
      :name => 'OS::Neutron::Net',
      :finalizer => :neutron_net_finalizer,
      :properties => {
        'CidrBlock' => 'cidr'
      }
    },
    'AWS::EC2::Subnet' => {
      :name => 'OS::Neutron::Subnet',
      :finalizer => :neutron_subnet_finalizer,
      :properties => {
        'CidrBlock' => 'cidr',
        'VpcId' => 'network',
        'AvailabilityZone' => 'availability_zone'
      }
    }
  }
}
REF_MAPPING =
{
  'AWS::StackName' => 'OS::stack_name',
  'AWS::StackId' => 'OS::stack_id',
  'AWS::Region' => 'OS::stack_id' # @todo i see it set in source, but no function. wat
}
FN_MAPPING =
{
  'Fn::GetAtt' => 'get_attr',
  'Fn::Join' => 'list_join'
}

Instance Attribute Summary

Attributes inherited from SparkleFormation::Translation

#logger, #options, #original, #template, #translated

Instance Method Summary collapse

Methods inherited from SparkleFormation::Translation

#apply_function, #apply_rename, #attr_mapping, #dereference, #dereference_processor, #format_properties, #initialize, #map, #mappings, #outputs, #parameters, #rename_processor, #resource_name, #resource_translation, #resources, #translate_default, #translate_resources

Methods included from SparkleAttribute

#_account_id, #_and, #_cf_attr, #_cf_base64, #_cf_get_azs, #_cf_join, #_cf_map, #_cf_ref, #_cf_select, #_condition, #_depends_on, #_equals, #_if, #_no_value, #_not, #_notification_arns, #_on_condition, #_or, #_platform=, #_region, #_stack_id, #_stack_name, #_stack_output, #_system, #debian?, #dynamic!, #nest!, #registry!, #rhel?, #taggable?

Methods included from Utils::AnimalStrings

#camel, #snake

Constructor Details

This class inherits a constructor from SparkleFormation::Translation

Instance Method Details

#autoscaling_group_launchconfig(value, args = {}) ⇒ Array<String, Object>

TODO:

implement

Custom mapping for ASG launch configuration

Parameters:

  • value (Object)

    original property value

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :new_resource (Hash)
  • :new_properties (Hash)
  • :original_resource (Hash)

Returns:

  • (Array<String, Object>)

    name and new value



313
314
315
# File 'lib/sparkle_formation/translation/heat.rb', line 313

def autoscaling_group_launchconfig(value, args={})
  ['resource', value]
end

#complete_launch_config_lb_setupsObject

Update any launch configuration which define load balancers to ensure they are attached to the correct resources when multiple listeners (ports) have been defined resulting in multiple isolated LB resources



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/sparkle_formation/translation/heat.rb', line 172

def complete_launch_config_lb_setups
  translated['resources'].find_all do |resource_name, resource|
    resource['type'] == 'OS::Heat::AutoScalingGroup'
  end.each do |name, value|
    if(lbs = value['properties'].delete('load_balancers'))
      lbs.each do |lb_ref|
        lb_name = resource_name(lb_ref)
        lb_resource = translated['resources'][lb_name]
        vip_resources = translated['resources'].find_all do |k, v|
          k.match(/#{lb_name}Vip\d+/) && v['type'] == 'OS::Neutron::LoadBalancer'
        end
        value['properties']['load_balancers'] = vip_resources.map do |vip_name|
          {'get_resource' => vip_name}
        end
      end
    end
  end
  true
end

#default_key_format(key) ⇒ String

Default keys to snake cased format (underscore)

Parameters:

  • key (String, Symbol)

Returns:

  • (String)


321
322
323
# File 'lib/sparkle_formation/translation/heat.rb', line 321

def default_key_format(key)
  snake(key)
end

#neutron_loadbalancer_finalizer(resource_name, new_resource, old_resource) ⇒ Object

Finalizer for the neutron load balancer resource. This finalizer may generate new resources if the load balancer has multiple listeners defined (neutron lb implementation defines multiple isolated resources sharing a common virtual IP)

Parameters:

  • resource_name (String)
  • new_resource (Hash)
  • old_resource (Hash)

Returns:

  • (Object)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sparkle_formation/translation/heat.rb', line 77

def neutron_loadbalancer_finalizer(resource_name, new_resource, old_resource)
  listeners = new_resource['Properties'].delete('listeners') || []
  healthcheck = new_resource['Properties'].delete('health_check')
  subnet = (new_resource['Properties'].delete('subnets') || []).first

  # if health check is provided, create resource and apply to
  # all pools generated
  if(healthcheck)
    healthcheck_name = "#{resource_name}HealthCheck"
    check = {
      healthcheck_name => {
        'Type' => 'OS::Neutron::HealthMonitor',
        'Properties' => {}.tap{ |properties|
          {'Timeout' => 'timeout', 'Interval' => 'delay', 'HealthyThreshold' => 'max_retries'}.each do |aws, hot|
            if(healthcheck[aws])
              properties[hot] = healthcheck[aws]
            end
          end
          type, port, path = healthcheck['Target'].split(/(:|\/.*)/).find_all{|x| x != ':'}
          properties['type'] = type
          if(path)
            properties['url_path'] = path
          end
        }
      }
    }
    translated['Resources'].merge!(check)
  end

  base_listener = listeners.shift
  base_pool_name = "#{resource_name}Pool"
  base_pool = {
    base_pool_name => {
      'Type' => 'OS::Neutron::Pool',
      'Properties' => {
        'lb_method' => 'ROUND_ROBIN',
        'monitors' => [
          {'get_resource' => healthcheck_name}
        ],
        'protocol' => base_listener['Protocol'],
        'vip' => {
          'protocol_port' => base_listener['LoadBalancerPort']
        },
        'subnet' => subnet
      }
    }
  }
  if(healthcheck)
    base_pool[base_pool_name]['Properties'].merge(
      'monitors' => [
        {'get_resource' => healthcheck_name}
      ]
    )
  end

  translated['Resources'].merge!(base_pool)
  new_resource['Properties']['pool_id'] = {'get_resource' => base_pool_name}
  new_resource['Properties']['protocol_port'] = base_listener['InstancePort']

  listeners.each_with_index do |listener, count|
    pool_name = "#{resource_name}PoolVip#{count}"
    pool = {
      pool_name => {
        'Type' => 'OS::Neutron::Pool',
        'Properties' => {
          'lb_method' => 'ROUND_ROBIN',
          'protocol' => listener['Protocol'],
          'subnet' => subnet,
          'vip' => {
            'protocol_port' => listener['LoadBalancerPort']
          }
        }
      }
    }
    if(healthcheck)
      pool[pool_name]['Properties'].merge(
        'monitors' => [
          {'get_resource' => healthcheck_name}
        ]
      )
    end

    lb_name = "#{resource_name}Vip#{count}"
    lb = {lb_name => MultiJson.load(MultiJson.dump(new_resource))}
    lb[lb_name]['Properties']['pool_id'] = {'get_resource' => pool_name}
    lb[lb_name]['Properties']['protocol_port'] = listener['InstancePort']
    translated['Resources'].merge!(pool)
    translated['Resources'].merge!(lb)
  end
end

#neutron_net_finalizer(resource_name, new_resource, old_resource) ⇒ TrueClass

Finalizer for the neutron net resource. Scrub properties.

Parameters:

  • resource_name (String)
  • new_resource (Hash)
  • old_resource (Hash)

Returns:

  • (TrueClass)


284
285
286
287
# File 'lib/sparkle_formation/translation/heat.rb', line 284

def neutron_net_finalizer(resource_name, new_resource, old_resource)
  new_resource['Properties'].clear
  true
end

#neutron_subnet_finalizer(resource_name, new_resource, old_resource) ⇒ TrueClass

Finalizer for the neutron subnet resource. Creates a stub network to attach subnet if availability zones are defined (aws classic)

Parameters:

  • resource_name (String)
  • new_resource (Hash)
  • old_resource (Hash)

Returns:

  • (TrueClass)


266
267
268
269
270
271
272
273
274
275
276
# File 'lib/sparkle_formation/translation/heat.rb', line 266

def neutron_subnet_finalizer(resource_name, new_resource, old_resource)
  azs = new_resource['Properties'].delete('availability_zone')
  if(azs)
    network_name = "NetworkFor#{resource_name}"
    translated['Resources'][network_name] = {
      'type' => 'OS::Neutron::Network'
    }
    new_resource['Properties']['network'] = {'get_resource' => network_name}
  end
  true
end

#nova_server_block_device_mapping(value, args = {}) ⇒ Array<String, Object>

TODO:

implement

Custom mapping for block device

Parameters:

  • value (Object)

    original property value

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :new_resource (Hash)
  • :new_properties (Hash)
  • :original_resource (Hash)

Returns:

  • (Array<String, Object>)

    name and new value



201
202
203
# File 'lib/sparkle_formation/translation/heat.rb', line 201

def nova_server_block_device_mapping(value, args={})
  ['block_device_mapping', value]
end

#nova_server_finalizer(resource_name, new_resource, old_resource) ⇒ Object

Finalizer for the nova server resource. Fixes bug with remotes in metadata

Parameters:

  • resource_name (String)
  • new_resource (Hash)
  • old_resource (Hash)

Returns:

  • (Object)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/sparkle_formation/translation/heat.rb', line 226

def nova_server_finalizer(resource_name, new_resource, old_resource)
  if(old_resource['Metadata'])
    new_resource['Metadata'] = old_resource['Metadata']
    proceed = new_resource['Metadata'] &&
      new_resource['Metadata']['AWS::CloudFormation::Init'] &&
      config = new_resource['Metadata']['AWS::CloudFormation::Init']['config']
    if(proceed)
      # NOTE: This is a stupid hack since HOT gives the URL to
      # wget directly and if special characters exist, it fails
      if(files = config['files'])
        files.each do |key, args|
          if(args['source'])
            if(args['source'].is_a?(String))
              args['source'].replace("\"#{args['source']}\"")
            else
              args['source'] = {
                'Fn::Join' => [
                  "", [
                    "\"",
                    args['source'],
                    "\""
                  ]
                ]
              }
            end
          end
        end
      end
    end
  end
end

#nova_server_user_data(value, args = {}) ⇒ Array<String, Object>

Custom mapping for server user data

Parameters:

  • value (Object)

    original property value

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :new_resource (Hash)
  • :new_properties (Hash)
  • :original_resource (Hash)

Returns:

  • (Array<String, Object>)

    name and new value



213
214
215
216
217
# File 'lib/sparkle_formation/translation/heat.rb', line 213

def nova_server_user_data(value, args={})
  args[:new_properties][:user_data_format] = 'RAW'
  args[:new_properties][:config_drive] = 'true'
  [:user_data, Hash[value.values.first]]
end

#resource_finalizer(resource_name, new_resource, old_resource) ⇒ TrueClass

Finalizer applied to all new resources

Parameters:

  • resource_name (String)
  • new_resource (Hash)
  • old_resource (Hash)

Returns:

  • (TrueClass)


295
296
297
298
299
300
301
302
# File 'lib/sparkle_formation/translation/heat.rb', line 295

def resource_finalizer(resource_name, new_resource, old_resource)
  %w(DependsOn Metadata).each do |key|
    if(old_resource[key] && !new_resource[key])
      new_resource[key] = old_resource[key]
    end
  end
  true
end

#translate!TrueClass

TODO:

still needs replacements of functions and pseudo-params

Note:

this is an override to return in proper HOT format

Translate stack definition

Returns:

  • (TrueClass)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sparkle_formation/translation/heat.rb', line 11

def translate!
  super
  cache = MultiJson.load(MultiJson.dump(translated))
  # top level
  cache.each do |k,v|
    translated.delete(k)
    translated[snake(k).to_s] = v
  end
  # params
  cache.fetch('Parameters', {}).each do |k,v|
    translated['parameters'][k] = Hash[
      v.map do |key, value|
        if(key == 'Type')
          [snake(key).to_s, value.downcase]
        elsif(key == 'AllowedValues')
          # @todo fix this up to properly build constraints
          ['constraints', [{'allowed_values' => value}]]
        else
          [snake(key).to_s, value]
        end
      end
    ]
  end
  # resources
  cache.fetch('Resources', {}).each do |r_name, r_value|
    translated['resources'][r_name] = Hash[
      r_value.map do |k,v|
        [snake(k).to_s, v]
      end
    ]
  end
  # outputs
  cache.fetch('Outputs', {}).each do |o_name, o_value|
    translated['outputs'][o_name] = Hash[
      o_value.map do |k,v|
        [snake(k).to_s, v]
      end
    ]
  end
  translated.delete('awstemplate_format_version')
  translated['heat_template_version'] = '2013-05-23'
  # no HOT support for mappings, so remove and clean pseudo
  # params in refs
  if(translated['resources'])
    translated['resources'] = dereference_processor(translated['resources'], ['Fn::FindInMap', 'Ref'])
    translated['resources'] = rename_processor(translated['resources'])
  end
  if(translated['outputs'])
    translated['outputs'] = dereference_processor(translated['outputs'], ['Fn::FindInMap', 'Ref'])
    translated['outputs'] = rename_processor(translated['outputs'])
  end
  translated.delete('mappings')
  complete_launch_config_lb_setups
  true
end