示例#1
0
文件: vmManage.py 项目: houht/mypaas
    def extend_disk(self, vm_name, target, total_size):
        ""

        # 1 检测
        self.check(vm_name)

        disk_name = vm_name + "_" + target
        # 2 判断是否运行
        state = self.vm_tools.vm_states(vm_name)
        if state[vm_name] == "running":
            raise CustomException("vm name is running.")

        # 3 判断磁盘是否存在
        disks = self.info_disks(vm_name)
        disk_size = None
        for disk in disks:
            if disk_name == disk['disk_name']:
                disk_size = disk['disk_size']
                break
        if disk_size is None:
            raise CustomException("Error: disk name is not exist!")
            return

        extend_size = int(total_size) - int(disk_size)
        # 4 扩展磁盘
        try:
            self.storageManage.extend_lvm(disk_name, extend_size)
        except Exception, e:
            raise CustomException("Error: extend lvm failed!")
示例#2
0
文件: appManage.py 项目: houht/mypaas
    def app_drop(self, env):
        ""

        # 参数获取
        instanceId = env.get_service_env('instanceId')

        # 1 查找服务
        app_info = self.app_info(instanceId)
        if len(app_info) == 0:
            raise CustomException("the service [%s] not exists." % instanceId)
            return

        # 2 设置环境变量
        env.set_vm_env('name', app_info[0]['vmName'])

        # 3 得到改服务对象
        service = self.script.getService(app_info[0]['serviceId'])

        # 4 得到部署的列表
        actions = service.target.actions['drop']

        # 5 执行
        # 变量赋值
        self.executer.env = env
        self.executer.properties = service.propertys.parameter

        # 6 删除
        for action in actions:
            try:
                getattr(self.executer, action['action'])(action['param'])
            except Exception, e:
                raise CustomException("Error executing method %s !,%s" %
                                      (action['action'], str(e)))
                return
示例#3
0
    def download_image(self,image_name):
        ""
        
        if image_name is None :
            raise CustomException("image name is None.")
        
        image_conf = self.conf.get_image_conf()
        image_path = image_conf['path']
        image_file = os.path.join(image_path,image_name)
        
        if not os.path.exists(image_file):
            url = image_conf['download_addr']
            ics = iCloudStorage(url)
            result = ics.downloadShare(image_name,image_file)

            if result:
                infos = ics.getShareFileAttribute(image_name)
                save_sql = '''INSERT INTO images values (?, ?, ?,?, ?,?,?,?,?,?,?,?,?)'''
                data = [(infos['imageid'], image_name, infos['system'],infos['version'],infos['arch'], infos['machine'],infos['size'],image_file,infos['ext'],0,infos['vtype'],time.strftime("%Y-%m-%d %H:%M:%S"),infos['remark'])]
                
                images_db = image_conf['db_path']
                sql_tools = SqliteTools(images_db)
                sql_tools.save(save_sql,data)
            else:
                raise CustomException("download image Failure.")
        
        return image_file
示例#4
0
文件: vmManage.py 项目: houht/mypaas
    def create_vm(self, **kwargs):
        ""

        image_name = kwargs.pop('image_name', None)
        vm_name = kwargs.pop('vm_name', None)
        # VM大小,单位M
        vm_size = kwargs.pop('vm_size', None)
        # 内存大小,单位M
        mem_size = kwargs.pop('mem_size', None)
        # cpu大小,单位核数
        vcpus = kwargs.pop('vcpus', None)
        host_name = kwargs.pop('host_name', "")

        infos = {}

        # 判断
        if vm_name is None:
            raise CustomException("vm name is None.")
            return

        if self.info_vm(vm_name) is not None:
            raise CustomException("vm is exists.")
            return

        # 1 判断镜像文件是否存在,判断是否下载
        try:
            image_file = self.image.download_image(image_name)
        except Exception, e:
            raise CustomException("Error: %s" % str(e))
示例#5
0
文件: vmManage.py 项目: houht/mypaas
    def detach_disk(self, vm_name, disk_name):
        ""

        # 1 检测
        self.check(vm_name)

        # 2 查询
        disks = self.info_disks(vm_name)
        disk_target = None
        for disk in disks:
            if disk_name == disk['disk_name']:
                disk_target = disk['disk_target']
                break

        if disk_target is None:
            raise CustomException("Error: disk name is not exist!")
            return

        # 3 卸载磁盘
        self.vm_tools.detach_disk(vm_name, disk_target)

        # 4 删除lvm
        try:
            self.storageManage.remove_lvm(disk_name)
        except Exception, e:
            raise CustomException("Error: remove lvm failed!")
