Example #1
0
    def set_active_cpus(self, active_cpus_tgt_nr):
        '''
        TBD
        '''        
        _cpu_state = self.get_cpus_state()
        
        _total_cpus = len(_cpu_state)
        
        _active_cpus = []
        _inactive_cpus = []
        for _cpu_number, _cpu_state in enumerate(_cpu_state) :
            if _cpu_state == '1' and _cpu_number > 0 :
                _active_cpus.append(_cpu_number)
            elif _cpu_state == '0' :
                _inactive_cpus.append(_cpu_number)
        
        active_cpus_tgt_nr = int(active_cpus_tgt_nr)
        
        if active_cpus_tgt_nr > _total_cpus :
            _msg = "Unable to activate " + str(active_cpus_tgt_nr) + " CPUs on "
            _msg += "the guest \"" + self.host + "\" (maximum number of CPUs "
            _msg += "for this guest is " + str(_total_cpus) + ")."
            cberr(_msg)
            raise self.HotplugMgdConnException(_msg, 3)

        elif active_cpus_tgt_nr < 1 :
            _msg = "At least 1 CPU must be active on the guest "
            _msg += "\"" + self.host + "\"."
            cberr(_msg)
            raise self.HotplugMgdConnException(_msg, 3)
        
        active_cpus_tgt_nr -= 1 #CPU0 is always active
        
        _active_cpus_nr = len(_active_cpus)
        if active_cpus_tgt_nr > _active_cpus_nr :
            while _active_cpus_nr < active_cpus_tgt_nr :
                _cpu_nr = _inactive_cpus.pop()
                try :
                    self.set_cpu_state(_cpu_nr, "active")
                    _active_cpus_nr += 1
                    sleep(3)
                except self.HotplugMgdConnException :
                    _msg = "Error while checking active cpus"
                    raise self.HotplugMgdConnException(_msg, 3) 
        elif active_cpus_tgt_nr < _active_cpus_nr :
            while _active_cpus_nr > active_cpus_tgt_nr :
                _cpu_nr = _active_cpus.pop()
                try :
                    self.set_cpu_state(_cpu_nr, "inactive")
                    _active_cpus_nr -= 1
                    sleep(3)
                except self.HotplugMgdConnException :
                    _msg = "Error while checking active cpus"
                    raise self.HotplugMgdConnException(_msg, 3)
        else :
            _msg = "The number of active CPUs is equal the number of "
            _msg += " targeted active CPUs. There is nothing to be done."
            cbdebug(_msg)
        
        return True
Example #2
0
    def check_security_group(self, vmc_name, security_group_name):
        """
        TBD
        """

        _security_group_name = False

        if security_group_name:

            _msg = 'Checking if the security group "' + security_group_name
            _msg += '" is created on VMC ' + vmc_name + "...."
            cbdebug(_msg, True)

            _security_group_found = False
            for security_group in self.gceconn.get_all_security_groups():
                if security_group.name == security_group_name:
                    _security_group_found = True

            if not _security_group_found:
                _msg = 'ERROR! Please create the security group "'
                _msg += security_group_name + '" in '
                _msg += "Google CE before proceeding."
                _fmsg = _msg
                cberr(_msg, True)
        else:
            _security_group_found = True

        return _security_group_found
Example #3
0
    def is_vm_running(self, obj_attr_list):
        '''
        TBD
        '''
        try :
            _instance = self.get_vm_instance(obj_attr_list)

            if _instance :
                if _instance[u'state'] == u'Running' :
                    return _instance
                elif _instance[u'state'] == u'Error' :
                    _msg = "Instance \"" + obj_attr_list["cloud_vm_name"] + "\"" 
                    _msg += " reported an error (from CloudPlatform)"
                    _status = 1870
                    cberr(_msg)
                    raise CldOpsException(_msg, _status)                    
                else :
                    return False
            else :
                return False
        except Exception, msg :
            _fmsg = str(msg)
            _status = 23
            cberr(_fmsg)
            raise CldOpsException(_fmsg, _status)
Example #4
0
    def is_vm_running(self, obj_attr_list):
        '''
        TBD
        '''
        try :
            _instance = self.get_vm_instance(obj_attr_list)

            if _instance :
                if _instance[u'state'] == u'Running' :
                    return _instance
                elif _instance[u'state'] == u'Error' :
                    _msg = "Instance \"" + obj_attr_list["cloud_vm_name"] + "\"" 
                    _msg += " reported an error (from CloudPlatform)"
                    _status = 1870
                    cberr(_msg)
                    raise CldOpsException(_msg, _status)                    
                else :
                    return False
            else :
                return False
        except Exception, msg :
            _fmsg = str(msg)
            _status = 23
            cberr(_fmsg)
            raise CldOpsException(_fmsg, _status)
Example #5
0
 def get_vm_instance(self, obj_attr_list) :
     '''
     TBD
     '''
     try :
     
         if not self.cskconn :
             self.connect(obj_attr_list["access"], obj_attr_list["api_key"], obj_attr_list["secret_key"])
     
         if "cloud_uuid" in obj_attr_list and obj_attr_list["cloud_uuid"] != "NA" :
             _instances = self.cskconn.listVirtualMachines({u'id': obj_attr_list["cloud_uuid"]})
             if _instances :
                 obj_attr_list["instance_obj"] = _instances[0]
                 return _instances[0]
             else :
                 return False
         else :
             _instances = self.cskconn.listVirtualMachines()
             for _instance in _instances :
                 if _instance[u'name'] == obj_attr_list["cloud_vm_name"] :
                     obj_attr_list["instance_obj"] = _instance
                     return _instance
             return False
     except Exception, msg :
         _fmsg = str(msg)
         cberr(_fmsg)
         _status = 23
         raise CldOpsException(_fmsg, _status)
Example #6
0
    def check_security_group(self,vmc_name, security_group_name) :
        '''
        TBD
        '''

        _security_group_name = False
        
        if security_group_name :

            _msg = "Checking if the security group \"" + security_group_name
            _msg += "\" is created on VMC " + vmc_name + "...."
            cbdebug(_msg, True)

            _security_group_found = False
            for security_group in self.ec2conn.get_all_security_groups() :
                if security_group.name == security_group_name :
                    _security_group_found = True

            if not _security_group_found :
                _msg = "ERROR! Please create the security group \"" 
                _msg += security_group_name + "\" in "
                _msg += "Amazon EC2 before proceeding."
                _fmsg = _msg
                cberr(_msg, True)
        else :
            _security_group_found = True

        return _security_group_found
Example #7
0
    def retriable_run_os_command(self, cmdline, override_hostname = None, total_attempts = 2) :
        '''
        TBD
        '''
        _attempts = 0

        while _attempts < int(total_attempts) :
            _status, _result_stdout, _result_stderr = self.run_os_command(cmdline, override_hostname)
    
            if not _status and _result_stdout and not _result_stdout.count("NOK") :
                break
            else :
                _msg = "Command \"" + cmdline + "\" failed to execute on "
                _msg += "hostname " + str(override_hostname) + " after attempt "
                _msg += str(_attempts) + '.'
                cbdebug(_msg, True)
                _attempts += 1 
                sleep(3)

        if _attempts >= int(total_attempts) :
            _status = 17368
            _fmsg = "Giving up on executing command \"" + cmdline + "\" on hostname "
            _fmsg += str(override_hostname) + ". Too many attempts (" + str(_attempts) + ").\n"
            #_fmsg += "STDOUT is :\n" + str(_result_stdout) + '\n'
            #_fmsg += "STDERR is :\n" + str(_result_stderr) + '\n'
            cberr(_fmsg)
            return _status, _fmsg, {"status" : _status, "msg" : _msg, "result" : _status}
        else :
            _status = 0
            _msg = "Command \"" + cmdline + "\" executed on hostname "
            _msg += str(override_hostname) + " successfully."
            cbdebug(_msg)
            return _status, _msg, {"status" : _status, "msg" : _msg, "result" : _status}
    def get_info(self) :
        try :
            _output = []
            cursor = self.conn_check()
            cursor.execute("show variables")
            while True :
                rows = cursor.fetchmany(4)
                if not len(rows) :
                    break
                for row in rows :
                    if row[0] in ["version"] : 
                        _output.append([row[0], row[1]])
            
            cursor.execute("select sum(data_length + index_length)/1024/1024 'size' FROM information_schema.TABLES")
            while True :
                rows = cursor.fetchmany(4)
                if not len(rows) :
                    break
                for row in rows :
                    _output.append(["Data Size (MB)", str(float(row[0]))])

            cursor.close()
            return _output

        except mysql.connector.Error as err :
            _msg = "Unable to get info for database " + self.database + ": " 
            _msg += str(err)
            cberr(_msg)
            raise MetricStoreMgdConnException(str(_msg), 15)
        except Exception as e :
            cbdebug("No workey: " + str(e))
