-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-cluster.sh
More file actions
executable file
·151 lines (133 loc) · 4.46 KB
/
init-cluster.sh
File metadata and controls
executable file
·151 lines (133 loc) · 4.46 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
#!/bin/bash
# init-cluster.sh — Initialize a single-node Couchbase cluster for local development.
#
# Mounted into the container by docker-compose.yml and run manually after startup:
# docker compose exec couchbase bash /init-cluster.sh
#
# What it does:
# 1. Waits for the Couchbase REST API to be ready
# 2. Initializes the cluster (services, memory quotas, credentials)
# 3. Creates a default bucket, scope, and collection
# 4. Creates a primary index
# 5. Optionally loads the travel-sample bucket
#
# Override defaults via environment variables:
# CB_BUCKET, CB_SCOPE, CB_COLLECTION, KV_QUOTA, INDEX_QUOTA, FTS_QUOTA,
# EVENTING_QUOTA, ANALYTICS_QUOTA, LOAD_TRAVEL_SAMPLE
set -euo pipefail
CB_HOST="${CB_HOST:-localhost}"
CB_USER="${COUCHBASE_ADMINISTRATOR_USERNAME:-Administrator}"
CB_PASS="${COUCHBASE_ADMINISTRATOR_PASSWORD:-password}"
CB_BUCKET="${CB_BUCKET:-myapp}"
CB_SCOPE="${CB_SCOPE:-_default}"
CB_COLLECTION="${CB_COLLECTION:-_default}"
LOAD_TRAVEL_SAMPLE="${LOAD_TRAVEL_SAMPLE:-false}"
KV_QUOTA="${KV_QUOTA:-512}"
INDEX_QUOTA="${INDEX_QUOTA:-256}"
FTS_QUOTA="${FTS_QUOTA:-256}"
EVENTING_QUOTA="${EVENTING_QUOTA:-256}"
ANALYTICS_QUOTA="${ANALYTICS_QUOTA:-1024}"
CLI="couchbase-cli"
wait_for_couchbase() {
echo "Waiting for Couchbase REST API..."
for i in $(seq 1 60); do
if curl -sf "http://${CB_HOST}:8091/pools" > /dev/null 2>&1; then
echo "Couchbase is ready."
return 0
fi
sleep 2
done
echo "ERROR: Couchbase did not become ready in time." >&2
exit 1
}
init_cluster() {
echo "Initializing cluster..."
$CLI cluster-init \
--cluster "${CB_HOST}:8091" \
--cluster-username "${CB_USER}" \
--cluster-password "${CB_PASS}" \
--cluster-name "dev-cluster" \
--services "data,index,query,fts,eventing,analytics" \
--cluster-ramsize "${KV_QUOTA}" \
--cluster-index-ramsize "${INDEX_QUOTA}" \
--cluster-fts-ramsize "${FTS_QUOTA}" \
--cluster-eventing-ramsize "${EVENTING_QUOTA}" \
--cluster-analytics-ramsize "${ANALYTICS_QUOTA}" \
--index-storage-setting plasma
echo "Cluster initialized."
}
create_bucket() {
echo "Creating bucket '${CB_BUCKET}'..."
$CLI bucket-create \
--cluster "${CB_HOST}:8091" \
--username "${CB_USER}" \
--password "${CB_PASS}" \
--bucket "${CB_BUCKET}" \
--bucket-type couchbase \
--bucket-ramsize "${KV_QUOTA}" \
--bucket-replica 0 \
--enable-flush 1 \
--wait
echo "Bucket '${CB_BUCKET}' created."
}
create_scope() {
if [ "${CB_SCOPE}" = "_default" ]; then
return
fi
echo "Creating scope '${CB_SCOPE}'..."
$CLI collection-manage \
--cluster "${CB_HOST}:8091" \
--username "${CB_USER}" \
--password "${CB_PASS}" \
--bucket "${CB_BUCKET}" \
--create-scope "${CB_SCOPE}"
sleep 1
}
create_collection() {
if [ "${CB_COLLECTION}" = "_default" ]; then
return
fi
echo "Creating collection '${CB_COLLECTION}'..."
$CLI collection-manage \
--cluster "${CB_HOST}:8091" \
--username "${CB_USER}" \
--password "${CB_PASS}" \
--bucket "${CB_BUCKET}" \
--create-collection "${CB_SCOPE}.${CB_COLLECTION}"
sleep 1
}
create_primary_index() {
echo "Creating primary index on \`${CB_BUCKET}\`.\`${CB_SCOPE}\`.\`${CB_COLLECTION}\`..."
# couchbase-cli does not manage indexes — use the Query REST endpoint directly
curl -sf -X POST "http://${CB_HOST}:8093/query/service" \
-u "${CB_USER}:${CB_PASS}" \
--data-urlencode "statement=CREATE PRIMARY INDEX IF NOT EXISTS ON \`${CB_BUCKET}\`.\`${CB_SCOPE}\`.\`${CB_COLLECTION}\`" \
> /dev/null
echo "Primary index created."
}
load_travel_sample() {
echo "Loading travel-sample..."
$CLI sample-data-load \
--cluster "${CB_HOST}:8091" \
--username "${CB_USER}" \
--password "${CB_PASS}" \
--datasets travel-sample
echo "travel-sample load initiated (runs in background)."
}
# --- Main ---
wait_for_couchbase
init_cluster
create_bucket
create_scope
create_collection
create_primary_index
if [ "${LOAD_TRAVEL_SAMPLE}" = "true" ]; then
load_travel_sample
fi
echo ""
echo "Couchbase dev cluster ready."
echo " Web Console : http://localhost:8091"
echo " Credentials : ${CB_USER} / ${CB_PASS}"
echo " Bucket : ${CB_BUCKET}"
echo " Scope : ${CB_SCOPE}"
echo " Collection : ${CB_COLLECTION}"