@@ -18,7 +18,6 @@ if [ "${COUCH_ADMIN_PASSWORD}" ]; then
18
18
CURL_USERPASS_ARG+=" :${COUCH_ADMIN_PASSWORD} "
19
19
fi
20
20
21
- # Functions
22
21
resource_exists () {
23
22
response=$( curl -u " ${CURL_USERPASS_ARG} " -s -o /dev/null -I -w " %{http_code}" $1 ) ;
24
23
if [ " 200" == " ${response} " ]; then
@@ -29,16 +28,16 @@ resource_exists() {
29
28
}
30
29
31
30
db_exists () {
32
- resource_exists $COUCH_BASE_LOCAL / $OPENMCT_DATABASE_NAME
31
+ resource_exists " $COUCH_BASE_LOCAL " / " $OPENMCT_DATABASE_NAME "
33
32
}
34
33
35
34
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 "
38
37
}
39
38
40
39
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 " ) ;
42
41
if [ " 200" == " ${response} " ]; then
43
42
echo " TRUE"
44
43
else
@@ -48,26 +47,26 @@ admin_user_exists() {
48
47
49
48
create_admin_user () {
50
49
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 " \'
52
51
}
53
52
54
53
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
56
55
}
57
56
58
57
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"'
64
63
}
65
64
66
65
update_db_permissions () {
67
66
local db_name=$1
68
67
echo " Updating ${db_name} database permissions"
69
68
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 \
71
70
--header ' Content-Type: application/json' \
72
71
--data-raw ' { "admins": {"roles": []},"members": {"roles": []}}' )
73
72
if [ " {\" ok\" :true}" == " ${response} " ]; then
@@ -77,17 +76,24 @@ update_db_permissions() {
77
76
fi
78
77
}
79
78
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
91
97
}
92
98
93
99
# Main script execution
@@ -100,17 +106,24 @@ else
100
106
echo " Admin user exists"
101
107
fi
102
108
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
107
121
else
108
- echo " Fresh install, creating system tables"
109
- create_system_tables
122
+ echo " _replicator database already exists, skipping creation"
110
123
fi
111
124
112
125
# Check if the database exists; if not, create it.
113
- if [ " FALSE" == $( db_exists) ]; then
126
+ if [ " FALSE" == " $( db_exists) " ]; then
114
127
response=$( create_db)
115
128
if [ " {\" ok\" :true}" == " ${response} " ]; then
116
129
echo " Database successfully created"
@@ -126,7 +139,7 @@ update_db_permissions "_replicator"
126
139
update_db_permissions " ${OPENMCT_DATABASE_NAME} "
127
140
128
141
# Check if CORS is enabled; if not, enable it.
129
- if [ " FALSE" == $( is_cors_enabled) ]; then
142
+ if [ " FALSE" == " $( is_cors_enabled) " ]; then
130
143
echo " Enabling CORS"
131
144
enable_cors
132
145
else
0 commit comments