Skip to content

Instantly share code, notes, and snippets.

@cpswan
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpswan/036ec7127d30ad2b38db to your computer and use it in GitHub Desktop.
Save cpswan/036ec7127d30ad2b38db to your computer and use it in GitHub Desktop.
Launch and configure VNS3 from a single script
#!/bin/bash
VNS3_AMI=ami-ea084f82
VNS3_TYPE=t2.small
VNS3_SUBNET=subnet-b123b456
VNS3_GROUP=sg-ea123456
VNS3_NAME=myVNS3
VNS3_PW=pa55Word
VNS3_LIC=license.gpg
VNS3_TK=MyS3cret
# Allocate an EIP
EIP=$(aws ec2 allocate-address --domain vpc --output text)
EIP_IP=$(echo $EIP | cut -d ' ' -f3)
EIP_ID=$(echo $EIP | cut -d ' ' -f1)
echo EIP allocated $EIP_ID $EIP_IP
# Launch instance
VNS3_ID=$(aws ec2 run-instances --image-id $VNS3_AMI --count 1 --instance-type \
$VNS3_TYPE --subnet-id $VNS3_SUBNET --security-group-ids $VNS3_GROUP \
--no-associate-public-ip-address --block-device-mappings \
"[{\"DeviceName\": \"/dev/sda1\",\"Ebs\":{\"VolumeType\":\"gp2\"}}]" \
--query Instances[].InstanceId --output text)
echo Instance launched $VNS3_ID
# Name the instance
aws ec2 create-tags --resources $VNS3_ID --tags Key=Name,Value=$VNS3_NAME
echo Instance named $VNS3_NAME and waiting for it to be running
# Wait for instance to be running
aws ec2 wait instance-running --instance-ids $VNS3_ID
# Assign EIP
EIP_ASSOC=$(aws ec2 associate-address --instance-id $VNS3_ID \
--allocation-id $EIP_ID)
echo Assigned IP $EIP_IP
# Wait for instance to pass status checks and for API to be available
aws ec2 wait instance-status-ok --instance-ids $VNS3_ID
VNS3_UP=`curl --silent -k -X GET -u api:$VNS3_ID \
https://$EIP_IP:8000/api/status/system | grep licensed`
while [ "$VNS3_UP" = "" ]
do
sleep 15
echo -n "."
VNS3_UP=`curl --silent -k -X GET -u api:$VNS3_ID \
https://$EIP_IP:8000/api/status/system | grep licensed`
done
# Reset API password
APIPW_ST=$(curl --silent -k -X PUT -u api:$VNS3_ID -d \
'{"password":"'$VNS3_PW'"}' -H 'Content-Type:application/json' \
https://$EIP_IP:8000/api/api_password)
echo API password set to $VNS3_PW
# Upload license file
LICFL_ST=$(curl --silent -k -X PUT -u api:$VNS3_PW --data-binary @$VNS3_LIC \
-H 'Content-Type:text/plain' https://$EIP_IP:8000/api/license)
echo License file uploaded
# Set license (using defaults)
LICEN_ST=$(curl --silent -k -X PUT -u api:$VNS3_PW -d '{"default":true}' \
-H 'Content-Type: application/json' \
https://$EIP_IP:8000/api/license/parameters)
echo License set with defaults
# Wait for instance to restart
echo Waiting for VNS3 to restart
VNS3_UP=`curl --silent -k -X GET -u api:$VNS3_PW \
https://$EIP_IP:8000/api/status/system | grep uptime`
while [ "$VNS3_UP" = "" ]
do
sleep 15
echo -n "."
VNS3_UP=`curl --silent -k -X GET -u api:$VNS3_PW \
https://$EIP_IP:8000/api/status/system | grep uptime`
done
echo VNS3 restarted
# Reset user password
USRPW_ST=$(curl --silent -k -X PUT -u api:$VNS3_PW -d \
'{"enabled":"true","admin_username":"vnscubed","admin_password":"'$VNS3_PW'"}' \
-H 'Content-Type:application/json' https://$EIP_IP:8000/api/admin_ui)
echo User password set to $VNS3_PW
# Generate keyset
KEYGN_ST=$(curl --silent -k -X PUT -u api:$VNS3_PW -d \
'{"token":"'$VNS3_TK'", "topology_name":"'$VNS3_NAME'"}' \
-H 'Content-Type:application/json' https://$EIP_IP:8000/api/keyset)
echo Generating keyset
export KEYSET_ST=$(curl --silent -k -X GET -u api:$VNS3_PW \
https://$EIP_IP:8000/api/keyset | grep false)
while [ "$KEYSET_ST" != "" ]
do
sleep 15
echo -n "."
export KEYSET_ST=$(curl --silent -k -X GET -u api:$VNS3_PW \
https://$EIP_IP:8000/api/keyset | grep false)
done
echo Keyset generated
# Make self peer
export MGRPR_ST=$(curl --silent -k -X PUT -u api:$VNS3_PW -d '{"id":"1"}' \
-H 'Content-Type:application/json' https://$EIP_IP:8000/api/peering/self)
echo Self peered
echo 'All ready to go at https://'$EIP_IP':8000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment