Commit 83b0744a by Feanil Patel

Add back newlines.

We need them because we are sometimes joining variables with existing lines.
parent fb29a2d4
...@@ -1709,128 +1709,128 @@ ...@@ -1709,128 +1709,128 @@
"PropagateAtLaunch":true "PropagateAtLaunch":true
} }
], ],
"UserData": { "Fn::Base64" : { "Fn::Join" : ["\n", [ "UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -v", "#!/bin/bash -v\n",
"mkdir -p /opt/edx/bin", "mkdir -p /opt/edx/bin\n",
"cd /opt", "cd /opt\n",
"apt-get update", "apt-get update\n",
"apt-get install openjdk-6-jre-headless unzip -y", "apt-get install openjdk-6-jre-headless unzip -y\n",
"wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip", "wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip\n",
"unzip ec2-api-tools.zip", "unzip ec2-api-tools.zip\n",
"rm ec2-api-tools.zip", "rm ec2-api-tools.zip\n",
"ln -sf ec2-api-tools-* ec2-api-tools", "ln -sf ec2-api-tools-* ec2-api-tools\n",
"cat <<'EOF' > /opt/edx/bin/nat_monitor.sh", "cat <<'EOF' > /opt/edx/bin/nat_monitor.sh\n",
"#!/bin/sh", "#!/bin/sh\n",
"# This script will monitor another NAT instance and take over its routes", "# This script will monitor another NAT instance and take over its routes\n",
"# if communication with the other instance fails", "# if communication with the other instance fails\n",
"", "\n",
"# NAT instance variables", "# NAT instance variables\n",
"# Other instance's IP to ping and route to grab if other node goes down", "# Other instance's IP to ping and route to grab if other node goes down\n",
"PRIMARY_NAT_ID=", { "Ref":"NATDevice" }, "", "PRIMARY_NAT_ID=", { "Ref":"NATDevice" }, "\n",
"BACKUP_NAT_ID=", { "Ref": "BackupNATDevice" }, "", "BACKUP_NAT_ID=", { "Ref": "BackupNATDevice" }, "\n",
"NAT_RT_ID=", { "Ref": "PrivateRouteTable" }, "", "NAT_RT_ID=", { "Ref": "PrivateRouteTable" }, "\n",
"", "\n",
"# Specify the EC2 region that this will be running in (e.g. https://ec2.us-east-1.amazonaws.com)", "# Specify the EC2 region that this will be running in (e.g. https://ec2.us-east-1.amazonaws.com)\n",
"EC2_URL=https://ec2.",{ "Ref": "AWS::Region" },".amazonaws.com", "EC2_URL=https://ec2.",{ "Ref": "AWS::Region" },".amazonaws.com\n",
"", "\n",
"# Health Check variables", "# Health Check variables\n",
"Num_Pings=3", "Num_Pings=3\n",
"Ping_Timeout=1", "Ping_Timeout=1\n",
"Wait_Between_Pings=2", "Wait_Between_Pings=2\n",
"Wait_for_Instance_Stop=60", "Wait_for_Instance_Stop=60\n",
"Wait_for_Instance_Start=300", "Wait_for_Instance_Start=300\n",
"", "\n",
"# leverage AWS security credentials provided by EC2 roles", "# leverage AWS security credentials provided by EC2 roles\n",
"# Setup environment for ec2 api tools", "# Setup environment for ec2 api tools\n",
"export EC2_HOME=/opt/ec2-api-tools", "export EC2_HOME=/opt/ec2-api-tools\n",
"export AWS_IAM_HOME=/opt/IAMCli", "export AWS_IAM_HOME=/opt/IAMCli\n",
"export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64", "export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64\n",
"PATH=/opt/ec2-api-tools/bin:$PATH", "PATH=/opt/ec2-api-tools/bin:$PATH\n",
"", "\n",
"# Determine the NAT instance private IP so we can ping the other NAT instance, take over", "# Determine the NAT instance private IP so we can ping the other NAT instance, take over\n",
"# its route, and reboot it. Requires EC2 DescribeInstances, ReplaceRoute, and Start/RebootInstances", "# its route, and reboot it. Requires EC2 DescribeInstances, ReplaceRoute, and Start/RebootInstances\n",
"# permissions. The following example EC2 Roles policy will authorize these commands:", "# permissions. The following example EC2 Roles policy will authorize these commands:\n",
"# {", "# {\n",
"# \"Statement\": [", "# \"Statement\": [\n",
"# {", "# {\n",
"# \"Action\": [", "# \"Action\": [\n",
"# \"ec2:DescribeInstances\",", "# \"ec2:DescribeInstances\",\n",
"# \"ec2:CreateRoute\",", "# \"ec2:CreateRoute\",\n",
"# \"ec2:ReplaceRoute\",", "# \"ec2:ReplaceRoute\",\n",
"# \"ec2:StartInstances\",", "# \"ec2:StartInstances\",\n",
"# \"ec2:StopInstances\"", "# \"ec2:StopInstances\"\n",
"# ],", "# ],\n",
"# \"Effect\": \"Allow\",", "# \"Effect\": \"Allow\",\n",
"# \"Resource\": \"*\"", "# \"Resource\": \"*\"\n",
"# }", "# }\n",
"# ]", "# ]\n",
"# }", "# }\n",
"", "\n",
"# Get the primary NAT instance's IP", "# Get the primary NAT instance's IP\n",
"PRIMARY_NAT_IP=`/opt/ec2-api-tools/bin/ec2-describe-instances $PRIMARY_NAT_ID -U $EC2_URL | grep PRIVATEIPADDRESS -m 1 | awk '{print $2;}'`", "PRIMARY_NAT_IP=`/opt/ec2-api-tools/bin/ec2-describe-instances $PRIMARY_NAT_ID -U $EC2_URL | grep PRIVATEIPADDRESS -m 1 | awk '{print $2;}'`\n",
"BACKUP_NAT_IP=`/opt/ec2-api-tools/bin/ec2-describe-instances $BACKUP_NAT_ID -U $EC2_URL | grep PRIVATEIPADDRESS -m 1 | awk '{print $2;}'`", "BACKUP_NAT_IP=`/opt/ec2-api-tools/bin/ec2-describe-instances $BACKUP_NAT_ID -U $EC2_URL | grep PRIVATEIPADDRESS -m 1 | awk '{print $2;}'`\n",
"", "\n",
"echo `date` \"-- Starting NAT monitor\"", "echo `date` \"-- Starting NAT monitor\"\n",
"", "\n",
"while [ . ]; do", "while [ . ]; do\n",
" # Check the health of both instances.", " # Check the health of both instances.\n",
" primary_pingresult=`ping -c $Num_Pings -W $Ping_Timeout $PRIMARY_NAT_IP| grep time= | wc -l`", " primary_pingresult=`ping -c $Num_Pings -W $Ping_Timeout $PRIMARY_NAT_IP| grep time= | wc -l`\n",
"", "\n",
" if [ \"$primary_pingresult\" == \"0\" ]; then", " if [ \"$primary_pingresult\" == \"0\" ]; then\n",
" backup_pingresult=`ping -c $Num_Pings -W $Ping_Timeout $BACKUP_NAT_IP| grep time= | wc -l`", " backup_pingresult=`ping -c $Num_Pings -W $Ping_Timeout $BACKUP_NAT_IP| grep time= | wc -l`\n",
" if [ \"$backup_pingresult\" == \"0\" ]; then", " if [ \"$backup_pingresult\" == \"0\" ]; then\n",
" echo `date` \"-- Both NAT devices un reachable.\"", " echo `date` \"-- Both NAT devices un reachable.\"\n",
" #TODO: Notify alert that both NATs are down.", " #TODO: Notify alert that both NATs are down.\n",
" else #Backup nat is healthy.", " else #Backup nat is healthy.\n",
" # Set HEALTHY variables to unhealthy (0)", " # Set HEALTHY variables to unhealthy (0)\n",
" ROUTE_HEALTHY=0", " ROUTE_HEALTHY=0\n",
" NAT_HEALTHY=0", " NAT_HEALTHY=0\n",
" STOPPING_NAT=0", " STOPPING_NAT=0\n",
" while [ \"$NAT_HEALTHY\" == \"0\" ]; do", " while [ \"$NAT_HEALTHY\" == \"0\" ]; do\n",
" # Primary NAT instance is unhealthy, loop while we try to fix it", " # Primary NAT instance is unhealthy, loop while we try to fix it\n",
" if [ \"$ROUTE_HEALTHY\" == \"0\" ]; then", " if [ \"$ROUTE_HEALTHY\" == \"0\" ]; then\n",
" echo `date` \"-- NAT($PRIMARY_NAT_ID) heartbeat failed, using $BACKUP_NAT_ID for $NAT_RT_ID default route\"", " echo `date` \"-- NAT($PRIMARY_NAT_ID) heartbeat failed, using $BACKUP_NAT_ID for $NAT_RT_ID default route\"\n",
" /opt/ec2-api-tools/bin/ec2-replace-route $NAT_RT_ID -r 0.0.0.0/0 -i $BACKUP_NAT_ID -U $EC2_URL", " /opt/ec2-api-tools/bin/ec2-replace-route $NAT_RT_ID -r 0.0.0.0/0 -i $BACKUP_NAT_ID -U $EC2_URL\n",
" ROUTE_HEALTHY=1", " ROUTE_HEALTHY=1\n",
" fi", " fi\n",
" # Check NAT state to see if we should stop it or start it again", " # Check NAT state to see if we should stop it or start it again\n",
" NAT_STATE=`/opt/ec2-api-tools/bin/ec2-describe-instances $PRIMARY_NAT_ID -U $EC2_URL | grep INSTANCE | awk '{print $5;}'`", " NAT_STATE=`/opt/ec2-api-tools/bin/ec2-describe-instances $PRIMARY_NAT_ID -U $EC2_URL | grep INSTANCE | awk '{print $5;}'`\n",
" if [ \"$NAT_STATE\" == \"stopped\" ]; then", " if [ \"$NAT_STATE\" == \"stopped\" ]; then\n",
" echo `date` \"-- NAT($PRIMARY_NAT_ID) instance stopped, starting it back up\"", " echo `date` \"-- NAT($PRIMARY_NAT_ID) instance stopped, starting it back up\"\n",
" /opt/ec2-api-tools/bin/ec2-start-instances $PRIMARY_NAT_ID -U $EC2_URL", " /opt/ec2-api-tools/bin/ec2-start-instances $PRIMARY_NAT_ID -U $EC2_URL\n",
" NAT_HEALTHY=1", " NAT_HEALTHY=1\n",
" sleep $Wait_for_Instance_Start", " sleep $Wait_for_Instance_Start\n",
" else", " else\n",
" if [ \"$STOPPING_NAT\" == \"0\" ]; then", " if [ \"$STOPPING_NAT\" == \"0\" ]; then\n",
" echo `date` \"-- NAT($PRIMARY_NAT_ID) instance $NAT_STATE, attempting to stop for reboot\"", " echo `date` \"-- NAT($PRIMARY_NAT_ID) instance $NAT_STATE, attempting to stop for reboot\"\n",
" /opt/ec2-api-tools/bin/ec2-stop-instances $PRIMARY_NAT_ID -U $EC2_URL", " /opt/ec2-api-tools/bin/ec2-stop-instances $PRIMARY_NAT_ID -U $EC2_URL\n",
" STOPPING_NAT=1", " STOPPING_NAT=1\n",
" fi", " fi\n",
" sleep $Wait_for_Instance_Stop", " sleep $Wait_for_Instance_Stop\n",
" fi", " fi\n",
" done", " done\n",
"", "\n",
" # Backup nat was healthy so we switched to it. It is now the primary.", " # Backup nat was healthy so we switched to it. It is now the primary.\n",
" if [ \"$NAT_HEALTHY\" == \"1\" ]; then", " if [ \"$NAT_HEALTHY\" == \"1\" ]; then\n",
" TEMP_NAT_ID=$PRIMARY_NAT_ID", " TEMP_NAT_ID=$PRIMARY_NAT_ID\n",
" TEMP_NAT_IP=$PRIMARY_NAT_IP", " TEMP_NAT_IP=$PRIMARY_NAT_IP\n",
"", "\n",
" PRIMARY_NAT_ID=$BACKUP_NAT_ID", " PRIMARY_NAT_ID=$BACKUP_NAT_ID\n",
" PRIMARY_NAT_IP=$BACKUP_NAT_IP", " PRIMARY_NAT_IP=$BACKUP_NAT_IP\n",
"", "\n",
" BACKUP_NAT_ID=$TEMP_NAT_ID", " BACKUP_NAT_ID=$TEMP_NAT_ID\n",
" BACKUP_NAT_IP=$TEMP_NAT_IP", " BACKUP_NAT_IP=$TEMP_NAT_IP\n",
" fi", " fi\n",
" fi", " fi\n",
" else", " else\n",
" sleep $Wait_Between_Pings", " sleep $Wait_Between_Pings\n",
" fi", " fi\n",
"done", "done\n",
"EOF", "EOF\n",
"chmod u+x /opt/edx/bin/nat_monitor.sh", "chmod u+x /opt/edx/bin/nat_monitor.sh\n",
"echo '@reboot /root/nat_monitor.sh > /var/log/nat_monitor.log' | crontab", "echo '@reboot /root/nat_monitor.sh > /var/log/nat_monitor.log' | crontab\n",
"/opt/edx/bin/nat_monitor.sh > /var/log/nat_monitor.log &" "/opt/edx/bin/nat_monitor.sh > /var/log/nat_monitor.log &\n"
]]}} ]]}}
} }
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment