Class: StackMate::CloudStackInstance

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

Instance Attribute Summary

Attributes inherited from CloudStackResource

#name

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(opts) ⇒ CloudStackInstance

Returns a new instance of CloudStackInstance.



70
71
72
73
74
# File 'lib/stackmate/participants/cloudstack.rb', line 70

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

Instance Method Details

#default_zone_idObject



137
138
139
140
141
142
143
# File 'lib/stackmate/participants/cloudstack.rb', line 137

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

#image_id(imgstring, resolved, mappings) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/stackmate/participants/cloudstack.rb', line 145

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



129
130
131
132
133
134
135
# File 'lib/stackmate/participants/cloudstack.rb', line 129

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



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

def on_workitem
  workitem[participant_name] = {}
  myname = participant_name
  @name = myname
  resolved = workitem.fields['ResolvedNames']
  props = workitem.fields['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.fields['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_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
  reply
end

#user_data(datum, resolved) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/stackmate/participants/cloudstack.rb', line 119

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