-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
308 lines (265 loc) · 11 KB
/
install.sh
File metadata and controls
308 lines (265 loc) · 11 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
set -e
echo -e "
_____ _ ___ _ _ _____
|_ _| | | / _ \ | |(_) / __ \\
| | _ __ ___ _ __ __ _ ___ | |_ / /_\ \ _ __ __ _ | | _ ___ _ _ ___ ' / /'
| | | '_ ' _ \| '_ \ / _' | / __|| __| | _ || '_ \ / _' || || |/ __|| | | |/ __| / /
_| |_| | | | | || |_) || (_| || (__ | |_ | | | || | | || (_| || || |\__ \| |_| |\__ \ ./ /___
\___/|_| |_| |_|| .__/ \__,_| \___| \__| \_| |_/|_| |_| \__,_||_||_||___/ \__, ||___/ \_____/
| | __/ |
|_| |___/
#### Impact Analysis 2 Installer ####
"
sed_wrap() {
if sed --version 2>/dev/null | grep -q "GNU sed"; then
sed -i "$@"
else
sed -i '' "$@"
fi
}
check_dependencies() {
echo "Checking dependencies..."
local missing_deps=0
for cmd in git docker; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: Dependency '$cmd' is not installed. Please install it to proceed."
missing_deps=1
fi
done
if ! docker compose version &>/dev/null; then
echo "Error: 'docker compose' (Docker Compose V2 plugin) is not available or not working."
echo "Please ensure Docker Desktop or Docker Engine with Compose plugin is correctly installed."
missing_deps=1
fi
if [ "$missing_deps" -eq 1 ]; then
exit 1
fi
echo "All dependencies found."
}
check_docker_running() {
echo "Checking Docker service..."
if ! docker info &>/dev/null; then
echo "Error: Docker is installed but the Docker daemon is not running."
echo "Please start Docker and try again."
exit 1
fi
echo "Docker service is running."
}
GIT_TOKEN=""
clone_or_update_repo() {
local REPO_URL_BASE="https://github.com/Arcan-Tech/impact-infrastructure-2.git"
local INSTALL_DIR_DEFAULT="./impact-infrastructure-2"
local REPO_URL_TO_USE="$REPO_URL_BASE"
read -p "Enter installation directory [default: $INSTALL_DIR_DEFAULT]: " INSTALL_DIR
INSTALL_DIR="${INSTALL_DIR:-$INSTALL_DIR_DEFAULT}"
read -p "GitHub Personal Access Token (PAT) for clone and registry (access to repo and packages): " GIT_TOKEN
if [ -n "$GIT_TOKEN" ]; then
REPO_URL_TO_USE="${REPO_URL_BASE/https:\/\//https:\/\/$GIT_TOKEN@}"
else
exit 1
echo "No GitHub token provided. Exiting."
fi
if [ -d "$INSTALL_DIR/.git" ]; then
echo "Existing repository found in $INSTALL_DIR."
read -p "Do you want to update it (git pull)? [y/n]: " PULL_CHOICE
PULL_CHOICE=$(echo "$PULL_CHOICE" | tr '[:upper:]' '[:lower:]')
if [[ "$PULL_CHOICE" == "y" ]]; then
echo "Updating repository in $INSTALL_DIR..."
(
cd "$INSTALL_DIR"
if [ -n "$GIT_TOKEN" ]; then
echo "Temporarily setting remote URL with token for pull operation..."
git remote set-url origin "$REPO_URL_TO_USE"
fi
git fetch --all && git pull
) || { echo "Error updating repository. Please check for conflicts or issues."; exit 1; }
echo "Repository updated."
else
echo "Skipping repository update."
fi
elif [ -d "$INSTALL_DIR" ] && [ "$(ls -A "$INSTALL_DIR" 2>/dev/null)" ]; then
echo "Error: Directory '$INSTALL_DIR' exists but is not a git repository and is not empty."
echo "Please choose a different directory or clear its content."
exit 1
else
echo "Cloning repository into $INSTALL_DIR..."
mkdir -p "$INSTALL_DIR"
git clone "$REPO_URL_TO_USE" "$INSTALL_DIR" || { echo "Error cloning repository."; rm -rf "$INSTALL_DIR"; exit 1; }
echo "Repository cloned successfully."
fi
cd "$INSTALL_DIR"
echo "Successfully set up repository in $(pwd)"
mkdir -p ./docker-conf
echo "Created ./docker-conf directory for Docker configuration files."
}
DOCKER_CONFIG_FILE_PATH="./docker-conf/config.json"
create_docker_config_json() {
if [ -f "$DOCKER_CONFIG_FILE_PATH" ]; then
echo "Docker config file ($DOCKER_CONFIG_FILE_PATH) already exists."
read -p "Do you want to overwrite it? [y/n]: " OVERWRITE_DOCKER_CONFIG
OVERWRITE_DOCKER_CONFIG=$(echo "$OVERWRITE_DOCKER_CONFIG" | tr '[:upper:]' '[:lower:]')
if [[ "$OVERWRITE_DOCKER_CONFIG" != "y" ]]; then
echo "Skipping Docker config.json creation."
return
fi
fi
if [ -z "$GIT_TOKEN" ]; then
echo "GitHub token for ghcr.io not provided. Skipping $DOCKER_CONFIG_FILE_PATH creation."
echo "Watchtower might not be able to update private images from ghcr.io."
return
fi
local base64_cmd
if base64 --version 2>/dev/null | grep -q "GNU coreutils"; then
base64_cmd="base64 -w 0"
else
base64_cmd="base64"
fi
ENCODED_AUTH=$(echo -n "username:${GIT_TOKEN}" | $base64_cmd)
cat >"$DOCKER_CONFIG_FILE_PATH" <<EOF
{
"auths": {
"ghcr.io": {
"auth": "${ENCODED_AUTH}"
}
}
}
EOF
echo "$DOCKER_CONFIG_FILE_PATH created successfully for ghcr.io authentication."
}
configure_installation_type() {
echo "Select the type of environment to run:"
echo "1) Snapshot"
echo "2) Stable"
read -p "Choice [1/2]: " ENV_CHOICE_INPUT
INSTALL_TYPE_VAR=""
ENV_FILE=""
case $ENV_CHOICE_INPUT in
1)
INSTALL_TYPE_VAR="snapshot"
ENV_FILE=".snapshot.env"
if [ ! -f "$ENV_FILE" ]; then
echo "Error: $ENV_FILE not found in the repository."
echo "Please ensure the repository is correctly cloned/updated and the file exists."
exit 1
fi
echo "Using snapshot environment with $ENV_FILE"
;;
2)
INSTALL_TYPE_VAR="stable"
ENV_FILE=".stable.env"
if [ ! -f "$ENV_FILE" ]; then
echo "Error: $ENV_FILE not found in the repository."
echo "Please ensure the repository is correctly cloned/updated and the file exists."
exit 1
fi
echo "Using stable environment with $ENV_FILE"
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
}
replace_passwords_in_env() {
local source_env_file="$1"
local target_env_file=".env"
if [ ! -f "$source_env_file" ]; then
echo "Warning: Source environment file '$source_env_file' not found. Skipping .env setup and password generation."
echo "If this is a release and .stable.env was missing in the tag, this is expected."
echo "Ensure you have a correctly configured .env file before running the application."
return
fi
echo "Copying '$source_env_file' to '$target_env_file' to be used as the active configuration."
cp "$source_env_file" "$target_env_file"
echo "The environment file '$target_env_file' (copied from '$source_env_file') may contain default passwords."
read -p "Do you want to replace POSTGRES_PASSWORD and NEO4J_PASSWORD in '$target_env_file' with securely generated random passwords? (Recommended) [y/n]: " REPLACE_PASS
REPLACE_PASS=$(echo "$REPLACE_PASS" | tr '[:upper:]' '[:lower:]')
if [[ "$REPLACE_PASS" != "y" ]]; then
echo "Skipping password replacement. Using passwords as defined in '$target_env_file'."
return
fi
echo "Proceeding to replace passwords in '$target_env_file'."
cp "$target_env_file" "${target_env_file}.bak.$(date +%s)"
echo "Backup of '$target_env_file' saved as '${target_env_file}.bak....'"
local modified=0
if grep -q "^POSTGRES_PASSWORD=" "$target_env_file"; then
new_pg_pass=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 20)
sed_wrap "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${new_pg_pass}|" "$target_env_file"
echo "POSTGRES_PASSWORD updated in '$target_env_file'."
modified=1
else
echo "POSTGRES_PASSWORD not found in '$target_env_file'."
fi
if grep -q "^NEO4J_PASSWORD=" "$target_env_file"; then
new_neo_pass=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 20)
sed_wrap "s|^NEO4J_PASSWORD=.*|NEO4J_PASSWORD=${new_neo_pass}|" "$target_env_file"
echo "NEO4J_PASSWORD updated in '$target_env_file'."
modified=1
else
echo "NEO4J_PASSWORD not found in '$target_env_file'."
fi
if [ "$modified" -eq 1 ]; then
echo "Passwords updated in '$target_env_file'. Note: If DATABASE_URL (or similar) uses these passwords, it should reference them via \${VAR_NAME} to pick up changes."
else
echo "No passwords were found or updated in '$target_env_file'."
fi
}
create_required_dirs() {
echo "Creating required directories..."
mkdir -p ./logs
echo "Directory ./logs created/ensured."
}
generate_run_script() {
local run_script_name="run-impact-2.sh"
local install_type_param="$1"
local docker_compose_base_cmd="docker compose"
local final_docker_compose_cmd="$docker_compose_base_cmd"
echo "Generating run script: $run_script_name ..."
cat > "$run_script_name" <<EOF
#!/bin/bash
set -e
echo "Starting Impact Infrastructure 2 (snapshot environment)..."
if [[ "$(uname)" == "Darwin" ]]; then
echo "Running on macOS. MY_UID=\$MY_UID, MY_GID=\$MY_GID"
EFFECTIVE_USER=$(id -un)
DOCKER_HOST=unix:///Users/\$EFFECTIVE_USER/.docker/run/docker.sock MY_UID="$(id -u)" MY_GID="$(id -g)" docker --config ./docker-conf compose up \$@
else
echo "Running on Linux or other Unix-like OS. MY_UID=\$MY_UID, MY_GID=\$MY_GID"
MY_UID="$(id -u)" MY_GID="$(id -g)" docker --config ./docker-conf compose up \$@
fi
EOF
chmod +x "$run_script_name"
echo "$run_script_name created successfully."
echo "You can start the application using: ./$run_script_name"
echo "Use './$run_script_name -d' to run in detached mode."
}
check_dependencies
check_docker_running
clone_or_update_repo
create_docker_config_json
configure_installation_type
replace_passwords_in_env "$ENV_FILE"
create_required_dirs
generate_run_script "$INSTALL_TYPE_VAR"
echo ""
echo "-----------------------------------------------------"
echo "Impact Infrastructure 2 installation process complete!"
echo "-----------------------------------------------------"
echo ""
echo "The application will use the '.env' file for its configuration."
echo "If you chose to generate random passwords, they are in '.env'."
echo ""
echo "To start the application, navigate to $(pwd) and run:"
echo " ./run-impact-2.sh"
echo ""
echo "To run in detached mode (background):"
echo " ./run-impact-2.sh -d"
echo ""
echo "To view logs:"
echo " ./run-impact-2.sh logs -f"
echo ""
echo "To stop the application (from the same directory):"
echo " docker compose down" # docker compose down will also use .env if present
echo ""
echo "Enjoy using Impact Infrastructure 2!"