<commit_before># LAST UPDATED: NOV 6, 2015
# Revised on OCT 25, 13:12:11 PM
# <NAME>
# UPDATED INSTRUCTIONS UPON LAUNCHING NOV 4th, 2015
# 10:09:35
# 7 args to be put in the sequence below:

# $1 image-ids
# $2 count of instances to launch
# $3 instance-type
# $4 security-group-ids
# $5 subnet-id
# $6 key-name
# $7 iam-instance-profile

# 1. execute the cleanup.sh
./cleanup.sh

# 2. declare an array in bash

declare -a instanceARR

# 3. mapfile (updated Nov 4, 2015)
mapfile -t instanceARR < <(aws ec2 run-instances --image-id $1 --count $2 --instance-type $3 --security-group-ids $4 --subnet-id $5 --key-name $6  --associate-public-ip-address --iam-instance-profile Name=$7 --user-data file://../Environment-setup/install-webserver.sh --output table | grep InstanceId | sed "s/|//g" | tr -d ' ' | sed "s/InstanceId//g" --debug)

echo ${instanceARR[@]}

# 4. wait cli 
aws ec2 wait instance-running --instance-ids ${instanceARR[@]}

echo "instances are running."

# 5. create ELB (updated 11/02/2015, Nov 4, 2015. dif *** ???security-group representations for ec2 and elb?)
ELBURL=(`aws elb create-load-balancer --load-balancer-name SIMMON-THE-CAT --listeners Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80 --security-groups $4 --subnets $5 --output=text`) 
echo $ELBURL

#sleep
echo -e "\nELB created, sleep 7 seconds"
for i in {0..7}; do echo -ne '.'; sleep 1; done
echo -e "\n"

# 6. register instances with ELB (updated 11/02/2015)
aws elb register-instances-with-load-balancer --load-balancer-name SIMMON-THE-CAT --instances ${instanceARR[@]}

# 7. ELB health-check configuration (updated Nov 4, 2015)
aws elb configure-health-check --load-balancer-name SIMMON-THE-CAT --health-check Target=HTTP:80/index.html,Interval=30,UnhealthyThreshold=2,HealthyThreshold=2,Timeout=3

# wait additional 1 min
#echo -e "\nOpening the ELB in 1 minute in web browser"
#for i in {0..60}; do echo -ne '.'; sleep 1; done


# 8. launch configuration (updated Nov 4, 2015)
echo -e "\nCreating launch configuration: SIMMON-CONFIG-LAUNCH"
aws autoscaling create-launch-configuration --launch-configuration-name SIMMON-CONFIG-LAUNCH --image-id $1 --instance-type $3 --security-groups $4 --key-name $6 --user-data file://../Environment-setup/install-webserver.sh --iam-instance-profile $7 --debug

# 9. auto-scaling (updated Nov 4, 2015)
echo -e "\nCreating the auto scaling group"
aws autoscaling create-auto-scaling-group --auto-scaling-group-name SIMMON-AUTO-SCALE --launch-configuration-name SIMMON-CONFIG-LAUNCH --load-balancer-name SIMMON-THE-CAT --health-check-type ELB --min-size 1 --max-size 3 --desired-capacity 2 --default-cooldown 600 --health-check-grace-period 120 --vpc-zone-identifier $5

# secure the count of total instances
# ref: http://docs.aws.amazon.com/AutoScaling/latest/APIReference/API_PutScalingPolicy.html
aws autoscaling put-scaling-policy --auto-scaling-group-name SIMMON-AUTO-SCALE --policy-name SIMMON-SCALE-POLICY --scaling-adjustment 1 --adjustment-type ExactCapacity


# 10. rds instance 
echo -e "\nCreating database"
mapfile -t dbInstanceARR < <(aws rds describe-db-instances --output json | grep "\"DBInstanceIdentifier" | sed "s/[\"\:\, ]//g" | sed "s/DBInstanceIdentifier//g")

#if [ ${#dbInstanceARR[@]} -gt 0 ]
#   then 
#echo ${#dbInstanceARR[@]}

LENGTH=${#dbInstanceARR[@]}
#echo $LENGTH	

for (( i=0; i<=${LENGTH}; i++));
do
   	if [[ ${dbInstanceARR[i]} == "SIMMON-THE-CAT-DB" ]]
    then 
    echo "SIMMON-THE-CAT-DB exists"
    else
    	# create the database instance, aws rds
    	aws rds create-db-instance --db-instance-identifier SIMMON-THE-CAT-DB --db-instance-class db.t1.micro --engine MySQL --master-username LN1878 --master-user-password <PASSWORD> --allocated-storage 5
    fi  
done

#wait additional 3 min for creating the database
echo -e "\nPlease wait 3 min, creating database : SIMMON-THE-CAT"
for i in {0..180}; do echo -ne '. '; sleep 1; done



# 11 cloudwatch metrics (updatd Nov 6, 2015)
# ref: http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/UsingAlarmActions.html#UsingCLIorAPI
aws cloudwatch put-metric-alarm --alarm-name SIMMON-ALARM --alarm-description "Terminate the instance when it is idle for a day" --namespace "AWS/EC2" --dimensions Name=InstanceId,Value="i-abc123" --statistic Average  --metric-name CPUUtilization --comparison-operator LessThanThreshold --threshold 1 --period 86400 --evaluation-periods 4 -- alarm-actions arn:aws:automate:us-east-1:ec2:terminate
<commit_msg>comment out cloudwatch metrics for testing<commit_after># LAST UPDATED: NOV 6, 2015
# Revised on OCT 25, 13:12:11 PM
# <NAME>
# UPDATED INSTRUCTIONS UPON LAUNCHING NOV 4th, 2015
# 10:09:35
# 7 args to be put in the sequence below:

# $1 image-ids
# $2 count of instances to launch
# $3 instance-type
# $4 security-group-ids
# $5 subnet-id
# $6 key-name
# $7 iam-instance-profile

# 1. execute the cleanup.sh
./cleanup.sh

# 2. declare an array in bash

declare -a instanceARR

# 3. mapfile (updated Nov 4, 2015)
mapfile -t instanceARR < <(aws ec2 run-instances --image-id $1 --count $2 --instance-type $3 --security-group-ids $4 --subnet-id $5 --key-name $6  --associate-public-ip-address --iam-instance-profile Name=$7 --user-data file://../Environment-setup/install-webserver.sh --output table | grep InstanceId | sed "s/|//g" | tr -d ' ' | sed "s/InstanceId//g" --debug)

echo ${instanceARR[@]}

# 4. wait cli 
aws ec2 wait instance-running --instance-ids ${instanceARR[@]}

echo "instances are running."

# 5. create ELB (updated 11/02/2015, Nov 4, 2015. dif *** ???security-group representations for ec2 and elb?)
ELBURL=(`aws elb create-load-balancer --load-balancer-name SIMMON-THE-CAT --listeners Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80 --security-groups $4 --subnets $5 --output=text`) 
echo $ELBURL

#sleep
echo -e "\nELB created, sleep 7 seconds"
for i in {0..7}; do echo -ne '.'; sleep 1; done
echo -e "\n"

# 6. register instances with ELB (updated 11/02/2015)
aws elb register-instances-with-load-balancer --load-balancer-name SIMMON-THE-CAT --instances ${instanceARR[@]}

# 7. ELB health-check configuration (updated Nov 4, 2015)
aws elb configure-health-check --load-balancer-name SIMMON-THE-CAT --health-check Target=HTTP:80/index.html,Interval=30,UnhealthyThreshold=2,HealthyThreshold=2,Timeout=3

# wait additional 1 min
#echo -e "\nOpening the ELB in 1 minute in web browser"
#for i in {0..60}; do echo -ne '.'; sleep 1; done


# 8. launch configuration (updated Nov 4, 2015)
echo -e "\nCreating launch configuration: SIMMON-CONFIG-LAUNCH"
aws autoscaling create-launch-configuration --launch-configuration-name SIMMON-CONFIG-LAUNCH --image-id $1 --instance-type $3 --security-groups $4 --key-name $6 --user-data file://../Environment-setup/install-webserver.sh --iam-instance-profile $7 --debug

# 9. auto-scaling (updated Nov 4, 2015)
echo -e "\nCreating the auto scaling group"
aws autoscaling create-auto-scaling-group --auto-scaling-group-name SIMMON-AUTO-SCALE --launch-configuration-name SIMMON-CONFIG-LAUNCH --load-balancer-name SIMMON-THE-CAT --health-check-type ELB --min-size 1 --max-size 3 --desired-capacity 2 --default-cooldown 600 --health-check-grace-period 120 --vpc-zone-identifier $5

# secure the count of total instances
# ref: http://docs.aws.amazon.com/AutoScaling/latest/APIReference/API_PutScalingPolicy.html
aws autoscaling put-scaling-policy --auto-scaling-group-name SIMMON-AUTO-SCALE --policy-name SIMMON-SCALE-POLICY --scaling-adjustment 1 --adjustment-type ExactCapacity


# 10. rds instance 
echo -e "\nCreating database"
mapfile -t dbInstanceARR < <(aws rds describe-db-instances --output json | grep "\"DBInstanceIdentifier" | sed "s/[\"\:\, ]//g" | sed "s/DBInstanceIdentifier//g")

#if [ ${#dbInstanceARR[@]} -gt 0 ]
#   then 
#echo ${#dbInstanceARR[@]}

LENGTH=${#dbInstanceARR[@]}
#echo $LENGTH	

for (( i=0; i<=${LENGTH}; i++));
do
   	if [[ ${dbInstanceARR[i]} == "SIMMON-THE-CAT-DB" ]]
    then 
    echo "SIMMON-THE-CAT-DB exists"
    else
    	# create the database instance, aws rds
    	aws rds create-db-instance --db-instance-identifier SIMMON-THE-CAT-DB --db-instance-class db.t1.micro --engine MySQL --master-username LN1878 --master-user-password <PASSWORD> --allocated-storage 5
    fi  
done

#wait additional 3 min for creating the database
echo -e "\nPlease wait 3 min, creating database : SIMMON-THE-CAT"
for i in {0..180}; do echo -ne '. '; sleep 1; done



# 11 cloudwatch metrics (updatd Nov 6, 2015)
# ref: http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/UsingAlarmActions.html#UsingCLIorAPI
#aws cloudwatch put-metric-alarm --alarm-name SIMMON-ALARM --alarm-description "Terminate the instance when it is idle for a day" --namespace "AWS/EC2" --dimensions Name=InstanceId,Value="i-abc123" --statistic Average  --metric-name CPUUtilization --comparison-operator LessThanThreshold --threshold 1 --period 86400 --evaluation-periods 4 -- alarm-actions arn:aws:automate:us-east-1:ec2:terminate

{'fraction_non_alphanumeric': 0.14820159857904086, 'fraction_numerical': 0.037300177619893425, 'mean_word_length': 3.8022388059701493, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3647489', 'n_tokens_mistral': 3362, 'n_tokens_neox': 3007, 'n_words': 895}