Class: StackMate::CloudStackInstance

Inherits:
CloudStackResource show all
Defined in:
lib/stackmate/participants/cloudstack.rb

Constant Summary

Constants included from Resolver

Resolver::INTEXP, Resolver::STRINGEXP, Resolver::UUIDEXP

Instance Attribute Summary

Attributes inherited from CloudStackResource

#name

Instance Method Summary collapse

Methods inherited from CloudStackResource

#set_metadata

Methods included from Resolver

#get_named_tag, #get_resolved, #recursive_resolve, #resolve_tags, #resolve_to_deviceid, #validate_param

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(opts) ⇒ CloudStackInstance

Returns a new instance of CloudStackInstance.



155
156
157
158
159
# File 'lib/stackmate/participants/cloudstack.rb', line 155

def initialize(opts)
  super (opts)
  @localized = {}
  load_local_mappings()
end

Instance Method Details

#createObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/stackmate/participants/cloudstack.rb', line 161

def create
  workitem[participant_name] = {}
  myname = participant_name
  @name = myname
  resolved = workitem['ResolvedNames']
  props = workitem['Resources'][workitem.participant_name]['Properties']
  security_group_names = []
  props['SecurityGroups'].each do |sg|
    sg_name = resolved[sg['Ref']]
    security_group_names << sg_name
  end
  keypair = resolved[props['KeyName']['Ref']] if props['KeyName']
  userdata = nil
  if props['UserData']
    userdata = user_data(props['UserData'], resolved)
  end
  templateid = image_id(props['ImageId'], resolved, workitem['Mappings'])
  templateid = @localized['templates'][templateid] if @localized['templates']
  svc_offer = resolved[props['InstanceType']['Ref']] #TODO fragile
  svc_offer = @localized['service_offerings'][svc_offer] if @localized['service_offerings']
  args = { 'serviceofferingid' => svc_offer,
           'templateid' => templateid,
           'zoneid' => default_zone_id,
           'securitygroupnames' => security_group_names.join(','),
           'displayname' => myname,
           #'name' => myname
           }
  args['keypair'] = keypair if keypair
  args['userdata'] = userdata if userdata
  resultobj = make_async_request('deployVirtualMachine', args)
  logger.debug("Created resource #{myname}")

  logger.debug("result = #{resultobj.inspect}")
  workitem[participant_name][:physical_id] = resultobj['virtualmachine']['id']
  workitem[participant_name][:AvailabilityZone] = resultobj['virtualmachine']['zoneid']
  ipaddress = resultobj['virtualmachine']['nic'][0]['ipaddress']
  workitem[participant_name][:PrivateDnsName] = ipaddress
  workitem[participant_name][:PublicDnsName] = ipaddress
  workitem[participant_name][:PrivateIp] = ipaddress
  workitem[participant_name][:PublicIp] = ipaddress
end

#default_zone_idObject



240
241
242
243
244
245
246
# File 'lib/stackmate/participants/cloudstack.rb', line 240

def default_zone_id
  if @localized['zoneid']
    @localized['zoneid']
  else
    '1'
  end
end

#deleteObject



203
204
205
206
207
208
209
210
211
# File 'lib/stackmate/participants/cloudstack.rb', line 203

def delete
  logger.info "In delete #{participant_name}"
  return nil if !workitem[participant_name]
  physical_id = workitem[participant_name]['physical_id']
  if physical_id
    args = {'id' => physical_id}
    del_resp = make_async_request('destroyVirtualMachine', args)
  end
end

#image_id(imgstring, resolved, mappings) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/stackmate/participants/cloudstack.rb', line 248

def image_id(imgstring, resolved, mappings)
  #TODO convoluted logic only handles the cases
  #ImageId : {"Ref" : "FooBar"}
  #ImageId : { "Fn::FindInMap" : [ "Map1", { "Ref" : "OuterKey" },
  # { "Fn::FindInMap" : [ "Map2", { "Ref" : "InnerKey" }, "InnerVal" ] } ] },
  #ImageId : { "Fn::FindInMap" : [ "Map1", { "Ref" : "Key" }, "Value" ] } ] },
  if imgstring['Ref']
    return resolved[imgstring['Ref']]
  else
    if imgstring['Fn::FindInMap']
      key = resolved[imgstring['Fn::FindInMap'][1]['Ref']]
      #print "Key = ", key, "\n"
      if imgstring['Fn::FindInMap'][2]['Ref']
        val = resolved[imgstring['Fn::FindInMap'][2]['Ref']]
        #print "Val [Ref] = ", val, "\n"
      else
        if imgstring['Fn::FindInMap'][2]['Fn::FindInMap']
          val = image_id(imgstring['Fn::FindInMap'][2], resolved, mappings)
          #print "Val [FindInMap] = ", val, "\n"
        else
          val = imgstring['Fn::FindInMap'][2]
        end
      end
    end
    return mappings[imgstring['Fn::FindInMap'][0]][key][val]
  end
end

#load_local_mappingsObject



232
233
234
235
236
237
238
# File 'lib/stackmate/participants/cloudstack.rb', line 232

def load_local_mappings()
  begin
    @localized = YAML.load_file('local.yml')
  rescue
    logger.warning "Warning: Failed to load localized mappings from local.yaml\n"
  end
end

#on_workitemObject



213
214
215
216
217
218
219
220
# File 'lib/stackmate/participants/cloudstack.rb', line 213

def on_workitem
  if workitem['params']['operation'] == 'create'
    create
  else
    delete
  end
  reply
end

#user_data(datum, resolved) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'lib/stackmate/participants/cloudstack.rb', line 222

def user_data(datum, resolved)
  #TODO make this more general purpose
  actual = datum['Fn::Base64']['Fn::Join']
  delim = actual[0]
  data = actual[1].map { |d|
    d.kind_of?(Hash) ? resolved[d['Ref']]: d
  }
  Base64.urlsafe_encode64(data.join(delim))
end