Skip to content

Commit 42b13c4

Browse files
unlikelyzeroozyx
andauthored
Fix couchdb setup and add a note on how to remove the container (#6915)
* discrete steps * update script to remove couchdb * address comments and add shellcheck linting --------- Co-authored-by: Jesse Mazzella <[email protected]>
1 parent 351800b commit 42b13c4

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

src/plugins/persistence/couch/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ sh ./src/plugins/persistence/couch/replace-localstorage-with-couchdb-indexhtml.s
4343

4444
Open MCT will now use your local CouchDB container as its persistence store. Access the CouchDB instance manager by visiting <http://localhost:5984/_utils>.
4545

46+
### Removing CouchDB Container completely
47+
48+
To completely remove the CouchDB container and volumes:
49+
50+
```sh
51+
docker stop couch-couchdb-1;docker rm couch-couchdb-1;docker volume rm couch_couchdb
52+
```
53+
4654
## macOS
4755

4856
While we highly recommend using the CouchDB docker-compose installation, it is still possible to install CouchDB through other means.

src/plugins/persistence/couch/setup-couchdb.sh

+45-32
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ if [ "${COUCH_ADMIN_PASSWORD}" ]; then
1818
CURL_USERPASS_ARG+=":${COUCH_ADMIN_PASSWORD}"
1919
fi
2020

21-
# Functions
2221
resource_exists() {
2322
response=$(curl -u "${CURL_USERPASS_ARG}" -s -o /dev/null -I -w "%{http_code}" $1);
2423
if [ "200" == "${response}" ]; then
@@ -29,16 +28,16 @@ resource_exists() {
2928
}
3029

3130
db_exists() {
32-
resource_exists $COUCH_BASE_LOCAL/$OPENMCT_DATABASE_NAME
31+
resource_exists "$COUCH_BASE_LOCAL"/"$OPENMCT_DATABASE_NAME"
3332
}
3433

3534
create_db() {
36-
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT $COUCH_BASE_LOCAL/$OPENMCT_DATABASE_NAME);
37-
echo $response
35+
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT "$COUCH_BASE_LOCAL"/"$OPENMCT_DATABASE_NAME");
36+
echo "$response"
3837
}
3938