示例#6
0
文件: vmManage.py 项目: houht/mypaas
    def check(self, vm_name):
        ""

        if vm_name is None:
            raise CustomException("vm name is None.")
            return

        if self.info_vm(vm_name) is None:
            raise CustomException("vm is not exists.")
            return
示例#7
0
    def extend_lvm(self, lvm_name, extend_size):
        ""

        # 1 ²éѯ
        infos = self.info_lvm(lvm_name)
        if infos is None:
            raise CustomException("lvm is not exist.")

        vg_name = infos['vg_name']
        lvm_size = infos['lvm_size']

        # 2 À©Õ¹lvm
        lvm = LvmDriver(vg_name)
        lvm.extend_volume(lvm_name, "+" + str(lvm_size) + "M")

        cmd = Command()
        lvm_path = self.get_lvm_path(lvm_name, vg_name)

        # 3 ɨÃè
        cmd.execute("e2fsck -f -y %s" % lvm_path)

        # 4 ÖØÖÃ
        cmd.execute("resize2fs %s" % lvm_path)

        lvm_new_size = int(infos['lvm_size']) + int(extend_size)
        # 5 ¸üÐÂÊý¾Ý¿â
        update_sql = 'UPDATE lvm SET lvmSize = ? WHERE lvmName = ? '
        data = [(lvm_new_size, lvm_name)]

        storage_conf = self.conf.get_storage_conf()
        storage_db = storage_conf['db_path']
        sql_tools = SqliteTools(storage_db)
        sql_tools.update(update_sql, data)
示例#8
0
    def remove_lvm(self, lvm_name, mount=False):
        ""

        # 1 ²éѯÐÅÏ¢
        infos = self.info_lvm(lvm_name)
        if infos is None:
            raise CustomException("lvm  is not exist.")

        vg_name = infos['vg_name']
        lvm_dir = infos['lvm_dir']
        mount_dir = infos['mount_dir']

        # 2 жÔعÒÔØ
        if mount:
            self.umount_dir(lvm_name)

        # 3 ɾ³ýlvm
        lvm = LvmDriver(vg_name)
        lvm.remove_volume(lvm_dir)

        # 4 ɾ³ýÊý¾Ý¿â
        delete_sql = 'DELETE FROM lvm WHERE lvmName = ? '
        data = [(lvm_name, )]
        storage_conf = self.conf.get_storage_conf()
        storage_db = storage_conf['db_path']
        sql_tools = SqliteTools(storage_db)
        sql_tools.delete(delete_sql, data)
示例#9
0
def _get_dom(vm_):
    '''
    Return a domain object for the named vm
    '''
    conn = __get_conn()
    if vm_ not in list_vms():
        raise CustomException('The specified vm is not present')
    return conn.lookupByName(vm_)
示例#10
0
文件: dbTool.py 项目: houht/mypaas
 def execute(self,sql):
     ""
     
     try:
         connection = self.get_connection()   
         cursor=connection.cursor()   
         cursor.execute(sql) 
         #cursor.execute("commit")         
         connection.commit()
         cursor.close()
     except Exception , e:
         raise CustomException("Error: execute sql [%s]!"%sql)
示例#11
0
def __get_conn():
    '''
    Detects what type of dom this node is and attempts to connect to the
    correct hypervisor via libvirt.
    '''
    # This has only been tested on kvm and xen, it needs to be expanded to
    # support all vm layers supported by libvirt
    try:
        conn = libvirt.open('qemu:///system')
    except Exception:
        raise CustomException(
            'Sorry, failed to open a connection to the hypervisor ')
    return conn
示例#12
0
 def portProxyRegister(self,
                       ip,
                       port,
                       proxyIp=None,
                       proxyPort=None,
                       proxyType=None):
     '''
     代理端口注册:
     ip表示被代理的内部ip地址
     port表示被代理的内容port
     proxyIp表示对外的IP地址,参数为None表示使用cattle.conf中配置的agent_ip
     proxyPort表示对外的端口,参数为None表示使用cattle.conf中配置的agent_port_range中的端口随机分配一个
     '''
     #判断IP和端口是否为空
     try:
         IpUtils.ip2int(ip)
         tmpPort = int(port)
         if tmpPort < 1 and tmpPort > 65535:
             raise CustomException(
                 'port must greater than 1 and less than 65535')
     except Exception, e:
         raise CustomException("ip can not None, ip:(%s,%s)" % (ip, port))
