-
Notifications
You must be signed in to change notification settings - Fork 1
/
linux-pull-run.sh
executable file
·243 lines (205 loc) · 9.67 KB
/
linux-pull-run.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
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
#!/bin/bash
usage="\033[1mSYNOPSIS
\t./linux-pull-run.sh\033[0m [\033[4mOPTIONS\033[0m]\n
\033[1mOPTIONS\033[0m
\t\033[1m-o\033[0m, \033[1m--no-open\033[0m
\t Disables automatic opening of the dashboard.\n
\t\033[1m-t\033[0m <\033[4mTAG\033[0m>, \033[1m--tag\033[0m=<\033[4mTAG\033[0m>
\t Specifies the tag of the chase-car-dashboard-image to pull and run.
\t If no tag is specified, the image tagged \"latest\" will be used.\n
\t\033[1m-d\033[0m <\033[4mTAG\033[0m>, \033[1m--dist-tag\033[0m=<\033[4mTAG\033[0m>
\t Specifies the tag of the engineering-data-distributor-image to pull and run.
\t If no tag is specified, the image tagged \"latest\" will be used.
\t - NOTE: The sc1-data-format submodule in the engineering-data-distributor-image will automatically be synced with that of the chase-car-dashboard-image being run.\n
\t\033[1m-c\033[0m <\033[4mCONFIG\033[0m>, \033[1m--config\033[0m=<\033[4mCONFIG\033[0m>
\t Specifies the configuration in which to run the dashboard and data distribution server. The following are the available configurations:
\t - \033[1m\"competition\"\033[0m: Run only the chase-car-dashboard-image on this computer. This computer will be connected to another device running the engineering-data-distributor application
\t via a LAN. The device running the distribution server will receive data from the solar car and broadcast it to the computers connected to it.
\t - \033[4mNOTE\033[0m: In order to run this configuration, the Ethernet port connected to the device running the distribution server has to be on the 192.168.1.0/24 subnet, and it cannot
\t have an IP address in the following range: 192.168.1.15-17. You can test the connection by pinging 192.168.1.15.
\t - \033[1m\"dev\"\033[0m: Run only the chase-car-dashboard-image on this computer. This configuration will run the data generator within the dashboard to create random mock datasets to display.\n
\t\033[1m-h\033[0m, \033[1m--help\033[0m
\t Displays this help message.\n"
# _validate_arg: Checks if an argument was provided for a certain field
# - Parameters: 1) The field which requires an argument
# 2) The argument passed for that field
_validate_arg() {
# Check if an argument was provided
if [[ -z $2 ]]; then
# An argument wasn't provided. Print error message/usage and exit
echo -e "\n\033[1;31m[ERROR] $1 requires a value to be provided.\033[0m\n\n$usage\n" >&2
exit 1
fi
}
# _enforce_one_tag: Checks if a tag was already specified. Called when the -t or --tag option is specified.
_enforce_one_tag() {
# If a tag was already specified, print error message/usage and exit
if [[ -n $tag ]]; then
echo -e "\n\033[1;31m[ERROR] More than one tag specified.\033[0m\n\n$usage\n" >&2
exit 1
fi
}
# _enforce_one_distributor_tag: Checks if a tag was already specified. Called when the -d or --dist-tag option is
# specified.
_enforce_one_distributor_tag() {
# If an engineering-data-distributor tag was already specified, print error message/usage and exit
if [[ -n $dist_tag ]]; then
echo -e "\n\033[1;31m[ERROR] More than one engineering-data-distributor tag specified.\033[0m\n\n$usage\n" >&2
exit 1
fi
}
# _enforce_one_configuration: Checks if a configuration was already specified. Called when the -c or --config option is
# specified.
_enforce_one_configuration() {
# If a configuration was already specified, print error message/usage and exit
if [[ -n $config ]]; then
echo -e "\n\033[1;31m[ERROR] More than one configuration specified.\033[0m\n\n$usage\n" >&2
exit 1
fi
}
# Parse command line arguments
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-n | --no-db )
no_db=true
;;
-o | --no-open )
no_open=true
;;
-t | --tag )
# Make sure a tag was not already specified
_enforce_one_tag
# Get the tag specified after the -t or --tag flag
tag=$2
# Check that a tag was provided
# Keep only characters before a hyphen from the CLA after -t | --tag, effectively ignoring that CLA if it is a flag
_validate_arg $1 "${tag%%-*}"
shift
;;
--tag=* )
# Make sure a tag was not already specified
_enforce_one_tag
# Get the characters immediately after the equals sign in the CLA
tag="${1##--tag=}"
# Check that a tag was provided
# Strip the equals sign and anything after it from the CLA to make the field "--tag"
_validate_arg "${1%%=*}" $tag
;;
-d | --dist-tag )
# Make sure an engineering-data-distributor tag was not already specified
_enforce_one_distributor_tag
# Get the engineering-data-distributor tag specified after the -d or --dist-tag flag
dist_tag=$2
# Check that a tag was provided
# Keep only characters before a hyphen from the CLA after -d | --dist-tag, effectively ignoring that CLA if it is a
# flag
_validate_arg $1 "${dist_tag%%-*}"
shift
;;
--dist-tag=* )
# Make sure an engineering-data-distributor tag was not already specified
_enforce_one_distributor_tag
# Get the characters immediately after the equals sign in the CLA
dist_tag="${1##--dist-tag=}"
# Check that a tag was provided
# Strip the equals sign and anything after it from the CLA to make the field "--dist-tag"
_validate_arg "${1%%=*}" $dist_tag
;;
-c | --config )
# Make sure a configuration was not already specified
_enforce_one_configuration
# Get the configuration specified after the -c or --config flag
config=$2
# Check that a configuration was provided
# Keep only characters before a hyphen from the CLA after -c | --config, effectively ignoring that CLA if it is a
# flag
_validate_arg $1 "${config%%-*}"
# Check that the configuration provided is valid
if [[ ! "$config" =~ ^(competition|dev|individual)$ ]]; then
echo -e "\n\033[1;31m[ERROR] Invalid configuration specified: $config\033[0m\n\n$usage\n" >&2
exit 1
fi
shift
;;
--config=* )
# Make sure a configuration was not already specified
_enforce_one_configuration
# Get the characters immediately after the equals sign in the CLA
config="${1##--config=}"
# Check that a configuration was provided
# Strip the equals sign and anything after it from the CLA to make the field "--config"
_validate_arg "${1%%=*}" $config
# Check that the configuration provided is valid
if [[ ! "$config" =~ ^(competition|dev|individual)$ ]]; then
echo -e "\n\033[1;31m[ERROR] Invalid configuration specified: $config\033[0m\n\n$usage\n" >&2
exit 1
fi
;;
-h | --help )
echo -e "\n$usage\n"
exit 0
;;
* )
echo -e "\n\033[1;31m[ERROR] Invalid option: $1\033[0m\n\n$usage\n" >&2
exit 1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
# If a tag, engineering-data-distributor tag, or configuration was specified as a CLA, use that. Otherwise, default to
# "latest" for the tags and "individual" for the configuration
tag=${tag:=latest}
dist_tag=${dist_tag:=latest}
config=${config:=individual}
# Pull the Docker image with the specified tag
docker pull ghcr.io/badgerloop-software/chase-car-dashboard-image:$tag
# Validate the version of the image if the "individual" configuration was specified
if [[ "$config" == "individual" ]]; then
# Create a temporary container to parse package.json for the image's version. If it is lower than 3.8.0, making it
# incompatible with the "individual" configuration, store it in low_ver so that the script can exit below
low_ver=$(docker run --rm ghcr.io/badgerloop-software/chase-car-dashboard-image:$tag bash -c 'grep -oP "(?<=\"version\": \")(\d)+\.(\d)+\.(\d)+" package.json > v; if [[ $(cut -d "." -f 1 v) -lt 3 || ($(cut -d "." -f 1 v) -eq 3 && $(cut -d "." -f 2 v) -lt 8) ]]; then cat v; fi')
if [[ -n "$low_ver" ]]; then
echo -e "\n\033[1;31m[ERROR] The specified chase-car-dashboard-image (v$low_ver) does not support the \"$config\" configuration. The image must be v3.8.0 or newer.\033[0m\n"
exit 1
fi
# Pull the engineering-data-distributor image with the specified tag
docker pull ghcr.io/badgerloop-software/engineering-data-distributor-image:$dist_tag
fi
# Unless -o or --no-open was specified, open the dashboard
if [[ ! $no_open ]]; then
[[ $OSTYPE != 'darwin'* ]] && cmd=xdg-open
[[ $OSTYPE == 'darwin'* ]] && cmd=open
if [[ -n $(command -v $cmd) ]]; then
$cmd http://localhost:3000
else
echo -e "\nCould not run \`$cmd\`, please open 'http://localhost:3000' on your browser\n"
fi
fi
# Create the Docker volume
path=`pwd`
mkdir -p recordedData
docker volume create --name chasecar --opt type=none --opt device=${path}/recordedData --opt o=bind
# Setting the time zone of host machine
[[ $OSTYPE == 'darwin'* ]] && timezone=`readlink /etc/localtime | sed 's#/var/db/timezone/zoneinfo/##g'`
[[ $OSTYPE != 'darwin'* ]] && timezone=`cat /etc/timezone`
# Arguments that remain constant for all instances of running the Docker images
const_chase_car_args="-e TZ=$timezone -p 3000:3000 -p 4001:4001 -v chasecar:/chase-car-dashboard/Backend/recordedData/processedData ghcr.io/badgerloop-software/chase-car-dashboard-image:$tag"
# Run the image(s) according to the configuration specified
case $config in
competition )
# Run chese car dashboard and get container id
cid=$(docker run -d $const_chase_car_args)
# log output
docker logs $cid
# Run the chase-car-dashboard image
docker run --net=container:$cid redis/redis-stack-server:latest
;;
dev )
# Run chese car dashboard and get container id
cid=$(docker run -d $const_chase_car_args npm run start-dev)
# log output
docker logs $cid
# Run the chase-car-dashboard image
docker run --net=container:$cid redis/redis-stack-server:latest
;;
esac
#The server will be run at http://localhost:3000, it will take one to two minutes to start up
#if this window does not automatically pop up then please enter the URL manually