forked from rootfs/rhcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·501 lines (411 loc) · 16 KB
/
entrypoint.sh
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/bin/bash
set -e
: ${CLUSTER:=ceph}
: ${CEPH_CLUSTER_NETWORK:=${CEPH_PUBLIC_NETWORK}}
: ${CEPH_DAEMON:=${1}} # default daemon to first argument
: ${CEPH_GET_ADMIN_KEY:=0}
: ${HOSTNAME:=$(hostname -s)}
: ${MON_NAME:=${HOSTNAME}}
: ${MON_IP_AUTO_DETECT:=0}
: ${MDS_NAME:=mds-${HOSTNAME}}
: ${OSD_FORCE_ZAP:=0}
: ${OSD_JOURNAL_SIZE:=100}
: ${CRUSH_LOCATION:=root=default host=${HOSTNAME}}
: ${CEPHFS_CREATE:=0}
: ${CEPHFS_NAME:=cephfs}
: ${CEPHFS_DATA_POOL:=${CEPHFS_NAME}_data}
: ${CEPHFS_DATA_POOL_PG:=8}
: ${CEPHFS_METADATA_POOL:=${CEPHFS_NAME}_metadata}
: ${CEPHFS_METADATA_POOL_PG:=8}
: ${RGW_NAME:=${HOSTNAME}}
: ${RGW_CIVETWEB_PORT:=80}
: ${RGW_REMOTE_CGI:=0}
: ${RGW_REMOTE_CGI_PORT:=9000}
: ${RGW_REMOTE_CGI_HOST:=0.0.0.0}
: ${RESTAPI_IP:=0.0.0.0}
: ${RESTAPI_PORT:=5000}
: ${RESTAPI_BASE_URL:=/api/v0.1}
: ${RESTAPI_LOG_LEVEL:=warning}
: ${RESTAPI_LOG_FILE:=/var/log/ceph/ceph-restapi.log}
: ${KV_TYPE:=none} # valid options: consul, etcd or none
: ${KV_IP:=127.0.0.1}
: ${KV_PORT:=4001} # PORT 8500 for Consul
CEPH_OPTS="--cluster ${CLUSTER}"
# ceph config file exists or die
function check_config {
if [[ ! -e /etc/ceph/${CLUSTER}.conf ]]; then
echo "ERROR- /etc/ceph/${CLUSTER}.conf must exist; get it from your existing mon"
exit 1
fi
}
# ceph admin key exists or die
function check_admin_key {
if [[ ! -e /etc/ceph/${CLUSTER}.client.admin.keyring ]]; then
echo "ERROR- /etc/ceph/${CLUSTER}.client.admin.keyring must exist; get it from your existing mon"
exit 1
fi
}
###########################
# Configuration generator #
###########################
# Load in the bootstrapping routines
# based on the data store
case "$KV_TYPE" in
etcd|consul)
source /config.kv.sh
;;
static)
source /config.static.sh
;;
*)
source /config.no-gen.sh
;;
esac
#######
# MON #
#######
function start_mon {
if [ ! -n "$CEPH_PUBLIC_NETWORK" ]; then
echo "ERROR- CEPH_PUBLIC_NETWORK must be defined as the name of the network for the OSDs"
exit 1
fi
if [ ${MON_IP_AUTO_DETECT} -eq 1 ]; then
MON_IP=$(ip -6 -o a | grep scope.global | awk '/eth/ { sub ("/..", "", $4); print $4 }' | head -n1)
if [ -z "$MON_IP" ]; then
MON_IP=$(ip -4 -o a | awk '/eth/ { sub ("/..", "", $4); print $4 }')
fi
elif [ ${MON_IP_AUTO_DETECT} -eq 4 ]; then
MON_IP=$(ip -4 -o a | awk '/eth/ { sub ("/..", "", $4); print $4 }')
elif [ ${MON_IP_AUTO_DETECT} -eq 6 ]; then
MON_IP=$(ip -6 -o a | grep scope.global | awk '/eth/ { sub ("/..", "", $4); print $4 }' | head -n1)
fi
if [ ! -n "$MON_IP" ]; then
echo "ERROR- MON_IP must be defined as the IP address of the monitor"
exit 1
fi
# get_mon_config is also responsible for bootstrapping the
# cluster, if necessary
get_mon_config
# If we don't have a monitor keyring, this is a new monitor
if [ ! -e /var/lib/ceph/mon/${CLUSTER}-${MON_NAME}/keyring ]; then
if [ ! -e /etc/ceph/${CLUSTER}.mon.keyring ]; then
echo "ERROR- /etc/ceph/${CLUSTER}.mon.keyring must exist. You can extract it from your current monitor by running 'ceph auth get mon. -o /etc/ceph/${CLUSTER}.mon.keyring' or use a KV Store"
exit 1
fi
monmapOpt="--monmap /etc/ceph/monmap"
if [ ! -e /etc/ceph/monmap ]; then
echo "/etc/ceph/monmap must exist. You can extract it from your current monitor by running 'ceph mon getmap -o /etc/ceph/monmap' or use a KV Store"
monmapOpt=""
fi
# Testing if it's not the first monitor, if one key doesn't exist we assume none of them exist
ceph-authtool /tmp/${CLUSTER}.mon.keyring --create-keyring --import-keyring /etc/ceph/${CLUSTER}.client.admin.keyring
ceph-authtool /tmp/${CLUSTER}.mon.keyring --import-keyring /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring
ceph-authtool /tmp/${CLUSTER}.mon.keyring --import-keyring /var/lib/ceph/bootstrap-mds/${CLUSTER}.keyring
ceph-authtool /tmp/${CLUSTER}.mon.keyring --import-keyring /var/lib/ceph/bootstrap-rgw/${CLUSTER}.keyring
ceph-authtool /tmp/${CLUSTER}.mon.keyring --import-keyring /etc/ceph/${CLUSTER}.mon.keyring
# Make the monitor directory
mkdir -p /var/lib/ceph/mon/${CLUSTER}-${MON_NAME}
# Prepare the monitor daemon's directory with the map and keyring
ceph-mon --mkfs -i ${MON_NAME} ${monmapOpt} --keyring /tmp/${CLUSTER}.mon.keyring
# Clean up the temporary key
rm /tmp/${CLUSTER}.mon.keyring
fi
# start MON
exec /usr/bin/ceph-mon ${CEPH_OPTS} -d -i ${MON_NAME} --public-addr "${MON_IP}:6789"
}
################
# OSD (common) #
################
function start_osd {
get_config
check_config
if [ ${CEPH_GET_ADMIN_KEY} -eq "1" ]; then
get_admin_key
check_admin_key
fi
case "$OSD_TYPE" in
directory)
osd_directory
;;
disk)
osd_disk
;;
activate)
osd_activate
;;
prepare)
osd_disk_prepare
;;
*)
if [[ ! -d /var/lib/ceph/osd || -n "$(find /var/lib/ceph/osd -prune -empty)" ]]; then
echo "No bootstrapped OSDs found; trying ceph-disk"
osd_disk
else
if [ -z "${OSD_DEVICE}" ]; then
echo "Bootstrapped OSD(s) found; using OSD directory"
osd_directory
else
echo "Bootstrapped OSD(s) found; using ${OSD_DEVICE}"
osd_activate
fi
fi
;;
esac
}
#################
# OSD_DIRECTORY #
#################
function osd_directory {
if [[ ! -d /var/lib/ceph/osd ]]; then
echo "ERROR- could not find any OSD, did you bind mount the OSD data directory?"
echo "ERROR- use -v <host_osd_data_dir>:<container_osd_data_dir>"
exit 1
fi
# check if anything is there, if not create an osd with directory
if [[ -n "$(find /var/lib/ceph/osd -prune -empty)" ]]; then
echo "Creating osd with ceph osd create"
OSD_ID=$(ceph osd create)
if [ "$OSD_ID" -eq "$OSD_ID" ] 2>/dev/null; then
echo "OSD created with ID: ${OSD_ID}"
else
echo "OSD creation failed: ${OSD_ID}"
exit 1
fi
# create the folder
mkdir -p /var/lib/ceph/osd/${CLUSTER}-${OSD_ID}
echo "created folder /var/lib/ceph/osd/${CLUSTER}-${OSD_ID}"
fi
for OSD_ID in $(ls /var/lib/ceph/osd | awk 'BEGIN { FS = "-" } ; { print $2 }'); do
if [ -n "${JOURNAL_DIR}" ]; then
OSD_J="${JOURNAL_DIR}/journal.${OSD_ID}"
else
if [ -n "${JOURNAL}" ]; then
OSD_J=${JOURNAL}
else
OSD_J=/var/lib/ceph/osd/${CLUSTER}-${OSD_ID}/journal
fi
fi
# Check to see if our OSD has been initialized
if [ ! -e /var/lib/ceph/osd/${CLUSTER}-${OSD_ID}/keyring ]; then
# Create OSD key and file structure
ceph-osd ${CEPH_OPTS} -i $OSD_ID --mkfs --mkkey --mkjournal --osd-journal ${OSD_J}
if [ ! -e /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring ]; then
ceph auth get client.bootstrap-osd -o /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring
fi
timeout 10 ceph ${CEPH_OPTS} --name client.bootstrap-osd --keyring /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring health || exit 1
# Add the OSD key
ceph ${CEPH_OPTS} --name client.bootstrap-osd --keyring /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring auth add osd.${OSD_ID} -i /var/lib/ceph/osd/${CLUSTER}-${OSD_ID}/keyring osd 'allow *' mon 'allow profile osd'
# Add the OSD to the CRUSH map
if [ ! -n "${HOSTNAME}" ]; then
echo "HOSTNAME not set; cannot add OSD to CRUSH map"
exit 1
fi
OSD_WEIGHT=$(df -P -k /var/lib/ceph/osd/${CLUSTER}-$OSD_ID/ | tail -1 | awk '{ d= $2/1073741824 ; r = sprintf("%.2f", d); print r }')
ceph ${CEPH_OPTS} --name=osd.${OSD_ID} --keyring=/var/lib/ceph/osd/${CLUSTER}-${OSD_ID}/keyring osd crush create-or-move -- ${OSD_ID} ${OSD_WEIGHT} ${CRUSH_LOCATION}
fi
#TODO: use systemd instead
#service ceph start osd
exec ceph-osd ${CEPH_OPTS} -f -d -i ${OSD_ID} --osd-journal ${OSD_J} -k /var/lib/ceph/osd/ceph-${OSD_ID}/keyring
done
}
#########################
# OSD_CEPH_DISK_PREPARE #
#########################
function osd_disk_prepare {
if [[ -z "${OSD_DEVICE}" ]];then
echo "ERROR- You must provide a device to build your OSD ie: /dev/sdb"
exit 1
fi
if [ ! -e /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring ]; then
echo "ERROR- /var/lib/ceph/bootstrap-ods/${CLUSTER}.keyring must exist. You can extract it from your current monitor by running 'ceph auth get client.bootstrap-ods -o /var/lib/ceph/bootstrap-ods/${CLUSTER}.keyring'"
exit 1
fi
timeout 10 ceph ${CEPH_OPTS} --name client.bootstrap-osd --keyring /var/lib/ceph/bootstrap-osd/${CLUSTER}.keyring health || exit 1
mkdir -p /var/lib/ceph/osd
# TODO:
# - add device format check (make sure only one device is passed
if [[ "$(parted --script ${OSD_DEVICE} print | egrep '^ 1.*ceph data')" && ${OSD_FORCE_ZAP} -ne "1" ]]; then
echo "ERROR- It looks like this device is an OSD, set OSD_FORCE_ZAP=1 to use this device anyway and zap its content"
exit 1
elif [[ "$(parted --script ${OSD_DEVICE} print | egrep '^ 1.*ceph data')" && ${OSD_FORCE_ZAP} -eq "1" ]]; then
ceph-disk -v zap ${OSD_DEVICE}
fi
if [[ ! -z "${OSD_JOURNAL}" ]]; then
ceph-disk -v prepare ${OSD_DEVICE} ${OSD_JOURNAL}
else
ceph-disk -v prepare ${OSD_DEVICE}
fi
}
#################
# OSD_CEPH_DISK #
#################
function osd_disk {
osd_disk_prepare
osd_activate
}
##########################
# OSD_CEPH_DISK_ACTIVATE #
##########################
function osd_activate {
if [[ -z "${OSD_DEVICE}" ]];then
echo "ERROR- You must provide a device to build your OSD ie: /dev/sdb"
exit 1
fi
timeout 10 bash -c "while [ ! -e ${OSD_DEVICE}1 ]; do sleep 1; done"
mkdir -p /var/lib/ceph/osd
ceph-disk -v activate ${OSD_DEVICE}1
OSD_ID=$(cat /var/lib/ceph/osd/$(ls -ltr /var/lib/ceph/osd/ | tail -n1 | awk -v pattern="$CLUSTER" '$0 ~ pattern {print $9}')/whoami)
OSD_WEIGHT=$(df -P -k /var/lib/ceph/osd/${CLUSTER}-$OSD_ID/ | tail -1 | awk '{ d= $2/1073741824 ; r = sprintf("%.2f", d); print r }')
ceph ${CEPH_OPTS} --name=osd.${OSD_ID} --keyring=/var/lib/ceph/osd/${CLUSTER}-${OSD_ID}/keyring osd crush create-or-move -- ${OSD_ID} ${OSD_WEIGHT} ${CRUSH_LOCATION}
# ceph-disk activiate has exec'ed /usr/bin/ceph-osd ${CEPH_OPTS} -f -d -i ${OSD_ID}
# wait till ceph-osd exit
OSD_PID=$(ps -ef |grep ceph-osd |grep osd.${OSD_ID} |awk '{print $2}')
if [ ! -z ${OSD_ID} ]; then
while [ -e /proc/${OSD_PID} ]; do sleep 1;done
else
exec /usr/bin/ceph-osd ${CEPH_OPTS} -f -d -i ${OSD_ID}
fi
}
#######
# MDS #
#######
function start_mds {
get_config
check_config
# Check to see if we are a new MDS
if [ ! -e /var/lib/ceph/mds/${CLUSTER}-${MDS_NAME}/keyring ]; then
mkdir -p /var/lib/ceph/mds/${CLUSTER}-${MDS_NAME}
if [ -e /etc/ceph/${CLUSTER}.client.admin.keyring ]; then
KEYRING_OPT="--name client.admin --keyring /etc/ceph/${CLUSTER}.client.admin.keyring"
elif [ -e /var/lib/ceph/bootstrap-mds/${CLUSTER}.keyring ]; then
KEYRING_OPT="--name client.bootstrap-mds --keyring /var/lib/ceph/bootstrap-mds/${CLUSTER}.keyring"
else
echo "ERROR- Failed to bootstrap MDS: could not find admin or bootstrap-mds keyring. You can extract it from your current monitor by running 'ceph auth get client.bootstrap-mds -o /var/lib/ceph/bootstrap-mds/${CLUSTER}.keyring'"
exit 1
fi
timeout 10 ceph ${CEPH_OPTS} $KEYRING_OPT health || exit 1
# Generate the MDS key
ceph ${CEPH_OPTS} $KEYRING_OPT auth get-or-create mds.$MDS_NAME osd 'allow rwx' mds 'allow' mon 'allow profile mds' -o /var/lib/ceph/mds/${CLUSTER}-${MDS_NAME}/keyring
fi
# NOTE (leseb): having the admin keyring is really a security issue
# If we need to bootstrap a MDS we should probably create the following on the monitors
# I understand that this handy to do this here
# but having the admin key inside every container is a concern
# Create the Ceph filesystem, if necessary
if [ $CEPHFS_CREATE -eq 1 ]; then
get_admin_key
check_admin_key
if [[ "$(ceph fs ls | grep -c name:.${CEPHFS_NAME},)" -eq "0" ]]; then
# Make sure the specified data pool exists
if ! ceph ${CEPH_OPTS} osd pool stats ${CEPHFS_DATA_POOL} > /dev/null 2>&1; then
ceph ${CEPH_OPTS} osd pool create ${CEPHFS_DATA_POOL} ${CEPHFS_DATA_POOL_PG}
fi
# Make sure the specified metadata pool exists
if ! ceph ${CEPH_OPTS} osd pool stats ${CEPHFS_METADATA_POOL} > /dev/null 2>&1; then
ceph ${CEPH_OPTS} osd pool create ${CEPHFS_METADATA_POOL} ${CEPHFS_METADATA_POOL_PG}
fi
ceph ${CEPH_OPTS} fs new ${CEPHFS_NAME} ${CEPHFS_METADATA_POOL} ${CEPHFS_DATA_POOL}
fi
fi
# NOTE: prefixing this with exec causes it to die (commit suicide)
/usr/bin/ceph-mds ${CEPH_OPTS} -d -i ${MDS_NAME}
}
#######
# RGW #
#######
function start_rgw {
get_config
check_config
if [ ${CEPH_GET_ADMIN_KEY} -eq "1" ]; then
get_admin_key
check_admin_key
fi
# Check to see if our RGW has been initialized
if [ ! -e /var/lib/ceph/radosgw/${RGW_NAME}/keyring ]; then
mkdir -p /var/lib/ceph/radosgw/${RGW_NAME}
if [ ! -e /var/lib/ceph/bootstrap-rgw/${CLUSTER}.keyring ]; then
echo "ERROR- /var/lib/ceph/bootstrap-rgw/${CLUSTER}.keyring must exist. You can extract it from your current monitor by running 'ceph auth get client.bootstrap-rgw -o /var/lib/ceph/bootstrap-rgw/${CLUSTER}.keyring'"
exit 1
fi
timeout 10 ceph ${CEPH_OPTS} --name client.bootstrap-rgw --keyring /var/lib/ceph/bootstrap-rgw/${CLUSTER}.keyring health || exit 1
# Generate the RGW key
ceph ${CEPH_OPTS} --name client.bootstrap-rgw --keyring /var/lib/ceph/bootstrap-rgw/${CLUSTER}.keyring auth get-or-create client.rgw.${RGW_NAME} osd 'allow rwx' mon 'allow rw' -o /var/lib/ceph/radosgw/${RGW_NAME}/keyring
fi
if [ "$RGW_REMOTE_CGI" -eq 1 ]; then
/usr/bin/radosgw -d ${CEPH_OPTS} -n client.rgw.${RGW_NAME} -k /var/lib/ceph/radosgw/$RGW_NAME/keyring --rgw-socket-path="" --rgw-frontends="fastcgi socket_port=$RGW_REMOTE_CGI_PORT socket_host=$RGW_REMOTE_CGI_HOST"
else
/usr/bin/radosgw -d ${CEPH_OPTS} -n client.rgw.${RGW_NAME} -k /var/lib/ceph/radosgw/$RGW_NAME/keyring --rgw-socket-path="" --rgw-frontends="civetweb port=$RGW_CIVETWEB_PORT"
fi
}
###########
# RESTAPI #
###########
function start_restapi {
get_config
check_config
# Ensure we have the admin key
get_admin_key
check_admin_key
# Check to see if we need to add a [client.restapi] section; add, if necessary
if [[ ! "$(egrep "\[client.restapi\]" /etc/ceph/${CLUSTER}.conf)" ]]; then
cat <<ENDHERE >>/etc/ceph/${CLUSTER}.conf
[client.restapi]
public addr = ${RESTAPI_IP}:${RESTAPI_PORT}
restapi base url = ${RESTAPI_BASE_URL}
restapi log level = ${RESTAPI_LOG_LEVEL}
log file = ${RESTAPI_LOG_FILE}
ENDHERE
fi
# start ceph-rest-api
exec /usr/bin/ceph-rest-api ${CEPH_OPTS} -n client.admin
}
###############
# CEPH_DAEMON #
###############
# Normalize DAEMON to lowercase
CEPH_DAEMON=$(echo ${CEPH_DAEMON} |tr '[:upper:]' '[:lower:]')
# If we are given a valid first argument, set the
# CEPH_DAEMON variable from it
case "$CEPH_DAEMON" in
mds)
start_mds
;;
mon)
start_mon
;;
osd)
start_osd
;;
osd_directory)
OSD_TYPE="directory"
start_osd
;;
osd_ceph_disk)
OSD_TYPE="disk"
start_osd
;;
osd_ceph_disk_prepare)
OSD_TYPE="prepare"
start_osd
;;
osd_ceph_disk_activate)
OSD_TYPE="activate"
start_osd
;;
rgw)
start_rgw
;;
restapi)
start_restapi
;;
*)
if [ ! -n "$CEPH_DAEMON" ]; then
echo "ERROR- One of CEPH_DAEMON or a daemon parameter must be defined as the name "
echo "of the daemon you want to deploy."
echo "Valid values for CEPH_DAEMON are MON, OSD, OSD_DIRECTORY, OSD_CEPH_DISK, OSD_CEPH_DISK_PREPARE, OSD_CEPH_DISK_ACTIVATE, MDS, RGW, RESTAPI"
echo "Valid values for the daemon parameter are mon, osd, osd_directory, osd_ceph_disk, osd_ceph_disk_prepare, osd_ceph_disk_activate, mds, rgw, restapi"
exit 1
fi
;;
esac
exit 0