Skip to content

Instantly share code, notes, and snippets.

@nacx
Created November 26, 2012 17:42
Show Gist options
  • Save nacx/4149582 to your computer and use it in GitHub Desktop.
Save nacx/4149582 to your computer and use it in GitHub Desktop.
Load initial data and mocks in the scalability environment
#!/usr/bin/jython
from __future__ import with_statement
import urllib2
import base64
from com.abiquo.model.enumerator import HypervisorType
from com.abiquo.model.enumerator import RemoteServiceType
from kahuna.session import ContextLoader
from org.jclouds.abiquo.domain.cloud import VirtualDatacenter
from org.jclouds.abiquo.domain.exception import AbiquoException
from org.jclouds.abiquo.domain.network import PrivateNetwork
from org.jclouds.abiquo.domain.infrastructure import Datacenter
from org.jclouds.abiquo.domain.infrastructure import Rack
from org.jclouds.abiquo.domain.infrastructure import RemoteService
from org.jclouds.abiquo.predicates.cloud import \
VirtualMachineTemplatePredicates
# Mock configuration
MOCKS = [
"10.60.1.222", # Ignasi
# "10.60.1.249", # Serafin
# "10.60.1.246", # Alessia
"10.60.1.241", # Enric
# "10.60.20.60", # Alessia IT
# "10.60.11.187" # Gerardo
"10.60.1.223" # Albert
# "10.60.12.20" # Mothership
]
MOCK_NUM_PORTS = 1
MOCK_AIM_PORT_START = 8889 # Do not change
MOCK_HYP_PORT_START = 18083 # Do not change
# Number of virtual datacenters to create
VDC_NUM = 10
# Gatling data configuration
GATLING_VMS_IN_VAPP = 4
GATLING_TEMPLATE_NAME = "Core"
GATLING_DATA_FILE = "/home/ibarrera/workspace/api-load-test/" \
"src/main/resources/data/virtualdatacenter.csv"
# Workload rules configuration
FIT_POLICY = "PERFORMANCE"
CPU_PERCENT = 1000
RAM_PERCENT = 1000
# Environment coniguration
API = "10.60.14.2"
RS_CONFIG = {
RemoteServiceType.VIRTUAL_SYSTEM_MONITOR: "10.60.14.6",
RemoteServiceType.VIRTUAL_FACTORY: "10.60.14.7",
RemoteServiceType.BPM_SERVICE: "10.60.14.8",
RemoteServiceType.STORAGE_SYSTEM_MONITOR: "10.60.14.8",
RemoteServiceType.NODE_COLLECTOR: "10.60.14.8",
RemoteServiceType.APPLIANCE_MANAGER: "10.60.14.8"
}
USER = "admin"
PASS = "xabiquo"
ctx = ContextLoader().load()
api = ctx.getApiContext()
print "Creating datacenter"
dc = Datacenter.builder(api) \
.name("Scalability") \
.location("Honolulu") \
.build()
dc.save()
for type, address in RS_CONFIG.items():
print "Adding %s at %s" % (type.name(), address)
rs = RemoteService.builder(api, dc) \
.type(type) \
.ip(address) \
.build()
rs.save()
print "Refreshing template repository"
ent = ctx.getAdministrationService().getCurrentEnterprise()
ent.refreshTemplateRepository(dc)
gatling_template = ent.findTemplateInRepository(dc,
VirtualMachineTemplatePredicates.name(GATLING_TEMPLATE_NAME))
print "Creating rack"
rack = Rack.builder(api, dc).name("Rack").build()
rack.save()
print "Creating machines with %s mocks" % MOCK_NUM_PORTS
nst = dc.defaultNetworkServiceType()
for mock in MOCKS:
print "Adding %s" % mock
m = dc.discoverSingleMachine(mock, HypervisorType.VBOX, "", "")
[ds.setEnabled(True) for ds in m.getDatastores()]
m.getNetworkInterfaces()[0].setNetworkServiceType(nst)
m.setRack(rack)
for i in xrange(MOCK_NUM_PORTS):
hyp_port = MOCK_HYP_PORT_START + i
aim_port = MOCK_AIM_PORT_START + i
print "Adding mock #%s (port: %s, aim: %s)" % (i, hyp_port,
aim_port)
m.unwrap().setId(None) # Allow to reuse the same object in save()
m.setUser("")
m.setPassword("")
m.setPort(hyp_port)
m.setIpmiIp(m.getIp())
m.setIpmiPort(aim_port)
try:
m.save()
except AbiquoException, ex:
print " Failed: %s" % ex.getMessage()
print "Creating %s virtual datacenters" % VDC_NUM
# Also write data to gatling data file
with open(GATLING_DATA_FILE, 'w') as f:
f.write("loginuser,loginpassword,datacenterId,templateId," \
"virtualdatacenterId,numVirtualMachinesInVapp\n")
for i in xrange(VDC_NUM):
print "Adding virtual datacenter #%s (network: 192.168.%s.0)" % (i, i)
net = PrivateNetwork.builder(api) \
.name("Default Network") \
.address("192.168.%s.0" % i) \
.mask(24) \
.gateway("192.168.%s.1" % i) \
.build()
vdc = VirtualDatacenter.builder(api, dc, ent) \
.name("Scalability") \
.network(net) \
.hypervisorType(HypervisorType.VBOX) \
.build()
vdc.save()
f.write("%s,%s,%s,%s,%s,%s\n" % (USER, PASS, dc.getId(),
gatling_template.getId(), vdc.getId(), GATLING_VMS_IN_VAPP))
# A substitute/supplement to urllib2.HTTPErrorProcessor
# that doesn't raise exceptions on status codes 201
class BetterHTTPErrorProcessor(urllib2.BaseHandler):
def http_error_201(self, request, response, code, msg, hdrs):
return response
rules = """<rules>
<fitPolicyRules>
<fitPolicyRule>
<fitPolicy>%(policy)s</fitPolicy>
</fitPolicyRule>
</fitPolicyRules>
<machineLoadRules>
<machineLoadRule>
<link href="http://localhost/api/admin/datacenters/%(datacenter)d"
rel="datacenter"/>
<ramLoadPercentage>%(ram)d</ramLoadPercentage>
<cpuLoadPercentage>%(cpu)d</cpuLoadPercentage>
</machineLoadRule>
</machineLoadRules>
<enterpriseExclusionRules />
</rules>"""
opener = urllib2.build_opener(BetterHTTPErrorProcessor)
urllib2.install_opener(opener)
req = urllib2.Request("http://%s/api/admin/rules?idDatacenter=%s"
% (API, dc.getId()))
auth = "Basic %s" % base64.urlsafe_b64encode("%s:%s" % (USER, PASS))
req.add_header('Authorization', auth)
req.add_header('Accept', 'application/vnd.abiquo.rules+xml')
req.add_header('Content-type', 'application/vnd.abiquo.rules+xml')
req.add_data(rules % {
'policy': FIT_POLICY,
'datacenter': dc.getId(),
'ram': RAM_PERCENT,
'cpu': CPU_PERCENT
})
print "Applying allocation rules (policy: %s, cpu: %s%%, ram: %s%%)" % (
FIT_POLICY, CPU_PERCENT, RAM_PERCENT)
urllib2.urlopen(req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment