-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-pfs.sh
executable file
·174 lines (155 loc) · 4.05 KB
/
start-pfs.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
#!/bin/bash
PROD_DPSEC="./docker-compose.yml"
DEV_DSPEC="./dev-docker-compose.yml"
CONFIG="./pfs.toml"
DBENV="./database.env"
ENV="prod"
VERBOSE=false
COLOR="on"
HELP_MAIN="start-pfs
USAGE:
start-pfs.sh [SUBCOMMAND] [FLAGS] [OPTION...]
SUBCOMMANDS:
prepare Prepares the docker environment for pfs.
start Starts the dockerized pfs service.
build Builds the pfs Docker image.
help Shows help information.
clear Clears the database environment file.
FLAGS:
-e, --env [dev|prod|path/to/file.yml]
Use environment specific Docker compose file. default: prod
-c, --config string Path to configuration file. default: pfs.toml
-d, --dbenv string default: database.env
Path to database environment file, used in the Docker compose file.
OPTIONS:
-h, --help Shows help information.
-n, --no-color Disables color output.
-v, --verbose Print verbose output.
"
OPTS=$(getopt -o d:e:c:vhn --long dbenv:env:,config:,verbose,help,no-color: -n 'start-pfs' -- "$@")
verbose_echo() {
if [ "$VERBOSE" = true ]; then
echo -e "$(if [ "$COLOR" = "on" ]; then echo -e "\e[1;33m[VERBOSE]\e[0m" "$@"; else echo "[VERBOSE]" "$@"; fi)"
fi
}
if [ ! "$OPTS" != 0 ]; then
echo "Failed parsing options."
exit 1
fi
eval set -- "$OPTS"
while true; do
case $1 in
-e | --env)
shift
if [ "$1" = "dev" ]; then
ENV="dev"
elif [ "$1" = "prod" ]; then
:
elif [ "$1" != "prod" ]; then
if [ ! -e "$1" ]; then
echo "Docker compose file does not exist."
exit 1
fi
ENV="$1"
fi
shift
;;
-C | --color)
shift
COLOR="off"
shift
;;
-d | --dbenv)
shift
if [ ! -e "$1" ]; then
echo "Database environment file does not exist."
exit
fi
DBENV="$1"
shift
;;
-c | --config)
shift
if [ ! -e "$1" ]; then
echo "Config file does not exist."
exit
fi
CONFIG="$1"
shift
;;
-h | --help)
echo "$HELP_MAIN" && exit
shift
;;
-v | --verbose)
VERBOSE=true
shift
;;
--)
shift
break
;;
*) break ;;
esac
done
verbose_echo "Parsed as"
verbose_echo "$OPTS"
verbose_echo "Recieved options:"
verbose_echo "\$ENV: $ENV"
verbose_echo "\$CONFIG: $CONFIG"
verbose_echo "\$VERBOSE: $VERBOSE"
verbose_echo "\$COLOR: $COLOR"
if [ "$1" != "" ]; then verbose_echo "\$1: $1"; fi
# Note, this will not grab multiline strings. If an issue is filed on this, will fix.
prepare() {
verbose_echo "Running prepare function"
clear
pgkeys=("POSTGRES_USER" "POSTGRES_DB" "POSTGRES_PASSWORD" "POSTGRES_INITDB_ARGS" "POSTGRES_INITDB_WALDIR" "POSTGRES_HOST_AUTH_METHOD" "PGDATA")
for key in "${pgkeys[@]}"; do
grep "$key" "$CONFIG" >>"$DBENV"
done
cp "$DBENV" frontend
cp "$DBENV" backend
cp "$CONFIG" backend
if [ ! -d "frontend/node_modules" ]; then
cd frontend && npm install && cd ..
fi
}
run() {
verbose_echo "Running run function"
if [ "$ENV" = "prod" ]; then
docker compose -f "$PROD_DPSEC" up
elif [ "$ENV" = "dev" ]; then
docker compose -f "$DEV_DSPEC" up
else
docker compose -f "$ENV" up
fi
}
clear_dbe() {
verbose_echo "Clearing $DBENV"
echo -n "" >"$DBENV"
}
build() {
prepare
verbose_echo "Running build function"
if [ "$ENV" = "prod" ]; then
docker compose -f "$PROD_DPSEC" build
elif [ "$ENV" = "dev" ]; then
docker compose -f "$DEV_DSPEC" build
else
docker compose -f "$ENV" build
fi
}
if [ "$#" = 0 ]; then
verbose_echo "\$# is 0"
prepare
run
else
case "$1" in
"prepare") prepare ;;
"start") run ;;
"build") build ;;
"help") echo "$HELP_MAIN" && exit ;;
"clear") clear_dbe && echo "Cleared database environment file." && exit ;;
esac
fi