def install_keys(key_name, user_name):

    ips = get_ips()

    print("Are you sure you would like to copy public key '{0}' to vms: {1}".format(
        key_name, ips
    ))

    validate = raw_input("Enter 'y' to continue or 'n' to exit: ")
    if validate != "y":
        print("Exiting...")
        sys.exit(1)

    print("Using ssh-copy-id...")
    for ip in ips:
        try:
            subprocess.check_output([
                "ssh-copy-id", "-i", "{0}/.ssh/{1}".format(os.environ["HOME"], key_name),
                "{0}@{1}".format(user_name, ip)
            ])
        except CalledProcessError as e:
            print("ssh-copy-id failed: {} with error: {}".format(key_name, e))
            print("Make sure '{}' is in ~/.ssh/".format(key_name))
            sys.exit(1)

    split_key = key_name.split(".")
    private_key = split_key[0]

    print("Add '{0}' to your ssh agent?".format(private_key))
    validate = raw_input("Enter 'y' to continue or 'n' to exit: ")
    if validate != "y":
        print("Exiting...")
        sys.exit(1)
    else:
        subprocess.call((["ssh-add", "{0}/.ssh/{1}".format(os.environ["HOME"], private_key)]))
def install_gh_deploy_keys(key_path, user_name):

    ips = get_ips()

    for ip in ips:

        if not os.path.exists(key_path):
            raise Exception("Cannot find key: {}".format(key_path))

        # Copy the key over to the /tmp directory.  Cannot copy directly to destination
        # (/root/.ssh/id_rsa) since it's not possible to use scp in conjunction w/ sudo.
        # Example: scp ~/tmp/cbmobile_private_repo_read_only vagrant@$TARGET:/tmp
        subprocess.check_output([
            "scp",
            "{}".format(key_path),
            "{}@{}:/tmp".format(user_name, ip)
        ])

        # Create the /root/.ssh directory if it doesn't already exist
        # The -tt flag is needed to avoid "sudo sorry, you must have a tty to run sudo" errors
        # See http://stackoverflow.com/questions/7114990/pseudo-terminal-will-not-be-allocated-because-stdin-is-not-a-terminal
        # Example: ssh vagrant@$TARGET "sudo mkdir /root/.ssh"
        subprocess.check_output([
            "ssh", "-tt", "{}@{}".format(
                user_name,
                ip,
            ), "sudo mkdir -p /root/.ssh"
        ])

        # Copy from the /tmp directory to the destination (/root/.ssh/id_rsa)
        # Example: ssh vagrant@$TARGET "sudo cp /tmp/cbmobile_private_repo_read_only /root/.ssh/id_rsa"
        key_filename = os.path.basename(key_path)
        subprocess.check_output([
            "ssh", "-tt", "{}@{}".format(
                user_name,
                ip,
            ), "sudo cp /tmp/{} /root/.ssh/id_rsa".format(
                key_filename,
            )
        ])

        # Chmod the id_rsa key to avoid the permissions are too open error
        # Example: ssh vagrant@$TARGET "sudo chmod /root/.ssh/id_rsa"
        subprocess.check_output([
            "ssh", "-tt", "{}@{}".format(
                user_name,
                ip,
            ), "sudo chmod 400 /root/.ssh/id_rsa"
        ])

        # Add the github.com public key to /root/.ssh/known_hosts
        # Example: ssh vagrant@$TARGET "sudo ssh-keyscan -t rsa github.com | sudo tee /root/.ssh/known_hosts"
        subprocess.check_output([
            "ssh", "-tt", "{}@{}".format(
                user_name,
                ip,
            ), "sudo ssh-keyscan -t rsa github.com | sudo tee /root/.ssh/known_hosts"
        ])