Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix couchdb setup and add a note on how to remove the container #6915

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/plugins/persistence/couch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ sh ./src/plugins/persistence/couch/replace-localstorage-with-couchdb-indexhtml.s

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

### Removing CouchDB Container completely

To completely remove the CouchDB container and volumes:

```sh
docker stop couch-couchdb-1;docker rm couch-couchdb-1;docker volume rm couch_couchdb
```

## macOS

While we highly recommend using the CouchDB docker-compose installation, it is still possible to install CouchDB through other means.
Expand Down
77 changes: 45 additions & 32 deletions src/plugins/persistence/couch/setup-couchdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ if [ "${COUCH_ADMIN_PASSWORD}" ]; then
CURL_USERPASS_ARG+=":${COUCH_ADMIN_PASSWORD}"
fi

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

db_exists() {
resource_exists $COUCH_BASE_LOCAL/$OPENMCT_DATABASE_NAME
resource_exists "$COUCH_BASE_LOCAL"/"$OPENMCT_DATABASE_NAME"
}

create_db() {
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT $COUCH_BASE_LOCAL/$OPENMCT_DATABASE_NAME);
echo $response
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT "$COUCH_BASE_LOCAL"/"$OPENMCT_DATABASE_NAME");
echo "$response"
}

admin_user_exists() {
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);
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");
if [ "200" == "${response}" ]; then
echo "TRUE"
else
Expand All @@ -48,26 +47,26 @@ admin_user_exists() {

create_admin_user() {
echo Creating admin user
curl -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/admins/$COUCH_ADMIN_USER -d \'"$COUCH_ADMIN_PASSWORD"\'
curl -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/admins/"$COUCH_ADMIN_USER" -d \'"$COUCH_ADMIN_PASSWORD"\'
}

is_cors_enabled() {
resource_exists $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/httpd/enable_cors
resource_exists "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/httpd/enable_cors
}

enable_cors() {
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/httpd/enable_cors -d '"true"'
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/cors/origins -d '"*"'
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT $COUCH_BASE_LOCAL/_node/$COUCH_NODE_NAME/_config/cors/credentials -d '"true"'
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"'
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"'
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/httpd/enable_cors -d '"true"'
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/cors/origins -d '"*"'
curl -su "${CURL_USERPASS_ARG}" -o /dev/null -X PUT "$COUCH_BASE_LOCAL"/_node/"$COUCH_NODE_NAME"/_config/cors/credentials -d '"true"'
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"'
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"'
}

update_db_permissions() {
local db_name=$1
echo "Updating ${db_name} database permissions"
response=$(curl -su "${CURL_USERPASS_ARG}" --location \
--request PUT $COUCH_BASE_LOCAL/$db_name/_security \
--request PUT "$COUCH_BASE_LOCAL"/"$db_name"/_security \
--header 'Content-Type: application/json' \
--data-raw '{ "admins": {"roles": []},"members": {"roles": []}}')
if [ "{\"ok\":true}" == "${response}" ]; then
Expand All @@ -77,17 +76,24 @@ update_db_permissions() {
fi
}

create_system_tables() {
local system_tables=("_users" "_replicator")
for table in "${system_tables[@]}"; do
echo "Creating $table database"
response=$(curl -su "${CURL_USERPASS_ARG}" -X PUT $COUCH_BASE_LOCAL/$table)
if [ "{\"ok\":true}" == "${response}" ]; then
echo "Successfully created $table database"
else
echo "Unable to create $table database"
fi
done
create_users_table() {
echo "Creating _users database"
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT "$COUCH_BASE_LOCAL"/_users)
if [ "{\"ok\":true}" == "${response}" ]; then
echo "Successfully created _users database"
else
echo "Unable to create _users database"
fi
}

create_replicator_table() {
echo "Creating _replicator database"
response=$(curl -su "${CURL_USERPASS_ARG}" -XPUT "$COUCH_BASE_LOCAL"/_replicator)
if [ "{\"ok\":true}" == "${response}" ]; then
echo "Successfully created _replicator database"
else
echo "Unable to create _replicator database"
fi
}

# Main script execution
Expand All @@ -100,17 +106,24 @@ else
echo "Admin user exists"
fi

# Check if system tables exist; if not, create them.
system_tables_exist=$(resource_exists $COUCH_BASE_LOCAL/_users)
if [ "TRUE" == "${system_tables_exist}" ]; then
echo "System tables exist, skipping creation"
# Check if the _users table exists; if not, create it.
users_table_exists=$(resource_exists "$COUCH_BASE_LOCAL"/_users)
if [ "FALSE" == "${users_table_exists}" ]; then
create_users_table
else
echo "_users database already exists, skipping creation"
fi

# Check if the _replicator database exists; if not, create it.
replicator_table_exists=$(resource_exists "$COUCH_BASE_LOCAL/_replicator")
if [ "FALSE" == "${replicator_table_exists}" ]; then
create_replicator_table
else
echo "Fresh install, creating system tables"
create_system_tables
echo "_replicator database already exists, skipping creation"
fi

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

# Check if CORS is enabled; if not, enable it.
if [ "FALSE" == $(is_cors_enabled) ]; then
if [ "FALSE" == "$(is_cors_enabled)" ]; then
echo "Enabling CORS"
enable_cors
else
Expand Down