Skip to content

Commit e0724ab

Browse files
authored
Create config-wizard.sh
Added support for "sudo" and improved error handling.
1 parent 9a0bec3 commit e0724ab

1 file changed

Lines changed: 268 additions & 0 deletions

File tree

config-wizard.sh

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
#!/bin/bash
2+
3+
echo
4+
echo "APPLICATION STUDY TOOL CONFIGURATION WIZARD"
5+
echo "Note: This script is meant to be run only at initial installation time. If you need to make changes afterwards or you make an error while inputting the required values, you will need to re-run the script and enter everything all over again. Alternatively, to just make one-off edits, you can manually edit the config files after the script exits."
6+
7+
# THE FOLLOWING STEPS ARE PREREQS FOR THIS SCRIPT:
8+
# (Install Docker)
9+
# git clone https://github.com/f5devcentral/application-study-tool.git
10+
# cd application-study-tool
11+
# chmod +x config-wizard.sh
12+
# Then the user can run this setup wizard by running "./config-wizard.sh".
13+
14+
# Check if .env file, .env.device-secrets, and config directory do not exist. If they don't, but the example files exist, copy the example files to the actual files.
15+
if [ ! -f "./.env" ]; then
16+
if [ -f "./.env-example" ]; then
17+
cp .env-example .env
18+
else # Neither file exists
19+
echo "Error: neither .env nor .env-example file exists in current directory. Exiting script."
20+
exit 1
21+
fi
22+
fi
23+
if [ ! -f "./.env.device-secrets" ]; then
24+
if [ -f "./.env.device-secrets-example" ]; then
25+
cp .env.device-secrets-example .env.device-secrets
26+
else # Neither file exists
27+
echo "Error: neither .env.device-secrets-example nor .env.device-secrets file exists in current directory. Exiting script."
28+
exit 1
29+
fi
30+
fi
31+
if [ ! -d "./config" ]; then
32+
echo "Error: ./config directory does not exist in current directory. Exiting script."
33+
exit 1
34+
fi
35+
36+
# Set up some defaults for script:
37+
DEFAULT_USER=admin
38+
DEFAULT_PASS=admin
39+
40+
# SET UP GRAFANA CREDENTIALS
41+
echo "Setting up Grafana dashboard credentials."
42+
echo "Enter desired Grafana username (or press ENTER to leave as default): "
43+
read GF_ADMIN_USER
44+
if [ -n "$GF_ADMIN_USER" ]; then # not empty
45+
stty -echo
46+
echo "Enter desired Grafana password (or press ENTER to leave as the default): "
47+
read GF_ADMIN_PASS
48+
stty echo
49+
echo
50+
GF_ADMIN_PASS="${GF_ADMIN_PASS:-$DEFAULT_PASS}"
51+
52+
# Update .env file
53+
sed -i -e s/^GF_SECURITY_ADMIN_USER/#GF_SECURITY_ADMIN_USER/g ./.env
54+
sed -i -e s/^GF_SECURITY_ADMIN_PASSWORD/#GF_SECURITY_ADMIN_PASSWORD/g ./.env
55+
echo >> .env
56+
echo "GF_SECURITY_ADMIN_USER=$GF_ADMIN_USER" >> .env
57+
echo "GF_SECURITY_ADMIN_PASSWORD=$GF_ADMIN_PASS" >> .env
58+
fi
59+
60+
# TODO: Do the same for SENSOR_ID and SENSOR_SECRET_TOKEN in .env
61+
62+
# CONFIGURE GLOBAL BIG-IP ACCESS
63+
# First, set global default credentials in config/ast_defaults.yaml
64+
echo "Setting up default global BIG-IP credentials."
65+
66+
echo "Enter default global username for BIG-IP (or press ENTER to leave as default: admin): "
67+
read BIGIP_ADMIN_USER
68+
stty -echo
69+
echo "Enter default BIG-IP password (or press ENTER to leave as default): "
70+
read BIGIP_ADMIN_PASS
71+
stty echo
72+
echo
73+
74+
if [ -n "$BIGIP_ADMIN_USER" ]; then # not empty
75+
# Update config/ast_defaults.yaml
76+
sed -i -e s/"username\:"/"username\: $BIGIP_ADMIN_USER #"/1 ./config/ast_defaults.yaml
77+
fi
78+
79+
if [ -n "$BIGIP_ADMIN_PASS" ]; then # not empty
80+
# Update config/ast_defaults.yaml
81+
sed -i -e s/"password\:"/"password\: $BIGIP_ADMIN_PASS #"/1 ./config/ast_defaults.yaml
82+
fi
83+
84+
# Ask user whether to validate certificates (skip when using self-signed certs)
85+
echo "Skip certificate verification (use self-signed certs) or verify CA certificates (requires CA file name)? "
86+
echo "Enter Y to use self-signed certificates, or N to verify the certificates (or press Enter to leave as default): "
87+
read DONT_VERIFY_CERTS
88+
89+
if [ -n "$DONT_VERIFY_CERTS" ]; then # not empty
90+
if [[ "$DONT_VERIFY_CERTS" == Y* ]] || [[ "$DONT_VERIFY_CERTS" == y* ]]; then
91+
# Don't verify certificates
92+
sed -i -e s/"insecure_skip_verify\:"/"insecure_skip_verify\: true #"/1 ./config/ast_defaults.yaml
93+
sed -i -e s/"ca_file\:"/"ca_file\: \"\" #"/1 ./config/ast_defaults.yaml
94+
else
95+
# Yes, verify certificates
96+
echo "Enter the full pathname of the CA file: "
97+
read CA_FILE_PATH
98+
sed -i -e s/"insecure_skip_verify\:"/"insecure_skip_verify\: false #"/1 ./config/ast_defaults.yaml
99+
if [ -n "$CA_FILE_PATH" ]; then # not empty
100+
sed -i -e s~"ca_file\:"~"ca_file\: \"$CA_FILE_PATH\" #"~1 ./config/ast_defaults.yaml
101+
fi
102+
fi
103+
fi
104+
105+
# DO WE NEED THIS BLOCK?
106+
# CONFIGURE INDIVIDUAL BIG-IP ACCESS
107+
# Re-check that this file still exists in the current directory
108+
if [ ! -f "./.env.device-secrets" ]; then
109+
exit 1
110+
fi
111+
112+
echo "Enter the first BIG-IP management IP address (or press Enter to leave unchanged): "
113+
read BIG_IP_ADDR
114+
115+
# Validate IPv4 IP address format
116+
while [[ -n "$BIG_IP_ADDR" ]] && ! [[ "${BIG_IP_ADDR}" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; do
117+
echo "Invalid IP address. Enter the first BIG-IP management IPv4 address in a.b.c.d format (or press Enter to stop adding devices): "
118+
read BIG_IP_ADDR
119+
done;
120+
121+
if [ -n "$BIG_IP_ADDR" ]; then # not empty
122+
# if file already exists, rename it and create a new file
123+
if [ -f "./config/bigip_receivers.yaml" ]; then
124+
mv ./config/bigip_receivers.yaml ./config/bigip_receivers.yaml.old
125+
fi
126+
touch ./config/bigip_receivers.yaml
127+
echo "# Your bigip targets" >> ./config/bigip_receivers.yaml
128+
fi
129+
BIG_IP_NUM=1
130+
131+
while [ -n "$BIG_IP_ADDR" ]; do # while not empty
132+
echo "CONFIGURING BIG-IP $BIG_IP_NUM ($BIG_IP_ADDR)..."
133+
echo "Enter this BIG-IP's admin username (press Enter to use global default username and password): "
134+
read BIGIP_LOCAL_USER
135+
if [ -n "$BIGIP_LOCAL_USER" ]; then # not empty
136+
stty -echo
137+
echo "Enter this BIG-IP's admin password (press Enter to use global default admin password): "
138+
read BIGIP_LOCAL_PASS
139+
stty echo
140+
echo
141+
fi
142+
echo "bigip/$BIG_IP_NUM:" >> ./config/bigip_receivers.yaml
143+
if [[ "$BIG_IP_ADDR" == http* ]]; then
144+
echo " endpoint: $BIG_IP_ADDR" >> ./config/bigip_receivers.yaml
145+
else
146+
echo " endpoint: https://$BIG_IP_ADDR" >> ./config/bigip_receivers.yaml
147+
fi
148+
if [ -n "$BIGIP_LOCAL_USER" ]; then
149+
echo " username: $BIGIP_LOCAL_USER" >> ./config/bigip_receivers.yaml
150+
if [ -n "$BIGIP_LOCAL_PASS" ]; then
151+
echo " password: $BIGIP_LOCAL_PASS" >> ./config/bigip_receivers.yaml
152+
fi
153+
fi
154+
echo "Enter the next BIG-IP management IP address (or press Enter to stop adding devices): "
155+
read BIG_IP_ADDR
156+
157+
# Validate IPv4 IP address format
158+
while [[ -n "$BIG_IP_ADDR" ]] && ! [[ "${BIG_IP_ADDR}" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; do
159+
echo "Invalid IP address. Enter the first BIG-IP management IPv4 address in a.b.c.d format (or press Enter to stop adding devices): "
160+
read BIG_IP_ADDR
161+
done;
162+
163+
164+
BIG_IP_NUM=$(($BIG_IP_NUM+1))
165+
done
166+
167+
# File configuration is complete. Now prompt the user to run the Configuration Generator container.
168+
169+
# CHECK FOR CONTAINER RUNTIME TOOLS AND PROMPT USER TO RUN THEM.
170+
CONTAINER_RUNTIME="#"
171+
echo "Checking for installation of Docker..."
172+
DOCKER=`command -v docker`
173+
if [ -n "$DOCKER" ]; then # not empty
174+
echo "Docker is installed."
175+
CONTAINER_RUNTIME="docker"
176+
else
177+
PODMAN=`command -v podman`
178+
if [ -n "$PODMAN" ]; then # not empty
179+
echo "Podman is installed."
180+
CONTAINER_RUNTIME="podman"
181+
else
182+
echo "Neither Docker nor Podman are installed. Please install one of these tools before continuing."
183+
exit 1
184+
fi
185+
fi
186+
187+
# If we got to this point, either Docker or Podman is installed on the sytem.
188+
echo "Would you like to run the configuration generator now (y/n)?"
189+
read RUN_CONFIG_GEN
190+
if [ -n "$RUN_CONFIG_GEN" ]; then # not empty
191+
if [[ "$RUN_CONFIG_GEN" == Y* ]] || [[ "$RUN_CONFIG_GEN" == y* ]]; then
192+
# Ask user if sudo is required before docker/podman command
193+
echo "Do you require 'sudo' to run ${CONTAINER_RUNTIME}? (If you are unsure, choose 'y'.) (y/n)"
194+
read USER_WANTS_SUDO
195+
if [[ "$USER_WANTS_SUDO" == Y* ]] || [[ "$USER_WANTS_SUDO" == y* ]]; then
196+
SUDO_REQUIRED=sudo
197+
else SUDO_REQUIRED=""
198+
fi
199+
# Quick check to see if docker/podman will run successfully.
200+
$SUDO_REQUIRED $CONTAINER_RUNTIME version > /dev/null
201+
if ! [[ "$?" == 0 ]]; then
202+
echo
203+
echo "$CONTAINER_RUNTIME failed. Check the permissions or try running again with 'sudo'."
204+
exit 1
205+
else
206+
# Initial check passed, so try to run the Config Generator.
207+
$SUDO_REQUIRED $CONTAINER_RUNTIME run --rm -it -w /app -v ${PWD}:/app --entrypoint /app/src/bin/init_entrypoint.sh python:3.12.6-slim-bookworm --generate-config
208+
if ! [[ "$?" == 0 ]]; then
209+
echo
210+
echo "$CONTAINER_RUNTIME failed. Check the permissions or try running again with 'sudo'."
211+
exit 1
212+
fi
213+
fi
214+
else
215+
echo "Configuration files have been created. The next step is to run the configuration generator with the following command (sudo may be required depending on your permissions):"
216+
echo " \$ $CONTAINER_RUNTIME run --rm -it -w /app -v ${PWD}:/app --entrypoint /app/src/bin/init_entrypoint.sh python:3.12.6-slim-bookworm --generate-config"
217+
exit 1
218+
fi
219+
fi
220+
echo
221+
echo "Configuration complete."
222+
echo
223+
224+
COMPOSE_TOOL="#"
225+
echo "Checking for installation of Docker Compose..."
226+
DOCKER_COMPOSE=`command -v docker-compose`
227+
228+
if [ -n "$DOCKER_COMPOSE" ]; then # not empty
229+
echo "Docker Compose is installed."
230+
COMPOSE_TOOL="docker-compose"
231+
else # Docker Compose is not installed. Let's check Podman Compose.
232+
PODMAN_COMPOSE=`command -v podman-compose`
233+
if [ -n "$PODMAN_COMPOSE" ]; then # not empty
234+
echo "Podman is installed."
235+
COMPOSE_TOOL="podman-compose"
236+
else # neither is installed
237+
echo "Neither Docker Compose nor Podman Compose are installed. Please install one of these tools in order to start the service."
238+
echo "Then run '$SUDO_REQUIRED $COMPOSE_TOOL up'"
239+
exit 1
240+
fi
241+
fi
242+
243+
# If we got to this point, either Docker Compose or Podman Compose is installed on the sytem.
244+
echo "Would you like to start the sevice now (y/n)?"
245+
read RUN_SERVICE
246+
if [ -n "$RUN_SERVICE" ]; then # not empty
247+
if [[ "$RUN_SERVICE" == Y* ]] || [[ "$RUN_SERVICE" == y* ]]; then
248+
# Quick check to ensure docker-compose file is present
249+
if [ -f "./docker-compose.yaml" ]; then
250+
# docker-compose up
251+
$SUDO_REQUIRED $COMPOSE_TOOL up
252+
if ! [[ "$?" == 0 ]]; then
253+
echo
254+
echo "$COMPOSE_TOOL failed. Check the permissions or try running again with 'sudo'."
255+
exit 1
256+
fi
257+
else
258+
echo "Error: docker-compose.yaml file does not exist in current directory. Cannot start docker compose service."
259+
exit 1
260+
fi
261+
else
262+
echo "Configuration is complete. The next step is to run the compose tool to start the service, using the following command:"
263+
echo " \$ $SUDO_REQUIRED $COMPOSE_TOOL up"
264+
fi
265+
else
266+
echo "Configuration is complete. The next step is to run the compose tool to start the service, using the following command:"
267+
echo " \$ $SUDO_REQUIRED $COMPOSE_TOOL up"
268+
fi

0 commit comments

Comments
 (0)