Example #9
0
    def check_security_group(self,vmc_name, security_group_name) :
        '''
        TBD
        '''

        _security_group_name = False
        
        if security_group_name :

            _msg = "Checking if the security group \"" + security_group_name
            _msg += "\" is created on VMC " + vmc_name + "...."
            cbdebug(_msg, True)

            _security_group_found = False
            for security_group in self.ec2conn.get_all_security_groups() :
                if security_group.name == security_group_name :
                    _security_group_found = True

            if not _security_group_found :
                _msg = "ERROR! Please create the security group \"" 
                _msg += security_group_name + "\" in "
                _msg += "Amazon EC2 before proceeding."
                _fmsg = _msg
                cberr(_msg, True)
        else :
            _security_group_found = True

        return _security_group_found
    def nmap(self, port = None, protocol = "TCP", reverse = False) :
        '''
        TBD
        '''
        try :
            if protocol == "TCP" :
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            elif protocol == "UDP" :
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.socket.settimeout(5)
            self.socket.connect((self.hostname, self.port if port is None else port))

            if not reverse :
                return True
            else :
                _msg = protocol + " port " + str(port) + " on host " 
                _msg += self.hostname + " is NOT free"
                cberr(_msg)
                raise NetworkException(str(_msg), "1")

        except socket.error, msg :
            if not reverse :
                _msg = "Unable to connect to " + protocol + " port " + str(port)
                _msg += " on host " + self.hostname + ": " + str(msg)
                cberr(_msg)
                raise NetworkException(str(_msg), "1")
            else :
                _msg = protocol + " port " + str(port) + " on host " 
                _msg += self.hostname + ": " + str(msg) + "is free"
                cbdebug(_msg)
                return True

            self.socket.close()
            self.socket = None
Example #11
0
    def is_vm_running(self, obj_attr_list):
        '''
        TBD
        '''
        try :

            if "instance_obj" not in obj_attr_list :
                _instance = self.get_instances(obj_attr_list, "vm", "vmuuid")
            else :
                _instance = obj_attr_list["instance_obj"]

            if _instance :
                _instance_state = _instance.update()
            else :
                _instance_state = "non-existent"
            
            if _instance_state == "running" :                
                return True
            else :
                return False

        except AWSException as obj :
            _status = int(obj.error_code)
            _fmsg = str(obj.error_message)
            raise CldOpsException(_fmsg, _status)
        
        except Exception as msg :
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
            raise CldOpsException(_fmsg, _status)
Example #12
0
    def zsk_create_vm(self, access, name, ins_offering_uuid, image_uuid, l3_uuid, disk_offering_uuid):
        '''
        Create vm via restful API
        '''

        try:
            _res = {}
            action = "vm-instances"
            action_url = access + action
            body = {
                "params": {
                    "name": name,
                    "instanceOfferingUuid": ins_offering_uuid,
                    "imageUuid": image_uuid,
                    "l3NetworkUuids": [l3_uuid],
                    "defaultL3NetworkUuid": l3_uuid,
                    "dataDiskOfferingUuids": [disk_offering_uuid],
                    "disk_tag": "virtio::diskOffering::" + disk_offering_uuid + "::num::1",
                    "systemTags": []
                }
            }
            try:
                if self.zsk_get_image_bootmode(access, image_uuid) == 'bootMode::UEFI':
                    body["params"]["systemTags"].append("vmMachineType::q35")
            except:
                pass
            _respon = self.zsk_execute_action_with_session(access, action_url, 'post', body)
            if _respon.get('error'):
                raise Exception('create VM failed, ' + _respon['error']['details'])
        except Exception as e:
            cberr(traceback.print_exc(file=sys.stdout))
        finally:
            return _respon
    def count_document(self, table, criteria, disconnect_finish=False):
        table = table.replace('-', "dash")

        self.operation_mutex.acquire()
        try:
            cursor = self.conn_check()
            statement = "select * from " + table + self.make_restrictions(
                criteria)
            result = cursor.execute(statement)
            count = 0
            if result is not None:
                count = result.rowcount
            else:
                rows = cursor.fetchmany(4)
                for resultset in rows:
                    count += 1
            cursor.close()
            if disconnect_finish:
                self.disconnect()
            self.operation_mutex.release()
            return count

        except mysql.connector.Error as err:
            self.operation_mutex.release()
            _msg = "Unable to count documents on the table \""
            _msg += table + ": " + str(err)
            cberr(_msg)
            raise MetricStoreMgdConnException(str(_msg), 11)
        except Exception as e:
            for line in traceback.format_exc().splitlines():
                cbdebug(line)
            raise e
Example #14
0
    def vmccleanup(self, obj_attr_list) :
        '''
        TBD
        '''
        try :
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _curr_tries = 0
            _max_tries = int(obj_attr_list["update_attempts"])
            _wait = int(obj_attr_list["update_frequency"])
            sleep(_wait)

            self.common_messages("VMC", obj_attr_list, "cleaning up vms", 0, '')
            _running_instances = True

            while _running_instances and _curr_tries < _max_tries :
                _running_instances = False

                for _endpoint in self.lxdconn :

                    _proc_man = ProcessManagement(username = "******", \
                                                  hostname = _endpoint, \
                                                  cloud_name = obj_attr_list["cloud_name"])

                    _cmd = "sudo pkill -9 -f 'rinetd -c /tmp/cb'; sudo rm -rf /tmp/cb-*.rinetd.conf"
                    _status, _result_stdout, _fmsg = _proc_man.run_os_command(_cmd, raise_exception=False) 
                    
                    _container_list = self.lxdconn[_endpoint].containers.all()
                    
                    for _container in _container_list :
                        if _container.name.count("cb-" + obj_attr_list["username"] + '-' + obj_attr_list["cloud_name"]) :

                            _running_instances = True
                            
                            _msg = "Terminating instance: " 
                            _msg += self.generate_random_uuid(_container.name) + " (" + str(_container.name) + ")"
                            cbdebug(_msg, True)
                            
                            if  _container.status == "Running" :
                                _container.stop()

                            _container.delete()
                            
                sleep(_wait)

                _curr_tries += 1

            if _curr_tries > _max_tries  :
                _status = 1077
                _fmsg = "Some instances on VMC \"" + obj_attr_list["name"] + "\""
                _fmsg += " could not be removed because they never became active"
                _fmsg += ". They will have to be removed manually."
                cberr(_msg, True)
            else :
                _status = 0

        except CldOpsException, obj :
            _status = obj.status
            _fmsg = str(obj.msg)
