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 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 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 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
break if proxyPort in [None, '']: raise CustomNotFoundException( "unable to allocate port,no available port, please configure the file(cattle.conf) agent_port_range." ) if proxyPort in useProxyPortList and proxyIp == agent_ip: raise CustomException("port is proxy, proxy:(%s,%d)" % (proxyIp, int(proxyPort))) #将端口代理加入到iptables iptablesTool = IptablesTools() #print ip, port, proxyIp, proxyPort iptablesTool.add_nat(ip, port, proxyIp, proxyPort, proxyType) sql = "insert into %s(id,dip,dPort,tIp,tPort,groupName)values(?,?,?,?,?,'')" % TABLE_PROXYPORT data = [(UUIDUtils.getId(), proxyIp, proxyPort, ip, port)] sql_tools.save(sql, data) return { "proxyIp": proxyIp, "proxyPort": proxyPort, "ip": ip, "port": port } #代理端口注销 def portProxyRemove(self, ip, port, proxyIp=None, proxyPort=None, proxyType=None): agent_ip = self.network_conf['agent_ip']