Skip to content

Commit 3a6ed60

Browse files
committed
Update and rename script
1 parent 08d08a3 commit 3a6ed60

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

scripts/tfbackend-cli renamed to scripts/tfbeadm

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#!/bin/bash
22

3-
# tfbackend-cli create-index
4-
# tfbackend-cli create-user myusername mypassword mytenant
5-
6-
MONGODB_URI="${MONGODB_URI:-mongodb://mongodb:27017}"
7-
MONGODB_DB="${MONGODB_DB:-test}"
8-
MONGODB_CONTAINERNAME="${MONGODB_CONTAINERNAME:-mongodb}"
3+
MONGODB_URI="${MONGODB_URI:-mongodb://mongodb:27017/test}"
4+
#MONGODB_CONTAINERNAME
5+
MONGODB_VERSION="${MONGODB_VERSION:-8.0}"
96

107
MONGODB_INDEX_COMMANDS=(
118
'db.tf_state.createIndex({"tenant": 1, "name": 1})'
12-
'db.tf_state_lock.createIndex({"tenant": 1, "name": 1})'
9+
'db.tf_state_lock.createIndex({"tenant": 1, "name": 1}, {unique: true})'
10+
'db.user.createIndex({"username": 1}, {unique: true})'
1311
)
1412

1513
log_info() {
@@ -40,7 +38,7 @@ log_error() {
4038
usage() {
4139
echo -e "\033[1;34mUsage:\033[0m $0 [command] [arguments...]"
4240
echo -e "\033[1;34mCommands:\033[0m"
43-
echo " create-index : Create indexes in database"
41+
echo " create-indexes : Create indexes in database"
4442
echo " create-user <username> <password> <tenant> : Create user in database"
4543
echo " help : Show this help message"
4644
# echo " (no arguments) : Run in interactive mode"
@@ -68,23 +66,27 @@ check_htpasswd() {
6866
}
6967

7068
execute_mongosh() {
71-
local mongo_cmd="$1"
69+
local mongodb_cmd="$1"
7270
if check_command "mongosh"; then
73-
execute_mongosh_package "$mongo_cmd" || exit 1
71+
execute_mongosh_package "$mongodb_cmd" || return 1
7472
else
7573
if check_command "docker"; then
76-
execute_mongosh_docker "$mongo_cmd" || exit 1
74+
if [[ -n "$MONGODB_CONTAINERNAME" ]]; then
75+
execute_mongosh_docker_link "$mongodb_cmd" || return 1
76+
else
77+
execute_mongosh_docker "$mongodb_cmd" || return 1
78+
fi
7779
else
7880
echo "Error: Neither mongosh nor Docker is installed. Please install one to proceed."
79-
exit 1
81+
return 1
8082
fi
8183
fi
8284
}
8385

8486
execute_mongosh_package() {
85-
local mongo_cmd="$1"
86-
log_info "Executing MongoDB command in the shell: $mongo_cmd"
87-
if output=$(mongosh "$MONGODB_URI/$MONGODB_DB" --quiet --eval "$mongo_cmd" 2>&1); then
87+
local mongodb_cmd="$1"
88+
log_info "Executing MongoDB command in the shell: $mongodb_cmd"
89+
if output=$(mongosh "$MONGODB_URI" --quiet --eval "$mongodb_cmd" 2>&1); then
8890
log_info "Output:\n$output"
8991
else
9092
log_warn "$output"
@@ -93,17 +95,28 @@ execute_mongosh_package() {
9395
}
9496

9597
execute_mongosh_docker() {
96-
local mongo_cmd="$1"
97-
log_info "Executing MongoDB command in a container: $mongo_cmd"
98-
if output=$(docker run --rm --link "$MONGODB_CONTAINERNAME" mongo:8.0 bash -c "mongosh \"$MONGODB_URI/$MONGODB_DB\" --quiet --eval \"$mongo_cmd\"" 2>&1); then
98+
local mongodb_cmd="$1"
99+
log_info "Executing MongoDB command in a container: $mongodb_cmd"
100+
if output=$(docker run --rm "mongo:$MONGODB_VERSION" bash -c "mongosh \"$MONGODB_URI\" --quiet --eval \"$mongodb_cmd\"" 2>&1); then
99101
log_info "Output:\n$output"
100102
else
101103
log_warn "$output"
102104
return 1
103105
fi
104106
}
105107

106-
create_mongodb_indexes() {
108+
execute_mongosh_docker_link() {
109+
local mongodb_cmd="$1"
110+
log_info "Executing MongoDB command in a container with a link: $mongodb_cmd"
111+
if output=$(docker run --rm --link "$MONGODB_CONTAINERNAME" "mongo:$MONGODB_VERSION" bash -c "mongosh \"$MONGODB_URI\" --quiet --eval \"$mongodb_cmd\"" 2>&1); then
112+
log_info "Output:\n$output"
113+
else
114+
log_warn "$output"
115+
return 1
116+
fi
117+
}
118+
119+
create_indexes() {
107120
local status=0
108121
log_header "Creating predefined MongoDB indexes"
109122
for cmd in "${MONGODB_INDEX_COMMANDS[@]}"; do
@@ -121,6 +134,7 @@ create_user() {
121134
local username=$1
122135
local password=$2
123136
local tenant=$3
137+
log_header "Creating user"
124138

125139
hash=$(htpasswd -bnBC 10 "" "$password" | tr -d ':\n')
126140
if [ $? -ne 0 ] || [ -z "$hash" ]; then
@@ -137,15 +151,19 @@ db.user.insertOne({
137151
EOF
138152
)
139153

140-
execute_mongosh "$creation_command" || return 1
141-
execute_mongosh "db.user.createIndex({\"username\": 1}, {unique: true})" || return 1
154+
if execute_mongosh "$creation_command"; then
155+
log_success "User created successfully"
156+
else
157+
log_error "User created failed"
158+
exit 1
159+
fi
142160
}
143161

144162
# checks for command-line arguments
145163
if [ $# -gt 0 ]; then
146164
case "$1" in
147-
"create-index")
148-
create_mongodb_indexes
165+
"create-indexes")
166+
create_indexes
149167
exit $?
150168
;;
151169
"create-user")

0 commit comments

Comments
 (0)