Example #15
0
    def is_vm_running(self, obj_attr_list, fail = True) :
        '''
        TBD
        '''
        try :
            
            _instance = self.get_instances(obj_attr_list, "vm", obj_attr_list["cloud_vm_name"])
            
            if _instance :
                
                if "activeTransaction" in _instance :
                    return False
                
                if _instance["status"]["name"].lower().count("active") : 
                    return True

                if _instance["status"]["name"].lower().count("error") :
                    _msg = "Instance \"" + obj_attr_list["cloud_vm_name"] + "\"" 
                    _msg += " reported an error (from " + self.get_description() + ")"
                    _status = 1870
                    cberr(_msg)
                    if fail :
                        raise CldOpsException(_msg, _status)                    

            return False

        except Exception, e :
            _status = 23
            _fmsg = str(e)
            if fail :
                raise CldOpsException(_fmsg, _status)
            else :
                return False
Example #16
0
    def get_vm_instance(self, obj_attr_list):
        """
        TBD
        """
        try:

            if not self.cskconn:
                self.connect(obj_attr_list["access"], obj_attr_list["api_key"], obj_attr_list["secret_key"])

            if "cloud_uuid" in obj_attr_list and obj_attr_list["cloud_uuid"] != "NA":
                _instances = self.cskconn.listVirtualMachines({u"id": obj_attr_list["cloud_uuid"]})
                if _instances:
                    obj_attr_list["instance_obj"] = _instances[0]
                    return _instances[0]
                else:
                    return False
            else:
                _instances = self.cskconn.listVirtualMachines()
                for _instance in _instances:
                    if _instance[u"name"] == obj_attr_list["cloud_vm_name"]:
                        obj_attr_list["instance_obj"] = _instance
                        return _instance
                return False
        except Exception, msg:
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
            raise CldOpsException(_fmsg, _status)
Example #17
0
    def get_ip_address(self, obj_attr_list):
        '''
        TBD
        '''
        try:
            obj_attr_list[
                "cloud_hostname"] = "vm" + obj_attr_list["name"].split("_")[1]

            obj_attr_list["prov_cloud_ip"] = obj_attr_list[
                "instance_obj"].private_ips[0]
            obj_attr_list["run_cloud_ip"] = obj_attr_list[
                "instance_obj"].private_ips[0]
            # NOTE: "cloud_ip" is always equal to "run_cloud_ip"
            obj_attr_list["cloud_ip"] = obj_attr_list["run_cloud_ip"]

            _msg = "Public IP = " + obj_attr_list["cloud_hostname"]
            _msg += " Private IP = " + obj_attr_list["cloud_ip"]
            cbdebug(_msg)
            _status = 0
            return True

        except:
            _msg = "Could not retrieve IP addresses for object " + obj_attr_list[
                "uuid"]
            _msg += "from vCloud Director \"" + obj_attr_list["cloud_name"]
            cberr(_msg)
            raise CldOpsException(_msg, _status)
Example #18
0
    def vmcregister(self, obj_attr_list) :
        _fmsg = "none"
        try :
            _msg = "Attempting to attach a new VMC..."
            cbdebug(_msg)

            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _time_mark_prs = int(time())
            obj_attr_list["mgt_002_provisioning_request_sent"] = _time_mark_prs - int(obj_attr_list["mgt_001_provisioning_request_originated"])


            if "cleanup_on_attach" in obj_attr_list and obj_attr_list["cleanup_on_attach"] == "True" :
                _msg = "Cleaning up VMC before attaching it."
                cbdebug(_msg)
                _status, _fmsg = self.vmccleanup(obj_attr_list)

            obj_attr_list["cloud_hostname"] = obj_attr_list["name"]
            obj_attr_list["cloud_ip"] = obj_attr_list["name"] + ".digitalocean.com"

            obj_attr_list["arrival"] = int(time())

            _time_mark_prc = int(time())

            obj_attr_list["mgt_003_provisioning_request_completed"] = _time_mark_prc - _time_mark_prs

            _status = 0

        except CldOpsException, obj :
            _status = obj.status
            _fmsg = str(obj.msg)
            cberr(str(obj))
Example #19
0
    def get_networks(self, obj_attr_list):
        '''
        TBD
        '''
        try:
            _status = 100

            _status = 0

        except LXDError.LXDAPIException as obj:
            _status = 18127
            _fmsg = str(obj)

        except Exception as e:
            _status = 23
            _fmsg = str(e)

        finally:
            if _status:
                _msg = "Network (" + obj_attr_list[
                    "prov_netname"] + " ) not found: " + _fmsg
                cberr(_msg, True)
                raise CldOpsException(_msg, _status)
            else:
                return True
    def add_document(self, table, document, disconnect_finish=False):
        table = table.replace('-', "dash")
        self.operation_mutex.acquire()
        lastrowid = -1

        try:
            cursor = self.conn_check()

            if "_id" in document and isinstance(document["_id"], bytes):
                document["_id"] = document["_id"].decode("utf-8")
            statement = "insert into " + table + " (document) values ('" + json.dumps(
                document) + "')"
            result = cursor.execute(statement)
            if cursor.rowcount != 1:
                self.mysql_conn.rollback()
                raise MetricStoreMgdConnException(
                    "Add failed w/ statement: " + statement, 65)
            cursor.close()
            self.mysql_conn.commit()
            lastrowid = cursor.lastrowid
            if disconnect_finish:
                self.disconnect()

        except mysql.connector.Error as err:
            self.operation_mutex.release()
            _msg = "Unable to insert document into table \"" + table + "\": "
            _msg += str(err)
            cberr(_msg)
            raise MetricStoreMgdConnException(str(_msg), 6)
        except Exception as e:
            self.operation_mutex.release()
            raise MetricStoreMgdConnException(str(e), 64)

        self.operation_mutex.release()
        return lastrowid
Example #21
0
    def is_vm_running(self, obj_attr_list, fail=True):
        """
        TBD
        """
        try:
            _instance = self.get_instances(obj_attr_list, "vm", obj_attr_list["cloud_vm_name"])
            if _instance:
                if not ("activeTransaction" in _instance):
                    return _instance
                else:
                    if _instance["status"]["name"].lower().count("error"):
                        _msg = 'Instance "' + obj_attr_list["cloud_vm_name"] + '"'
                        _msg += " reported an error (from SoftLayer)"
                        _status = 1870
                        cberr(_msg)
                        if fail:
                            raise CldOpsException(_msg, _status)
                    else:
                        return False
            else:
                return False

        except Exception, e:
            _status = 23
            _fmsg = str(e)
            raise CldOpsException(_fmsg, _status)
Example #22
0
    def is_vm_running(self, obj_attr_list, fail=True):
        '''
        TBD
        '''
        try:
            _instance = self.get_instances(obj_attr_list, "vm", \
                                           obj_attr_list["cloud_vm_name"])
            if _instance:
                if not ("activeTransaction" in _instance):
                    return _instance
                else:
                    if _instance["status"]["name"].lower().count("error"):
                        _msg = "Instance \"" + obj_attr_list[
                            "cloud_vm_name"] + "\""
                        _msg += " reported an error (from SoftLayer)"
                        _status = 1870
                        cberr(_msg)
                        if fail:
                            raise CldOpsException(_msg, _status)
                    else:
                        return False
            else:
                return False

        except Exception, e:
            _status = 23
            _fmsg = str(e)
            raise CldOpsException(_fmsg, _status)
