示例#1
0
文件: cli.py 项目: alanjds/aws-gate
def main():

    # We want to provide default values in cases they are not configured in ~/.aws/config or availabe as
    # environment variables
    default_region = get_default_region()
    if default_region is None:
        default_region = AWS_DEFAULT_REGION

    # We try to obtain default profile from the environment or use 'default' to save call to boto3.
    # boto3 will also return 'default': https://github.com/boto/boto3/blob/develop/boto3/session.py#L93
    default_profile = os.environ.get('AWS_PROFILE') or 'default'

    args = parse_arguments()

    if not DEBUG:
        sys.excepthook = lambda exc_type, exc_value, traceback: logger.error(
            exc_value)

    log_level = logging.ERROR
    log_format = '%(message)s'

    # We want to silence dependencies
    logging.getLogger('botocore').setLevel(logging.CRITICAL)
    logging.getLogger('boto3').setLevel(logging.CRITICAL)
    logging.getLogger('urllib3').setLevel(logging.CRITICAL)

    if args.verbose:
        log_level = logging.INFO

    if DEBUG:
        log_level = logging.DEBUG
        log_format = '%(asctime)s - %(name)-16s - %(levelname)-5s - %(message)s'

    logging.basicConfig(level=log_level, stream=sys.stderr, format=log_format)

    try:
        config = load_config_from_files()
    except (ValidationError, ScannerError) as e:
        raise ValueError('Invalid configuration provided: {}'.format(
            e.message))

    profile = _get_profile(args=args, config=config, default=default_profile)
    region = _get_region(args=args, config=config, default=default_region)

    #    breakpoint()

    if args.subcommand == 'session':
        session(config=config,
                instance_name=args.instance_name,
                region_name=region,
                profile_name=profile)
    if args.subcommand in ['ls', 'list']:
        list_instances(region_name=args.region, profile_name=args.profile)
示例#2
0
 def _load_config_files(self, config_files):
     config = load_config_from_files(config_files=config_files)
     self.assertIsInstance(config, GateConfig)
     return config
示例#3
0
def main():
    args = parse_arguments()

    if not DEBUG:
        sys.excepthook = lambda exc_type, exc_value, traceback: logger.error(
            exc_value)

    log_level = logging.ERROR
    log_format = "%(message)s"

    # We want to silence dependencies
    logging.getLogger("botocore").setLevel(logging.CRITICAL)
    logging.getLogger("boto3").setLevel(logging.CRITICAL)
    logging.getLogger("urllib3").setLevel(logging.CRITICAL)

    if args.verbose:
        log_level = logging.INFO

    if DEBUG:
        log_level = logging.DEBUG
        log_format = "%(asctime)s - %(name)-28s - %(levelname)-5s - %(message)s"

    logging.basicConfig(level=log_level, stream=sys.stderr, format=log_format)

    try:
        config = load_config_from_files()
    except (ValidationError, ScannerError) as e:
        raise ValueError("Invalid configuration provided: {}".format(e))

    # We want to provide default values in cases they are not configured
    # in ~/.aws/config or availabe a environment variables
    default_region = get_default_region()
    if default_region is None:
        default_region = AWS_DEFAULT_REGION

    # We try to obtain default profile from the environment or use 'default' to
    # save a call to boto3. In the environment, we check if we are being called
    # from aws-vault first or not. Then we return 'default' as boto3 will
    # https://github.com/boto/boto3/blob/develop/boto3/session.py#L93
    if "AWS_VAULT" in os.environ:
        logger.debug(
            "aws-vault usage detected, defaulting to the AWS profile from $AWS_VAULT"
        )

    default_profile = (os.environ.get("AWS_VAULT")
                       or os.environ.get("AWS_PROFILE") or AWS_DEFAULT_PROFILE)

    profile = _get_profile(args=args, config=config, default=default_profile)
    region = _get_region(args=args, config=config, default=default_region)

    logger.debug('Using AWS profile "%s" in region "%s"', profile, region)

    if args.subcommand == "bootstrap":
        bootstrap(force=args.force)
    if args.subcommand == "session":
        session(
            config=config,
            instance_name=args.instance_name,
            region_name=region,
            profile_name=profile,
        )
    if args.subcommand == "ssh":
        ssh(
            config=config,
            instance_name=args.instance_name,
            region_name=region,
            profile_name=profile,
            user=args.os_user,
            port=args.port,
            key_type=args.key_type,
            key_size=args.key_size,
            command=args.command,
        )
    if args.subcommand == "ssh-config":
        ssh_config(region_name=region,
                   profile_name=profile,
                   user=args.os_user,
                   port=args.port)
    if args.subcommand == "ssh-proxy":
        ssh_proxy(
            config=config,
            instance_name=args.instance_name,
            region_name=region,
            profile_name=profile,
            user=args.os_user,
            port=args.port,
            key_type=args.key_type,
            key_size=args.key_size,
        )
    if args.subcommand in ["ls", "list"]:
        fields = args.output.split(",")
        list_instances(
            region_name=region,
            profile_name=profile,
            output_format=args.format,
            fields=fields,
        )
示例#4
0
def _load_config_files(config_files):
    config = load_config_from_files(config_files=config_files)
    assert isinstance(config, GateConfig)
    return config