#!/bin/bash


ME=$(basename $0);

source //etc/ec2.rc;
source //etc/common-utils.rc;


function validate_paths()
{
    if [ -z "$EC2_CERT" ]; then
        fail "\$EC2_CERT variable not set";
    fi

    if [ -z "$EC2_PRIVATE_KEY" ]; then
        fail "\$EC2_PRIVATE_KEY variable not set";
    fi

    if [ -e $EC2_CERT ]; then
        fail "$EC2_CERT already exists";
    fi

    if [ -e $EC2_PRIVATE_KEY ]; then
        fail "$EC2_PRIVATE_KEY already exists";
    fi

    if [ ! -f $CERT ]; then
        fail "$CERT is not a file";
    fi

    if ! grep -q CERT $CERT; then
        fail "$CERT is not a valid certificate";
    fi

    if [ ! -f $PRIVATE_KEY ]; then
        fail "$PRIVATE_KEY does not exist";
    fi

    if ! grep -q PRIVATE $PRIVATE_KEY; then
        fail "$PRIVATE is not a valid private key";
    fi
}


function set_creds()
{
    echo -n "Verifying cert and key ... ";
    instance_id=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
    if [ -z "$instance_id" ]; then
        echo "failed to get current instance id.";
        exit 1;
    fi

    EC2_CERT=$CERT EC2_PRIVATE_KEY=$PRIVATE_KEY \
	ec2-describe-instance-attribute --region $ec2_placement_region \
	"$instance_id" --instance-type >/dev/null 2>&1;

    if [ $? -ne 0 ]; then
        echo "failed.";
        exit 1;
    fi

    echo "verified.";

    cat $CERT > $EC2_CERT;
    cat $PRIVATE_KEY > $EC2_PRIVATE_KEY;
}


function setup_ssh_keys()
{
    if [ -f ~/.ssh/gluster ]; then
        warn "~/.ssh/gluster already exists";
        return;
    fi

    ssh-keygen -t rsa -N '' -C gluster -f ~/.ssh/gluster;
    cat ~/.ssh/gluster.pub >> ~/.ssh/authorized_keys;
    if [ -f ~/.ssh/id_rsa ]; then
        mv ~/.ssh/id_rsa ~/.ssh/id_rsa.old.$$;
    fi
    cat ~/.ssh/gluster > ~/.ssh/id_rsa;
    chmod 600 ~/.ssh/id_rsa;
}


function tag_myself()
{
    cat <<EOF

Please provide a new DOMAIN name for this gluster deployment. A domain name
is used to tag all EC2 resources (instances, volumes) created and managed
by this deployment. For eg. this instance will get a tag name:

   i-DOMAIN-1

New instances launched with gluster-ami-newinstance will get tag names like:

   i-DOMAIN-2 i-DOMAIN-3 ...

Similarly, volumes created with gluster-provision-storage will be tagged like:

   vol-DOMAIN-1 vol-DOMAIN-2 ...

Typically all instances which are peer probed would belong to the same domain.
The default domain name is "gluster"

EOF

    echo -n "Enter a domain name [gluster]: "
    read DOMAIN;
    if [ -z "$DOMAIN" ]; then
        echo "Defaulting the domain name to gluster";
        DOMAIN=gluster;
    fi

    echo $DOMAIN > //etc/domain;

    //bin/gluster-ami-tagmyself;
}


function show_help()
{
    usage_banner;
    cat <<EOF

Usage:  $ME [-h] <EC2_CERT> <EC2_PRIVATE_KEY>

Prepare this instance to be used as a Gluster Storage Virtual
Appliance server. This script only needs to be run on the first
instance launched in each pool.

EC2_CERT and EC2_PRIVATE_KEY are path to AWS X.509 certificate
file and AWS X.509 private key file.

Miscellaneous:
  -h                        display this help and exit

Example:
  $ME cert-D426D453DDDFF20D426.pem pk-D426D453DDDFF20D426.pem
EOF
}

function display_next()
{
        cat << EOF

================================================================================
Bootstrapping has been successful. You may now use the following commands:

  gluster-ami-newinstance - To add a new instance of Gluster Storage Virtual
                            Appliance server to this cluster. The new instance
                            will be automatically bootstrapped.

  gluster-provision-storage - Create, attach, format and mount EBS volumes. You
                              may create gluster volumes on these mount points
================================================================================

EOF
}

function main()
{
    # Parse command line arguments.
    while getopts :h OPT; do
	case "$OPT" in
	    h)
		show_help
		exit 0
		;;
	    \?)
                # getopts issues an error message
		echo "Invalid option: -$OPTARG"
		show_help
		exit 1
		;;
	esac
    done

    # Remove the switches we parsed above.
    shift `expr $OPTIND - 1`

    # We want only two non-option arguments.
    if [ $# -ne 2 ]; then
	show_help
	exit 1
    fi

    CERT=$1;
    PRIVATE_KEY=$2;

    validate_paths;

    set_creds;

    setup_ssh_keys;

    tag_myself;

    if [ $? -eq 0 ]; then
            display_next;
    fi
}

main "$@";