Example #23
0
    def is_vm_running(self, obj_attr_list):
        """
        TBD
        """
        try:
            _instance = self.get_vm_instance(obj_attr_list)

            if _instance:
                if _instance[u"state"] == u"Running":
                    return _instance
                elif _instance[u"state"] == u"Error":
                    _msg = 'Instance "' + obj_attr_list["cloud_vm_name"] + '"'
                    _msg += " reported an error (from CloudPlatform)"
                    _status = 1870
                    cberr(_msg)
                    raise CldOpsException(_msg, _status)
                else:
                    return False
            else:
                return False
        except Exception, msg:
            _fmsg = str(msg)
            _status = 23
            cberr(_fmsg)
            raise CldOpsException(_fmsg, _status)
    def connect(self, tout):
        try:
            #if tout and tout > 0:
            #    MysqlMgdConn.conn.set_connection_timeout(tout)

            if not self.mysql_conn or not self.mysql_conn.is_connected():
                cbdebug("Opening to: " + self.database)
                self.mysql_conn = mysql.connector.connect(
                    host=self.host,
                    port=self.port,
                    user=self.username,
                    password=self.password)
                cursor = self.mysql_conn.cursor()
                try:
                    cursor.execute("use " + self.database)
                except mysql.connector.Error as err:
                    if err.errno == mysql.connector.errorcode.ER_BAD_DB_ERROR:
                        cbwarn("Database not found. Will create later.")
                cursor.close()

            _msg = "A connection to MySQL running on host "
            _msg += self.host + ", port " + str(self.port)
            _msg += ", with a timeout of "
            _msg += str(tout) + "s was established."
            cbdebug(_msg)

        except mysql.connector.Error as err:
            if err.errno == mysql.connector.errorcode.ER_ACCESS_DENIED_ERROR:
                _msg = "Something is wrong with your MySQL user name or password."
                cberr(_msg)
                raise MetricStoreMgdConnException(str(_msg), 1)
            else:
                _msg = "Unknown MySQL error: " + str(err)
                cberr(_msg)
                raise MetricStoreMgdConnException(str(_msg), 2)
    def get_experiment_list(self, table, disconnect_finish=False):
        table = table.replace('-', "dash")
        _experiment_list = []

        self.operation_mutex.acquire()
        try:
            cursor = self.conn_check()

            statement = "select distinct(expid) from " + table + " where expid is not NULL"
            cursor.execute(statement)

            while True:
                rows = cursor.fetchmany(4)
                if not len(rows):
                    break
                for (expid) in rows:
                    _experiment_list.append(expid)

            cursor.close()
            if disconnect_finish:
                self.disconnect()

            self.operation_mutex.release()
            return _experiment_list

        except mysql.connector.Error as err:
            self.operation_mutex.release()
            _msg = "Unable to get time experiment list for table \""
            _msg += table + ": " + str(err)
            cberr(_msg)
            raise MetricStoreMgdConnException(str(_msg), 14)
Example #26
0
    def vvdestroy(self, obj_attr_list, identifier):
        '''
        TBD
        '''
        try:
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _wait = int(obj_attr_list["update_frequency"])
            _curr_tries = 0
            _max_tries = int(obj_attr_list["update_attempts"])

            _instance = self.get_instances(obj_attr_list, "vv", identifier)

            if _instance:
                _msg = "Sending a destruction request for the Volume"
                _msg += " previously attached to \""
                _msg += obj_attr_list["name"] + "\""
                _msg += " (cloud-assigned uuid " + identifier + ")...."
                cbdebug(_msg, True)

                _volume_detached = False

                while not _volume_detached and _curr_tries < _max_tries:

                    _status = _instance.status

                    if _status == 'available':
                        _instance.delete()
                        _volume_detached = True

                    else:
                        _msg = " Volume previously attached to \""
                        _msg += obj_attr_list["name"] + "\""
                        _msg += " (cloud-assigned uuid "
                        _msg += identifier + ") status "
                        _msg += "is still \"" + _status + "\". "
                        _msg += "Will wait " + str(_wait)
                        _msg += " seconds and try again."
                        cbdebug(_msg)

                        sleep(_wait)
                        _curr_tries += 1

            if _curr_tries > _max_tries:
                _status = 1077
                _fmsg = " Volume previously attached to \""
                _fmsg += obj_attr_list["name"] + "\""
                _fmsg += " (cloud-assigned uuid "
                _fmsg += identifier + ") "
                _fmsg += "could not be destroyed after " + str(
                    _max_tries * _wait) + " seconds.... "
                cberr(_msg)
            else:
                _status = 0

        except CldOpsException, obj:
            _status = obj.status
            _fmsg = str(obj.msg)
Example #27
0
    def vvdestroy(self, obj_attr_list) :
        '''
        TBD
        '''
        try :
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _wait = int(obj_attr_list["update_frequency"])
            _curr_tries = 0          
            _max_tries = int(obj_attr_list["update_attempts"])
        
            if "cloud_vv_uuid" in obj_attr_list and obj_attr_list["cloud_vv_uuid"].lower() != "none" :
                
                _msg = "Sending a destruction request for the Volume" 
                _msg += " previously attached to \"" 
                _msg += obj_attr_list["name"] + "\""
                _msg += " (cloud-assigned uuid " 
                _msg += obj_attr_list["cloud_vv_uuid"] + ")...."
                cbdebug(_msg, True)

                _volume_detached = False
                while not _volume_detached and _curr_tries < _max_tries :

                    _instance = self.ec2conn.get_all_volumes(volume_ids = [obj_attr_list["cloud_vv_uuid"]])
    
                    _volume=_instance[0]

                    _status = _volume.status
                    if _status == 'available' :
                        _volume_detached = True

                    else :
                        _msg = " Volume previously attached to \"" 
                        _msg += obj_attr_list["name"] + "\""
                        _msg += " (cloud-assigned uuid " 
                        _msg += obj_attr_list["cloud_vv_uuid"] + ") status "
                        _msg += "is still \"" + _status + "\". "
                        _msg += "Will wait " + str(_wait)
                        _msg += " seconds and try again."
                        cbdebug(_msg)

                        sleep(_wait)
                        _curr_tries += 1                           

            if _curr_tries > _max_tries  :
                _status = 1077
                _fmsg = obj_attr_list["name"] + "\""
                _fmsg += " (cloud-assigned uuid " 
                _fmsg += obj_attr_list["cloud_vv_uuid"] + ") "
                _fmsg +=  "could not be destroyed after " + str(_max_tries * _wait) + " seconds.... "
                cberr(_msg)
            else :
                _volume.delete()
                _status = 0
                    
        except CldOpsException, obj :
            _status = obj.status
            _fmsg = str(obj.msg)
Example #28
0
    def vmccleanup(self, obj_attr_list) :
        '''
        TBD
        '''
        try :
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            # check for old instances and destroy
            self.connect(obj_attr_list["access"], obj_attr_list["credentials"], obj_attr_list["name"])

            _curr_tries = 0
            _max_tries = int(obj_attr_list["update_attempts"])
            _wait = int(obj_attr_list["update_frequency"])
            sleep(_wait)

            _running_instances = True
            _status = 0
            while _running_instances and not _status and _curr_tries < _max_tries :
                _running_instances = False                
                _instance_list = self.query("describe_all_instances", obj_attr_list)

                for _instance in _instance_list :
                    if _instance["instance_tag"].count("cb-" + obj_attr_list["username"]) :
                        _running_instances = True
                        if _instance["state"] == "running" :
                            _msg = "Terminating instance \""
                            _msg += _instance["instance_id"] + "\"" 
                            _msg += " (" + _instance["instance_tag"] + ")"
                            cbdebug(_msg, True)
                            _instance["cloud_uuid"] = _instance["gid"]
                            self.query("terminate_instances", _instance, False)

                        if _instance["state"] == "starting" :
                            _msg = "Will wait for instance "
                            _msg += _instance["instance_id"] + "\"" 
                            _msg += " (" + _instance["instance_tag"] + ") to"
                            _msg += "start and then destroy it."
                            cbdebug(_msg, True)

                    _msg = "Some instances are still starting on VMC \"" + obj_attr_list["name"] 
                    _msg += "\". Will wait for " + str(_wait) + " seconds and check again."
                    sleep(_wait)
                    _curr_tries += 1

            if _curr_tries > _max_tries  :
                _status = 1077
                _fmsg = "Some instances on VMC \"" + obj_attr_list["name"] + "\""
                _fmsg += " could not be removed because they never became active"
                _fmsg += ". They will have to be removed manually."
                cberr(_msg, True)

                sleep(int(obj_attr_list["update_frequency"]))

        except CldOpsException, obj :
            _status = int(obj.error_code)
            _fmsg = str(obj.error_message)
