monkey/monkey_island/cc/resources/root.py

42 lines
1.4 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
from cc.services.config import ConfigService
from cc.services.node import NodeService
2017-08-25 22:47:08 +08:00
from cc.utils import 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:
2017-09-18 19:18:57 +08:00
return jsonify(ip_addresses=local_ip_addresses(), mongo=str(mongo.db), completed_steps=self.get_completed_steps())
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.node.drop()
mongo.db.edge.drop()
ConfigService.init_config()
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({'dead': False}, {'$set': {'config.alive': False, 'modifytime': datetime.now()}}, upsert=False,
2017-08-25 22:47:08 +08:00
multi=True)
return jsonify(status='OK')
2017-08-25 22:47:08 +08:00
else:
2017-08-29 19:12:07 +08:00
return make_response(400, {'error': 'unknown action'})
2017-09-18 19:18:57 +08:00
def get_completed_steps(self):
is_any_exists = NodeService.is_any_monkey_exists()
is_any_alive = NodeService.is_any_monkey_alive()
return dict(run_server=True, run_monkey=is_any_exists, infection_done=(is_any_exists and not is_any_alive))