This repository has been archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployTestInstance.sh
99 lines (72 loc) · 2.5 KB
/
deployTestInstance.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
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
#!/bin/bash
##############################################################################
# Transitional script to forward old dev instance requests to the new system #
##############################################################################
# Requires packages: docker, docker-compose, procmail, git, sed, tac, potentially others depending on your distro
echo "Starting deployTestInstance.sh Script..."
#######################
# Argument Validation #
#######################
# Name the variables
ID=$1
MODE=$2
# Make sure exactly 2 arguments are passed
if [ $# -ne 2 ]; then
# 2 args not passed, see if 1 was
if [ $# -ne 1 ]; then
# 1 arg was not passed
echo "Invalid Number Of Arguments Specified, Aborting."; exit 1
else
# 1 arg was passed, use compatibility mode
echo "1 Argument specified, running in compatibility mode"
MODE="ISSUE"
fi
fi
# Validate MODE and ID (depending on MODE)
if [ "${MODE}" = "ISSUE" ]; then
# Only allow a-z, 0-9 in commit IDs
if [[ "${ID}" =~ [^abcdefghijklmnopqrstuvwxyz0123456789] ]]; then
echo "Invalid Commit ID, Aborting."; exit 1
fi
elif [ "${MODE}" = "PR" ]; then
# Only allow 0-9 in PR Numbers
if [[ "${ID}" =~ [^0123456789] ]]; then
echo "Invalid PR Number, Aborting."; exit 1
fi
else
echo "Invalid Mode Specified, Aborting."; exit 1
fi
echo "Arguments Validated."
#####################
# Exclusivity Check #
#####################
function finish {
# Remove lock files
echo "Trapped EXIT, removing lockfiles"
rm -f ~/deployTestInstance.lock
rm -f ~/deployTestInstance-"${ID}".lock
}
trap finish EXIT
# Try to acquire a lock every 5 seconds, not continuing until then.
# Given that this normally is run by GitHub, this should end up terminated by them if it never gets a lock
echo "Acquiring unique lock..."
# Acquire unique lock so that we can have parallel builds that don't interfere with each other
lockfile -5 ~/deployTestInstance-"${ID}".lock
echo "Unique lock acquired"
###############
# Basic Setup #
###############
# Echo out what we're doing
echo "Forwarding instance creation request for '${TYPE}' with ID '${ID}' to new system"
curl --location --request POST 'https://dev.pollbuddy.app/api/deployment/new' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "dev_instance_type=${MODE}" \
--data-urlencode "dev_instance_id=${ID}" \
--data-urlencode "key=$(cat ~/CICD_SECRET)"
##########
# Finish #
##########
# We're done!
echo "Forwarding complete"
# Exit and release locks
exit 0