2017-08-25 22:47:08 +08:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from shutil import copyfile
|
|
|
|
|
|
|
|
import sys
|
2017-09-03 05:40:43 +08:00
|
|
|
from flask import request, jsonify, make_response
|
2017-08-25 22:47:08 +08:00
|
|
|
import flask_restful
|
|
|
|
|
|
|
|
from cc.resources.monkey_download import get_monkey_executable
|
2017-09-06 19:49:58 +08:00
|
|
|
from cc.island_config import ISLAND_PORT
|
|
|
|
from cc.services.node import NodeService
|
2017-09-20 00:09:37 +08:00
|
|
|
from cc.utils import local_ip_addresses
|
2017-08-25 22:47:08 +08:00
|
|
|
|
|
|
|
__author__ = 'Barak'
|
|
|
|
|
|
|
|
|
2017-09-20 00:09:37 +08:00
|
|
|
def run_local_monkey():
|
2017-08-25 22:47:08 +08:00
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
import stat
|
|
|
|
|
|
|
|
# get the monkey executable suitable to run on the server
|
|
|
|
result = get_monkey_executable(platform.system().lower(), platform.machine().lower())
|
|
|
|
if not result:
|
2017-09-06 19:49:58 +08:00
|
|
|
return False, "OS Type not found"
|
2017-08-25 22:47:08 +08:00
|
|
|
|
|
|
|
monkey_path = os.path.join('binaries', result['filename'])
|
|
|
|
target_path = os.path.join(os.getcwd(), result['filename'])
|
|
|
|
|
|
|
|
# copy the executable to temp path (don't run the monkey from its current location as it may delete itself)
|
|
|
|
try:
|
|
|
|
copyfile(monkey_path, target_path)
|
|
|
|
os.chmod(target_path, stat.S_IRWXU | stat.S_IRWXG)
|
2017-09-06 19:49:58 +08:00
|
|
|
except Exception as exc:
|
|
|
|
return False, "Copy file failed: %s" % exc
|
2017-08-25 22:47:08 +08:00
|
|
|
|
|
|
|
# run the monkey
|
|
|
|
try:
|
2017-12-24 20:43:29 +08:00
|
|
|
args = ['"%s" m0nk3y -s %s:%s' % (target_path, local_ip_addresses()[0], ISLAND_PORT)]
|
2017-08-25 22:47:08 +08:00
|
|
|
if sys.platform == "win32":
|
|
|
|
args = "".join(args)
|
|
|
|
pid = subprocess.Popen(args, shell=True).pid
|
2017-09-06 19:49:58 +08:00
|
|
|
except Exception as exc:
|
|
|
|
return False, "popen failed: %s" % exc
|
2017-08-25 22:47:08 +08:00
|
|
|
|
2017-09-06 19:49:58 +08:00
|
|
|
return True, "pis: %s" % pid
|
2017-08-25 22:47:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LocalRun(flask_restful.Resource):
|
|
|
|
def get(self):
|
2017-09-20 20:55:02 +08:00
|
|
|
NodeService.update_dead_monkeys()
|
|
|
|
island_monkey = NodeService.get_monkey_island_monkey()
|
|
|
|
if island_monkey is not None:
|
|
|
|
is_monkey_running = not island_monkey["dead"]
|
|
|
|
else:
|
|
|
|
is_monkey_running = False
|
|
|
|
|
|
|
|
return jsonify(is_running=is_monkey_running)
|
2017-08-25 22:47:08 +08:00
|
|
|
|
|
|
|
def post(self):
|
2017-09-03 05:40:43 +08:00
|
|
|
body = json.loads(request.data)
|
2017-09-20 00:09:37 +08:00
|
|
|
if body.get('action') == 'run':
|
|
|
|
local_run = run_local_monkey()
|
2017-10-04 19:07:38 +08:00
|
|
|
return jsonify(is_running=local_run[0], error_text=local_run[1])
|
2017-08-25 22:47:08 +08:00
|
|
|
|
2017-09-03 05:40:43 +08:00
|
|
|
# default action
|
|
|
|
return make_response({'error': 'Invalid action'}, 500)
|