示例#13
0
文件: vmManage.py 项目: houht/mypaas
    def delete_vm(self, vm_name=None):
        ""

        # 1 检测
        self.check(vm_name)

        # 2 删除vm和取消定义
        self.vm_tools.vm_delete(vm_name)

        # 3 删除lvm
        try:
            self.storageManage.remove_lvm(vm_name, True)
        except Exception, e:
            raise CustomException("Error: remove lvm failed!")
示例#14
0
文件: vmManage.py 项目: houht/mypaas
    def win_reg_set(self, vm_name, reg_path, reg_key, reg_new_value):

        # 1 检测
        self.check(vm_name)

        # 2 检测是否关闭

        try:
            # 3 初始化对象
            regedit = WinRegedit(vm_name)
            # 4 修改
            regedit.reg_modify(reg_path, reg_key, reg_new_value)
            # 5 关闭
            regedit.reg_close()
        except Exception, e:
            raise CustomException("Error: modify windows regedit failed!")
示例#15
0
文件: vmManage.py 项目: houht/mypaas
    def attach_disk(self, vm_name, target, disk_size):
        ""

        # 1 检测
        self.check(vm_name)

        disk_name = vm_name + "_" + target
        # 2 判断磁盘是否存在
        if self.exist_disk(disk_name):
            raise CustomException("Error: disk name is exist!")
            return

        # 3 查询当前磁盘情况,得到未使用的磁盘那盘符
        disk_target = target
        #        disks = self.info_disks(vm_name)
        #        targets = []
        #        for disk in disks:
        #            targets.append(disk['disk_target'])
        #
        #        for i in range(98,123):
        #            i =  "vd"+ chr(i)
        #            if i not in targets:
        #                disk_target = i
        #                break

        # 4 创建lvm磁盘
        self.storageManage.create_lvm(lvm_name=disk_name, lvm_size=disk_size)
        disk_file = self.storageManage.get_lvm_path(disk_name)

        # 5 格式化系统格式
        self.disk_mkfs(vm_name, disk_file)

        # 6 硬盘挂载
        self.vm_tools.attach_disk(vm_name, disk_target, disk_file)

        # 7 保存数据库
        # 磁盘类型,ntfs or ext4
        disk_type = self.get_os_type(vm_name)

        virtual_conf = self.conf.get_virtual_conf()
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)
        save_sql = '''INSERT INTO %s values (?, ?, ?, ?, ?, ?, ?, ?)''' % DISKTABLE
        data = [(vm_name, disk_size, disk_name, disk_target, disk_file,
                 disk_type, time.strftime("%Y-%m-%d %H:%M:%S"), "")]
        sql_tools.save(save_sql, data)
示例#16
0
 def loadone(self, filename):
     try:
         apps = AppService(filename)
     except Exception, e:
         raise CustomException("Load xml error: %s" % str(e))
