def post(self): ''' 添加主机 ''' id = request.values.get('id', None) zone = request.values.get('zone', None) name = request.values.get('name', None) username = request.values.get('username', None) password = request.values.pop('password', None) hostname = request.values.get('hostname', None) port = request.values.get('port', None) if valid_ssh(hostname, port, username, password) is False: return self.jsonify('auth fail') if id: host = Host.get_by(id=id, to_dict=False, first=True) host.update(**request.values) elif Host.query.filter(db.exists().where( and_(Host.name == name, Host.deleted_by.is_(None)))).scalar(): return self.jsonify(error='已存在的主机名称【%s】' % name) else: request.values['created_by'] = g.user.id host = Host.create(**request.values) if g.user.role: g.user.role.add_host_perm(host.id) return self.jsonify(error='')
def app_detail(request, pk): try: try: pk = int(pk) app = App.objects.get(pk=pk) except: app = App.objects.filter(name=pk).first() except Group.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = AppSerializer(app) return JsonResponse(serializer.data) if request.method == 'DELETE': app.enable = 0 app.save() return JsonResponse(serializer.data) if request.method == 'PUT': if not app: res = {"code": 405, "message": "Not found this app"} return Response(data=res, status=405) ip = get_ip(request, right_most_proxy=True) if ip is not None: host = Host.objects.filter(ip=ip).first() if host is None: host = Host.create(ip) host.save() status = request.data.get("status") statistics = request.data.get('statistics') app.message = request.data.get("message", app.message) if status is None: res = {"code": 400, "message": "wong"} return Response(data=res, status=400) app.status = status app.last_update = datetime.now() app.host_id = host.id app.save() if statistics: try: json.loads(statistics) except: res = {"code": 400, "message": "Statistics format must json"} return Response(data=res, status=400) appStatistics = AppStatistics.create(statistics, app.id) appStatistics.save() return JsonResponse(object_to_json(app))
def _init_Host(self): Host.create("127.0.0.1", 'local').save() Host.create("192.168.26.17", "chengdu_17").save() Host.create("192.168.26.16", "chengdu_16").save()