def info_lvm(self, lvm_name): "" storage_conf = self.conf.get_storage_conf() storage_db = storage_conf['db_path'] sql_tools = SqliteTools(storage_db) fetchone_sql = 'SELECT * FROM lvm WHERE lvmName = ? ' data = lvm_name item = sql_tools.fetchone(fetchone_sql, data) infos = {} if item is not None: infos['lvm_name'] = item[0] infos['vg_name'] = item[1] infos['lvm_size'] = item[2] infos['lvm_dir'] = item[3] infos['mount_dir'] = item[4] infos['lvm_mkfs'] = item[5] infos['lvm_type'] = item[6] infos['createtime'] = item[7] infos['remark'] = item[8] else: return None return infos
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 info_image(self,image_name): "" image_conf = self.conf.get_image_conf() images_db = image_conf['db_path'] sql_tools = SqliteTools(images_db) fetchone_sql = 'SELECT * FROM images WHERE name = ? ' data = image_name item = sql_tools.fetchone(fetchone_sql,data) infos = {} if item is not None: infos['imageid'] = item[0] infos['name'] = item[1] infos['system'] = item[2] infos['version'] = item[3] infos['arch'] = item[4] infos['machine'] = item[5] infos['size'] = item[6] infos['savepath'] = item[7] infos['ext'] = item[8] infos['status'] = item[9] infos['vtype'] = item[10] infos['createtime']= item[11] infos['remark'] = item[12] return infos
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
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
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
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)
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
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)
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 create_app_table(self): "" app_conf = self.conf.get_app_conf() app_db = app_conf['db_path'] sql_tools = SqliteTools(app_db) # 1 服务表 create_app_table_sql = '''CREATE TABLE `%s` ( `instanceId` varchar(128) NOT NULL, `appName` varchar(128) default NULL, `appType` varchar(128) NOT NULL, `serviceId` varchar(128) NOT NULL, `vmName` varchar(128) NOT NULL, `domain` varchar(128) default NULL, `listenPort` int(10) default 0, `param` text default NULL, `status` varchar(12) NOT NULL, `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `remark` varchar(1024) DEFAULT NULL, PRIMARY KEY (`instanceId`) )''' % APPTABLE sql_tools.create_table(create_app_table_sql)
def proxy_del(self, **kwargs): "" # 必选 vm_name = kwargs.pop('vm_name', None) port = kwargs.pop('port', None) # 可选 ip = kwargs.pop('ip', None) proxy_ip = kwargs.pop('proxy_ip', None) proxy_port = kwargs.pop('proxy_port', None) proxy_type = kwargs.pop('proxy_type', None) # 1 检测 self.check(vm_name) # 2 查询ip if ip == None: info = self.info_vm(vm_name) ip = info['ip'] # 3 删除代理 self.net.portProxyRemove(ip, port, proxyType=proxy_type) # 4 删除数据库 delete_sql = 'DELETE FROM %s WHERE ip = ? and port = ? ' % VMPROXY data = [(ip, port)] virtual_conf = self.conf.get_virtual_conf() virtual_db = virtual_conf['db_path'] sql_tools = SqliteTools(virtual_db) sql_tools.delete(delete_sql, data)
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
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))
def set_vm_cpu(self, vm_name, vcpus): "" # 1 检测 self.check(vm_name) # 2 设置cpu self.vm_tools.set_vcpus(vm_name=vm_name, vcpus=vcpus) # 3 修改数据库 virtual_conf = self.conf.get_virtual_conf() virtual_db = virtual_conf['db_path'] sql_tools = SqliteTools(virtual_db) update_sql = 'UPDATE %s SET vcpus = ? WHERE vmName = ? ' % VMTABLE data = [(vcpus, vm_name)] sql_tools.update(update_sql, data)
def set_vm_mem(self, vm_name, size): "" # 1 检测 self.check(vm_name) # 2 设置内存 self.vm_tools.set_mem(vm_name=vm_name, memory=size) # 3 修改数据库 virtual_conf = self.conf.get_virtual_conf() virtual_db = virtual_conf['db_path'] sql_tools = SqliteTools(virtual_db) update_sql = 'UPDATE %s SET memSize = ? WHERE vmName = ? ' % VMTABLE data = [(size, vm_name)] sql_tools.update(update_sql, data)
def drop_image(self,image_name): "" image_conf = self.conf.get_image_conf() image_path = image_conf['path'] image_file = os.path.join(image_path,image_name) if os.path.isfile(image_file): os.remove(image_file) update_sql = 'UPDATE images SET status = ? WHERE name = ? ' data = [(9, image_name)] images_db = image_conf['db_path'] sql_tools = SqliteTools(images_db) sql_tools.update(update_sql,data)
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)
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
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
def create_network_table(self): ''' create sqlite table ''' ''' id, 主键ID uuid groupName, 对应的桥名称 macAddress, 虚拟的mack地址 ipAddress, 虚拟的静态IP hostname, 虚拟机主机名 ifName 虚拟机网口的名 ''' create_dhcp_table_sql = '''CREATE TABLE `dhcp` ( `id` varchar(64) PRIMARY KEY NOT NULL, `groupName` varchar(64) NOT NULL, `macAddress` varchar(64) NOT NULL, `ipAddress` varchar(64) NOT NULL, `hostname` varchar(256) NOT NULL, `ifName` varchar(64) NOT NULL )''' ''' id,主键ID uuid groupName, 对应的桥名称 dip, 通过groupName从配置中取得,外网IP dPort, 对外端口 tIp, 虚拟机IP tPort 虚拟机端口 ''' create_proxyPort_table_sql = '''CREATE TABLE `proxyPort` ( `id` varchar(64) PRIMARY KEY NOT NULL, `groupName` varchar(64), `dip` varchar(64) NOT NULL, `dPort` int, `tIp` varchar(64) NOT NULL, `tPort` varchar(64) NOT NULL )''' ''' id,主键ID uuid pType, 1:vnc端口,2:其它业务端口 ''' create_businessPort_table_sql = '''CREATE TABLE `businessPort` ( `id` varchar(64) PRIMARY KEY NOT NULL, `pType` varchar(5) NOT NULL, `dPort` int )''' network_conf = self.conf.get_network_conf() network_db = network_conf['db_path'] sql_tools = SqliteTools(network_db) sql_tools.create_table(create_dhcp_table_sql) sql_tools.create_table(create_proxyPort_table_sql) sql_tools.create_table(create_businessPort_table_sql)
def create_lvm_table(self): "" create_table_sql = '''CREATE TABLE `lvm` ( `lvmName` varchar(64) NOT NULL, `vgName` varchar(200) NOT NULL, `lvmSize` double(24) NOT NULL, `lvmDir` varchar(128) DEFAULT NULL, `mountDir` varchar(128) DEFAULT NULL, `lvmMkfs` varchar(32) DEFAULT NULL, `lvmType` varchar(32) DEFAULT NULL, `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `remark` varchar(1024) DEFAULT NULL, PRIMARY KEY (`lvmName`) )''' storage_conf = self.conf.get_storage_conf() storage_db = storage_conf['db_path'] sql_tools = SqliteTools(storage_db) sql_tools.create_table(create_table_sql)
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
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
def create_lvm(self, **kwargs): "" lvm_name = kwargs.pop('lvm_name', None) lvm_size = kwargs.pop('lvm_size', None) vg_name = kwargs.pop('vg_name', None) mount = kwargs.pop('mount', False) # 1 ÅжÏvg name storage_conf = self.conf.get_storage_conf() if vg_name is None: vg_name = storage_conf['vg_name'] # 2 ´´½¨lvm lvm = LvmDriver(vg_name) lvm.create_volume(lvm_name, str(lvm_size) + "M") # 3 ¸ñʽ»¯ lvm_dir = self.get_lvm_path(lvm_name, vg_name) self.format_dir(lvm_dir) lvm_mkfs = storage_conf['cmd_format'] mount_dir = "" lvm_type = "disk" # 4 ÅжÏÊÇ·ñ¹ÒÔØ if mount: lvm_type = "image" # 2 ¹ÒÔØ mount_dir = os.path.join(storage_conf['path'], lvm_name) self.mount_dir(lvm_dir, mount_dir) # 5 ±£´æÊý¾Ý¿â save_sql = '''INSERT INTO lvm values (?, ?, ?,?,?,?,?,?,?)''' data = [(lvm_name, vg_name, lvm_size, lvm_dir, mount_dir, lvm_mkfs, lvm_type, time.strftime("%Y-%m-%d %H:%M:%S"), "")] storage_db = storage_conf['db_path'] sql_tools = SqliteTools(storage_db) sql_tools.save(save_sql, data) return mount_dir
def proxy_add(self, **kwargs): "" # 必选 vm_name = kwargs.pop('vm_name', None) port = kwargs.pop('port', None) # 可选 ip = kwargs.pop('ip', None) proxy_ip = kwargs.pop('proxy_ip', None) proxy_port = kwargs.pop('proxy_port', None) proxy_type = kwargs.pop('proxy_type', None) # 1 检测 self.check(vm_name) # 2 查询ip if ip == None: info = self.info_vm(vm_name) ip = info['ip'] # 3 添加代理 if proxy_ip == None and proxy_port == None: proxy_info = self.net.portProxyRegister(ip, port, proxyType=proxy_type) proxy_ip = proxy_info['proxyIp'] proxy_port = proxy_info['proxyPort'] # 4 添加数据库 virtual_conf = self.conf.get_virtual_conf() virtual_db = virtual_conf['db_path'] sql_tools = SqliteTools(virtual_db) save_sql = '''INSERT INTO %s values (?, ?, ?, ?, ?, ?, ?)''' % VMPROXY data = [(vm_name, ip, port, proxy_ip, proxy_port, time.strftime("%Y-%m-%d %H:%M:%S"), "")] sql_tools.save(save_sql, data) return proxy_ip, proxy_port
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
def create_images_table(self): "" create_table_sql = '''CREATE TABLE `images` ( `imageId` varchar(64) NOT NULL, `name` varchar(200) DEFAULT NULL, `system` varchar(64) DEFAULT NULL, `version` varchar(64) DEFAULT NULL, `arch` varchar(64) DEFAULT NULL, `machine` varchar(64) DEFAULT NULL, `size` double(24) DEFAULT NULL, `savepath` varchar(64) DEFAULT NULL, `ext` varchar(64) DEFAULT NULL, `status` int(1) DEFAULT 0, `vtype` varchar(64) DEFAULT NULL, `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `remark` varchar(1024) DEFAULT NULL, PRIMARY KEY (`imageId`) )''' image_conf = self.conf.get_image_conf() images_db = image_conf['db_path'] sql_tools = SqliteTools(images_db) sql_tools.create_table(create_table_sql)
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 # 7 删除数据库 app_conf = self.conf.get_app_conf() app_db = app_conf['db_path'] sql_tools = SqliteTools(app_db) delete_sql = 'DELETE FROM %s WHERE instanceId = ? ' % APPTABLE data = [(instanceId, )] sql_tools.delete(delete_sql, data) # 服务负载 def app_balance(self): "" pass # 服务配置 def app_conf(self): ""
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