55# This script automates the process of building and running the Docker container
66# with version information dynamically injected at build time.
77
8- # Hidden feature: Preserve usage statistics across rebuilds
9- # Usage: ./docker-build.sh --with-usage
10- # First run prompts for management API key, saved to temp/stats/.api_secret
11-
128set -euo pipefail
139
14- STATS_DIR=" temp/stats"
15- STATS_FILE=" ${STATS_DIR} /.usage_backup.json"
16- SECRET_FILE=" ${STATS_DIR} /.api_secret"
17- WITH_USAGE=false
18-
19- get_port () {
20- if [[ -f " config.yaml" ]]; then
21- grep -E " ^port:" config.yaml | sed -E ' s/^port: *["' " '" ' ]?([0-9]+)["' " '" ' ]?.*$/\1/'
22- else
23- echo " 8317"
24- fi
25- }
26-
27- export_stats_api_secret () {
28- if [[ -f " ${SECRET_FILE} " ]]; then
29- API_SECRET=$( cat " ${SECRET_FILE} " )
30- else
31- if [[ ! -d " ${STATS_DIR} " ]]; then
32- mkdir -p " ${STATS_DIR} "
33- fi
34- echo " First time using --with-usage. Management API key required."
35- read -r -p " Enter management key: " -s API_SECRET
36- echo
37- echo " ${API_SECRET} " > " ${SECRET_FILE} "
38- chmod 600 " ${SECRET_FILE} "
39- fi
40- }
41-
42- check_container_running () {
43- local port
44- port=$( get_port)
45-
46- if ! curl -s -o /dev/null -w " %{http_code}" " http://localhost:${port} /" | grep -q " 200" ; then
47- echo " Error: cli-proxy-api service is not responding at localhost:${port} "
48- echo " Please start the container first or use without --with-usage flag."
49- exit 1
50- fi
51- }
52-
53- export_stats () {
54- local port
55- port=$( get_port)
56-
57- if [[ ! -d " ${STATS_DIR} " ]]; then
58- mkdir -p " ${STATS_DIR} "
59- fi
60- check_container_running
61- echo " Exporting usage statistics..."
62- EXPORT_RESPONSE=$( curl -s -w " \n%{http_code}" -H " X-Management-Key: ${API_SECRET} " \
63- " http://localhost:${port} /v0/management/usage/export" )
64- HTTP_CODE=$( echo " ${EXPORT_RESPONSE} " | tail -n1)
65- RESPONSE_BODY=$( echo " ${EXPORT_RESPONSE} " | sed ' $d' )
66-
67- if [[ " ${HTTP_CODE} " != " 200" ]]; then
68- echo " Export failed (HTTP ${HTTP_CODE} ): ${RESPONSE_BODY} "
69- exit 1
70- fi
71-
72- echo " ${RESPONSE_BODY} " > " ${STATS_FILE} "
73- echo " Statistics exported to ${STATS_FILE} "
74- }
75-
76- import_stats () {
77- local port
78- port=$( get_port)
79-
80- echo " Importing usage statistics..."
81- IMPORT_RESPONSE=$( curl -s -w " \n%{http_code}" -X POST \
82- -H " X-Management-Key: ${API_SECRET} " \
83- -H " Content-Type: application/json" \
84- -d @" ${STATS_FILE} " \
85- " http://localhost:${port} /v0/management/usage/import" )
86- IMPORT_CODE=$( echo " ${IMPORT_RESPONSE} " | tail -n1)
87- IMPORT_BODY=$( echo " ${IMPORT_RESPONSE} " | sed ' $d' )
88-
89- if [[ " ${IMPORT_CODE} " == " 200" ]]; then
90- echo " Statistics imported successfully"
91- else
92- echo " Import failed (HTTP ${IMPORT_CODE} ): ${IMPORT_BODY} "
93- fi
94-
95- rm -f " ${STATS_FILE} "
96- }
97-
98- wait_for_service () {
99- local port
100- port=$( get_port)
101-
102- echo " Waiting for service to be ready..."
103- for i in {1..30}; do
104- if curl -s -o /dev/null -w " %{http_code}" " http://localhost:${port} /" | grep -q " 200" ; then
105- break
106- fi
107- sleep 1
108- done
109- sleep 2
110- }
111-
112- case " ${1:- } " in
113- " " )
114- ;;
115- " --with-usage" )
116- WITH_USAGE=true
117- export_stats_api_secret
118- ;;
119- * )
120- echo " Error: unknown option '${1} '. Did you mean '--with-usage'?"
121- echo " Usage: ./docker-build.sh [--with-usage]"
122- exit 1
123- ;;
124- esac
10+ if [[ " ${1:- } " != " " ]]; then
11+ echo " Error: unknown option '${1} '."
12+ echo " Usage: ./docker-build.sh"
13+ exit 1
14+ fi
12515
12616# --- Step 1: Choose Environment ---
12717echo " Please select an option:"
@@ -133,14 +23,7 @@ read -r -p "Enter choice [1-2]: " choice
13323case " $choice " in
13424 1)
13525 echo " --- Running with Pre-built Image ---"
136- if [[ " ${WITH_USAGE} " == " true" ]]; then
137- export_stats
138- fi
13926 docker compose up -d --remove-orphans --no-build
140- if [[ " ${WITH_USAGE} " == " true" ]]; then
141- wait_for_service
142- import_stats
143- fi
14427 echo " Services are starting from remote image."
14528 echo " Run 'docker compose logs -f' to see the logs."
14629 ;;
@@ -167,18 +50,9 @@ case "$choice" in
16750 --build-arg COMMIT=" ${COMMIT} " \
16851 --build-arg BUILD_DATE=" ${BUILD_DATE} "
16952
170- if [[ " ${WITH_USAGE} " == " true" ]]; then
171- export_stats
172- fi
173-
17453 echo " Starting the services..."
17554 docker compose up -d --remove-orphans --pull never
17655
177- if [[ " ${WITH_USAGE} " == " true" ]]; then
178- wait_for_service
179- import_stats
180- fi
181-
18256 echo " Build complete. Services are starting."
18357 echo " Run 'docker compose logs -f' to see the logs."
18458 ;;
0 commit comments