def createInstance(self):
        try:
            if Environment.checkOrSet() == False:
                self.result = False
                logging.error("Failed to set environment credential")
            else:
                nr_available_vcpus = utils.get_nr_available_vcpus()

                if nr_available_vcpus < cvl_config.VM_CPU[self.cpu]:
                    message = "Required CPU " + cvl_config.VM_CPU[self.cpu] + " exceedes quota available CPU " + nr_available_vcpus
                    logging.error(message)
                    return False

                imageId = cvl_config.VM_IMAGE_ID[int(self.driver)]
                show = "nova boot " + "--flavor " + str(self.cpu) + " --image " + imageId + " --key_name " + cvl_config.VM_KEY_NAME + " --security_groups " + cvl_config.VM_SECURITY_GROUP + " \"" + self.serverName + "\""
                command = MultiOutputCommand(show)
                if command.run() == False:
                    self.result = False
                    logging.error("Failed to run command")
                else:
                    output = command.getOutput()
                    logging.debug('Output of nova boot command: ' + output)

                    for data in output:
                        dataList = data.split(":")
                        if dataList.__len__() > 0:
                            if dataList[0].strip() == "ERROR":
                                logging.error("Failed to launch an instance")
                                self.result = False
        except:
            self.result = False
            logging.error("Exception: " + traceback.format_exc())
        finally:
            logging.debug('Returning:', self.result)
            return self.result
    def run(self):
        # open('/tmp/debug.txt', 'w').write(str(self.action) + '\n'); return True

        try:
            assert Environment.checkOrSet()
            if self.action == enums.Action.CreateServer:
                self.result = self.CreateServer()            

            elif self.action == enums.Action.JoinServer:
                self.result = self.JoinServer()

            elif self.action == enums.Action.DeleteServer:
                self.result = self.DeleteServer()

            elif self.action == enums.Action.ChangePassword:
                self.result = self.ChangePassword()
            
            elif self.action == enums.Action.CreateVmUserAccount:
                self.result = self.CreateVmUserAccount()
            
            elif self.action == enums.Action.DeleteVmUserAccount:
                logging.debug("Delete VM user account")
                self.result = self.DeleteVmUserAccount()

            elif self.action == enums.Action.TerminateServer:
                logging.debug("Terminate VM instance")
                self.result = self.TerminateServer()

            elif self.action == enums.Action.AddServer:
                logging.debug("AddServer")
                self.result = self.AddServer()

            else:
                logging.error("Unsupported action: " + self.action)
                return False
            
        except:
            logging.error("Exeption: " + traceback.format_exc())                 
        finally:
            return self.result 
#!/usr/bin/python

from CvlEnvironment import Environment
import sys, traceback
import logging
import utils
import cvl_config

if len(sys.argv) < 2:
    logging.error("Missing argument: number of requested CPUs")
    sys.exit(1)

numberCpu = sys.argv[1]
try:
    if Environment.checkOrSet() == False:
        logging.error("Failed to set environment credential")
        sys.exit(1)
    else:
        availableCpu = utils.get_nr_available_vcpus()
        logging.debug("numberCpu index: " + str(numberCpu) + " number cpu: " + str(cvl_config.VM_CPU[int(numberCpu)]) + " available CPU: " + str(availableCpu));
        if availableCpu <= cvl_config.QUOTA_THRESHOLD:

            utils.send_email(cvl_config.CVL_HELP_EMAIL,
                             "CVL project resource warning",
                             "Warning: there are only " + str(availableCpu) + " CPUs available in the %s tenancy." % (utils.nova_client().project_id,))

        if availableCpu < cvl_config.VM_CPU[int(numberCpu)]:
            message = "Resources for " + str(cvl_config.VM_CPU[int(numberCpu)]) + " CPUs are not available"
            print message
            logging.error(message)
        print "OK"