Beispiel #1
0
    def portProxyList(self, ip=None, port=None, proxyIp=None, proxyPort=None):
        '''
        参数:
        ip 表示被代理的ip
        port 表示被代理的端口
        proxyIp 表示代理ip
        proxyPort 表示代理端口
        返回值result[][]
        result[x][id,proxyIp,proxyPort,ip,port]
        '''
        sql = "select id,dip,dPort,tIp,tPort from %s where 1=1" % TABLE_PROXYPORT
        cond_conf = ""
        data = ()
        if ip not in [None, '']:
            cond_conf += " and tIp=?"
            data += (ip, )
        if port != None:
            cond_conf += " and tPort=?"
            data += (port, )
        if proxyIp != None:
            cond_conf += " and dip=?"
            data += (proxyIp, )
        if proxyPort != None:
            cond_conf += " and dPort=?"
            data += (proxyPort, )
        sql += cond_conf

        sql_tools = SqliteTools(self.db_name)
        item = None
        if len(data) > 0:
            sql += cond_conf
            item = sql_tools.fetchall(sql, data)
        else:
            item = sql_tools.fetchall(sql)
        return item
Beispiel #2
0
    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
Beispiel #3
0
    def get_vnc_port(self):
        ""

        virtual_conf = self.conf.get_virtual_conf()
        vnc_port_range = virtual_conf['vnc_port_range']
        min_port, max_port = vnc_port_range.split('-')

        #查询当前vnc端口
        virtual_db = virtual_conf['db_path']
        sql_tools = SqliteTools(virtual_db)
        fetchall_sql = '''SELECT vncPort FROM %s order by vncPort''' % VMTABLE
        ports = sql_tools.fetchall(fetchall_sql)

        treated_ports = []
        for port in ports:
            treated_ports.append(port[0])

        for i in range(int(min_port), int(max_port) + 1):
            if i not in treated_ports:
                vnc_port = i
                break

        if vnc_port is not None:
            return vnc_port
        else:
            return None
Beispiel #4
0
    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
Beispiel #5
0
    def proxy_list(self, **kwargs):
        ""

        vm_name = kwargs.pop('vm_name', None)
        port = kwargs.pop('port', None)
        ip = kwargs.pop('ip', None)

        sql = "select * from %s where 1=1" % VMPROXY

        data = ()
        cond_conf = ''

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

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

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

        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)

        proxys = []
        for item in infos:
            proxy = {}
            if item is not None:
                proxy['vmName'] = item[0]
                proxy['ip'] = item[1]
                proxy['port'] = item[2]
                proxy['proxyIp'] = item[3]
                proxy['proxyPort'] = item[4]
            proxys.append(proxy)

        return proxys
Beispiel #6
0
    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
Beispiel #7
0
    def fixedIpList(self, groupName=None, ipAddress=None):
        sql = "select ipAddress, macAddress, groupName, hostname, ifName,id  from %s where 1=1" % TABLE_DHCP

        data = ()
        cond_conf = ''
        if groupName is not None:
            cond_conf += ' and groupName=?'
            data += (groupName, )
        if ipAddress is not None:
            cond_conf += ' and ipAddress=?'
            data += (ipAddress, )

        sql_tools = SqliteTools(self.db_name)
        item = None
        if len(data) > 0:
            sql += cond_conf
            item = sql_tools.fetchall(sql, data)
        else:
            item = sql_tools.fetchall(sql)
        return item
Beispiel #8
0
    def app_info(self, instanceId=None):
        ""

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

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

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

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

        apps = []
        for item in infos:
            app = {}
            if item is not None:
                app['instanceId'] = item[0]
                app['appName'] = item[1]
                app['appType'] = item[2]
                app['serviceId'] = item[3]
                app['vmName'] = item[4]
                app['domain'] = item[5]
                app['listenPort'] = item[6]

                app['param'] = item[7]
                app['status'] = item[8]

                app['createtime'] = item[9]
                app['remark'] = item[10]
            apps.append(app)

        return apps
Beispiel #9
0
    def fixedIpRemove(self, groupName, ipAddress):
        hdcp_subnets = {}

        bridge_list = self.network_conf['bridge']

        if self.net_type == "dhcp":
            #根据配置文件初始化dhcp配置
            hdcp_subnets = self._conf2dchp(bridge_list)

        #读取dhcp数据表
        sql = "select ipAddress, macAddress, groupName, hostname, ifName,id  from %s" % TABLE_DHCP
        sql_tools = SqliteTools(self.db_name)
        item = sql_tools.fetchall(sql)

        removeId = None
        for it in item:
            #print it[5],it[2], groupName,it[0],ipAddress
            if it[2] == groupName and it[0] == ipAddress:
                removeId = it[5]
                continue
            if self.net_type == "dhcp":
                #添加host到subnet中
                self._addHost2dhcp(hdcp_subnets, it[2], it[0], it[1], it[3],
                                   it[4])

        if removeId is None:
            message = "ip is not exists. param(%s,%s)" % (groupName, ipAddress)
            raise CustomNotFoundException(message)

        if self.net_type == "dhcp":
            #将新生产的ip添加到subnet中
            hdcp_subnet_list = []
            for (k, v) in hdcp_subnets.items():
                hdcp_subnet_list.append(v)
            #写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":
            #从libvirt的network中删除
            # groupName, macAddress, ipAddress, hostName
            try:
                del_host_dhcp(groupName, macAddress, ipAddress, hostName)
            except Exception, e:
                raise CustomExecException(
                    "add host ip to libvirt network Failure,Reason:%s" %
                    str(e))