示例#17
0
文件: vmManage.py 项目: houht/mypaas
            raise CustomException("Error: %s" % str(e))
        ip = info['ipAddress']

        interface['ip'] = info['ipAddress']
        interface['br_type'] = 'virtio'
        interface['br'] = info['groupName']
        interface['vif_name'] = info['ifName']
        interface['macs'] = info['macAddress']
        interfaces.append(interface)

        infos['ip'] = info['ipAddress']

        # 5 得到vnc port
        vnc_port = self.get_vnc_port()
        if vnc_port is None:
            raise CustomException("Error: vnc port is none.")

        # 6 创建vm

        # vnc密码,随机生成
        vnc_passwd = random.randint(1000, 9999)

        infos['vncPort'] = vnc_port
        infos['vncPasswd'] = vnc_passwd

        try:
            self.vm_tools.create_vm(vm_name=vm_name,
                                    mem_size=mem_size,
                                    vcpus=vcpus,
                                    img_vm=vm_file,
                                    vnc_port=vnc_port,
示例#18
0
文件: vmManage.py 项目: houht/mypaas
class VmManage:
    "virtualization management"

    def __init__(self, vm_name=None):
        ""

        self.vm = VmLib()
        self.conf = ConfigManage()
        self.image = ImageManage()
        self.storageManage = StorageManage()
        self.qemu = QemuTools()
        self.vm_tools = VmLib()
        self.net = NetworkService()

    # 查询数据库单个vm信息
    def info_vm(self, vm_name):
        ""

        virtual_conf = self.conf.get_virtual_conf()
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)

        fetchone_sql = 'SELECT * FROM %s WHERE vmName = ? ' % VMTABLE
        data = vm_name
        item = sql_tools.fetchone(fetchone_sql, data)
        infos = {}
        if item is not None:
            infos['vm_name'] = item[0]
            infos['vm_file'] = item[1]
            infos['image_name'] = item[2]
            infos['image_file'] = item[3]
            infos['vm_size'] = item[4]
            infos['mem_size'] = item[5]
            infos['vcpus'] = item[6]

            infos['vnc_port'] = item[7]
            infos['vnc_passwd'] = item[8]
            infos['interfaces'] = item[9]
            infos['ip'] = item[10]

            infos['createtime'] = item[11]
            infos['remark'] = item[12]
        else:
            return None

        return infos

    # 查询数据库所有vm信息
    def info_vms(self):
        ""

        virtual_conf = self.conf.get_virtual_conf()
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)

        fetchall_sql = 'SELECT * FROM %s ' % VMTABLE
        infos = sql_tools.fetchall(fetchall_sql)
        vms = []
        for item in infos:
            vm = {}
            if item is not None:
                vm['vm_name'] = item[0]
                vm['vm_file'] = item[1]
                vm['image_name'] = item[2]
                vm['image_file'] = item[3]
                vm['vm_size'] = item[4]
                vm['mem_size'] = item[5]
                vm['vcpus'] = item[6]

                vm['vnc_port'] = item[7]
                vm['vnc_passwd'] = item[8]
                vm['interfaces'] = item[9]
                vm['ip'] = item[10]

                vm['createtime'] = item[11]
                vm['remark'] = item[12]
            vms.append(vm)

        return vms

    def find_vm(self, vm_name=None):
        ""

        sql = "select * from %s where 1=1" % VMTABLE
        data = ()
        cond_conf = ''

        if vm_name is not None:
            cond_conf += ' and vmName=?'
            data += (vm_name, )

        virtual_conf = self.conf.get_virtual_conf()
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)

        if len(data) > 0:
            sql += cond_conf
            infos = sql_tools.fetchall(sql, data)
        else:
            infos = sql_tools.fetchall(sql)

        vms = []
        for item in infos:
            vm = {}
            if item is not None:
                vm['vmName'] = item[0]
                #vm['vm_file']      = item[1]
                vm['imageName'] = item[2]
                #vm['image_file']   = item[3]
                vm['vmSize'] = item[4]
                vm['memSize'] = item[5]
                vm['vcpus'] = item[6]

                vm['vncPort'] = item[7]
                vm['vncPasswd'] = item[8]
                vm['interfaces'] = item[9]
                vm['ip'] = item[10]

                vm['createtime'] = item[11]
                vm['remark'] = item[12]
            vms.append(vm)

        return vms

    # 查询vm的所有磁盘
    def info_disks(self, vm_name):
        ""

        virtual_conf = self.conf.get_virtual_conf()
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)

        fetchall_sql = 'SELECT * FROM %s where vmName = ?' % DISKTABLE
        data = (vm_name, )
        infos = sql_tools.fetchall(fetchall_sql, data)
        disks = []
        for item in infos:
            disk = {}
            if item is not None:
                disk['vm_name'] = item[0]

                disk['disk_size'] = item[1]
                disk['disk_name'] = item[2]
                disk['disk_target'] = item[3]
                disk['disk_file'] = item[4]
                disk['disk_type'] = item[5]

                disk['createtime'] = item[6]
                disk['remark'] = item[7]
            disks.append(disk)

        return disks

    # 判断磁盘是否存在
    def exist_disk(self, disk_name):
        ""

        virtual_conf = self.conf.get_virtual_conf()
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)

        fetchall_sql = 'SELECT * FROM %s where diskName = ?' % DISKTABLE
        data = disk_name
        infos = sql_tools.fetchone(fetchall_sql, data)
        if infos is None:
            return False
        else:
            return True

    # vm检查是否存在
    def check(self, vm_name):
        ""

        if vm_name is None:
            raise CustomException("vm name is None.")
            return

        if self.info_vm(vm_name) is None:
            raise CustomException("vm is not exists.")
            return

    # 创建VM
    def create_vm(self, **kwargs):
        ""

        image_name = kwargs.pop('image_name', None)
        vm_name = kwargs.pop('vm_name', None)
        # VM大小,单位M
        vm_size = kwargs.pop('vm_size', None)
        # 内存大小,单位M
        mem_size = kwargs.pop('mem_size', None)
        # cpu大小,单位核数
        vcpus = kwargs.pop('vcpus', None)
        host_name = kwargs.pop('host_name', "")

        infos = {}

        # 判断
        if vm_name is None:
            raise CustomException("vm name is None.")
            return

        if self.info_vm(vm_name) is not None:
            raise CustomException("vm is exists.")
            return

        # 1 判断镜像文件是否存在,判断是否下载
        try:
            image_file = self.image.download_image(image_name)
        except Exception, e:
            raise CustomException("Error: %s" % str(e))
        img_infos = self.image.info_image(image_name)

        arch = img_infos['arch']
        machine = img_infos['machine']

        # 2 创建lvm
        lvm_mount_dir = self.storageManage.create_lvm(lvm_name=vm_name,
                                                      lvm_size=vm_size,
                                                      mount=True)

        # 3 基于父创建img
        vm_file = os.path.join(lvm_mount_dir, vm_name + '.img')
        self.qemu.create_img(basefile=image_file, filename=vm_file)

        # 4 查询网络信息
        interfaces = []
        interface = {}
        try:
            info = self.net.fixedIpRegister(hostName=host_name)
        except Exception, e:
            raise CustomException("Error: %s" % str(e))