Example #29
0
    def vvdestroy(self, obj_attr_list, identifier) :
        '''
        TBD
        '''
        try :
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _wait = int(obj_attr_list["update_frequency"])
            _curr_tries = 0
            _max_tries = int(obj_attr_list["update_attempts"])

            if str(obj_attr_list["cloud_vv_uuid"]).lower() != "none" :

                _instance = self.get_instances(obj_attr_list, "vv", identifier)
    
                if _instance :
                    self.common_messages("VV", obj_attr_list, "destroying", 0, '')
    
                    _volume_detached = False
    
                    while not _volume_detached and _curr_tries < _max_tries :
    
                        _status = _instance.status
    
                        if _status == 'available' :
                            _instance.delete()                 
                            _volume_detached = True
    
                        else :
                            _msg = " Volume previously attached to \"" 
                            _msg += obj_attr_list["name"] + "\""
                            _msg += " (cloud-assigned uuid " 
                            _msg += identifier + ") status "
                            _msg += "is still \"" + _status + "\". "
                            _msg += "Will wait " + str(_wait)
                            _msg += " seconds and try again."
                            cbdebug(_msg)
    
                            sleep(_wait)
                            _curr_tries += 1                           
    
            if _curr_tries > _max_tries  :
                _status = 1077
                _fmsg = " Volume previously attached to \"" 
                _fmsg += obj_attr_list["name"] + "\""
                _fmsg += " (cloud-assigned uuid " 
                _fmsg += identifier + ") "
                _fmsg +=  "could not be destroyed after " + str(_max_tries * _wait) + " seconds.... "
                cberr(_msg)
            else :
                _status = 0
                    
        except CldOpsException, obj :
            _status = obj.status
            _fmsg = str(obj.msg)
Example #30
0
    def connect(self,
                access,
                credentials,
                vmc_name,
                extra_parms={},
                diag=False,
                generate_rc=False):
        '''
        TBD
        '''
        try:
            _status = 100
            _endpoint_ip = "NA"
            _fmsg = "An error has occurred, but no error message was captured"

            self.ssl_key = extra_parms["ssl_key"]
            self.ssl_cert = extra_parms["ssl_cert"]

            for _endpoint in access.split(','):
                _endpoint, _endpoint_name, _endpoint_ip = self.parse_endpoint(
                    _endpoint, "https", "8443")

                if _endpoint_ip not in self.lxdconn:
                    self.lxdconn[_endpoint_ip] = Client(endpoint=_endpoint,
                                                        cert=(self.ssl_cert,
                                                              self.ssl_key),
                                                        verify=False)
                    self.lxdconn[_endpoint_ip].authenticate(credentials)
                    if not self.lxdconn[_endpoint_ip].trusted:
                        _fmsg = "Unable to authenticate"
                        _status = 101

            _status -= 100

        except LXDError.ClientConnectionFailed as obj:
            _status = 18127
            _fmsg = str(obj.message)

        except LXDError.LXDAPIException as obj:
            _status = 18127
            _fmsg = str(obj)

        except Exception as e:
            _status = 23
            _fmsg = str(e)

        finally:
            if _status:
                _msg = self.get_description(
                ) + " connection to endpoint \"" + _endpoint_ip + "\" failed: " + _fmsg
                cberr(_msg)
                raise CldOpsException(_msg, _status)
            else:
                _msg = self.get_description() + " connection successful."
                cbdebug(_msg)
                return _status, _msg, ''
    def update_document(self, table, document, disconnect_finish=False):
        table = table.replace('-', "dash")

        self.operation_mutex.acquire()
        try:
            cursor = self.conn_check()

            if "_id" in document and isinstance(document["_id"], bytes):
                document["_id"] = document["_id"].decode("utf-8")

            if "original_mysql_id" not in document:
                if "_id" in document:
                    # Attempt to find the original ID first
                    statement = "select id from " + table + " where _id = '" + document[
                        "_id"] + "'"
                    cursor.execute(statement)
                    while True:
                        rows = cursor.fetchmany(1)
                        if not len(rows):
                            break
                        for (original_mysql_id, ) in rows:
                            document["original_mysql_id"] = original_mysql_id

                if "original_mysql_id" not in document:
                    cursor.close()
                    self.operation_mutex.release()
                    cbwarn(
                        "This document does not have a pre-existing identifier. Cannot update. Will insert first"
                    )
                    document["original_mysql_id"] = self.add_document(
                        table, document, disconnect_finish=disconnect_finish)
                    return

            statement = "update " + table + " set document = '" + json.dumps(
                document) + "' where id = " + str(
                    document["original_mysql_id"])
            result = cursor.execute(statement)
            cursor.close()
            self.mysql_conn.commit()

            if disconnect_finish:
                self.disconnect()

        except mysql.connector.Error as err:
            self.operation_mutex.release()
            _msg = "Unable to update documents from the table \""
            _msg += table + ": " + str(err)
            cberr(_msg)
            raise MetricStoreMgdConnException(str(_msg), 8)
        except Exception as e:
            self.operation_mutex.release()
            cberr(_msg)
            raise MetricStoreMgdConnException(str(_msg), 67)

        self.operation_mutex.release()
Example #32
0
    def test_vmc_connection(self, vmc_name, access, credentials, key_name, \
                            security_group_name, vm_templates, vm_defaults) :
        try :
            # Attempt to a connection using those login credentials
            self.connect(credentials)

        except CldOpsException, obj :
            _msg = str(obj.msg)
            cberr(_msg)
            _status = 2
            raise CldOpsException(_msg, _status)
Example #33
0
    def test_vmc_connection(self, vmc_name, access, credentials, key_name, \
                            security_group_name, vm_templates, vm_defaults) :
        try:
            # Attempt to a connection using those login credentials
            self.connect(credentials)

        except CldOpsException, obj:
            _msg = str(obj.msg)
            cberr(_msg)
            _status = 2
            raise CldOpsException(_msg, _status)
Example #34
0
    def connect(self, access, authentication_data, region):
        '''
        TBD
        '''

        try:
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _username, _api_key, _api_type = authentication_data.split('-')
            if access.lower().count("private"):
                self.slconn = SoftLayer.create_client_from_env (username = _username.strip(), \
                                                                api_key= _api_key.strip(), \
                                                                endpoint_url = SoftLayer.API_PRIVATE_ENDPOINT)
            else:
                self.slconn = SoftLayer.create_client_from_env (username = _username.strip(), \
                                                                api_key= _api_key.strip(), \
                                                                endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT)

            _resp = self.slconn.call('Account', 'getObject')

            _datacenters = SoftLayer.VSManager(
                self.slconn).get_create_options()['datacenters']
            for _dcitem in _datacenters:
                if region == _dcitem['template']['datacenter']['name']:
                    _region = _dcitem['template']['datacenter']['name']

            _msg = "Selected region is " + str(region) + " (" + _region + ")"

            if _api_type.lower().count("baremetal"):
                self.nodeman = SoftLayer.HardwareManager(self.slconn)
            else:
                self.nodeman = SoftLayer.VSManager(self.slconn)

            self.sshman = SoftLayer.SshKeyManager(self.slconn)

            self.imageman = SoftLayer.ImageManager(self.slconn)

            _status = 0

        except Exception as msg:
            _fmsg = str(msg)
            _status = 23

        finally:
            if _status:
                _msg = self.get_description() + " connection failure: " + _fmsg
                cberr(_msg)
                raise CldOpsException(_msg, _status)
            else:

                _msg = self.get_description() + " connection successful."
                cbdebug(_msg)
                return _status, _msg, _region
