2015-08-30 15:27:35 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
import logging.config
|
2015-09-29 22:58:06 +08:00
|
|
|
from config import WormConfiguration, EXTERNAL_CONFIG_FILE
|
2015-08-30 15:27:35 +08:00
|
|
|
from model import MONKEY_ARG, DROPPER_ARG
|
|
|
|
from dropper import MonkeyDrops
|
|
|
|
from monkey import ChaosMonkey
|
2015-10-14 22:22:05 +08:00
|
|
|
import argparse
|
2015-09-29 22:58:06 +08:00
|
|
|
import json
|
2015-08-30 15:27:35 +08:00
|
|
|
|
2015-11-30 20:11:19 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
2015-08-30 15:27:35 +08:00
|
|
|
__author__ = 'itamar'
|
|
|
|
|
|
|
|
LOG = None
|
|
|
|
|
|
|
|
LOG_CONFIG = {'version': 1,
|
|
|
|
'disable_existing_loggers': False,
|
|
|
|
'formatters': {'standard': {'format': '%(asctime)s [%(process)d:%(levelname)s] %(module)s.%(funcName)s.%(lineno)d: %(message)s'},
|
|
|
|
},
|
|
|
|
'handlers': {'console': {'class': 'logging.StreamHandler',
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'formatter': 'standard'},
|
|
|
|
'file': {'class': 'logging.FileHandler',
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'formatter': 'standard',
|
|
|
|
'filename': None}
|
|
|
|
},
|
|
|
|
'root': {'level': 'DEBUG',
|
|
|
|
'handlers': ['console']},
|
|
|
|
}
|
|
|
|
|
2015-11-26 21:48:47 +08:00
|
|
|
|
2015-08-30 15:27:35 +08:00
|
|
|
def main():
|
|
|
|
global LOG
|
|
|
|
|
|
|
|
if 2 > len(sys.argv):
|
|
|
|
return True
|
|
|
|
|
|
|
|
monkey_mode = sys.argv[1]
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
if not monkey_mode in [MONKEY_ARG, DROPPER_ARG]:
|
2015-11-26 21:48:47 +08:00
|
|
|
return True
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
config_file = EXTERNAL_CONFIG_FILE
|
|
|
|
|
2015-10-14 22:22:05 +08:00
|
|
|
arg_parser = argparse.ArgumentParser()
|
|
|
|
arg_parser.add_argument('-c', '--config')
|
|
|
|
opts, monkey_args = arg_parser.parse_known_args(sys.argv[2:])
|
|
|
|
if opts.config:
|
2015-11-26 21:48:47 +08:00
|
|
|
config_file = opts.config
|
2015-09-29 22:58:06 +08:00
|
|
|
if os.path.isfile(config_file):
|
|
|
|
# using print because config can also change log locations
|
|
|
|
print "Loading config from %s." % config_file
|
|
|
|
try:
|
2015-11-26 21:48:47 +08:00
|
|
|
with open(config_file) as config_fo:
|
|
|
|
json_dict = json.load(config_fo)
|
|
|
|
WormConfiguration.from_dict(json_dict)
|
|
|
|
except ValueError as e:
|
2016-01-13 16:27:49 +08:00
|
|
|
print "Error loading config: %s, using default" % (e,)
|
2015-12-03 22:21:16 +08:00
|
|
|
else:
|
2016-01-13 16:27:49 +08:00
|
|
|
print("Config file wasn't supplied and default path: %s wasn't found, using internal default" % (config_file,))
|
2015-12-08 15:58:06 +08:00
|
|
|
|
|
|
|
print "Loaded Configuration: %r" % WormConfiguration.as_dict()
|
2015-08-30 15:27:35 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
if MONKEY_ARG == monkey_mode:
|
|
|
|
log_path = WormConfiguration.monkey_log_path
|
|
|
|
monkey_cls = ChaosMonkey
|
|
|
|
elif DROPPER_ARG == monkey_mode:
|
|
|
|
log_path = WormConfiguration.dropper_log_path
|
|
|
|
monkey_cls = MonkeyDrops
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if WormConfiguration.use_file_logging:
|
|
|
|
LOG_CONFIG['handlers']['file']['filename'] = log_path
|
|
|
|
LOG_CONFIG['root']['handlers'].append('file')
|
2015-10-15 15:13:40 +08:00
|
|
|
else:
|
|
|
|
del LOG_CONFIG['handlers']['file']
|
2015-08-30 15:27:35 +08:00
|
|
|
|
|
|
|
logging.config.dictConfig(LOG_CONFIG)
|
|
|
|
LOG = logging.getLogger()
|
|
|
|
|
|
|
|
def log_uncaught_exceptions(ex_cls, ex, tb):
|
|
|
|
LOG.critical(''.join(traceback.format_tb(tb)))
|
|
|
|
LOG.critical('{0}: {1}'.format(ex_cls, ex))
|
|
|
|
|
|
|
|
sys.excepthook = log_uncaught_exceptions
|
|
|
|
|
|
|
|
LOG.info(">>>>>>>>>> Initializing monkey (%s): PID %s <<<<<<<<<<",
|
|
|
|
monkey_cls.__name__, os.getpid())
|
|
|
|
|
|
|
|
monkey = monkey_cls(monkey_args)
|
|
|
|
monkey.initialize()
|
|
|
|
|
|
|
|
try:
|
|
|
|
monkey.start()
|
2015-11-26 21:48:47 +08:00
|
|
|
|
2015-09-29 22:58:06 +08:00
|
|
|
if WormConfiguration.serialize_config:
|
|
|
|
with open(config_file, 'w') as config_fo:
|
|
|
|
json_dict = WormConfiguration.as_dict()
|
2015-10-08 18:39:52 +08:00
|
|
|
json.dump(json_dict, config_fo, skipkeys=True, sort_keys=True, indent=4, separators=(',', ': '))
|
2015-09-29 22:58:06 +08:00
|
|
|
|
2015-08-30 15:27:35 +08:00
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
monkey.cleanup()
|
|
|
|
|
|
|
|
if "__main__" == __name__:
|
|
|
|
if not main():
|
2015-11-26 21:48:47 +08:00
|
|
|
sys.exit(1)
|