My shell script for quickly creating / destorying DigitalOcean droplets https://molamphy.net/code/wrapper-doctl
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

101 lines
2.5KB

  1. #!/bin/bash
  2. # Lets set some vars!
  3. binarypath=/snap/bin
  4. defaultdomain=swarm.molamphy.net
  5. image="Ubuntu 20.04"
  6. slug=ubuntu-20-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-32-x64
  62. image="Fedora 32"
  63. fi
  64. if [[ $* == *"-image=centos"* ]]
  65. then
  66. slug=centos-7-x64
  67. image="CentOS 7"
  68. fi
  69. # Select a region at random
  70. arr[0]="nyc1"
  71. arr[1]="nyc3"
  72. arr[2]="ams3"
  73. arr[3]="fra1"
  74. arr[4]="sfo2"
  75. arr[5]="lon1"
  76. arr[6]="tor1"
  77. rand=$[ $RANDOM % 6 ]
  78. region=${arr[$rand]}
  79. # Summarize next action
  80. echo $hostname wil be $size running $image in $region
  81. # Prompt user
  82. read -p "Ready to create the droplet? (N/y)" -n 1 -r
  83. echo
  84. if [[ ! $REPLY =~ ^[Yy]$ ]]
  85. then
  86. [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
  87. fi
  88. echo Provisioning...
  89. # Lets throw it all together...
  90. echo $binarypath/doctl -v compute droplet create $hostname --region $region --image $slug --size $size
  91. $binarypath/doctl -v compute droplet create $hostname --region $region --image $slug --size $size --ssh-keys b0:0c:e5:11:dc:4a:a8:c0:81:79:03:fa:05:9b:81:5b --wait
  92. 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`"
  93. $binarypath/doctl -v compute domain records create --record-type A --record-name $1 --record-data $ip swarm.molamphy.net
  94. sleep 3
  95. echo "nslookup returned:"
  96. dig +short ns $hostname
  97. dig +short a $hostname