-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_chal.sh
executable file
·57 lines (46 loc) · 2.06 KB
/
run_chal.sh
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
#!/bin/bash
set -e
if [ ! $# -eq 3 ]; then
echo "usage: $0 ZONE INSTANCE_NAME CONFIG_FILE"
exit 1
fi
ZONE=$1
if [ -z $ZONE ]; then
ZONE="europe-west1-b"
fi
INSTANCE_NAME=$2
CONFIG_FILE=$3
source $CONFIG_FILE
if [ -z $CHAL_NAME ]; then
echo "chal name is empty"
exit 1
fi
echo '[*] killing existing containers and removing files'
gcloud compute ssh $INSTANCE_NAME --command "sudo sh -c \"docker kill $CHAL_NAME; docker rm $CHAL_NAME; chattr -i /chals/$CHAL_NAME/*; rm -R /chals/$CHAL_NAME; mkdir -p /chals/$CHAL_NAME\"" --zone $ZONE
echo '[*] copying files'
gcloud compute ssh $INSTANCE_NAME --command "rm -R chals/$CHAL_NAME; mkdir -p chals/$CHAL_NAME" --zone $ZONE
gcloud compute scp $FLAG_FILE $CHAL_FILES $INSTANCE_NAME:chals/$CHAL_NAME/ --zone $ZONE
gcloud compute ssh $INSTANCE_NAME --command "sudo sh -c \"cp -R chals/$CHAL_NAME /chals/\"" --zone $ZONE
if [ -f "install.sh" ]; then
echo '[*] running install script'
gcloud compute scp install.sh $INSTANCE_NAME:chals/$CHAL_NAME/install.sh --zone $ZONE
gcloud compute ssh $INSTANCE_NAME --command "chmod u+x ~/chals/$CHAL_NAME/install.sh && sudo ~/chals/$CHAL_NAME/install.sh" --zone $ZONE
else
echo '[*] no install script - skipping'
fi
echo '[*] starting container'
gcloud compute ssh $INSTANCE_NAME --command "sudo sh -c \"docker pull tsuro/nsjail-ctf && docker run -d --privileged --expose $PORT --publish $PORT:$PORT --name $CHAL_NAME --restart=always -v /chals/$CHAL_NAME:/home/user:ro tsuro/nsjail-ctf /usr/sbin/run_chal.sh $CHAL_NAME $PORT\"" --zone $ZONE
echo '[*] adding instance tag and label'
gcloud compute instances add-tags $INSTANCE_NAME --tags $CHAL_NAME --zone $ZONE
gcloud compute instances add-labels $INSTANCE_NAME --labels="$CHAL_NAME=1" --zone $ZONE
echo '[*] adding firewall rule'
FIREWALL_MSG=$(gcloud compute firewall-rules create $CHAL_NAME --allow tcp:$PORT --target-tags $CHAL_NAME 2>&1)
FIREWALL_RET=$?
if [ $FIREWALL_RET != 0 ]; then
if [[ $FIREWALL_MSG == *"already exists"* ]]; then
echo '[*] firewall rule already exists - skipping'
else
echo $FIREWALL_MSG
exit 1
fi
fi