Skip to content

Instantly share code, notes, and snippets.

@stefan-matic
Last active December 9, 2020 18:17
Show Gist options
  • Save stefan-matic/d73bc3a012323bab901c24fc85c2d123 to your computer and use it in GitHub Desktop.
Save stefan-matic/d73bc3a012323bab901c24fc85c2d123 to your computer and use it in GitHub Desktop.
Interactive droplet spawn tool using doctl and bash
#!/bin/bash
#todo is found at the top and randomly in the script
#spin for sleep time
#https://stackoverflow.com/questions/12498304/using-bash-to-display-a-progress-indicator
#needs a better implementation than wait n seconds to ssh - something like "wait for droplet status to become active"
#allow to use custom image_slug
#don't exit when fail, return to type the number correctly
#needs to check if doctl is installed and redirect accordingly, same goes for token apply
export sshkey="/home/user/.ssh/droplet-ssh-key"
export sshkeyfingerprint="xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:"
export region="fra1"
doctl compute droplet list --format "Name,PublicIPv4,VCPUs,Memory,Disk,Image,Status"
echo -e "
#~~~~~~~~~~~~~#
| 1.) Connect |
| 2.) Create |
| 3.) Destroy |
#~~~~~~~~~~~~~#\n"
read -e -p "What do you wish to do, master? " option
read -e -p "Server name? " servername
if [ $option == "1" ]
then
echo "Connecting to droplet..."
doctl compute ssh --ssh-key-path $sshkey $servername
elif [ $option == "2" ]
then
echo -e "
#~~~~~~~~~~~~~~~~~~~~~~~~~~~#
| 1.) Ubuntu 20.04 |
| 2.) Docker (Ubuntu 20.04) |
| 3.) CentOS 8.2 x64 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
read -e -p "Which OS? " os
if [ $os == "1" ]
then
export image=ubuntu-20-04-x64
elif [ $os == "2" ]
then
export image=docker-20-04
elif [ $os == "3" ]
then
export image=centos-8-x64
else
echo "this is not a valid choice"
exit 0
fi
echo -e "
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
| 1.) 1 vCPU / 1GB RAM / 5$/mo |
| 2.) 2 vCPU / 2GB RAM / 15$/mo |
| 3.) 2 vCPU / 4GB RAM / 20$/mo |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
read -e -p "What size ? " size
if [ $size == "1" ]
then
export size=s-1vcpu-1gb
elif [ $size == "2" ]
then
export size=s-2vcpu-2gb
elif [ $size == "3" ]
then
export size=s-2vcpu-4gb
else
echo "this is not a valid choice"
exit 0
fi
echo "Creating droplet..."
# create a nest here to choose which image and size I want to pick amogst 3 most popular
doctl compute droplet create $servername --size $size --image $image --region fra1 --ssh-keys $sshkeyfingerprint
echo "Waiting for server to spawn..."
# implement if the status of the $servername is active then innitiate connection
sleep 60
echo "Connecting to droplet..."
doctl compute ssh --ssh-key-path $sshkey $servername
elif [ $option == "3" ]
then
echo -e "
#~~~~~~~~~~~~~~~~~~~~#
| WARNING |
| Your droplet |
| will be destroyed! |
#~~~~~~~~~~~~~~~~~~~~#\n"
doctl compute droplet delete $servername
doctl compute droplet list --format "Name,PublicIPv4,VCPUs,Memory,Disk,Image,Status"
echo -e "You cold-hearted murderer :( \n"
exit 0
else
echo "this is not a valid choice"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment