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

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

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(opts) ⇒ CloudStackInstance



114
115
116
117
118
# File 'lib/stackmate/participants/cloudstack.rb', line 114

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

Instance Method Details

#createObject



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
# File 'lib/stackmate/participants/cloudstack.rb', line 120

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



199
200
201
202
203
204
205
# File 'lib/stackmate/participants/cloudstack.rb', line 199

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

#deleteObject



162
163
164
165
166
167
168
169
170
# File 'lib/stackmate/participants/cloudstack.rb', line 162

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/stackmate/participants/cloudstack.rb', line 207

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



191
192
193
194
195
196
197
# File 'lib/stackmate/participants/cloudstack.rb', line 191

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



172
173
174
175
176
177
178
179
# File 'lib/stackmate/participants/cloudstack.rb', line 172

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

#user_data(datum, resolved) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/stackmate/participants/cloudstack.rb', line 181

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