Class: Topo::Provision::CloudFormationConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/topo/converter/cloudformation/converter.rb

Constant Summary collapse

FIELDS =
{
  :boolean => {
    :bootstrap_options => %w[disable_api_termination associate_public_ip_address 
      ebs_optimized dedicated_tenancy monitoring monitoring_enabled ],
    :network_interface => %w[ requester_managed source_dest_check delete_on_termination ],
    :block_device_mapping_ebs => %w[delete_on_termination]

  },
  :integer => {
    :listener => %w[instance_port load_balancer_port],
    :network_interface => %w[ device_index ],
    :block_device_mapping_ebs => %w[volume_size iops],
    :auto_scaling_group => %w[max_size min_size desired_capacity cooldown default_cooldown health_check_grace_period]
  },
  :maptov1 => {
    :listener => { "load_balancer_port" => "port" },
    :bootstrap_options => { "monitoring" => "monitoring_enabled" },
    :launch_config => { "monitoring" => "detailed_instance_monitoring" }
  }
}

Instance Attribute Summary

Attributes inherited from Converter

#input, #topology

Instance Method Summary collapse

Methods inherited from Converter

convert, converter, #initialize, register_converter

Methods included from Topo::ParseGen

#convert_keys_to_sym, #convert_keys_to_sym_deep, #expand_ref, #lazy_attribute_to_s, #topo_refs, #value_from_path

Constructor Details

This class inherits a constructor from Topo::Converter

Instance Method Details

#camel_to_underscore(str) ⇒ Object



172
173
174
175
176
# File 'lib/topo/converter/cloudformation/converter.rb', line 172

def camel_to_underscore(str)
  str.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    downcase
end

#convert(data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/topo/converter/cloudformation/converter.rb', line 28

def convert(data)
  @input = data if data
  
  @output['name'] = @input['name'] || "CloudFormation"
  
  create_provisioning

  @input['Resources'].each do |name, resource|
    convert_resource(name, resource)
  end
  
  @output
end

#convert_auto_scaling_group(name, resource) ⇒ Object



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
167
168
# File 'lib/topo/converter/cloudformation/converter.rb', line 135

def convert_auto_scaling_group(name, resource)
  
  node = { 
    "name" => name,
    "provisioning" => { "machine_options" => {}, "node_group" => { "auto_scaling" => {}} }       
  }
  # find asg properties 
  asg_props = value_from_path(resource, %w[Properties]).clone || {}
  asg_props = fields_to_i(:auto_scaling_group, keys_to_underscore(asg_props))
  asg_props.delete("launch_configuration_name")
  asg_props.delete("load_balancer_names")
  %w[availability_zones load_balancers].each do |key|
    node["provisioning"]["node_group"]["auto_scaling"][key] = asg_props[key] if asg_props.key? key
    asg_props.delete(key)
  end 
  node["provisioning"]["node_group"]["auto_scaling"]["group_options"] = asg_props
            
  # find the launch config
  lc_name = value_from_path(resource, %w[Properties LaunchConfigurationName Ref])
  lc_props = keys_to_underscore(value_from_path(@input, ['Resources', lc_name, 'Properties'])) || {}
  lc_props = fields_to_bool(:bootstrap_options, lc_props)
  node["provisioning"]["node_group"]["auto_scaling"]["launch_configuration_options"] = lc_props
  
  # find the load balancers
  lb_refs = value_from_path(resource, %w[Properties LoadBalancerNames]) || []
  load_balancers = lb_refs.map{|lb_ref| lb_ref['Ref'] }
  # TODO should this be a topo ref instead? - needs to pick up load balancer name
  if load_balancers.length > 0
    node["provisioning"]["node_group"]["auto_scaling"]["load_balancers"] = load_balancers
  end
  
  node
  
end

#convert_instance(name, resource) ⇒ Object



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
# File 'lib/topo/converter/cloudformation/converter.rb', line 93

