#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.

import argparse
import sys
import cloud_ssh_config.ssh_config.ssh_config as ssh_config

# Read arguments
parser = argparse.ArgumentParser(
                    description='Get host names and ip adresses from a cloud')
parser.add_argument("cloud",
                    help="Supported clouds: aws")
parser.add_argument('-U', '--ssh_user',
                    help='SSH user')
parser.add_argument('-P', '--prefix', default='',
                    help='Prefix for hosts')
parser.add_argument('-K', '--ssh_key', default='~/.ssh/id_rsa',
                    help='Prefix for hosts')

args = parser.parse_args()

hosts = None
if args.cloud == 'aws':
    from cloud_ssh_config.cloud import aws
    cloud = aws.cloud()
    hosts = cloud.get_hosts()

if hosts:
    if not args.ssh_user:
        if args.cloud == 'aws':
            user = 'ubuntu'
        elif args.cloud == 'digitalocean' or args.cloud == 'scaleway':
            user = 'root'
    else:
        user = args.ssh_user

    ssh = ssh_config.config(user=user, prefix=args.prefix, key=args.ssh_key)
    ssh.get_config(hosts)
else:
    parser.print_help(sys.stderr)
    sys.exit(1)
