#!/usr/bin/env bash VERSION="3.4.0" VERBOSE=false SSLOPTION=false AWSREGION="us-west-2" US_W_1="ami-7be8a103" US_W_2="ami-7be8a103" US_E_1="ami-7be8a103" US_E_2="ami-7be8a103" AWS_CLI=$(which aws) DOCKER_CLI=$(which docker) DOCKER_IMG="hunterlong/statup" AWS_ECS="$AWS_CLI --output json" DOCKER_PORT=8080 function usage() { cat < /dev/null 2>&1 || { echo "Some of the required software is not installed:" echo " please install $1" >&2; exit 4; } } function setAWSPresets { if [ -z ${AWS_DEFAULT_REGION+x} ]; then unset AWS_DEFAULT_REGION else AWS_ECS="$AWS_ECS --region $AWS_DEFAULT_REGION" fi if [ -z ${AWS_PROFILE+x} ]; then unset AWS_PROFILE else AWS_ECS="$AWS_ECS --profile $AWS_PROFILE" fi } function awsAskRegion { if [ -z ${AWS_DEFAULT_REGION+x} ]; then read -p "Enter the AWS Region: " AWSREGION else AWSREGION=$AWS_DEFAULT_REGION fi } function askEC2Name { read -p "Enter the Name for EC2 Instance: " SERVERNAME } function askSSLOption { read -p "Do you want to install a SSL certificate? (y/N):" SSLOPTION } function askSSLDomain { read -p "Enter the Domain to attach the SSL certificate on: " SSLDOMAIN } function askSSLEmail { read -p "Enter the Email for Lets Encrypt: " SSLEMAIL } function askEC2KeyName { read -p "Enter the Keypair for EC2 Instance: " EC2KEYNAME } function askSecurityName { read -p "Enter a name for the new Security Group: " EC2SECGROUP } function awsSecurityGroup { echo "Running task: Creating Security Group"; GROUPID=`$AWS_ECS ec2 create-security-group --group-name "$EC2SECGROUP" --description "Statup HTTP Server on port 80 and 443" | jq -r .GroupId` echo "Created new security group: $GROUPID"; awsAuthSecurityGroup } function awsAuthSecurityGroup { $AWS_ECS ec2 authorize-security-group-ingress --group-id $GROUPID --protocol tcp --port 80 --cidr 0.0.0.0/0 $AWS_ECS ec2 authorize-security-group-ingress --group-id $GROUPID --protocol tcp --port 443 --cidr 0.0.0.0/0 echo "Authorize security group to be open on ports 80 and 443"; } function awsCreateEC2 { NEW_SRV=`$AWS_ECS ec2 run-instances --image-id $US_W_2 --count 1 --instance-type t2.nano --key-name $EC2KEYNAME --security-group-ids $GROUPID` INSTANCE_ID=`echo $NEW_SRV | jq .Instances[0].InstanceId` EC2_STATUS=`echo $NEW_SRV | .Instances[0].StateReason.Message` echo "New EC2 instance created: $INSTANCE_ID with status $EC2_STATUS"; } function ec2TaskComplete { echo "New EC2 instance is ready! $INSTANCE_ID with status $EC2_STATUS"; echo "Instance ID: $INSTANCE_ID with status $EC2_STATUS"; echo "Public DNS: $EC2_DNS"; if [ $SSLOPTION == "y" ]; then echo "Now you have to add a CNAME DNS record on $SSLDOMAIN pointing to $EC2_DNS" fi } function checkEC2Instance { SRV_INFO=`$AWS_ECS ec2 describe-instances --instance-ids $INSTANCE_ID` EC2_STATUS=$(echo "${SRV_INFO}" | jq .Reservations[0].Instances[0].State.Name) EC2_DNS=$(echo "${SRV_INFO}" | jq .Reservations[0].Instances[0].PublicDnsName) EC2_STATUS=$(echo "${SRV_INFO}" | jq .Reservations[0].Instances[0].State.Name) if [ $EC2_STATUS == '"pending"' ]; then echo "EC2 instance is still being created: $INSTANCE_ID"; sleep 3 checkEC2Instance fi } function awsTest { INSTANCE_ID="i-0768e3d5ba00897af" checkEC2Instance ec2TaskComplete } function awsTask { setAWSPresets askEC2Name awsAskRegion askSecurityName askEC2KeyName askSSLOption if [ $SSLOPTION == "y" ]; then askSSLDomain askSSLEmail fi awsSecurityGroup awsCreateEC2 checkEC2Instance ec2TaskComplete } function localTask { echo "installing locally" } function dockerTask { echo "Starting Statup Docker container on port $DOCKER_PORT" $DOCKER_CLI run -d -p $DOCKER_PORT:8080 $DOCKER_IMG } if [ "$BASH_SOURCE" == "$0" ]; then set -o errexit set -o pipefail set -u set -e # If no args are provided, display usage information if [ $# == 0 ]; then usage; fi COMMD=$1 # Loop through arguments, two at a time for key and value while [[ $# -gt 0 ]] do key="$1" case $key in -k|--aws-access-key) AWS_ACCESS_KEY_ID="$2" shift # past argument ;; -s|--aws-secret-key) AWS_SECRET_ACCESS_KEY="$2" shift # past argument ;; -r|--region) AWS_DEFAULT_REGION="$2" shift # past argument ;; -p|--port) DOCKER_PORT="$2" shift # past argument ;; -x|--verbose) VERBOSE=true ;; -v|--version) echo ${VERSION} usage exit 2 exit 0 ;; *) ;; esac shift # past argument or value done if [ $VERBOSE == true ]; then set -x fi case $COMMD in aws) require aws require jq awsTask exit 0 ;; awstest) require aws require jq awsTest exit 0 ;; docker) require docker dockerTask exit 0 ;; docker-compose) require docker-compose dockerComposeTask exit 0 ;; local) localTask shift # past argument ;; *) ;; esac shift # past argument or value fi exit 0 fi