# Path: pytorchrl/config.py
# PROJECT_PATH = osp.abspath(osp.join(osp.dirname(__file__), '..'))
# LOG_DIR = PROJECT_PATH + "/data"
# USE_TF = False
# DOCKER_IMAGE = "DOCKER_IMAGE"
# DOCKERFILE_PATH = "/path/to/Dockerfile"
# KUBE_PREFIX = "rllab_"
# DOCKER_LOG_DIR = "/tmp/expt"
# POD_DIR = PROJECT_PATH + "/.pods"
# AWS_S3_PATH = None
# AWS_IMAGE_ID = None
# AWS_INSTANCE_TYPE = "m4.xlarge"
# AWS_KEY_NAME = "AWS_KEY_NAME"
# AWS_SPOT = True
# AWS_SPOT_PRICE = '1.0'
# AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY", None)
# AWS_ACCESS_SECRET = os.environ.get("AWS_ACCESS_SECRET", None)
# AWS_IAM_INSTANCE_PROFILE_NAME = "pytorchrl"
# AWS_SECURITY_GROUPS = ["pytorchrl"]
# AWS_SECURITY_GROUP_IDS = []
# AWS_NETWORK_INTERFACES = []
# AWS_EXTRA_CONFIGS = dict()
# AWS_REGION_NAME = "us-east-1"
# CODE_SYNC_IGNORES = ["*.git/*", "*data/*", "*.pod/*"]
# DOCKER_CODE_DIR = "/root/code/pytorchrl"
# AWS_CODE_SYNC_S3_PATH = "s3://to/be/overriden/in/personal"
# FAST_CODE_SYNC = True
# FAST_CODE_SYNC_IGNORES = [".git", "data", ".pods"]
# KUBE_DEFAULT_RESOURCES = {
#     "requests": {
#         "cpu": 0.8,
#     }
# }
# KUBE_DEFAULT_NODE_SELECTOR = {
#     "aws/type": "m4.xlarge",
# }
# MUJOCO_KEY_PATH = osp.expanduser("~/.mujoco")
# ENV = {}
# EBS_OPTIMIZED = True
# LABEL = ""


import boto3
import re
import sys
import json
import botocore
import os
from rllab.misc import console
from string import Template
from pytorchrl import config
                if not query_yes_no('Key pair with name %s exists. Proceed to delete and recreate?' % key_name, 'no'):
                    sys.exit()
                print('Deleting existing key pair with name %s' % key_name)
                ec2_client.delete_key_pair(KeyName=key_name)
                print('Recreating key pair with name %s' % key_name)
                key_pair = ec2_client.create_key_pair(KeyName=key_name)
            else:
                raise e

        key_pair_folder_path = os.path.join(config.PROJECT_PATH, 'private', 'key_pairs')
        file_name = os.path.join(key_pair_folder_path, '%s.pem' % key_name)

        print('Saving keypair file')
        console.mkdir_p(key_pair_folder_path)
        with os.fdopen(os.open(file_name, os.O_WRONLY | os.O_CREAT, 0o600), 'w') as handle:
            handle.write(key_pair['KeyMaterial'] + '\n')

        # adding pem file to ssh
        os.system("ssh-add %s" % file_name)

        ALL_REGION_AWS_KEY_NAMES[region] = key_name


def write_config():
    print("Writing config file...")
    content = CONFIG_TEMPLATE.substitute(
        all_region_aws_key_names=json.dumps(ALL_REGION_AWS_KEY_NAMES, indent=4),
        all_region_aws_security_group_ids=json.dumps(ALL_REGION_AWS_SECURITY_GROUP_IDS, indent=4),
        s3_bucket_name=S3_BUCKET_NAME,
    )

# Path: pytorchrl/config.py
# PROJECT_PATH = osp.abspath(osp.join(osp.dirname(__file__), '..'))
# LOG_DIR = PROJECT_PATH + "/data"
# USE_TF = False
# DOCKER_IMAGE = "DOCKER_IMAGE"
# DOCKERFILE_PATH = "/path/to/Dockerfile"
# KUBE_PREFIX = "rllab_"
# DOCKER_LOG_DIR = "/tmp/expt"
# POD_DIR = PROJECT_PATH + "/.pods"
# AWS_S3_PATH = None
# AWS_IMAGE_ID = None
# AWS_INSTANCE_TYPE = "m4.xlarge"
# AWS_KEY_NAME = "AWS_KEY_NAME"
# AWS_SPOT = True
# AWS_SPOT_PRICE = '1.0'
# AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY", None)
# AWS_ACCESS_SECRET = os.environ.get("AWS_ACCESS_SECRET", None)
# AWS_IAM_INSTANCE_PROFILE_NAME = "pytorchrl"
# AWS_SECURITY_GROUPS = ["pytorchrl"]
# AWS_SECURITY_GROUP_IDS = []
# AWS_NETWORK_INTERFACES = []
# AWS_EXTRA_CONFIGS = dict()
# AWS_REGION_NAME = "us-east-1"
# CODE_SYNC_IGNORES = ["*.git/*", "*data/*", "*.pod/*"]
# DOCKER_CODE_DIR = "/root/code/pytorchrl"
# AWS_CODE_SYNC_S3_PATH = "s3://to/be/overriden/in/personal"
# FAST_CODE_SYNC = True
# FAST_CODE_SYNC_IGNORES = [".git", "data", ".pods"]
# KUBE_DEFAULT_RESOURCES = {
#     "requests": {
#         "cpu": 0.8,
#     }
# }
# KUBE_DEFAULT_NODE_SELECTOR = {
#     "aws/type": "m4.xlarge",
# }
# MUJOCO_KEY_PATH = osp.expanduser("~/.mujoco")
# ENV = {}
# EBS_OPTIMIZED = True
# LABEL = ""


# Path: scripts/setup_ec2.py
import boto3
import re
import sys
import json
import botocore
import os
from rllab.misc import console
from string import Template
from pytorchrl import config

                if not query_yes_no('Key pair with name %s exists. Proceed to delete and recreate?' % key_name, 'no'):
                    sys.exit()
                print('Deleting existing key pair with name %s' % key_name)
                ec2_client.delete_key_pair(KeyName=key_name)
                print('Recreating key pair with name %s' % key_name)
                key_pair = ec2_client.create_key_pair(KeyName=key_name)
            else:
                raise e

        key_pair_folder_path = os.path.join(config.PROJECT_PATH, 'private', 'key_pairs')
        file_name = os.path.join(key_pair_folder_path, '%s.pem' % key_name)

        print('Saving keypair file')
        console.mkdir_p(key_pair_folder_path)
        with os.fdopen(os.open(file_name, os.O_WRONLY | os.O_CREAT, 0o600), 'w') as handle:
            handle.write(key_pair['KeyMaterial'] + '\n')

        # adding pem file to ssh
        os.system("ssh-add %s" % file_name)

        ALL_REGION_AWS_KEY_NAMES[region] = key_name


def write_config():
    print("Writing config file...")
    content = CONFIG_TEMPLATE.substitute(
        all_region_aws_key_names=json.dumps(ALL_REGION_AWS_KEY_NAMES, indent=4),
        all_region_aws_security_group_ids=json.dumps(ALL_REGION_AWS_SECURITY_GROUP_IDS, indent=4),
        s3_bucket_name=S3_BUCKET_NAME,
    )

    config_personal_file = os.path.join(config.PROJECT_PATH, 'pytorchrl/config_personal.py')