Skip to content

Instantly share code, notes, and snippets.

@double-z
Forked from gburd/README.md
Created November 27, 2012 05:56
Show Gist options
  • Save double-z/4152612 to your computer and use it in GitHub Desktop.
Save double-z/4152612 to your computer and use it in GitHub Desktop.
CloudStack DevCloud bootstrap
#!/usr/bin/env python
# encoding: utf-8
"""
Utility script to configure DevCloud
Assumes integration.api.port setting has been set to 8096
"""
import sys
import os
import time
import urllib
import CloudStack
from xml.dom.minidom import parseString
def GetRootId(url):
req=url + 'command=listUsers'
dom=parseString(urllib.urlopen(req).read())
return dom.getElementsByTagName('id')[0].toxml().replace('<id>','').replace('</id>','')
def EnableEC2(url):
'Sets enable.ec2.api Global Setting to TRUE'
req=url + 'command=updateConfiguration&name=enable.ec2.api&value=true'
return urllib.urlopen(req).read()
def EnableS3(url):
'Sets enable.s3.api Global Settting to TRUE'
req=url + 'command=updateConfiguration&name=enable.s3.api&value=true'
return urllib.urlopen(req).read()
def GetKeys(url,rootid):
req=url + 'command=registerUserKeys&id=' + rootid
dom=parseString(urllib.urlopen(req).read())
apikey = dom.getElementsByTagName('apikey')[0].toxml().replace('<apikey>','').replace('</apikey>','')
secretkey = dom.getElementsByTagName('secretkey')[0].toxml().replace('<secretkey>','').replace('</secretkey>','')
return (apikey,secretkey)
def main():
'Uses the integration api, no need for authentication and signing'
url = 'http://localhost:8096/client/api?'
rootid = GetRootId(url)
(apikey,secretkey) = GetKeys(url,rootid)
print apikey
print secretkey
'Enable EC2 and S3 api for testing of cloudbridge'
EnableEC2(url)
EnableS3(url)
'''
With the root user api keys we can now authenticate and sign api calls
We could keep on using the integration api but that way I can use the CloudStack module
'''
apiurl = 'http://localhost:8080/client/api'
cloudstack = CloudStack.Client(apiurl,apikey,secretkey)
'Create DevCloud Infrastructure'
devcloudZone = {'dns1':'8.8.8.8','internaldns1':'10.0.2.3','name':'devcloud','networktype':'Basic','securitygroupenabled':'False'}
zones = cloudstack.createZone(devcloudZone)
print zones
'Assume no other zones in the mgt server'
zoneid = zones['zone']['id']
print zoneid
res=cloudstack.createPhysicalNetwork({'name':'devcloud','zoneid':zoneid})
physnetid=res['id']
'Physical Network needs to be enabled before creating a Guest Network...'
jobres = cloudstack.queryAsyncJobResult({'jobid':res['jobid']})
print jobres
while jobres['jobstatus'] != 1:
time.sleep(2)
jobres = cloudstack.queryAsyncJobResult({'jobid':res['jobid']})
cloudstack.updatePhysicalNetwork({'id':physnetid,'state':'Enabled'})
cloudstack.addTrafficType({'physicalnetworkid':physnetid,'traffictype':'Guest'})
cloudstack.addTrafficType({'physicalnetworkid':physnetid,'traffictype':'Management'})
'Enable the VirtualRouter for the physical network, need to configure it first'
nspid = cloudstack.listNetworkServiceProviders({'name':'VirtualRouter'})[0]['id']
print nspid
vrprovid = cloudstack.listVirtualRouterElements({'nspid':nspid})[0]['id']
res = cloudstack.configureVirtualRouterElement({'id':vrprovid,'enabled':'True'})
confres = cloudstack.queryAsyncJobResult({'jobid':res['jobid']})
while confres['jobstatus'] != 1:
time.sleep(2)
confres = cloudstack.queryAsyncJobResult({'jobid':res['jobid']})
'Update Network Serive Provider'
upnetprovres = cloudstack.updateNetworkServiceProvider({'id':nspid,'state':'Enabled'})
jobres = cloudstack.queryAsyncJobResult({'jobid':upnetprovres['jobid']})
while jobres['jobstatus'] != 1:
time.sleep(2)
jobres = cloudstack.queryAsyncJobResult({'jobid':upnetprovres['jobid']})
print cloudstack.listNetworkServiceProviders({'physicalnetworkid':physnetid})
'''
To create a Guest Network, we need to get the Network Offering ID for a Shared Network (for instance)
'''
offerings = cloudstack.listNetworkOfferings({'name':'DefaultSharedNetworkOffering'})
print offerings
'The first offering is the one with security group, does not seem to be an exact match on name'
NetworkOfferingId = offerings[1]['id']
print 'netofferid = ' + NetworkOfferingId
devcloudGuestNetwork = {'displaytext':'devcloud','name':'devcloud','networkofferingid':NetworkOfferingId,'zoneid':zoneid, \
'physicalnetworkid':physnetid}
netwrk = cloudstack.createNetwork(devcloudGuestNetwork)
netid = netwrk['network']['id']
devcloudPod = {'gateway':'10.0.2.2','name':'devcloud','netmask':'255.255.255.0','startip':'10.0.2.200','endip':'10.0.2.220','zoneid':zoneid}
pod = cloudstack.createPod(devcloudPod)
podid = pod['pod']['id']
vlanrange={'startip':'10.0.2.100','endip':'10.0.2.199','gateway':'10.0.2.2','netmask':'255.255.255.0', \
'networkid':netid,'podid':podid,'forvirtualnetwork':'false'}
cloudstack.createVlanIpRange(vlanrange)
'''
devcloudSecondaryStorage = {'name'='devcloud','url'='10.0.2.15','zoneid'=zoneid}
cloudstack.createStoragePool(devcloudSecondaryStorage)
'''
devcloudCluster = {'clustername':'devcloud','clustertype':'CloudManaged','hypervisor':'XenServer','zoneid':zoneid,'podid':podid}
cluster = cloudstack.addCluster(devcloudCluster)
print cluster
clusterid=cluster['cluster'][0]['id']
devcloudHost = {'hypervisor':'XenServer','password':'password','url':'http://10.0.2.15/','username':'root','zoneid':zoneid,'podid':podid,'clusterid':clusterid}
cloudstack.addHost(devcloudHost)
cloudstack.addSecondaryStorage({'url':'nfs://10.0.2.15/opt/storage/secondary','zoneid':zoneid})
print cloudstack.updateZone({'id':zoneid,'allocationstate':'Enabled'})
print 'Zoneid = ', zoneid
print
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment