My shell script for quickly creating / destorying DigitalOcean droplets https://molamphy.net/code/wrapper-doctl
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.6KB

  1. #!/bin/sh
  2. # Lets set some vars!
  3. binarypath=/usr/local/bin
  4. defaultdomain=swarm.molamphy.net
  5. image="Ubuntu 19.04"
  6. slug=ubuntu-19-04-x64
  7. size=512mb
  8. # First, check for binaries
  9. if [ ! -f $binarypath/doctl ]
  10. then
  11. echo "doctl binary not found in /usr/local/bin https://github.com/digitalocean/doctl/releases"
  12. exit 1
  13. fi
  14. if [ ! -f /usr/bin/jq ]
  15. then
  16. echo "jq binary not found in /usr/bin, install with package manager"
  17. exit 1
  18. fi
  19. # Next, check arguements. If none, give us a hand
  20. if [ $# -eq 0 ]
  21. then
  22. echo "ocean.sh is a wrapper for creating virtual machines using doctl and the DigitalOcean API."
  23. echo " "
  24. echo "Usage:"
  25. echo " "
  26. echo "Options:"
  27. echo " -size= Smallest unless specified. Choose from 1, 2, or 4 GB of RAM"
  28. echo " -image= Ubuntu by default. Choose from Debian, Fedora, or CentOS"
  29. echo " "
  30. exit 0
  31. fi
  32. # Make sure we have a hostname
  33. if [[ $1 == *"-"* ]]
  34. then
  35. echo "The first arguement should be a hostname"
  36. exit 1
  37. else
  38. hostname=$1.$defaultdomain
  39. fi
  40. # Override size if passed
  41. if [[ $* == *"-size=1"* ]]
  42. then
  43. size=1gb
  44. fi
  45. if [[ $* == *"-size=2"* ]]
  46. then
  47. size=2gb
  48. fi
  49. if [[ $* == *"-size=4"* ]]
  50. then
  51. size=4gb
  52. fi
  53. # Override slug if image is passed
  54. if [[ $* == *"-image=debian"* ]]
  55. then
  56. slug=debian-9-x64
  57. image="Debian 9"
  58. fi
  59. if [[ $* == *"-image=fedora"* ]]
  60. then
  61. slug=fedora-26-x64
  62. image="Fedora 26"
  63. fi
  64. if [[ $* == *"-image=centos"* ]]
  65. then
  66. slug=centos-7-x64
  67. image="CentOS 7"
  68. fi
  69. if [[ $* == *"-image=rancher"* ]]
  70. then
  71. slug=rancheros
  72. image="RancherOS Latest"
  73. fi
  74. # Select a region at random
  75. arr[0]="nyc1"
  76. arr[1]="nyc3"
  77. arr[2]="ams3"
  78. arr[3]="fra1"
  79. arr[4]="sfo2"
  80. arr[5]="lon1"
  81. arr[6]="tor1"
  82. rand=$[ $RANDOM % 6 ]
  83. region=${arr[$rand]}
  84. # Summarize next action
  85. echo $hostname wil be $size running $image in $region
  86. # Prompt user
  87. read -p "Ready to create the droplet? (N/y)" -n 1 -r
  88. echo
  89. if [[ ! $REPLY =~ ^[Yy]$ ]]
  90. then
  91. [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
  92. fi
  93. echo Provisioning...
  94. # Lets throw it all together...
  95. echo $binarypath/doctl -v compute droplet create $hostname --region $region --image $slug --size $size
  96. $binarypath/doctl -v compute droplet create $hostname --region $region --image $slug --size $size --ssh-keys bb:a0:44:ab:d4:d6:eb:74:83:84:61:80:0e:ec:0d:77 --wait
  97. ip="`$binarypath/doctl -v compute droplet list $hostname --output json | grep -v 138.68.63.234 | jq '.[].networks.v4' | jq '.[].ip_address' | grep -v null`"
  98. $binarypath/doctl -v compute domain records create --record-type A --record-name $1 --record-data $ip swarm.molamphy.net
  99. sleep 3
  100. dig $hostname