Example #35
0
 def test_vmc_connection(self, vmc_name, access, credentials, extra_info, dummy1, dummy2, dummy3) :
     '''
     TBD
     '''
     try :
         self.connect(credentials, access, extra_info, '1.5')
                     
     except CldOpsException, obj :
         _msg = str(obj.msg)
         cberr(_msg)
         _status = 2
         raise CldOpsException(_msg, _status)
Example #36
0
    def unlock(self, cloud_name, obj_type, obj_id, lock):
        '''
        TBD
        '''
        try:
            self.osci.release_lock(cloud_name, obj_type, obj_id, lock)
            return True

        except RedisMgdConn.ObjectStoreMgdConnException, obj:
            _msg = str(obj.msg)
            cberr(_msg)
            return False
Example #37
0
    def get_instances(self, obj_attr_list, obj_type = "vm", identifier = "all") :
        '''
        TBD
        '''
        
        try :
            if obj_type == "vm" :
                if identifier == "vmuuid" :
                    if str(obj_attr_list["cloud_vm_uuid"]).lower() != "none" :
                        _reservations = self.ec2conn.get_all_instances(obj_attr_list["cloud_vm_uuid"])
                        if _reservations :
                            _instance = _reservations[0].instances[0]
                            obj_attr_list["instance_obj"] = _instance
                            return _instance
                        
                if identifier == "name" :
                    _reservations = self.ec2conn.get_all_instances()
                    _instances = [i for r in _reservations for i in r.instances]
                    if _instance :
                        for _instance in _instances :
                            if "Name" in _instance.tags :
                                if _instance.tags["Name"] == obj_attr_list["cloud_vm_name"] :
                                    obj_attr_list["instance_obj"] = _instance
                                    return _instance
                return False
            
            else :
                _instance = []

                if identifier == "vvuid" :
                    if obj_attr_list["cloud_vv_uuid"].lower() != "none" :
                        _instance = self.ec2conn.get_all_volumes(volume_ids = [obj_attr_list["cloud_vv_uuid"]])

                if identifier == "vmuuid" :
                    if str(obj_attr_list["cloud_vm_uuid"]).lower() != "none" :
                        _instance = self.ec2conn.get_all_volumes(filters={'attachment.instance-id': obj_attr_list["cloud_vm_uuid"]})

                if len(_instance) :    
                    _volume=_instance[0]
                    return _volume
                else :
                    return False
                    
        except AWSException as obj :
            _status = int(obj.error_code)
            _fmsg = str(obj.error_message)
            raise CldOpsException(_fmsg, _status)
        
        except Exception as msg :
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
            raise CldOpsException(_fmsg, _status)
Example #38
0
    def aiundefine(self, obj_attr_list):
        '''
        TBD
        '''
        try:
            _fmsg = "An error has occurred, but no error message was captured"
            _status = 0

        except Exception, msg:
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
Example #39
0
    def lock (self, cloud_name, obj_type, obj_id, id_str):
        '''
        TBD
        '''
        try:
            _lock = self.osci.acquire_lock(cloud_name, obj_type, obj_id, id_str, 1)
            return _lock

        except RedisMgdConn.ObjectStoreMgdConnException, obj :
            _msg = str(obj.msg)
            cberr(_msg)
            return False
Example #40
0
    def aiundefine(self, obj_attr_list) :
        '''
        TBD
        '''
        try :
            _fmsg = "An error has occurred, but no error message was captured"
            _status = 0

        except Exception, msg :
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
Example #41
0
    def unlock (self, cloud_name, obj_type, obj_id, lock) :
        '''
        TBD
        '''
        try:
            self.osci.release_lock(cloud_name, obj_type, obj_id, lock)
            return True

        except RedisMgdConn.ObjectStoreMgdConnException, obj :
            _msg = str(obj.msg)
            cberr(_msg)
            return False
Example #42
0
    def lock(self, cloud_name, obj_type, obj_id, id_str):
        '''
        TBD
        '''
        try:
            _lock = self.osci.acquire_lock(cloud_name, obj_type, obj_id,
                                           id_str, 1)
            return _lock

        except RedisMgdConn.ObjectStoreMgdConnException, obj:
            _msg = str(obj.msg)
            cberr(_msg)
            return False
Example #43
0
    def aiundefine(self, obj_attr_list, current_step):
        '''
        TBD
        '''
        try:
            _fmsg = "An error has occurred, but no error message was captured"
            self.take_action_if_requested("AI", obj_attr_list, current_step)
            _status = 0

        except Exception, msg:
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
Example #44
0
    def aiundefine(self, obj_attr_list, current_step) :
        '''
        TBD
        '''
        try :
            _fmsg = "An error has occurred, but no error message was captured"
            self.take_action_if_requested("AI", obj_attr_list, current_step)            
            _status = 0

        except Exception, msg :
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
Example #45
0
    def test_vmc_connection(self, vmc_name, access, credentials, extra_info,
                            dummy1, dummy2, dummy3):
        '''
        TBD
        '''
        try:
            self.connect(credentials, access, extra_info, '1.5')

        except CldOpsException, obj:
            _msg = str(obj.msg)
            cberr(_msg)
            _status = 2
            raise CldOpsException(_msg, _status)
Example #46
0
    def get_images(self, obj_attr_list):
        '''
        TBD
        '''
        try:
            _status = 100
            _hyper = ''

            _fmsg = "An error has occurred, but no error message was captured"

            _vmc_attr_list = self.osci.get_object(obj_attr_list["cloud_name"],
                                                  "VMC", False,
                                                  obj_attr_list["vmc"], False)
            _map_uuid_to_name = str2dic(_vmc_attr_list["images_uuid2name"])
            _map_name_to_uuid = str2dic(_vmc_attr_list["images_name2uuid"])

            if self.is_cloud_image_uuid(obj_attr_list["imageid1"]):

                obj_attr_list["boot_volume_imageid1"] = obj_attr_list[
                    "imageid1"]
                if obj_attr_list["imageid1"] in _map_uuid_to_name:
                    obj_attr_list["imageid1"] = _map_uuid_to_name[
                        obj_attr_list["imageid1"]]
                    _status = 0
                else:
                    _fmsg = "image does not exist"
                    _status = 1817
            else:
                if obj_attr_list["imageid1"] in _map_name_to_uuid:
                    obj_attr_list["boot_volume_imageid1"] = _map_name_to_uuid[
                        obj_attr_list["imageid1"]]
                    _status = 0
                else:
                    _fmsg = "image does not exist"
                    _status = 1817
#                    obj_attr_list["boot_volume_imageid1"] = self.generate_random_uuid(obj_attr_list["imageid1"])

        except Exception as e:
            _status = 23
            _fmsg = str(e)

        finally:
            if _status:
                _msg = "Image Name (" + obj_attr_list[
                    "imageid1"] + ") not found: " + _fmsg
                cberr(_msg)
                raise CldOpsException(_msg, _status)
            else:
                return True
