-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath-vm-bootstrap
More file actions
executable file
·345 lines (243 loc) · 9.97 KB
/
math-vm-bootstrap
File metadata and controls
executable file
·345 lines (243 loc) · 9.97 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
# Mathematica Virtualization Bootstrap Script 0.1
# Logic:
# Download image through HTTP. Run with cloud client, injecting public-key for root access
# Use cloud client with template 1, *with cloud credentials*
# SSH into the VM using the public key.
# Create a user account, using keyboard input for the username and password.
# Save VM back to Cumulus.
# Defaults
CLOUD_CLIENT=/opt/nimbus-cloud-client/
INSTALL_DIR=$(mktemp -d)
SCRIPT=`basename $0`
echo "Mathematica Virtual Machine Bootstrap Script."
echo " "
USAGE="Usage:
`basename $0` [-c|--conf custom-cloud.properties] [-h|--help]"
# Parse command line arguments
while [ "$1" != "" ]; do
case $1 in
-c | --conf ) shift
CUSTOM_CONF=$1
;;
-h | --help ) echo $USAGE
exit
;;
* ) echo $USAGE
exit 1
esac
shift
done
grid-proxy-info -exists
if [ $? == '1' ]; then
echo "Please run grid-proxy-init to obtain a proxy certificate."
exit
fi
cd $INSTALL_DIR
if [ ! $CUSTOM_CONF ]
then
# Grab cloud credentials.
echo "Enter your repository S3id"
read S3ID
echo "Enter your repository s3key"
read S3KEY
echo "Enter your repository canonicalid"
read CANONICALID
fi
echo "Enter the new remote username:"
read REMOTE_USER
stty -echo
while printf "Enter the remote user's password: "; do
read USER_PASSWORD && echo -e "\n"
printf "Please re-enter the password: "
read USER_PASSWORD2 && echo -e "\n"
if [ "$USER_PASSWORD" = "$USER_PASSWORD2" ]; then
break
else
echo -e "Passwords do not match. Please try again."
fi
done
stty echo
echo "Generating RSA key..."
ssh-keygen -N "" -f $INSTALL_DIR/id_rsa &> /dev/null
echo "Done."
CREDENTIALS="###########################################################################
# Your image repository credentials
###########################################################################
vws.repository.s3id=${S3ID}
vws.repository.s3key=${S3KEY}
vws.repository.canonicalid=${CANONICALID}"
CLOUD_PROPERTIES="###########################################################################
# Path to SSH public key to log in with.
#
# If left blank or deleted, the client will proceed calling the service
# without an SSH just-in-time configuration request. You can override
# using the --ssh-pubkey flag.
###########################################################################
ssh.pubkey=$INSTALL_DIR/id_rsa.pub
###########################################################################
# Path to SSH known_hosts file
#
# If a remote host's SSH public key is made available, the presence of
# this configuration signals the cloud client to replace the corresponding
# IP and/or hostname entry in the SSH known_hosts file.
#
# Only used when you invoke --cluster and use contextualization
#
# You can disable this behavior by leaving blank or deleting.
###########################################################################
ssh.hostsfile=~/.ssh/known_hosts
###########################################################################
# Host+port of the Nimbus central service (not the URL)
###########################################################################
vws.factory=elephant.heprc.uvic.ca:8443
###########################################################################
# Default settings for this Nimbus cloud
###########################################################################
vws.memory.request=256
vws.cores.request=1
###########################################################################
# Host+port of image repository (not the URL)
###########################################################################
vws.repository=elephant.heprc.uvic.ca:8888
###########################################################################
# Virtal Workspace Service identity, to verify we are talking to the
# right machine.
###########################################################################
vws.factory.identity=/C=CA/O=Grid/CN=host/elephant.heprc.uvic.ca
###########################################################################
# Image repository settings
###########################################################################
vws.repository.type=cumulus
vws.repository.s3basekey=VMS
vws.repository.s3bucket=Repo
vws.repository.s3https=false
vws.repository.s3acceptallcerts=false
${CREDENTIALS}
# Extra stuff
vws.metadata.mountAs=sda"
if [ $CUSTOM_CONF ]; then
cp $CUSTOM_CONF $INSTALL_DIR/cloud.properties
sed -i s/ssh.pubkey/\#ssh.pubkey/g cloud.properties
echo "ssh.pubkey=$INSTALL_DIR/id_rsa.pub" >> cloud.properties
else
echo "${CLOUD_PROPERTIES}" > cloud.properties
fi
# Download the stock image
echo "Downloading template image..."
wget http://vmrepo.heprc.uvic.ca/Mathematica-VM.img.gz &> /dev/null
echo "Done."
# Load the image into the user's personal repository in Cumulus
echo "Transferring image to Cumulus..."
$CLOUD_CLIENT/bin/cloud-client.sh --conf ./cloud.properties --transfer --sourcefile ./Mathematica-VM.img.gz > /dev/null
echo "Done."
# Launch vm.
echo "Launching VM..."
#$CLOUD_CLIENT/bin/cloud-client.sh --conf ./cloud.properties --run --name Mathematica-VM.img.gz --hours 1 2>&1 | tee runlog.txt
$CLOUD_CLIENT/bin/cloud-client.sh --conf ./cloud.properties --run --name Mathematica-VM.img.gz --hours 1 >> runlog.txt
echo "Done."
# Parse output of cloud-client to grab the IP address
IP_ADDRESS=$(cat runlog.txt | grep IP | awk '{print $3}')
HANDLE=$(cat runlog.txt | grep Running: | sed s/"'"/" "/g | awk '{print $2}')
i="0"
# if there's an error, $? will be nonzero
while [ $i -lt 30 ]; do
ssh -i $INSTALL_DIR/id_rsa -o StrictHostKeyChecking=no root@$IP_ADDRESS echo "SSH appears functional." &> /dev/null
if [ $? == "0" ]; then
echo "SSH appears functional."
break
else
sleep 10
i=$[$i+1]
fi
done
echo "Adding $REMOTE_USER..."
ssh -i $INSTALL_DIR/id_rsa -o StrictHostKeyChecking=no root@$IP_ADDRESS /usr/sbin/adduser $REMOTE_USER
ssh -i $INSTALL_DIR/id_rsa -o StrictHostKeyChecking=no root@$IP_ADDRESS passwd $REMOTE_USER <<EOF
$USER_PASSWORD
$USER_PASSWORD
EOF
echo "Adding $REMOTE_USER to the sudoers file..."
ssh -i $INSTALL_DIR/id_rsa -o StrictHostKeyChecking=no root@$IP_ADDRESS 'echo "# Adding remote sudo access" >> /etc/sudoers'
ssh -i $INSTALL_DIR/id_rsa -o StrictHostKeyChecking=no root@$IP_ADDRESS 'cp /etc/sudoers /etc/sudoers.bak'
ssh -i $INSTALL_DIR/id_rsa -o StrictHostKeyChecking=no root@$IP_ADDRESS "echo '$REMOTE_USER ALL=(ALL) ALL' >> /etc/sudoers"
echo "Done."
# Shutdown-save the virtual machine
echo "Saving VM..."
$CLOUD_CLIENT/bin/cloud-client.sh --conf ./cloud.properties --save --handle $HANDLE &> /dev/null
echo "Done."
# Remove old cloud.properties, write new one without root RSA key
if [ $CUSTOM_CONF ]; then
sed -i s/ssh.pubkey/\#ssh.pubkey/g cloud.properties
else
rm cloud.properties
fi
# Write new cloud.properties
CLOUD_PROPERTIES="###########################################################################
# Path to SSH public key to log in with.
#
# If left blank or deleted, the client will proceed calling the service
# without an SSH just-in-time configuration request. You can override
# using the --ssh-pubkey flag.
###########################################################################
#ssh.pubkey=~/.ssh/id_rsa.pub
###########################################################################
# Path to SSH known_hosts file
#
# If a remote host's SSH public key is made available, the presence of
# this configuration signals the cloud client to replace the corresponding
# IP and/or hostname entry in the SSH known_hosts file.
#
# Only used when you invoke --cluster and use contextualization
#
# You can disable this behavior by leaving blank or deleting.
###########################################################################
ssh.hostsfile=~/.ssh/known_hosts
###########################################################################
# Host+port of the Nimbus central service (not the URL)
###########################################################################
vws.factory=elephant.heprc.uvic.ca:8443
###########################################################################
# Default settings for this Nimbus cloud
###########################################################################
vws.memory.request=256
vws.cores.request=1
###########################################################################
# Host+port of image repository (not the URL)
###########################################################################
vws.repository=elephant.heprc.uvic.ca:8888
###########################################################################
# Virtal Workspace Service identity, to verify we are talking to the
# right machine.
###########################################################################
vws.factory.identity=/C=CA/O=Grid/CN=host/elephant.heprc.uvic.ca
###########################################################################
# Image repository settings
###########################################################################
vws.repository.type=cumulus
vws.repository.s3basekey=VMS
vws.repository.s3bucket=Repo
vws.repository.s3https=false
vws.repository.s3acceptallcerts=false
${CREDENTIALS}
# Extra stuff
vws.metadata.mountAs=sda"
if [ $CUSTOM_CONF ]; then
sed -i "\%ssh.pubkey=$INSTALL_DIR/id_rsa.pub%d" cloud.properties
else
echo "${CLOUD_PROPERTIES}" > cloud.properties
fi
if [ ! -d ~/.nimbus/ ]
then
mkdir ~/.nimbus
fi
if [ -f ~/.nimbus/cloud.properties ]; then
mv ~/.nimbus/cloud.properties ~/.nimbus/cloud.properties.bak
echo "Your current ~/.nimbus/cloud.properties has been moved to cloud.properties.bak."
cp cloud.properties ~/.nimbus/cloud.properties
else
cp cloud.properties ~/.nimbus/cloud.properties
fi
echo "Virtual machine bootstrap procedure complete. You can now run your new VM with cloud-client.sh --run --name --vm-name --hours number of hours"
#Remove bootstrap files.
rm -r $INSTALL_DIR