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 test_get_caches_pending_to_purge_for_a_host(self): ''' Get the list of urls to be purged by a specific host ''' hostname = 'myservername' # these 2 urls should return on the GET below new_url_1 = 'http://domain.com/path/to/purge_1' new_url_2 = 'http://domain.com/path/to/purge_2' # must not return on GET because it was purged purged_url = 'http://domain.com/path/already_purge' # must not return on GET because it was regitered before the host old_url = 'http://domain.com/path/old_url' db.session.add( Host(id=1, hostname=hostname, created_at=datetime.datetime(2016, 7, 12))) db.session.add( Url(id=1, url=old_url, created_at=datetime.datetime(2015, 7, 12))) db.session.add(Url(id=2, url=purged_url)) db.session.add(Url(id=3, url=new_url_1)) db.session.add(Url(id=4, url=new_url_2)) db.session.add(Purge(id=1, url_id=2, host_id=1)) db.session.commit() response = self.client.get('/hosts/pending_purge', query_string={'hostname': hostname}) self.assertEqual(response.status_code, 200) computed = json.loads(response.get_data(as_text=True)) self.assertEqual(computed, [new_url_1, new_url_2]) self.assertNotIn(purged_url, computed)
def add_host(hostname): ''' Add host do DB ''' host = Host(hostname=hostname) db.session.add(host) try: db.session.commit() except sqlalchemy.exc.IntegrityError: return 'Duplicated host', 500 return '', 201
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 get(self): ''' 获取所有的zones和主机 ''' host_id = request.values.get('id') if host_id: if not g.user.has_host_perm(host_id): return self.jsonify(error='无权访问该主机') return self.jsonify( Host.get_by(id=host_id, to_dict=True, first=True)) hosts = Host.query.filter(Host.deleted_at.is_(None)).all() zones = [i.zone for i in hosts if i.zone] perms = [x.id for x in hosts] if g.user.is_supper else g.user.host_perms return self.jsonify({ 'zones': list(set(zones)), 'hosts': [x.to_dict() for x in hosts], 'perms': perms })
def test_notify_purged_url(self): hostname = 'myservername' url = 'http://domain.com/path/to/purge' # Populate DB for test db.session.add(Url(id=1, url=url)) db.session.add(Host(id=1, hostname=hostname)) db.session.commit() response = self.client.post('/purge', data={ 'hostname': 'myservername', 'url': 'http://domain.com/path/to/purge', 'command_output': 'result_from_purge_command', }) self.assertEqual(response.status_code, 201)
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()