monkey/monkey_island/cc/resources/root.py

37 lines
1.1 KiB
Python
Raw Normal View History

2017-08-25 22:47:08 +08:00
from datetime import datetime
2017-08-29 19:12:07 +08:00
from flask import request, make_response, jsonify
2017-08-25 22:47:08 +08:00
import flask_restful
from cc.database import mongo
2017-08-29 19:12:07 +08:00
from cc.utils import init_collections, local_ip_addresses
2017-08-25 22:47:08 +08:00
__author__ = 'Barak'
class Root(flask_restful.Resource):
def get(self, action=None):
if not action:
action = request.args.get('action')
2017-08-29 19:12:07 +08:00
2017-08-25 22:47:08 +08:00
if not action:
return jsonify(ip_addresses=local_ip_addresses(), mongo=str(mongo.db))
2017-08-29 19:12:07 +08:00
2017-08-25 22:47:08 +08:00
elif action == "reset":
mongo.db.config.drop()
mongo.db.monkey.drop()
mongo.db.telemetry.drop()
mongo.db.usernames.drop()
mongo.db.passwords.drop()
mongo.db.node.drop()
mongo.db.edge.drop()
init_collections()
2017-08-29 19:12:07 +08:00
return jsonify(status='OK')
2017-08-25 22:47:08 +08:00
elif action == "killall":
mongo.db.monkey.update({}, {'$set': {'config.alive': False, 'modifytime': datetime.now()}}, upsert=False,
multi=True)
2017-08-29 19:12:07 +08:00
return 200
2017-08-25 22:47:08 +08:00
else:
2017-08-29 19:12:07 +08:00
return make_response(400, {'error': 'unknown action'})