-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtugshuttle
More file actions
executable file
·215 lines (196 loc) · 8.25 KB
/
tugshuttle
File metadata and controls
executable file
·215 lines (196 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
#####################################################################################
# #
# The MIT License (MIT) #
# #
# Copyright (c) 2016 Louis van Harten #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
# #
#####################################################################################
show_help() {
printf "usage:\n\ttugshuttle loc#\n\nwith loc# in:\n\tams1, ams2, ams3, fra1, lon1, nyc1, nyc2, nyc3, sfo1, sgp1, tor1\n"
printf "\nIf the Tugshuttle process receives a SIGKILL while it is running,\nit will not be able to clean up the Droplet. "
printf "You will be billed \nDigitalOcean credit for Droplets that are not destroyed.\n"
printf "To clean up lingering Tugshuttle Droplets, run:\n\n\ttugshuttle clean\n"
}
show_defaults () {
if [ ! -f "/home/$USER/.tugboat" ];
then
echo "Default values are stored in ~/.tugboat and this file doesn't exist!"
echo
echo "Either run tugshuttle or tugboat at least once to generate a ~/.tugboat file."
exit 0
else
echo "Default tugboat values:"
echo
echo "Region: $(grep "region:" ~/.tugboat | sed "s@^.*region: @@")"
echo "SSH Key: $(grep "ssh_key_path:" ~/.tugboat | sed "s@^.*ssh_key_path: @@")"
echo
echo "Digital Ocean Access Token:"
echo "$(grep "access_token" ~/.tugboat | sed "s@^.*access_token: @@")"
echo
echo "Change these values by editing ~/.tugboat"
exit 0
fi
}
clean_up_droplets() {
if [[ -z "$(tugboat droplets | grep tugshuttle-to-)" ]]
then
printf "\nNo more Tugshuttle Droplets found!\n"
else
tugboat destroy tugshuttle-to-
printf "\nConfirming deletion"
for loopvar in `seq 1 10`;
do
printf "."
sleep 1
done
printf ".\n"
printf "\nOne Tugshuttle Droplet deleted!\n"
clean_up_droplets
fi
exit
}
list_regions() {
echo "Please select a region for the Droplet:"
echo
PS3="==> "
do_regions=($(tugboat regions | grep slug |sed -e "s/.*slug:\ //" | tr ')' ' '))
if [[ -z "$do_regions" ]]
then
do_regions=("ams2" "ams3" "blr1" "fra1" "lon1" "nyc1" "nyc2" "nyc3" "sfo1" "sfo2" "sgp1" "tor1")
fi
select opt in "${do_regions[@]}" "Help" "Quit";
do
printf "%s\n\n\n" "-------------------------------------------"
if (( REPLY == 2 + ${#do_regions[@]} )) ; then
echo "No Tunnel created."
exit 0
elif (( REPLY == 1 + ${#do_regions[@]} )) ; then
show_help
exit 0
elif (( REPLY > 0 && REPLY <= ${#do_regions[@]} )) ; then
echo
echo ""$opt" selected."
region="$opt"
break
else
echo "Invalid option."
fi
done
}
#first set-up in case tugboat isn't configured
if [ ! -z "$(tugboat info | grep authorize)" ] || [ ! -z "$(tugboat droplets | grep from\ API:\ unauthorized)" ]
then
printf "Tugboat could not find a valid (read/write) DigitalOcean Access Token.\nYou can get an Access Token from "
printf "https://cloud.digitalocean.com/settings/tokens/new\n\nEnter your access token: "
read acctoken
if [ -e ~/.tugboat ]; then mv ~/.tugboat ~/.tugboat.bak; printf "Existing ~/.tugboat settings stored in ~/.tugboat.bak\n\n"; fi
printf "%s\n%s\n%s\n" "---" "authentication:" " access_token: $acctoken" > ~/.tugboat
printf "%s\n%s\n%s\n%s\n" "ssh:" " ssh_user: root" " ssh_key_path: ~/.ssh/id_rsa" " ssh_port: '22'" >> ~/.tugboat
printf "%s\n%s\n%s\n%s\n" "defaults:" " region: lon1" " image: ubuntu-14-04-x64" " size: 512mb" >> ~/.tugboat
printf "%s\n%s\n%s\n" " ssh_key: ''" " private_networking: 'false'" " backups_enabled: 'false'" >> ~/.tugboat
#correct token if invalid
validAuth=''
while [ -z "$validAuth" ]
do
if [ ! -z "$(tugboat droplets 2>/dev/null | grep from\ API:\ unauthorized)" ]
then
printf "\n (!) Access Token invalid.\n\nYou can get an Access Token from "
printf "https://cloud.digitalocean.com/settings/tokens/new\n\nEnter your access token: "
read acctoken
sed -e "s/access_token:.*/access_token:\ $acctoken/" ~/.tugboat > ~/.tugboat.tmp && mv ~/.tugboat.tmp ~/.tugboat
else
validAuth='success'
fi
done
while [ -z "$(tugboat keys 2>/dev/null | grep id:)" ]
do
printf "\n%s\n" "No SSH keys linked to this DO account."
printf "%s\n\n" "Add your public key at https://cloud.digitalocean.com/settings/security"
read -p "Press [Enter] when done"
done
printf "\n\n$(tugboat keys 2>/dev/null | grep id: | grep -nT Name | sed -e s/,\ fingerprint:.*//)\n\n"
read -p "Choose a linked SSH key ["1", "2", "3", ...] "
keyID="$(tugboat keys 2>/dev/null | grep id: | grep -nT Name | sed -e s/,\ fingerprint:.*// | sed -n "$REPLY""p" |\
sed -e s/.*id:\ // | sed -e "s/)//" )"
sed -e "s/ssh_key:.*/ssh_key:\ $keyID/" ~/.tugboat > ~/.tugboat.tmp && mv ~/.tugboat.tmp ~/.tugboat
printf "\nChanges written to ~/.tugboat\nIf any values are incorrect or change, edit ~/.tugboat.\n\n"
fi
if [[ ! $# > 0 ]]
then
show_help
printf "\n%s" "-------------------------------------------------"
printf "\n\nAssuming you want to start a tunnel...\n"
list_regions
fi
while [[ $# > 0 ]]
do
key="$1"
case $key in
-r|--region)
region="$2"
shift
;;
-h|--help)
show_help
exit 0
;;
-c|--clean|clean)
clean_up_droplets
exit 0
;;
--defaults|--default)
show_defaults
;;
-*)
echo "Error: Unknown option: $1" >&2
show_help
exit 0
;;
ams2|ams3|blr1|fra1|lon1|nyc1|nyc2|nyc3|sfo1|sfo2|sgp1|tor1)
region="$key"
;;
*)
echo "Unrecognized region or option: ${key}"
list_regions
;;
esac
shift
done
keyID=$(cat ~/.tugboat | grep ssh_key: | sed -e "s/.*ssh_key://" -e "s/'//g")
if [[ -z "$(tugboat droplets | grep tugshuttle-to-$region)" ]]
then
tugboat create tugshuttle-to-$region -r $region -s 512mb -i ubuntu-14-04-x64 -k $keyID
else
printf "\nExisting Droplet found in $region. Using that one...\n"
fi
tugboat wait tugshuttle-to-$region --state active
IPtunnel=$(tugboat info tugshuttle-to-$region | grep IP4: | sed s/IP4:// | tr -d '[[:space:]]')
printf "\nIP of server: $IPtunnel\n\n"
keyscanout=''
while [ -z "$keyscanout" ]
do
sleep 2
keyscanout=$(ssh-keyscan $IPtunnel 2>&1 >> ~/.ssh/known_hosts)
printf "Attempting to add server to known host\n\n"
done
sshuttle -r root@$IPtunnel 0.0.0.0/0 -vv
yes | tugboat destroy tugshuttle-to-$region