Example #47
0
    def connect(self, name, host, password, version):
        '''
        TBD
        '''
        try:

            _status = 100

            libcloud.security.VERIFY_SSL_CERT = False

            _msg = "Connecting to vcloud director host " + host + " using username " + name
            _msg += " password = "******" api version = " + version
            cbdebug(_msg)

            vcloud = get_driver(Provider.VCLOUD)
            _status = 110

            # Assign login credentials and host information to libcloud handler
            self.vcdconn = vcloud(name,
                                  password,
                                  host=host,
                                  api_version=version)
            # should the above be self.vcdconn ???
            _status = 120

            # Attempt to a connection using those login credentials
            # nodes = self.vcdconn.list_nodes()

            _status = 0

        except:
            _msg = "Error connecting to vCloud Director.  Status = " + str(
                _status)
            cbdebug(_msg, True)
            cberr(_msg)

            _status = 23

        finally:
            if _status:
                _msg = "vCloud Director connection failure.  Failed to connect to host " + host + " using credentials " + name + password + " with API version " + version
                cbdebug(_msg, True)
                cberr(_msg)
                raise CldOpsException(_msg, _status)
            else:
                _msg = "vCloud Director connection successful."
                cbdebug(_msg)
                return _status, _msg
Example #48
0
    def vmcregister(self, obj_attr_list):
        """
        TBD
        """
        try:
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _time_mark_prs = int(time())
            obj_attr_list["mgt_002_provisioning_request_sent"] = _time_mark_prs - int(
                obj_attr_list["mgt_001_provisioning_request_originated"]
            )

            if "cleanup_on_attach" in obj_attr_list and obj_attr_list["cleanup_on_attach"] == "True":
                _msg = 'Removing all VMs previously created on VMC "'
                _msg += obj_attr_list["name"] + '" (only VMs names starting with'
                _msg += ' "' + "cb-" + obj_attr_list["username"] + "-" + obj_attr_list["cloud_name"]
                _msg += '").....'
                cbdebug(_msg, True)
                _status, _fmsg = self.vmccleanup(obj_attr_list)
            else:
                _status = 0

            if not _status:
                _x, _y = self.connect(obj_attr_list["access"], obj_attr_list["api_key"], obj_attr_list["secret_key"])

                _zoneid = self.get_zone(obj_attr_list["name"])

                obj_attr_list["cloud_hostname"] = obj_attr_list["name"]
                _x, obj_attr_list["cloud_ip"] = hostname2ip(obj_attr_list["access"].split(":")[1].replace("//", ""))
                obj_attr_list["arrival"] = int(time())

                if obj_attr_list["discover_hosts"].lower() == "true":
                    _msg = 'Discovering hosts on VMC "' + obj_attr_list["name"] + '".....'
                    cbdebug(_msg, True)
                    _status, _fmsg = self.discover_hosts(obj_attr_list, _time_mark_prs)
                else:
                    obj_attr_list["hosts"] = ""
                    obj_attr_list["host_list"] = {}
                    obj_attr_list["host_count"] = "NA"

                _time_mark_prc = int(time())

                obj_attr_list["mgt_003_provisioning_request_completed"] = _time_mark_prc - _time_mark_prs

        except CldOpsException, obj:
            _fmsg = str(obj.msg)
            cberr(_msg)
            _status = 2
Example #49
0
    def aidefine(self, obj_attr_list):
        """
        TBD
        """
        try:
            _fmsg = "An error has occurred, but no error message was captured"

            self.take_action_if_requested("AI", obj_attr_list, "all_vms_booted")

            _status = 0

        except Exception, msg:
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
Example #50
0
    def test_vmc_common(self, vmc_name, access, credentials, key_name, \
                            security_group_name, vm_templates, vm_defaults) :
        '''
        TBD
        '''
        _status = 100
        _fmsg = "An error has occurred, but no error message was captured"

        _access_url, _iaas_endpoint = access.split('-')
        _target, _port_number = _access_url.split(':')
        _deploy_python_proxy_script = self.path + "/scripts/common/scp_python_proxy.sh"
        _python_proxy_script = "scp2_python_proxy.rb"
        _iaas_access_id, _iaas_private_key, _iaas_service_public_key = credentials.split(
            '-')

        _cmd = _deploy_python_proxy_script + ' ' + _target + ' ' + _port_number
        _cmd += ' ' + _python_proxy_script + ' ' + _iaas_access_id
        _cmd += ' http://' + _iaas_endpoint + ' ' + _iaas_private_key
        _cmd += ' ' + _iaas_service_public_key

        _proc_h = Popen(_cmd, shell=True, stdout=PIPE, stderr=PIPE)

        if _proc_h.pid:
            _msg = "Python proxy daemon deployment on service VM "
            _msg += "\"" + _target + "\" started successfully with the "
            _msg += "command \"" + _cmd + "\"."
            cbdebug(_msg)
        else:
            _msg = "Error while deploying python proxy daemon on the "
            _msg += "service VM \"" + _target + "\" with the command "
            _msg += "\"" + _cmd + "\""
            cberr(_msg)
            raise CldOpsException(_msg, 98)

        _proc_h.communicate()

        if not _proc_h.returncode:
            _msg = "Python proxy deployed successfully on service VM "
            _msg += "\"" + _target + "\"."
            cbdebug(_msg, True)
            # This is very important! Give the script a couple of seconds
            # to start.
            sleep(2)
        else:
            _msg = "Python proxy failed to deploy on service VM "
            _msg += "\"" + _target + "\"."
            cberr(_msg, True)
            raise CldOpsException(_msg, 98)
Example #51
0
    def get_ip_address(self, obj_attr_list) :
        try :
            _status = 100
            node = self.get_vm_instance(obj_attr_list)

            if len(node.private_ips) > 0 and obj_attr_list["run_netname"].lower() == "private" :
                obj_attr_list["run_cloud_ip"] = node.private_ips[0]
            else :
                if len(node.public_ips) > 0 :
                    obj_attr_list["run_cloud_ip"] = node.public_ips[0]
                else :
                    cbdebug("Droplet Public address not yet available.")
                    return False

            # NOTE: "cloud_ip" is always equal to "run_cloud_ip"
            obj_attr_list["cloud_ip"] = obj_attr_list["run_cloud_ip"]

            if obj_attr_list["hostname_key"] == "cloud_vm_name" :
                obj_attr_list["cloud_hostname"] = obj_attr_list["cloud_vm_name"]
            elif obj_attr_list["hostname_key"] == "cloud_ip" :
                obj_attr_list["cloud_hostname"] = obj_attr_list["cloud_ip"].replace('.','-')

            _msg = "Public IP = " + str(node.public_ips)
            _msg += " Private IP = " + str(node.private_ips)
            cbdebug(_msg)

            if str(obj_attr_list["use_vpn_ip"]).lower() == "true" and str(obj_attr_list["vpn_only"]).lower() == "true" :
                assert(self.get_attr_from_pending(obj_attr_list))

                if "cloud_init_vpn" not in obj_attr_list :
                    cbdebug("Droplet VPN address not yet available.")
                    return False
                cbdebug("Found VPN IP: " + obj_attr_list["cloud_init_vpn"])
                obj_attr_list["prov_cloud_ip"] = obj_attr_list["cloud_init_vpn"]
            else :
                if len(node.private_ips) > 0 and obj_attr_list["prov_netname"].lower() == "private" :
                    obj_attr_list["prov_cloud_ip"] = node.private_ips[0]
                else :
                    obj_attr_list["prov_cloud_ip"] = node.public_ips[0]

            _status = 0
            return True

        except Exception, e :
            _msg = "Could not retrieve IP addresses for object " + obj_attr_list["uuid"]
            _msg += " from DigitalOcean \"" + obj_attr_list["cloud_name"] + ": " + str(e)
            cberr(_msg)
            raise CldOpsException(_msg, _status)