4039
admin_user_exists() {
41-
response=$(curl -su "${CURL_USERPASS_ARG}" -o /dev/null -I -w "%{http_code}" $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/admins/$COUCH_ADMIN_USER);
40+
response=$(curl -su "${CURL_USERPASS_ARG}" -o /dev/null -I -w "%{http_code}" "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/admins/"$COUCH_ADMIN_USER");
4241
if [ "200" == "${response}" ]; then
4342
echo "TRUE"
4443
else
@@ -48,26 +47,26 @@ admin_user_exists() {
4847

4948
create_admin_user() {
5049
echo Creating admin user
51-
curl -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/admins/$COUCH_ADMIN_USER -d \'"$COUCH_ADMIN_PASSWORD"\'
50+
curl -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/admins/"$COUCH_ADMIN_USER" -d \'"$COUCH_ADMIN_PASSWORD"\'
5251
}
5352

5453
is_cors_enabled() {
55-
resource_exists $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/httpd/enable_cors
54+
resource_exists "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/httpd/enable_cors
5655
}
5756

5857
enable_cors() {
59-
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/httpd/enable_cors -d '"true"'
60-
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/cors/origins -d '"*"'
61-
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/cors/credentials -d '"true"'
62-
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/cors/methods -d '"GET, PUT, POST, HEAD, DELETE"'
63-
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/cors/headers -d '"accept, authorization, content-type, origin, referer, x-csrf-token"'
58+
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/httpd/enable_cors -d '"true"'
59+
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/cors/origins -d '"*"'
60+
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/cors/credentials -d '"true"'
61+
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/cors/methods -d '"GET, PUT, POST, HEAD, DELETE"'
62+
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/cors/headers -d '"accept, authorization, content-type, origin, referer, x-csrf-token"'
6463
}
6564

6665
update_db_permissions() {
6766
local db_name=$1
6867
echo "Updating ${db_name} database permissions"
6968
response=$(curl -su "${CURL_USERPASS_ARG}" --location \
70-
--request PUT $COUCH_BASE_LOCAL/$db_name/_security \
69+
--request PUT "$COUCH_BASE_LOCAL"/"$db_name"/_security \
7170
--header 'Content-Type: application/json' \
7271
--data-raw '{ "admins": {"roles": []},"members": {"roles": []}}')
7372
if [ "{\"ok\":true}" == "${response}" ]; then
@@ -77,17 +76,24 @@ update_db_permissions() {
7776
fi
7877
}
7978

80-
create_system_tables() {
81-
local system_tables=("_users" "_replicator")
82-
for table in "${system_tables[@]}"; do
83-
echo "Creating $table database"
84-
response=$(curl -su "${CURL_USERPASS_ARG}" -X PUT $COUCH_BASE_LOCAL/$table)
85-
if [ "{\"ok\":true}" == "${response}" ]; then
86-
echo "Successfully created $table database"
87-
else
88-
echo "Unable to create $table database"
89-
fi
90-
done
79+
create_users_table() {
80+
echo "Creating _users database"
81+
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT "$COUCH_BASE_LOCAL"/_users)
82+
if [ "{\"ok\":true}" == "${response}" ]; then
83+
echo "Successfully created _users database"
84+
else
85+
echo "Unable to create _users database"
86+
fi
87+
}
88+
89+
create_replicator_table() {
90+
echo "Creating _replicator database"
91+
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT "$COUCH_BASE_LOCAL"/_replicator)
92+
if [ "{\"ok\":true}" == "${response}" ]; then
93+
echo "Successfully created _replicator database"
94+
else
95+
echo "Unable to create _replicator database"
96+
fi
9197
}
9298

9399
# Main script execution
@@ -100,17 +106,24 @@ else
100106
echo "Admin user exists"
101107
fi
102108

103-
# Check if system tables exist; if not, create them.
104-
system_tables_exist=$(resource_exists $COUCH_BASE_LOCAL/_users)
105-
if [ "TRUE" == "${system_tables_exist}" ]; then
106-
echo "System tables exist, skipping creation"
109+
# Check if the _users table exists; if not, create it.
110+
users_table_exists=$(resource_exists "$COUCH_BASE_LOCAL"/_users)
111+
if [ "FALSE" == "${users_table_exists}" ]; then
112+
create_users_table
113+
else
114+
echo "_users database already exists, skipping creation"
115+
fi
116+
117+
# Check if the _replicator database exists; if not, create it.
118+
replicator_table_exists=$(resource_exists "$COUCH_BASE_LOCAL/_replicator")
119+
if [ "FALSE" == "${replicator_table_exists}" ]; then
120+
create_replicator_table
107121
else
108-
echo "Fresh install, creating system tables"
109-
create_system_tables
122+
echo "_replicator database already exists, skipping creation"
110123
fi
111124

112125
# Check if the database exists; if not, create it.
113-
if [ "FALSE" == $(db_exists) ]; then
126+
if [ "FALSE" == "$(db_exists)" ]; then
114127
response=$(create_db)
115128
if [ "{\"ok\":true}" == "${response}" ]; then
116129
echo "Database successfully created"
@@ -126,7 +139,7 @@ update_db_permissions "_replicator"
126139
update_db_permissions "${OPENMCT_DATABASE_NAME}"
127140

128141
# Check if CORS is enabled; if not, enable it.
129-
if [ "FALSE" == $(is_cors_enabled) ]; then
142+
if [ "FALSE" == "$(is_cors_enabled)" ]; then
130143
echo "Enabling CORS"
131144
enable_cors
132145
else

0 commit comments

Comments
 (0)