Beispiel #10
0
    def portProxyRemove(self,
                        ip,
                        port,
                        proxyIp=None,
                        proxyPort=None,
                        proxyType=None):
        agent_ip = self.network_conf['agent_ip']
        if proxyIp in [None, '']:
            proxyIp = agent_ip

        sql = "select dip,dPort,tIp,tPort,id from %s where " % TABLE_PROXYPORT
        cond_conf = ''
        cond_conf += "tIp=? and tPort=? and dip=?"
        data = (ip, port, proxyIp)

        if proxyPort not in [None]:
            cond_conf += " and dPort=?"
            data += (proxyPort, )
        sql += cond_conf

        #执行查询
        sql_tools = SqliteTools(self.db_name)
        item = sql_tools.fetchall(sql, data)
        if len(item) != 1:
            raise CustomNotFoundException(
                "Not found the proxy port (ip=%s,port=%s,proxyIp=%s,proxyPort=%s) find result num=%d"
                % (ip, port, proxyIp, proxyPort, len(item)))
        dip, dPort, tIp, tPort, id = item[0]

        #将端口代理从iptables中删除
        iptablesTool = IptablesTools()
        iptablesTool.del_nat(tIp, tPort, dip, dPort, proxyType)

        sql = "delete from %s where id=?" % TABLE_PROXYPORT
        data = [(id, )]
        sql_tools.delete(sql, data)
        return True
Beispiel #11
0
    def fixedIpRegister(self,
                        groupName=None,
                        hostName=None,
                        ifName=None,
                        macAddress=None,
                        ipAddress=None):
        '''
        groupName表示网桥的名称,非必填,默认读取配置文件中的default_bridge字段的值
        hostName表示虚拟机的主机名称,非必填
        ifName 虚拟机的网口名称,非必填
        macAddress 虚拟机的网口的mac地址,非必填
        ipAddress 虚拟机的静态IP,非必填
        ifName 网口名称
        '''
        hdcp_subnets = {}

        if groupName in [None, '']:
            groupName = self.network_conf['default_bridge']
        bridge_list = self.network_conf['bridge']
        #根据配置文件初始化dhcp配置
        if self.net_type == "dhcp":
            hdcp_subnets = self._conf2dchp(bridge_list)

        #已使用ip和已经使用mac
        used_ips = []
        used_mac = []
        #读取dhcp数据表
        sql = "select ipAddress, macAddress, groupName, hostname, ifName  from %s" % TABLE_DHCP
        sql_tools = SqliteTools(self.db_name)
        item = sql_tools.fetchall(sql)

        for it in item:
            used_ips.append(IpUtils.ip2int(it[0]))

            macs = it[1].split(':')
            mac_int = (int(macs[3], 16) << 16) + (int(macs[4], 16) << 8) + (
                int(macs[5], 16))
            used_mac.append(mac_int)

            if self.net_type == "dhcp":
                #添加host到subnet中
                self._addHost2dhcp(hdcp_subnets, it[2], it[0], it[1], it[3],
                                   it[4])

        used_ips.sort()
        used_mac.sort()

        #获取IP
        if ipAddress in [None, '']:
            #开始和结束ip
            static_bootp = bridge_list[groupName]['static-bootp']
            range = static_bootp.split()
            begin_ip = IpUtils.ip2int(range[0])
            end_ip = IpUtils.ip2int(range[1])

            #获取ip
            if len(used_ips) == 0:
                ipAddress = IpUtils.int2ip(begin_ip)
            elif used_ips[len(used_ips) - 1] < end_ip:
                ipAddress = IpUtils.int2ip(used_ips[len(used_ips) - 1] + 1)
            else:
                for num in range(begin_ip, end_ip + 1):
                    if num not in used_ips:
                        ipAddress = IpUtils.int2ip(num)
                        break
        #验证ip是否获得
        if ipAddress in [None, '']:
            raise CustomNotFoundException(
                "unable to allocate ip,no available ip, please configure the file(cattle.conf) bridge:%s[static-bootp] "
                % groupName)
        #网口的名称
        if ifName in [None, '']:
            ifName = "p%s" % IpUtils.ip_remove_point(ipAddress)
        #网口mac地址
        if macAddress in [None, '']:
            begin_mac = 0x010101
            end_mac = 0xffffff
            select_mac = begin_mac
            if len(used_mac) == 0:
                select_mac = begin_mac
            elif used_mac[len(used_mac) - 1] < end_mac:
                select_mac = used_mac[len(used_mac) - 1] + 1
            else:
                for num in range(begin_mac, end_mac + 1):
                    if num not in used_mac:
                        select_mac = num
                        break
            macAddress = "%s:%02x:%02x:%02x" % (MAC_PREFIX,
                                                (select_mac >> 16) & 0xff,
                                                (select_mac >> 8) & 0xff,
                                                (select_mac >> 0) & 0xff)

        if self.net_type == "dhcp":
            #将新生产的ip添加到subnet中
            self._addHost2dhcp(hdcp_subnets, groupName, ipAddress, macAddress,
                               hostName, ifName)
            hdcp_subnet_list = []
            for (k, v) in hdcp_subnets.items():
                hdcp_subnet_list.append(v)

            #写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))
Beispiel #12
0
        #判断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))

        agent_ip = self.network_conf['agent_ip']
        agent_port_range = self.network_conf['agent_port_range']

        sql = "select dip,dPort,tIp,tPort,id from %s where 1=1" % TABLE_PROXYPORT
        sql_tools = SqliteTools(self.db_name)
        item = sql_tools.fetchall(sql)

        useProxyPortList = []
        for it in item:
            #it[0]
            useProxyPortList.append(it[1])
        useProxyPortList.sort()

        if proxyIp in [None, '']:
            proxyIp = agent_ip

        if proxyPort in [None, '']:
            port_range = agent_port_range.split("-")
            begin_port = int(port_range[0])
            end_port = int(port_range[1])