-
Notifications
You must be signed in to change notification settings - Fork 1
/
askci.sh
executable file
·239 lines (214 loc) · 7.69 KB
/
askci.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
#!/bin/bash
# Copyright (C) 2019-2020 Vanessa Sochat.
# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
# AskCI CLI script.
SCRIPTNAME="askci.sh";
SCRIPTPATH=$(realpath $0);
SCRIPTDIR=$(dirname $SCRIPTPATH);
IN_RED="\033[0;31m";
IN_GRY="\033[0;44m";
IN_DEF="\033[0m";
# Generates a random string of desired length.
# e.g.
# random-string 16
function random-string {
cat /dev/urandom | tr -dc 'a-zA-Z0-9!@#$%^&*()_+-=><?[]{}' | fold -w ${1:-32} | head -n 1
}
# Ask a yes/no question for confirmation.
function read_yes_no {
printf "${IN_RED}[y|n] ${IN_DEF}";
read yesno;
while [[ "${yesno}" != "y" && "${yesno}" != "n" ]]
do
printf "${IN_RED}please answer [y|n] ${IN_DEF}";
read yesno;
done
if [[ "${yesno}" == "n" ]]; then
return 1;
else
return 0;
fi
}
# Prints a message if AskCI is not installed.
function alert_if_not_installed {
if ! is_installed; then
echo;
printf "${IN_GRY}.=----------------------------------------------=.\n";
printf "| AskCI CLI |\n";
printf ".=----------------------------------------------=.${IN_DEF}";
echo;
echo " Apparently you haven't installed AskCI";
echo " or your environment variables are tampered.";
echo " Please run './askci.sh' to get started.";
echo;
exit 1;
fi
}
# Prints a list of commands with their description.
function script_help {
printf "${IN_GRY}.=----------------------------------------------=.\n";
printf "| AskCI CLI |\n";
printf "| Available Instructions |\n";
printf ".=----------------------------------------------=.${IN_DEF}\n";
echo;
printf "${IN_RED}dev${IN_DEF}\n";
echo " Starts development containers."; echo;
printf "${IN_RED}prod${IN_DEF}\n";
echo " Starts production stack."; echo;
printf "${IN_RED}stop <prod|dev>${IN_DEF}\n";
echo " Stops containers."; echo;
printf "${IN_RED}rm <prod|dev>${IN_DEF}\n";
echo " Remove containers."; echo;
printf "${IN_RED}up <prod|dev>${IN_DEF}\n";
echo " Just brings up the containers. no building."; echo;
printf "${IN_RED}restart <prod|dev>${IN_DEF}\n";
echo " Restart containers."; echo;
printf "${IN_RED}logs <prod|dev>${IN_DEF}\n";
echo " View logs."; echo;
}
# Prompts user to input something with a message and an example
# and puts the result in the given variable.
#
# e.g.
# prompt_user "log storage path" "(e.g. /path/to/smthing)" OUTPUT
function prompt_user {
WHAT="$1";
EXAMPLE="$2";
OUTPUT_VAR="$3";
ASKED_VAR="";
while [ "$ASKED_VAR" == "" ]; do
read -p "Enter $WHAT $EXAMPLE: " -e ASKED_VAR;
if [[ "$ASKED_VAR" != "" ]]; then
printf "${IN_RED}Do you confirm $ASKED_VAR for $WHAT? ${IN_DEF}";
if ! read_yes_no; then
ASKED_VAR="";
fi
fi
done
export "$OUTPUT_VAR"="$ASKED_VAR";
echo;
}
# Returns state 0 if askci is installed else 1.
function is_installed {
if [ -f "$SCRIPTDIR/.env" ]; then
source "$SCRIPTDIR/.env";
if [[ "$INSTALLED_IN" =~ ^[0-9]{10,11}$ ]]; then
return 0;
fi
fi
return 1;
}
# Initializes required environment variables.
function setup {
if ! is_installed; then
if [ -f "$SCRIPTDIR/.env" ]; then
rm "$SCRIPTDIR/.env";
fi
touch "$SCRIPTDIR/.env";
printf "${IN_GRY}.=----------------------------------------------=.\n";
printf "| AskCI CLI |\n";
printf "| Welcome to the setup process for AskCI! |\n";
printf "| This won't take more than a minute. |\n";
printf ".=----------------------------------------------=.${IN_DEF}\n";
ask_for_envs;
echo "INSTALLED_IN=$(date +%s)" >> "$SCRIPTDIR/.env";
fi
}
# Generates a random string and puts in environment variables
# to be used in ASKCIango settings.
function put_secret_key {
echo "ASKCI_KEY='$(random-string 40)'" >> "$SCRIPTDIR/.env";
}
# Prompts user to enter required environment variables
# for askci to start.
function ask_for_envs {
echo;
echo "Asking for host details :->"; echo;
ask_for_hostname;
echo "Asking for server details:->";echo;
ask_for_configs;
printf "Generating a secret for you... ";
put_secret_key;
printf "done \xE2\x9C\x94 \n";
echo ">>> (We'are all set and ready to go.) <<<";
echo;
}
# Asks user for host IP address and domain name.
function ask_for_hostname {
prompt_user "your host IP address" "(e.g. 173.194.122.231)" ASKCIHIPADDR;
prompt_user "your host domain name" "(e.g. google.com)" ASKCIHDOMNN;
echo "DOMAIN_NAME=\"$ASKCIHDOMNN\"" >> "$SCRIPTDIR/.env";
echo "ASKCI_HOSTS=['$ASKCIHIPADDR','$ASKCIHDOMNN', 'www.$ASKCIHDOMNN']" >> "$SCRIPTDIR/.env";
}
# As user for variables that are put in configuration
function ask_for_configs {
prompt_user "a help contact email for your server" "(e.g., name@institution.edu)" HELP_CONTACT_EMAIL;
prompt_user "an institutional help or support site" "(e.g., https://srcc.stanford.edu)" HELP_INSTITUTION_SITE;
prompt_user "your institution or affiliation " "(e.g., Stanford University)" NODE_INSTITUTION;
prompt_user "a lowercase (no spaces) unique resource identifier for the server " "(e.g., askci-server)" NODE_URI;
prompt_user "the name of the server " "(e.g., AskCI)" NODE_NAME;
prompt_user "a Twitter account " "(e.g., askcyberinfra)" NODE_TWITTER;
echo "HELP_CONTACT_EMAIL=\"$HELP_CONTACT_EMAIL\"" >> "$SCRIPTDIR/.env";
echo "HELP_INSTITUTION_SITE=\"$HELP_INSTITUTION_SITE\"" >> "$SCRIPTDIR/.env";
echo "NODE_INSTITUTION=\"$NODE_INSTITUTION\"" >> "$SCRIPTDIR/.env";
echo "NODE_URI=\"$NODE_URI\"" >> "$SCRIPTDIR/.env";
echo "NODE_NAME=\"$NODE_NAME\"" >> "$SCRIPTDIR/.env";
echo "NODE_TWITTER=\"$NODE_TWITTER\"" >> "$SCRIPTDIR/.env";
}
# Starts development Docker containers.
function dev {
setup;
docker-compose build;
docker-compose up -d;
}
# Starts production stack.
function prod {
setup;
docker-compose -f https/docker-compose.yml build;
docker-compose -f https/docker-compose.yml up -d;
}
# Just brings up the containers. no building.
function bring_up {
setup;
if [[ "$1" == "dev" ]]; then
docker-compose up -d;
elif [[ "$1" == "prod" ]]; then
docker-compose -f https/docker-compose.yml up -d;
fi
}
# Run a compose command (logs, restart, stop, rm)
function compose_command {
level="${1}";
command="${2}";
shift; shift;
if [[ "${level}" == "dev" ]]; then
docker-compose "${command}" "${@}";
elif [[ "${level}" == "prod" ]]; then
docker-compose -f https/docker-compose.yml "${command}" "${@}";
fi
}
# Let's see what user wants.
if [[ "$1" == "dev" ]]; then
dev;
elif [[ "$1" == "prod" ]]; then
prod;
elif [[ "$1" == "up" ]]; then
if [[ "$2" == "prod" || "$2" == "dev" ]]; then
bring_up $2 $3;
else
echo "You should type in 'prod' or 'dev' after the up command.";
fi
elif [[ "$1" == "restart" || "$1" == "stop" || "$1" == "logs" || "$1" == "rm" ]]; then
if [[ "$2" == "prod" || "$2" == "dev" ]]; then
command="${1}"
level="${2}"
shift; shift;
compose_command "${level}" "${command}" "${@}";
else
echo "You should type in 'prod' or 'dev' after the command.";
fi
else
script_help;
fi