dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

quik (7378B)


      1 #!/bin/sh
      2 #
      3 # quik - mispelt script for deploying vps' quikly
      4 
      5 # Copyright (c) 2020 wfnintr (gopher://wfnintr.net)
      6 # Copyright (c) 2020 haydenh <hayden@haydenvh.com> (gopher://haydenvh.com)
      7 #
      8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     11 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     12 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     13 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     14 # SOFTWARE.
     15 #
     16 # This work is free. You can redistribute it and/or modify it under the      
     17 # terms of the Do What The Fuck You Want To Public License, Version 2,       
     18 # as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.   
     19 
     20 echo(){
     21 	printf "%s\n" "$@"
     22 }
     23 
     24 usage(){
     25 	printf "quik is a script for deploying vps' quickly\n\nusage\n"
     26 	printf "  quik <command>\n\n"
     27 	printf "available commands:\n"
     28 	printf "  auth\t\tauthenticate your digital ocean api key\n"
     29 	printf "  list\t\tlist options considering budget\n"
     30 	printf "  list -l\tlist all options\n"
     31 	printf "  distros\tshow all supported distros\n"
     32 	printf "  deploy\tdeploy instance\n"
     33 	printf "  ls\t\tshow all instances\n"
     34 	printf "  ls-run\tshow running instances\n"
     35 	printf "  rm\t\tremove instance\n"
     36 	printf "  rm-all\tremove all instances\n\n"
     37 	printf "examples:\n"
     38 	printf "  quik list -l\t\t\t\tlist all options\n" 
     39 	printf "  quik deploy 1gb 1 void-linux\t\tdeploy 1 instance running void linux\n"
     40 	printf "  quik deploy 1gb 10 debian-10-x64\tdeploy 10 instances running debian-10-x64\n" 
     41 	exit 1
     42 }
     43 
     44 findKey() {
     45 
     46         echo "$1" | grep -q '[[:alnum:]]' && {
     47                 ssh_key="$1"
     48                 return 0
     49         }
     50 
     51         ssh_key="$(find ~/.ssh/ -type f -print 2>/dev/null| grep .*.pub | head -n1)"
     52 
     53         echo "$ssh_key" | grep -q '[[:alnum:]]' && {
     54                 ssh_key="$ssh_key"
     55                 return 0
     56         }
     57 	
     58 	[ -z "$ssh_key" ] && printf "no key provided\n" && exit 1 
     59 }
     60 
     61 
     62 doctl_auth(){
     63 	doctl auth init 2>&1 | grep -q 'Using token' 2>&1 > /dev/null && {
     64 		return 0 # access token already found
     65 	}
     66 
     67 	echo "$1" | grep -q '[[:alnum:]]' && {
     68 		KEY="$1"
     69 	} || {
     70 		printf 'Enter your API V2 access token: '
     71 		read "KEY" > /dev/null
     72 	}
     73 
     74 	doctl auth init -t "$KEY" 2>&1 > /dev/null
     75 
     76 	echo "$?" | grep -q '0' && {
     77 		mkdir -p ""${XDG_CONFIG_HOME:-$HOME/.config}/quik""
     78 		echo "$KEY" > ""${XDG_CONFIG_HOME:-$HOME/.config}"/quik/api"
     79 		echo "Access token validated!"
     80 	} || {
     81 		echo "Access token invalid"
     82 		return 1
     83 	}
     84 
     85 }
     86 
     87 list(){
     88 	list=$(doctl compute size list)
     89 	[ "$1" = "-l" ] && echo "$list" && exit
     90 	[ -z $1 ] || [ -z $2 ] && {
     91 		printf "quik list - shows all available options, considering budget\n\nusage:\n"
     92 		printf "  $pname list <max cost> <hours> <number of instances>\n\n"
     93 		printf "examples:\n  $pname list -l\t\tlist all options\n"
     94 		printf "  $pname list 1 1 1\tonly show options that fit a budget of 1/USD, 1/hr 1/instance\n"
     95 		exit 1
     96 	}
     97 	n=${3:-1}
     98 
     99 	echo "$list" | awk -v "m=$1" -v "h=$2" -v "i=$n" '
    100 		BEGIN {
    101 			print "ID/Name	Memory	vCPUs	Cost (total)"
    102 		}
    103 
    104 		/[[:space:]][0-9]/ && (i * ($6 * h)) < m {
    105 			print $1 "\t" $2 "\t" $3 "\t" (i * ($6 * h))
    106 		}
    107 	' | column -t -s "	"
    108 }
    109 
    110 list_all(){
    111 	doctl compute droplet list --format Status,Name,Region,PublicIPv4,Memory,VCPUs,Disk
    112 }
    113 
    114 listrunning(){
    115 	doctl compute droplet list --format Status,Name,Region,PublicIPv4,Memory,VCPUs,Disk | awk '
    116 		BEGIN {
    117 			OFS="\t"
    118 			print "Status\tName\tRegion\tIPv4\tMem\tvCPUS\tDisk"
    119 		}
    120 
    121 		$1 == "active" {print $1, $2, $3, $4, $5, $6, $7}' | column -t -s "	"
    122 }
    123 
    124 list_distros(){
    125 	printf 'quik can deploy any of the following distros:\n\n'
    126 	printf '  debian-10-x64\n  centos-7-x64\n  ubuntu-18-04-x64\n  void-linux\n\n'
    127 	printf 'example:\n'
    128 	printf '  quik deploy 1gb 1 debian-10-x64\n'
    129 	exit 0
    130 }
    131 
    132 deploy(){
    133 	[ "$1" = "darkvoid" ] && darkvoid # do not remove, testing drist playbook
    134 	[ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] && {
    135 		printf "quik deploy - deploy an instance, quickly\n\nusage:\n"
    136 		printf "  $pname deploy <size-slug> <number of instances> <distro> [/path/to/sshpubkey]\n"
    137 		exit 1
    138 	}
    139 	size_slug="$1"
    140 	instances=$2
    141 	list -l | awk '{if (NR != 1) {print $1}}' | grep -q "^"$size_slug"$" || {
    142 		list -l
    143 		exit
    144 	}
    145 
    146 
    147 	doctl_auth
    148 	echo "$?" | grep -q "1" && {
    149 		exit
    150 	}
    151 
    152 	printf 'importing your ssh key...\n'
    153 	findKey "$4"
    154 	doctl compute ssh-key import "$USER" --public-key-file "$ssh_key" > /dev/null 2>&1
    155 	ssh_key=$(doctl compute ssh-key list | grep "$USER" | awk '{print $3}')
    156 	printf "deploying %d instances of size %s...\n" "${instances:-1}" "$size_slug" # preferred: deploying num instances with 1vpcu, 1gb ram, 30gb hdd
    157         image_id=$(doctl compute image list --public | awk "/$3/"'{print $1}')
    158 	echo "$image_id" | grep -q '[[:alnum:]]' || {
    159 		list_distros
    160 		exit
    161 	}
    162 
    163 
    164 	for void in $(seq ${instances:-1})
    165 	do
    166 		(
    167 			name="$3-$(head -c 1000 /dev/urandom | tr -dc 'A-Za-z0-9' | head -n 1 | cut -zc-10)"
    168 			
    169 			# this is better but some regions are restricted
    170 			# region=${REGION:-$(doctl compute region ls --no-header | sort -R | head -n1 | awk '{print $1}')}
    171 			region=nyc1
    172 			
    173 			doctl compute droplet create "$name" --image "$image_id" --size "$size_slug" --region "$region" --ssh-keys "$ssh_key" 1>/dev/null
    174 			printf '\ncreating instance %s in region %s' "$name" "$region"
    175 		) &
    176 	done
    177 	sleep 5 #!was 20
    178 	printf '\n\n' && list_all # !was listrunning
    179 	printf "\n\nPlease wait a few moments before logging in, to allow the sshd(s) to start, and ssh key(s) to be copied over.\n"
    180 }	
    181 
    182 delete(){
    183         [ -z "$1" ] && {
    184 		printf "quik rm - remove an instance, quickly\n\n"
    185 		list_all
    186 		printf "\nusage:\n  $pname rm <name or ip> [name2 or ip2] ..."
    187 	}
    188 	for nameip in $@
    189 	do
    190 		name=$(doctl compute droplet ls --format Name,PublicIPv4 | awk "\$1 == \"$nameip\" || \$2 == \"$nameip\""' {print $1}')
    191 		[ "$name" != "" ] && doctl compute droplet d "$name" -f || echo "$(tput bold)error:$(tput sgr0) no droplet has name/ip: $nameip"
    192 	done
    193 }
    194 
    195 rmall(){
    196 	list=$(doctl compute droplet ls --format Name | awk '$1 != "Name"')
    197 	for droplet in $list
    198 	do
    199 		echo "Deleting $droplet..."
    200 		doctl compute droplet d "$droplet" -f &
    201 	done
    202 }
    203 
    204 # do not change, testing
    205 darkvoid() {
    206 	printf 'importing your ssh key...\n'
    207 	findKey $1
    208 	ssh_key=$(doctl compute ssh-key list | grep "$USER" | awk '{print $3}')
    209 	printf 'creating instance...\n'
    210 	image_id=$(doctl compute image list | awk '/darkvoid/{print $1}')
    211 	doctl compute droplet create "darkvoid" --wait --image "$image_id" --size "s-2vcpu-4gb" --region "nyc1" --ssh-keys "$ssh_key" 1>/dev/null
    212 	printf 'initializating...\n' && sleep 5 
    213 	ip=$(doctl compute droplet list --format Name,PublicIPv4 | awk '/darkvoid/{print $2}')
    214 	printf '+++execute+++\n' && sleep 10
    215 	ssh-keyscan "$ip" >> ~/.ssh/known_hosts
    216 	cd playbooks/common
    217 	printf 'SERVERS = void@%s' "$ip" > config.mk
    218 	make
    219 	make clean
    220 	cd -
    221 	cd playbooks/offensive
    222 	printf 'SERVERS = void@%s' "$ip" > config.mk
    223 	make
    224 	make clean
    225 }
    226 
    227 
    228 pname=$(basename $0)
    229 [ -z $1 ] && usage
    230 cmd="$1" && shift
    231 case "$cmd" in
    232 	list) list $@ ;;
    233 	-l) list -l;;
    234 	deploy) deploy $@ ;;
    235 	rm) delete $@ ;;
    236 	list-running|ls-run) listrunning ;;
    237 	ls) list_all ;;
    238 	rm-all) rmall ;;
    239 	auth) doctl_auth $@ ;;
    240 	distros) list_distros ;;
    241 	*) usage ;;
    242 esac