def remove(self, request, *args, **kwargs): # 卸载插件 # 读取插件 try: pid = request.query_params.get('id') except Exception as e: print(e) return errors(400, '必须指定插件') try: info = List.objects.get(id=pid) except Exception as e: print(e) return errors(404, '没找到该插件') try: container = Container.objects.get(plugins=info) except Exception as e: print(e) return errors(410, '您已经卸载') conf = dict(json.loads(info.config)) # 调用插件安装 ip_module = __import__('%s.%s' % ( info.name, info.name), fromlist=[info.class_name]) # 使用getattr()获取imp_module的类 plugins_class = getattr(ip_module, info.class_name) cls_obj = plugins_class(**conf) container = cls_obj.remove(container) # 移除容器 container.delete() # 返回提示 return errors(200)
def createdb(self, request, *args, **kwargs): # 创建数据库 # 读取插件 try: ver = request.query_params.get('ver') except Exception as e: pass if not ver: ver = 5 try: name = request.query_params.get('name') except Exception as e: return errors(400, '必须指定名称') try: vhost = request.query_params.get('vhost') except Exception as e: vhost = None error, msg = createDB(name, ver=ver, vhost=vhost) if error: return errors(404, msg) # 返回提示 return errors(201)
def create(self, request, *args, **kwargs): try: Vhosts.objects.get(domain=request.data.get('domain')) return errors(422, {'domain': ['域名已经存在']}) except Exception as e: pass serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def removedb(self, request, *args, **kwargs): # 删除数据库 # 读取插件 try: mid = int(request.query_params.get('id')) except Exception as e: return errors(400, '必须指定数据库') try: mysqldb = List.objects.get(id=mid) except Exception as e: return errors(404, '没找到该数据库') try: info = Plugins.objects.get(name='mysql', version=mysqldb.version) except Exception as e: return errors(404, '没找到该插件') conf = dict(json.loads(info.config)) cls_obj = Mysql(**conf) container = cls_obj.removeDB(mysqldb.version, mysqldb.name) # 删除数据库 mysqldb.delete() if not container: return errors(400, '请手动删除数据库') # 返回提示 return errors(204)
def setup(self, request, *args, **kwargs): # 安装插件 # 读取插件 try: pid = request.query_params.get('id') except Exception as e: print(e) return errors(400, '必须指定插件') try: resetup = int(request.query_params.get('re', 0)) except Exception as e: print(e) resetup = 0 try: info = List.objects.get(id=pid) except Exception as e: print(e) return errors(404, '没找到该插件') try: conf = dict(json.loads(request.query_params.get('config'))) except Exception as e: print(e) conf = dict(json.loads(info.config)) try: container = Container.objects.get(plugins=info) if not resetup: return errors(422, '您已经安装该插件') except Exception as e: print(e) container = Container() container.plugins = info # 调用插件安装 ip_module = __import__('%s.%s' % ( info.name, info.name), fromlist=[info.class_name]) # 使用getattr()获取imp_module的类 plugins_class = getattr(ip_module, info.class_name) cls_obj = plugins_class(**conf) container = cls_obj.setup(container) # 安装信息入库 try: container.save() except Exception as e: print(e) return errors(500, '保存容器信息失败') try: info.config = json.dumps(conf) info.save() except Exception as e: print(e) return errors(500, '保存配置信息失败') # 返回提示 return errors(200)
def update(self, request, *args, **kwargs): # 保存前先校验域名是否存在于主域名 try: Vhosts.objects.get(domain=request.data.get('domain')) return errors(422, {'domain': ['域名已经存在']}) except Exception as e: pass partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return Response(serializer.data)
def create(self, request, *args, **kwargs): # 创建同名数据库 try: mysql = int(request.data.get('mysql')) except Exception as e: mysql = 0 serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) # print(mysql) # print(serializer.data) if mysql: error, msg = createDB(serializer.data['domain'], ver=mysql, vhost=serializer.data['id']) if error: return errors(404, 'Vhost创建成功,但是Mysql创建失败' + msg) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)