def convert_instance(name, resource)

  instance = { 
    "name" => name,
    "provisioning" => { "machine_options" => {} }
  }
    
  props = value_from_path(resource, ["Properties"]) || {}
  props = keys_to_underscore props
  props = fields_to_v1(:bootstrap_options, fields_to_bool(:bootstrap_options, props))
  
  # Need to convert object arrays (TODO helpers)
  if props["tags"]
    props["tags"].each_with_index do | tag, index | 
      props["tags"][index] = keys_to_underscore(tag) 
      STDERR.puts "WARN: Setting AWS tags on instances is not supported yet"
      instance["provisioning"]["tags"] = props["tags"]
      props.delete("tags")
    end
  end
  if props["network_interfaces"]
    props["network_interfaces"].each_with_index do | network_interface, index | 
      temp = fields_to_i(:network_interface, keys_to_underscore(network_interface))
      temp = fields_to_bool(:network_interface, temp) 
      props["network_interfaces"][index] = temp
      # if one of the network interfaces had associate_public_ip_address: true, then
      # set that in bootstrap
      if temp['associate_public_ip_address']
        props['associate_public_ip_address'] = temp['associate_public_ip_address']
      end
    end
    STDERR.puts "WARN: Setting network_interfaces on instances is not supported"
    instance["provisioning"]["network_interfaces"] = props["network_interfaces"]
    props.delete("network_interfaces")
  end
  
  instance["provisioning"]["machine_options"]["bootstrap_options"] = props
  
  instance
  
end

#convert_resource(name, resource) ⇒ Object

CloudFormation keys are camelcase - convert to underscore CloudFormation values are all strings - convert to proper type CloudFormation is using AWS V2, chef-provisioning-aws is using AWS V1



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/topo/converter/cloudformation/converter.rb', line 71

def convert_resource(name, resource)
  
  props = value_from_path(resource, ["Properties"]) || {}
  props = keys_to_underscore props
   
  case resource['Type']
  when "AWS::EC2::Instance"
    @output['nodes'] << convert_instance(name, resource)

  when "AWS::ElasticLoadBalancing::LoadBalancer"
    listeners = value_from_path(props, ["listeners"]) || []
    props["listeners"] = listeners.map{|listener|             
      fields_to_v1(:listener, fields_to_i(:listener, keys_to_underscore(listener)))
    } 
    @output['services'] << { "name" => name, "type" => "load_balancer", 
      "provisioning" => { "load_balancer_options" => props } }

  when "AWS::AutoScaling::AutoScalingGroup"
    @output['nodes'] << convert_auto_scaling_group(name, resource)
  end
end

#create_provisioningObject



43
44
45
# File 'lib/topo/converter/cloudformation/converter.rb', line 43

def create_provisioning
  @output['provisioning'] = {'driver' => "aws", 'machine_options'=> {  }}
end

#fields_to_bool(field_name, hash) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/topo/converter/cloudformation/converter.rb', line 197

def fields_to_bool(field_name, hash)
  FIELDS[:boolean][field_name].each do | key |
    if hash.key? key
      hash[key] = (hash[key].downcase == "true")
    end
  end
  hash
end

#fields_to_i(field_name, hash) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/topo/converter/cloudformation/converter.rb', line 189

def fields_to_i(field_name, hash)
  FIELDS[:integer][field_name].each do | key |
    hash[key] = hash[key].to_i if hash.key? key
  end
 
  hash
end

#fields_to_v1(field_name, hash) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/topo/converter/cloudformation/converter.rb', line 206

def fields_to_v1(field_name, hash)
    FIELDS[:maptov1][field_name].each do | key, value |
     if hash.key? key
       hash[value] = hash[key]
       hash.delete(key)
     end
    end
   
    hash
end

#keys_to_underscore(hash) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/topo/converter/cloudformation/converter.rb', line 178

def keys_to_underscore(hash)
  converted = Hash[hash.map{|key, val|
    if (val.kind_of? Hash)
      [ camel_to_underscore(key), keys_to_underscore(val) ]
    else
      [camel_to_underscore(key), val]
    end
  }]
end