mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			138 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			138 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/python
 | 
						|
import os
 | 
						|
import logging
 | 
						|
import sys
 | 
						|
import socket
 | 
						|
import subprocess
 | 
						|
import time
 | 
						|
 | 
						|
from cloudutils.cloudException import CloudRuntimeException, CloudInternalException
 | 
						|
from cloudutils.utilities import initLoging, bash
 | 
						|
from cloudutils.configFileOps import  configFileOps
 | 
						|
from cloudutils.globalEnv import globalEnv
 | 
						|
from cloudutils.networkConfig import networkConfig
 | 
						|
from cloudutils.syscfg import sysConfigFactory
 | 
						|
 | 
						|
from optparse import OptionParser
 | 
						|
    
 | 
						|
url="http://rightscale-cloudstack.s3.amazonaws.com/kvm/centos/5.4/RightImage_CentOS_5.4_x64_v5.6.34.qcow2.bz2"
 | 
						|
destFolder="/mnt/template/tmpl/1/4/"
 | 
						|
metaFile="template.properties"
 | 
						|
 | 
						|
def getUserInputs():
 | 
						|
    print "Welcome to myCloud Setup:"
 | 
						|
 | 
						|
    mgtSvr = "myagent.cloud.com"
 | 
						|
 | 
						|
    cfo = configFileOps("/etc/cloud/agent/agent.properties")
 | 
						|
    oldToken = cfo.getEntry("zone")
 | 
						|
    if oldToken == "default":
 | 
						|
        oldToken = ""
 | 
						|
    zoneToken = raw_input("Please input the Zone Token:[%s]"%oldToken)
 | 
						|
    
 | 
						|
    if zoneToken == "":
 | 
						|
        if oldToken == "":
 | 
						|
            print "Please input a valid zone token"
 | 
						|
            exit(1)
 | 
						|
        zoneToken = oldToken
 | 
						|
 | 
						|
    try:
 | 
						|
        defaultNic = networkConfig.getDefaultNetwork()
 | 
						|
    except:
 | 
						|
        print "Failed to get default route. Please configure your network to add a default route"
 | 
						|
        exit(1)
 | 
						|
        
 | 
						|
    network = defaultNic.name
 | 
						|
 | 
						|
    return [mgtSvr, zoneToken, network]
 | 
						|
 | 
						|
def downloadTemplate():
 | 
						|
    if not os.path.exists(destFolder):
 | 
						|
        os.makedirs(destFolder)
 | 
						|
    oldName =url.split("/")[-1]
 | 
						|
    templateFile=url.split("/")[-1].replace(".bz2","")
 | 
						|
 | 
						|
    templateFullPath = destFolder + templateFile
 | 
						|
    metaFullPath = destFolder + metaFile
 | 
						|
    if os.path.exists(templateFullPath):
 | 
						|
        if os.path.exists(metaFullPath):
 | 
						|
            return True
 | 
						|
        os.remove(templateFullPath) 
 | 
						|
 | 
						|
    print "Need to download myCloud template into your local disk, from " + url + " to " + destFolder + " :"
 | 
						|
    try:
 | 
						|
        proc = subprocess.Popen(["/bin/bash", "-c", "wget -O - " + url + " | bunzip2 > " + destFolder + templateFile])
 | 
						|
        proc.communicate()
 | 
						|
        ret = proc.poll()
 | 
						|
        if ret is None or ret < 0:
 | 
						|
            raise CloudRuntimeException("Failed to download template")
 | 
						|
    except KeyboardInterrupt:
 | 
						|
        if os.path.exists(templateFullPath):
 | 
						|
            os.remove(templateFullPath) 
 | 
						|
        raise CloudRuntimeException("Downloading process is interrupted")
 | 
						|
    
 | 
						|
    file = open(metaFullPath, "w")
 | 
						|
    physicalSize = os.stat(templateFullPath).st_size
 | 
						|
    virtualSize = bash("qemu-img info " + templateFullPath + " |grep virtual").getStdout().split("(")[1].split(" ")[0]
 | 
						|
    cfo = configFileOps(metaFullPath)
 | 
						|
    cfo.addEntry("filename", templateFile)
 | 
						|
    cfo.addEntry("id", "4")
 | 
						|
    cfo.addEntry("qcow2.size", str(physicalSize))
 | 
						|
    cfo.addEntry("public", "true")
 | 
						|
    cfo.addEntry("uniquename", "Rightscale CentOS 5.4")
 | 
						|
    cfo.addEntry("qcow2.virtualsize", virtualSize)
 | 
						|
    cfo.addEntry("virtualsize", virtualSize)
 | 
						|
    cfo.addEntry("hvm", "true")
 | 
						|
    cfo.addEntry("description", "Rightscale CentOS 5.4")
 | 
						|
    cfo.addEntry("qcow2", "true")
 | 
						|
    cfo.addEntry("qcow2.filename", templateFile)
 | 
						|
    cfo.addEntry("size", str(physicalSize))
 | 
						|
    cfo.save()
 | 
						|
 | 
						|
 | 
						|
if __name__ == '__main__':
 | 
						|
    initLoging("/var/log/cloud/setupAgent.log")
 | 
						|
    
 | 
						|
    glbEnv = globalEnv()
 | 
						|
 | 
						|
    glbEnv.mode = "Agent"
 | 
						|
    glbEnv.agentMode = "myCloud"
 | 
						|
    parser = OptionParser()
 | 
						|
    parser.add_option("-z", "--zone-token", dest="zone", help="zone token")
 | 
						|
 | 
						|
    (options, args) = parser.parse_args()
 | 
						|
    if options.zone is None:
 | 
						|
        userInputs = getUserInputs()
 | 
						|
        glbEnv.mgtSvr = userInputs[0]
 | 
						|
        glbEnv.zone = userInputs[1]
 | 
						|
        glbEnv.defaultNic = userInputs[2]
 | 
						|
    else:
 | 
						|
        glbEnv.zone = options.zone
 | 
						|
        try:
 | 
						|
            defaultNic = networkConfig.getDefaultNetwork()
 | 
						|
            glbEnv.defaultNic = defaultNic.name
 | 
						|
        except:
 | 
						|
            print "Failed to get default route. Please configure your network to have a default route"
 | 
						|
            sys.exit(2)
 | 
						|
 | 
						|
    #generate UUID
 | 
						|
    glbEnv.uuid = configFileOps("/etc/cloud/agent/agent.properties").getEntry("guid")
 | 
						|
    if glbEnv.uuid == "":
 | 
						|
            glbEnv.uuid = bash("uuidgen").getStdout()
 | 
						|
        
 | 
						|
    print "Starting to configure your system:"
 | 
						|
    syscfg = sysConfigFactory.getSysConfigFactory(glbEnv)
 | 
						|
    try:
 | 
						|
        syscfg.config()
 | 
						|
        downloadTemplate()
 | 
						|
        syscfg.svo.stopService("cloud-agent")
 | 
						|
        syscfg.svo.enableService("cloud-agent")
 | 
						|
        print "myCloud setup is Done!"
 | 
						|
    except (CloudRuntimeException,CloudInternalException), e:
 | 
						|
        print e
 | 
						|
        print "Try to restore your system:"
 | 
						|
        try:
 | 
						|
            syscfg.restore()
 | 
						|
        except:
 | 
						|
            pass
 |