1- # TODO: write install script
1+ #! /bin/bash
2+ set -eu
3+
4+ exists () {
5+ command -v " $1 " 1> /dev/null 2>&1
6+ }
7+
8+ check_dependencies () {
9+ if ! exists curl; then
10+ echo " curl is not installed."
11+ exit 1
12+ fi
13+
14+ if ! exists docker; then
15+ echo " docker is not installed."
16+ exit 1
17+ fi
18+
19+ if ! exists docker compose; then
20+ echo " docker compose is not installed."
21+ exit 1
22+ fi
23+ }
24+
25+ download () {
26+ curl --fail --silent --location --output " $2 " " $1 "
27+ }
28+
29+ generate_variables () {
30+ auth_secret=$( tr -dc A-Za-z0-9 < /dev/urandom | head -c 64 ; echo ' ' )
31+ crypted_data_secret=$( openssl rand -hex 32)
32+ app_db_host=slashbase-db
33+ app_db_port=5432
34+ app_db_user=$postgres_root_user
35+ app_db_pass=$postgres_root_password
36+ app_db_name=$postgres_root_user
37+ }
38+
39+ get_containers () {
40+ echo " fetching docker-compose.yml from slashbase repo"
41+ download https://raw.githubusercontent.com/slashbaseide/slashbase/main/deploy/docker-compose.yml docker-compose.yml
42+ }
43+
44+ generate_docker_env_file () {
45+ touch docker.env & cat << EOF > docker.env
46+ POSTGRES_DB=\$ {postgres_root_user}
47+ POSTGRES_USER=\$ {postgres_root_user}
48+ POSTGRES_PASSWORD=\$ {postgres_root_password}
49+ EOF
50+
51+ rm -f replace.sed
52+ touch replace.sed
53+
54+ variables=( " postgres_root_user"
55+ $postgres_root_user
56+ " postgres_root_password"
57+ $postgres_root_password
58+ )
59+
60+ flip=0
61+ for i in " ${variables[@]} " ; do
62+ if [ $flip -eq 0 ]; then
63+ printf " s/\$ {$i }/" >> replace.sed
64+ flip=1
65+ else
66+ printf " $i /\n" >> replace.sed
67+ flip=0
68+ fi
69+ done
70+
71+ sed -f replace.sed docker.env > .env
72+ rm -f replace.sed docker.env
73+ }
74+
75+ generate_app_config_file () {
76+ echo " fetching env file from slashbase repo"
77+ download https://raw.githubusercontent.com/slashbaseide/slashbase/main/deploy/server.env.sample server.env.sample
78+
79+ rm -f replace.sed
80+ touch replace.sed
81+
82+ variables=( " auth_secret"
83+ $auth_secret
84+ " crypted_data_secret"
85+ $crypted_data_secret
86+ " slashbase_root_email"
87+ $slashbase_root_email
88+ " slashbase_root_password"
89+ $slashbase_root_password
90+ " app_db_host"
91+ $app_db_host
92+ " app_db_port"
93+ $app_db_port
94+ " app_db_user"
95+ $app_db_user
96+ " app_db_pass"
97+ $app_db_pass
98+ " app_db_name"
99+ $app_db_name
100+ )
101+
102+ flip=0
103+ for i in " ${variables[@]} " ; do
104+ if [ $flip -eq 0 ]; then
105+ printf " s/\$ {$i }/" >> replace.sed
106+ flip=1
107+ else
108+ printf " $i /\n" >> replace.sed
109+ flip=0
110+ fi
111+ done
112+
113+ sed -f replace.sed server.env.sample > app.env
114+ rm -f replace.sed server.env.sample
115+ }
116+
117+ wait_for_containers_start () {
118+ local timeout=$1
119+
120+ # The while loop is important because for-loops don't work for dynamic values
121+ while [[ $timeout -gt 0 ]]; do
122+ status_code=" $( curl -s -o /dev/null -w " %{http_code}" http://localhost:3000/api/v1/health || true) "
123+ if [[ status_code -eq 401 ]]; then
124+ break
125+ else
126+ echo -ne " Waiting for all containers to start. This check will timeout in $timeout seconds...\r\c"
127+ fi
128+ (( timeout-- ))
129+ sleep 1
130+ done
131+
132+ echo " "
133+ }
134+
135+ # This function prompts the user for an input for a non-empty slashbase root user email.
136+ read_rootuser_email () {
137+ read -rp ' Set the slashbase root user email (admin user): ' slashbase_root_email
138+ while [[ -z $slashbase_root_email ]]; do
139+ echo " "
140+ echo " "
141+ echo " +++++++++++ ERROR ++++++++++++++++++++++"
142+ echo " The slashbase user email cannot be empty. Please input a valid slashbase user email string."
143+ echo " ++++++++++++++++++++++++++++++++++++++++"
144+ echo " "
145+ read -rp ' Set the slashbase root user email (admin user): ' slashbase_root_email
146+ done
147+ }
148+
149+ # This function prompts the user for an input for a non-empty slashbase root user password.
150+ read_rootuser_password () {
151+ read -srp ' Set the slashbase root user password (admin user): ' slashbase_root_password
152+ while [[ -z $slashbase_root_password ]]; do
153+ echo " "
154+ echo " "
155+ echo " +++++++++++ ERROR ++++++++++++++++++++++"
156+ echo " The slashbase user password cannot be empty. Please input a valid slashbase user password string."
157+ echo " ++++++++++++++++++++++++++++++++++++++++"
158+ echo " "
159+ read -srp ' Set the slashbase root user password (admin user): ' slashbase_root_password
160+ done
161+ echo " "
162+ }
163+
164+ # This function prompts the user for an input for a non-empty postgres username.
165+ read_postgres_username () {
166+ read -rp ' Set the postgres root user: ' postgres_root_user
167+ while [[ -z $postgres_root_user ]]; do
168+ echo " "
169+ echo " +++++++++++ ERROR ++++++++++++++++++++++"
170+ echo " The postgres username cannot be empty. Please input a valid username string."
171+ echo " ++++++++++++++++++++++++++++++++++++++++"
172+ echo " "
173+ read -rp ' Set the postgres root user: ' postgres_root_user
174+ done
175+ }
176+
177+ # This function prompts the user for an input for a non-empty postgres root password.
178+ read_postgres_password () {
179+ read -srp ' Set the postgres password: ' postgres_root_password
180+ while [[ -z $postgres_root_password ]]; do
181+ echo " "
182+ echo " "
183+ echo " +++++++++++ ERROR ++++++++++++++++++++++"
184+ echo " The postgres password cannot be empty. Please input a valid password string."
185+ echo " ++++++++++++++++++++++++++++++++++++++++"
186+ echo " "
187+ read -srp ' Set the postgres password: ' postgres_root_password
188+ done
189+ echo " "
190+ }
191+
192+ check_dependencies
193+ read_postgres_username
194+ read_postgres_password
195+ read_rootuser_email
196+ read_rootuser_password
197+ generate_variables
198+ generate_app_config_file
199+ generate_docker_env_file
200+ get_containers
201+
202+ echo " "
203+ echo " Pulling the latest container images"
204+ sudo docker compose pull
205+
206+ echo " "
207+ echo " Starting the Slashbase containers"
208+ sudo docker compose up --detach --remove-orphans || true
209+
210+ wait_for_containers_start 30
211+ echo " "
212+
213+ if [[ $status_code -ne 200 ]]; then
214+ echo " +++++++++++ ERROR ++++++++++++++++++++++"
215+ exit 1
216+ fi
217+
218+
219+ if [[ $status_code -eq 200 ]]; then
220+ echo " ++++++++++++++++++ SUCCESS ++++++++++++++++++++++"
221+ echo " Slashbase app is running on port 3000"
222+ echo " +++++++++++++++++++++++++++++++++++++++++++++++++"
223+ fi
0 commit comments