import argparse import logging import os import stat import sys from utils import utils logging.basicConfig(level=logging.INFO, format='%(threadName)s %(asctime)s - %(name)s - %(levelname)s - %(message)s') LOGGER = logging.getLogger(__file__) LOGGER.setLevel(logging.INFO) PEMS = [ "virginia-key-benchmark.pem", "ohio-key-benchmark.pem", "california-key-benchmark.pem", "oregon-key-benchmark.pem", "tokyo-key-benchmark.pem", "singapore-key-benchmark.pem", "frankfurt-key-benchmark.pem", "ireland-key-benchmark.pem", ] if __name__ == "__main__": parser = argparse.ArgumentParser(description='This script helps you to genereate distribution config') parser.add_argument('--distribution_config', type=str, dest='distribution_config', default='distribution_config.txt') parser.add_argument('--commander_logging', type=str, dest='commander_logging', default='commander_logging.sh') parser.add_argument('--commander_info', type=str, dest='commander_info', default='commander_info.txt') args = parser.parse_args() if not os.path.exists(args.distribution_config): sys.exit(1) with open(args.distribution_config, "r") as fin: lines = fin.readlines() commander_address = None commander_region = None with open(args.distribution_config, "w") as fout, open(args.commander_info, "w") as fout2: for line in lines: if "commander" in line: items = [item.strip() for item in line.split(" ")] commander_address = items[0] commander_region = int(items[4][0]) fout2.write("\n".join(items)) else: fout.write(line.strip() + "\n") if not commander_address or not commander_region: LOGGER.info("Failed to extract commander address and commander region.") sys.exit(1) with open(args.commander_logging, "w") as fout: fout.write("scp -i ../keys/%s %s ec2-user@%s:~/projects/src/harmony-benchmark/bin/distribution_config.txt\n" % (PEMS[commander_region - 1], args.distribution_config, commander_address)) fout.write("scp -i ../keys/%s %s ec2-user@%s:~/projects/src/harmony-benchmark/bin/commander_info.txt\n" % (PEMS[commander_region - 1], args.commander_info, commander_address)) fout.write("if [ $? -eq 0 ]; then\n\t") fout.write("ssh -i ../keys/%s ec2-user@%s\n" % (PEMS[commander_region - 1], commander_address)) fout.write("else\n\techo \"Failed to send %s to the commander machine\"\nfi\n" % args.distribution_config) st = os.stat(args.commander_logging) os.chmod(args.commander_logging, st.st_mode | stat.S_IEXEC) LOGGER.info("DONE.")