1+ #! /bin/bash
2+
3+ # This script runs on the host machine BEFORE the container is created.
4+ # It is cross-platform compatible (Windows with Git Bash, Linux, macOS)
5+
6+ set -e
7+
8+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
9+ REPO_ROOT=" $( cd " $SCRIPT_DIR /.." && pwd) "
10+
11+ TEMPLATE_FILE=" $REPO_ROOT /.env.template"
12+ OUTPUT_FILE=" $REPO_ROOT /.env"
13+ KONG_FILE=" $REPO_ROOT /kong.yaml"
14+ CYPRESS_CONFIG=" $REPO_ROOT /sample-web-ui/cypress.config.ts"
15+ DOCKER_COMPOSE_FILE=" $REPO_ROOT /docker-compose.yml"
16+ README_FILE=" $REPO_ROOT /Readme.md"
17+ REPO_TYPE=" "
18+
19+ # Function to generate random 32-character string (cross-platform)
20+ generate_random_string () {
21+ if command -v openssl & > /dev/null; then
22+ openssl rand -base64 32 | tr -dc ' a-zA-Z0-9' | cut -c1-32
23+ elif [[ -c /dev/urandom ]]; then
24+ tr -dc ' a-zA-Z0-9' < /dev/urandom | head -c 32
25+ elif command -v python3 & > /dev/null; then
26+ python3 -c " import random; import string; print(''.join(random.choices(string.ascii_letters + string.digits, k=32)))"
27+ else
28+ # Fallback: use timestamp + random
29+ date +%s%N | sha256sum | tr -dc ' a-zA-Z0-9' | head -c 32
30+ fi
31+ }
32+
33+ # Function to get the host IP address (cross-platform)
34+ get_host_ip () {
35+ if command -v hostname & > /dev/null && hostname -I & > /dev/null 2>&1 ; then
36+ # Linux
37+ hostname -I | awk ' {print $1}'
38+ elif [[ " $OSTYPE " == " darwin" * ]]; then
39+ # macOS
40+ ipconfig getifaddr en0 2> /dev/null || echo " localhost"
41+ else
42+ # Windows (Git Bash) or fallback
43+ echo " localhost"
44+ fi
45+ }
46+
47+ # Cross-platform sed replacement (handles both GNU sed and BSD sed)
48+ sed_replace () {
49+ local file=" $1 "
50+ local pattern=" $2 "
51+ local replacement=" $3 "
52+
53+ if [[ " $OSTYPE " == " darwin" * ]]; then
54+ # macOS uses BSD sed
55+ sed -i ' ' " s|$pattern |$replacement |g" " $file "
56+ else
57+ # Linux and others use GNU sed
58+ sed -i " s|$pattern |$replacement |g" " $file "
59+ fi
60+ }
61+
62+ # Function to validate repository
63+ validate_repository () {
64+ if [ -f " $README_FILE " ] && grep -q " Device Management Toolkit (formerly known as Open AMT Cloud Toolkit)" " $README_FILE " ; then
65+ REPO_TYPE=" DMT"
66+ echo " ✓ Detected: Device Management Toolkit repository"
67+ return 0
68+ fi
69+
70+ echo " ✗ Error: Unrecognized repository. This script must be run from the Device Management Toolkit repository."
71+ exit 1
72+ }
73+
74+ # Function to handle DMT-specific operations
75+ dmt_operations () {
76+ echo " =========================================="
77+ echo " Environment Configuration Setup"
78+ echo " =========================================="
79+
80+ dmt_generate_defaults
81+ dmt_populate_env_file
82+ dmt_update_kong_config
83+ dmt_update_cypress_config
84+ dmt_update_docker_compose
85+
86+ echo " =========================================="
87+ echo " Configuration complete!"
88+ echo " =========================================="
89+ }
90+
91+ # Function to generate default values for DMT
92+ dmt_generate_defaults () {
93+ DEFAULT_MPS_COMMON_NAME=$( get_host_ip) # Automatically detected system IP address
94+ DEFAULT_MPS_WEB_ADMIN_USER=" hspeoob" # Default admin username for MPS web interface
95+ DEFAULT_MPS_WEB_ADMIN_PASSWORD=" Apple-1234" # Default admin password for MPS web interface
96+ DEFAULT_MPS_JWT_SECRET=$( generate_random_string) # Randomly generated 32-char JWT secret
97+ DEFAULT_MPS_JWT_ISSUER=$( generate_random_string) # Randomly generated 32-char JWT issuer key
98+ DEFAULT_POSTGRES_PASSWORD=" Apple-1234" # Default PostgreSQL database password
99+ DEFAULT_VAULT_TOKEN=$( generate_random_string) # Randomly generated 32-char Vault token
100+ }
101+
102+ # Function to populate .env file for DMT
103+ dmt_populate_env_file () {
104+ if [ -f " $OUTPUT_FILE " ] && grep -qE " ^MPS_JWT_SECRET=.+$" " $OUTPUT_FILE " ; then
105+ echo " ✓ Existing .env with MPS_JWT_SECRET found; skipping .env generation."
106+ return 0
107+ fi
108+
109+ echo " Creating and populating .env file..."
110+ cp " $TEMPLATE_FILE " " $OUTPUT_FILE "
111+
112+ sed_replace " $OUTPUT_FILE " " ^MPS_COMMON_NAME=.*" " MPS_COMMON_NAME=$DEFAULT_MPS_COMMON_NAME "
113+ sed_replace " $OUTPUT_FILE " " ^MPS_WEB_ADMIN_USER=.*" " MPS_WEB_ADMIN_USER=$DEFAULT_MPS_WEB_ADMIN_USER "
114+ sed_replace " $OUTPUT_FILE " " ^MPS_WEB_ADMIN_PASSWORD=.*" " MPS_WEB_ADMIN_PASSWORD=$DEFAULT_MPS_WEB_ADMIN_PASSWORD "
115+ sed_replace " $OUTPUT_FILE " " ^MPS_JWT_SECRET=.*" " MPS_JWT_SECRET=$DEFAULT_MPS_JWT_SECRET "
116+ sed_replace " $OUTPUT_FILE " " ^MPS_JWT_ISSUER=.*" " MPS_JWT_ISSUER=$DEFAULT_MPS_JWT_ISSUER "
117+ sed_replace " $OUTPUT_FILE " " ^POSTGRES_PASSWORD=.*" " POSTGRES_PASSWORD=$DEFAULT_POSTGRES_PASSWORD "
118+ sed_replace " $OUTPUT_FILE " " ^VAULT_TOKEN=.*" " VAULT_TOKEN=$DEFAULT_VAULT_TOKEN "
119+
120+ echo " ✓ .env file has been created and populated with default values."
121+ }
122+
123+ # Function to update kong.yaml for DMT
124+ dmt_update_kong_config () {
125+ if [ -f " $KONG_FILE " ]; then
126+ # Check if MPS_JWT_SECRET in .env is empty
127+ if grep -qE " ^MPS_JWT_SECRET=.+$" " $OUTPUT_FILE " ; then
128+ echo " Updating kong.yaml with JWT secrets..."
129+ # Read the actual values from .env file
130+ ACTUAL_JWT_SECRET=$( grep " ^MPS_JWT_SECRET=" " $OUTPUT_FILE " | cut -d' =' -f2)
131+ ACTUAL_JWT_ISSUER=$( grep " ^MPS_JWT_ISSUER=" " $OUTPUT_FILE " | cut -d' =' -f2)
132+
133+ sed_replace " $KONG_FILE " " key: [a-zA-Z0-9]* #sample key" " key: $ACTUAL_JWT_ISSUER #sample key"
134+ sed_replace " $KONG_FILE " " secret:.*" " secret: \" $ACTUAL_JWT_SECRET \" "
135+ echo " ✓ kong.yaml has been updated with JWT secrets."
136+ else
137+ echo " ⚠ Warning: MPS_JWT_SECRET in .env is empty, skipping Kong configuration."
138+ fi
139+ else
140+ echo " ⚠ Warning: kong.yaml not found, skipping Kong configuration."
141+ fi
142+ }
143+
144+ # Function to update cypress.config.ts for DMT
145+ dmt_update_cypress_config () {
146+ if [ -f " $CYPRESS_CONFIG " ]; then
147+ echo " Updating cypress.config.ts with credentials..."
148+ sed_replace " $CYPRESS_CONFIG " " MPS_USERNAME: '.*'" " MPS_USERNAME: '$DEFAULT_MPS_WEB_ADMIN_USER '"
149+ sed_replace " $CYPRESS_CONFIG " " MPS_PASSWORD: '.*'" " MPS_PASSWORD: '$DEFAULT_MPS_WEB_ADMIN_PASSWORD '"
150+ sed_replace " $CYPRESS_CONFIG " " VAULT_TOKEN: '.*'" " VAULT_TOKEN: '$DEFAULT_VAULT_TOKEN '"
151+ echo " ✓ cypress.config.ts has been updated with credentials."
152+ else
153+ echo " ⚠ Warning: cypress.config.ts not found, skipping Cypress configuration."
154+ fi
155+ }
156+
157+ # Function to update docker-compose.yml with proxy and workspace folder
158+ dmt_update_docker_compose () {
159+ if [ -f " $DOCKER_COMPOSE_FILE " ]; then
160+ echo " Updating docker-compose.yml with workspace folder variable..."
161+
162+ # Replace relative volume paths with ${LOCAL_WORKSPACE_FOLDER:-.} literal variable reference
163+ sed_replace " $DOCKER_COMPOSE_FILE " " - \\ ./nginx\\ .conf:" " - \$ {LOCAL_WORKSPACE_FOLDER:-.}/nginx.conf:"
164+ sed_replace " $DOCKER_COMPOSE_FILE " " - \\ ./data:" " - \$ {LOCAL_WORKSPACE_FOLDER:-.}/data:"
165+ sed_replace " $DOCKER_COMPOSE_FILE " " - \\ ./kong\\ .yaml:" " - \$ {LOCAL_WORKSPACE_FOLDER:-.}/kong.yaml:"
166+ sed_replace " $DOCKER_COMPOSE_FILE " " - \\ ./mosquitto\\ .conf:" " - \$ {LOCAL_WORKSPACE_FOLDER:-.}/mosquitto.conf:"
167+ echo " ✓ Volume paths updated with \$ {LOCAL_WORKSPACE_FOLDER:-.}"
168+
169+ # Update proxy environment variables from host system
170+ HTTP_PROXY=" ${HTTP_PROXY:- } "
171+ HTTPS_PROXY=" ${HTTPS_PROXY:- } "
172+ NO_PROXY=" ${NO_PROXY:- } "
173+ http_proxy=" ${http_proxy:- } "
174+ https_proxy=" ${https_proxy:- } "
175+ no_proxy=" ${no_proxy:- } "
176+
177+ if [ -n " $HTTP_PROXY " ] || [ -n " $HTTPS_PROXY " ] || [ -n " $NO_PROXY " ]; then
178+ echo " Configuring HTTP proxy: $HTTP_PROXY "
179+ echo " Configuring HTTPS proxy: $HTTPS_PROXY "
180+ echo " Configuring NO_PROXY: $NO_PROXY "
181+ fi
182+
183+ # Note: The proxy settings are already parameterized in docker-compose.yml
184+ # They will be picked up from the environment when docker-compose runs
185+ echo " ✓ docker-compose.yml has been updated with workspace folder and proxy settings."
186+ else
187+ echo " ⚠ Warning: docker-compose.yml not found, skipping Docker Compose configuration."
188+ fi
189+ }
190+
191+ # Main execution
192+ main () {
193+ validate_repository
194+
195+ if [ " $REPO_TYPE " = " DMT" ]; then
196+ dmt_operations
197+ fi
198+ }
199+
200+ # Run main function
201+ main
0 commit comments