示例#19
0
文件: appManage.py 项目: houht/mypaas
    def app_deploy(self, env):
        ""

        # 参数获取
        serviceId = env.get_service_env('serviceId')

        instanceId = env.get_service_env('instanceId')

        appName = env.get_app_env('appName')
        if appName is None:
            appName = ""

        param = env.get_app_env('param')

        listenPort = env.get_router_env('listenPort')
        if listenPort is None:
            listenPort = 0

        domain = env.get_router_env('domain')
        if domain is None:
            domain = ""

        name = serviceId
        # 1 判断服务实例是否已经安装
        app_info = self.app_info(instanceId)
        if len(app_info) == 1:
            raise CustomException("the service [%s]  already exists" %
                                  instanceId)
            return

        # 2 判断服务是否存在
        if not self.script.hasService(name):
            raise CustomException("Could not find the service %s" % str(name))
            return

        # 3 得到改服务对象
        service = self.script.getService(name)

        # 4 得到部署的列表
        actions = service.target.actions['deploy']

        # 5 保存数据库
        appType = service.serviceType
        vmName = env.get_vm_env('name')
        status = 'creating'

        app_conf = self.conf.get_app_conf()
        app_db = app_conf['db_path']
        sql_tools = SqliteTools(app_db)

        save_sql = '''INSERT INTO %s values (?, ?, ?,?, ?,?,?,?,?,?,?)''' % APPTABLE
        data = [(instanceId, appName, appType, serviceId, vmName,
                 domain, listenPort, param, status,
                 time.strftime("%Y-%m-%d %H:%M:%S"), "")]
        sql_tools.save(save_sql, data)

        # 6 循环部署响应列表,逐个操作

        # 变量赋值
        self.executer.env = env
        self.executer.properties = service.propertys.parameter

        # 处理
        for action in actions:
            try:
                getattr(self.executer, action['action'])(action['param'])
            except Exception, e:
                # 删除数据库
                delete_sql = 'DELETE FROM %s WHERE instanceId = ? ' % APPTABLE
                data = [(instanceId, )]
                sql_tools.delete(delete_sql, data)
                # 删除VM
                self.vm.delete_vm(vmName)
                # 异常
                raise CustomException("Error executing method %s !,%s" %
                                      (action['action'], str(e)))
                return
示例#20
0
            #写dhcp文件
            dhcp_obj = Dhcp(self.network_conf['dhcp_conf'],
                            self.network_conf['dhcp_service'])
            dhcp_obj.write_conf(hdcp_subnet_list, None, None)
            dhcp_obj.restart()
        elif self.net_type == "libvirt":
            #写libvert的network
            # groupName, macAddress, ipAddress, hostName
            try:
                add_host_dhcp(groupName, macAddress, ipAddress, hostName)
            except Exception, e:
                raise CustomExecException(
                    "add host ip to libvirt network Failure,Reason:%s" %
                    str(e))
        else:
            raise CustomException("Configuration error,net_type=%s" %
                                  self.net_type)

        #插入数据表
        sql = "insert into %s(id, groupName, macAddress, ipAddress, hostname, ifName) values(?,?,?,?,?,?)" % TABLE_DHCP
        tmp_hostname = hostName
        if hostName is None:
            tmp_hostname = ''
        data = [(UUIDUtils.getId(), groupName, macAddress, ipAddress,
                 tmp_hostname, ifName)]
        sql_tools.save(sql, data)
        return {
            "groupName": groupName,
            "ipAddress": ipAddress,
            "macAddress": macAddress,
            "hostName": hostName,
            "ifName": ifName