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

#initializeCloudStackInstance

Returns a new instance of CloudStackInstance.



68
69
70
71
72
# File 'lib/stackmate/participants/cloudstack.rb', line 68

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

Instance Method Details

#default_zone_idObject



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

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

#image_id(imgstring, resolved, mappings) ⇒ 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
# File 'lib/stackmate/participants/cloudstack.rb', line 135

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



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

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

#on_workitemObject



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

def on_workitem
  myname = workitem.participant_name
  @name = myname
  resolved = workitem.fields['ResolvedNames']
  resolved['AWS::StackId'] = workitem.fei.wfid #TODO put this at launch time
  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}")

  reply
end

#user_data(datum, resolved) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/stackmate/participants/cloudstack.rb', line 109

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