Example #52
0
    def vmcregister(self, obj_attr_list) :
        '''
        TBD
        '''
        try :
            _status = 100
            _fmsg = "An error has occurred, but no error message was captured"

            _time_mark_prs = int(time())
            obj_attr_list["mgt_002_provisioning_request_sent"] = _time_mark_prs - int(obj_attr_list["mgt_001_provisioning_request_originated"])
            
            if "cleanup_on_attach" in obj_attr_list and obj_attr_list["cleanup_on_attach"] == "True" :
                _msg = "Removing all VMs previously created on VMC \""
                _msg += obj_attr_list["name"] + "\" (only VMs names starting with"
                _msg += " \"" + "cb-" + obj_attr_list["username"] + '-' + obj_attr_list["cloud_name"]
                _msg += "\")....."
                cbdebug(_msg, True)
                _status, _fmsg = self.vmccleanup(obj_attr_list)
            else :
                _status = 0

            if not _status :
                _x, _y = self.connect(obj_attr_list["access"], obj_attr_list["api_key"], obj_attr_list["secret_key"])
            
                _zoneid = self.get_zone(obj_attr_list["name"])

                obj_attr_list["cloud_hostname"] = obj_attr_list["name"];
                _x, obj_attr_list["cloud_ip"] = hostname2ip(obj_attr_list["access"].split(':')[1].replace('//',''))
                obj_attr_list["arrival"] = int(time())

                if obj_attr_list["discover_hosts"].lower() == "true" :
                    _msg = "Discovering hosts on VMC \"" + obj_attr_list["name"] + "\"....."
                    cbdebug(_msg, True)
                    _status, _fmsg = self.discover_hosts(obj_attr_list, _time_mark_prs)
                else :
                    obj_attr_list["hosts"] = ''
                    obj_attr_list["host_list"] = {}
                    obj_attr_list["host_count"] = "NA"

                _time_mark_prc = int(time())

                obj_attr_list["mgt_003_provisioning_request_completed"] = _time_mark_prc - _time_mark_prs

        except CldOpsException, obj :
            _fmsg = str(obj.msg)
            cberr(_msg)
            _status = 2
Example #53
0
    def test_vmc_common(self, vmc_name, access, credentials, key_name, \
                            security_group_name, vm_templates, vm_defaults) :
        '''
        TBD
        '''
        _status = 100
        _fmsg = "An error has occurred, but no error message was captured"

        _access_url, _iaas_endpoint = access.split('-')            
        _target, _port_number = _access_url.split(':')
        _deploy_python_proxy_script = self.path + "/scripts/common/scp_python_proxy.sh"
        _python_proxy_script = "scp2_python_proxy.rb"
        _iaas_access_id, _iaas_private_key, _iaas_service_public_key = credentials.split('-')

        _cmd = _deploy_python_proxy_script + ' ' + _target + ' ' + _port_number
        _cmd += ' ' + _python_proxy_script + ' ' + _iaas_access_id
        _cmd += ' http://' + _iaas_endpoint + ' ' + _iaas_private_key
        _cmd += ' ' + _iaas_service_public_key

        _proc_h = Popen(_cmd, shell=True, stdout=PIPE, stderr=PIPE)

        if _proc_h.pid :
            _msg = "Python proxy daemon deployment on service VM "
            _msg += "\"" + _target + "\" started successfully with the "
            _msg += "command \"" + _cmd + "\"."
            cbdebug(_msg)
        else :
            _msg = "Error while deploying python proxy daemon on the "
            _msg += "service VM \"" + _target + "\" with the command "
            _msg += "\"" + _cmd + "\""
            cberr(_msg)
            raise CldOpsException(_msg, 98)

        _proc_h.communicate()

        if not _proc_h.returncode :
            _msg = "Python proxy deployed successfully on service VM "
            _msg += "\"" + _target + "\"."
            cbdebug(_msg, True)
            # This is very important! Give the script a couple of seconds
            # to start.
            sleep (2)
        else :
            _msg = "Python proxy failed to deploy on service VM "
            _msg += "\"" + _target + "\"."
            cberr(_msg, True)
            raise CldOpsException(_msg, 98)
Example #54
0
 def get_ip_address(self, obj_attr_list) :
     '''
     TBD
     '''
     try :
         obj_attr_list["cloud_hostname"] = "vm" + obj_attr_list["name"].split("_")[1]
         obj_attr_list["cloud_ip"] = obj_attr_list["instance_obj"].private_ips[0]
         obj_attr_list["prov_cloud_ip"] = obj_attr_list["cloud_ip"]
         _msg = "Public IP = " + obj_attr_list["cloud_hostname"]
         _msg += " Private IP = " + obj_attr_list["cloud_ip"]
         cbdebug(_msg)
         return True
     except :
         _msg = "Could not retrieve IP addresses for object " + obj_attr_list["uuid"]
         _msg += "from vCloud Director \"" + obj_attr_list["cloud_name"]
         cberr(_msg)
         raise CldOpsException(_msg, _status)
Example #55
0
    def connect(self, name, host, password, version) :
        '''
        TBD
        '''
        try :

            _status = 100

            libcloud.security.VERIFY_SSL_CERT = False

            _msg = "Connecting to vcloud director host " + host + " using username " + name
            _msg += " password = "******" api version = " + version
            cbdebug(_msg)

            vcloud = get_driver(Provider.VCLOUD)
            _status = 110    
    
            # Assign login credentials and host information to libcloud handler
            self.vcdconn = vcloud(name, password, host=host, api_version = version)
            # should the above be self.vcdconn ???
            _status = 120

            # Attempt to a connection using those login credentials
            # nodes = self.vcdconn.list_nodes()
          
            _status = 0
             
        except :
            _msg = "Error connecting to vCloud Director.  Status = " + str(_status) 
            cbdebug(_msg, True)
            cberr(_msg)

            _status = 23

        finally :
            if _status :
                _msg = "vCloud Director connection failure.  Failed to connect to host " + host + " using credentials " + name + password + " with API version " + version
                cbdebug(_msg, True)
                cberr(_msg)
                raise CldOpsException(_msg, _status)
            else :
                _msg = "vCloud Director connection successful."
                cbdebug(_msg)
                return _status, _msg
Example #56
0
    def get_ip_address(self, obj_attr_list) :
        '''
        TBD
        '''
        if "cloud_ip" in obj_attr_list and obj_attr_list["cloud_ip"] != "undefined" :
            obj_attr_list["prov_cloud_ip"] = obj_attr_list["cloud_ip"]
            obj_attr_list["run_cloud_ip"] = obj_attr_list["cloud_ip"]
        else :
            _status = 1181
            _msg = "For " + self.get_description() + ", the IP address of each VM has to be" 
            _msg += " supplied on the \"vmattach\" (e.g., \"vmattach <role>"
            _msg += " [optional parametes] cloud_ip=X.Y.Z.W)."
            cberr(_msg)
            raise CldOpsException(_msg, _status)

        obj_attr_list["cloud_hostname"] = "vm_" + obj_attr_list["cloud_ip"].replace('.','_')
        
        return True
Example #57
0
    def is_vm_stopped(self, obj_attr_list):
        '''
        TBD
        '''
        try :
            _instance = self.get_vm_instance(obj_attr_list)

            if _instance :
                if _instance[u'state'] == u'Stopped' :
                    return _instance
                else :
                    return False
            else :
                return False
        except Exception, msg :
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
            raise CldOpsException(_fmsg, _status)
Example #58
0
    def is_vm_stopped(self, obj_attr_list):
        """
        TBD
        """
        try:
            _instance = self.get_vm_instance(obj_attr_list)

            if _instance:
                if _instance[u"state"] == u"Stopped":
                    return _instance
                else:
                    return False
            else:
                return False
        except Exception, msg:
            _fmsg = str(msg)
            cberr(_fmsg)
            _status = 23
            raise CldOpsException(_fmsg, _status)