Add unified Ceph CLI script

This change adds a script in /usr/local/bin that "wraps" the ceph
command so that ceph cluster queries look the same regardless of
which ceph backend is enabled ("ceph" or "ceph-rook).

Obtaining which backend is defined is done through flags
'/etc/platform/.node_rook_configured' and
'/etc/platform/.node_ceph_configured'. If none of them exist,
a message will be displayed that ceph is not enabled.

Test Plan:
- PASS: Build rook-ceph package
- PASS: Install the deb file on SX
        (with ceph defined and another with rook-ceph)
- PASS: Run ceph commands
- PASS: Validate bash completion
- PASS: Delete rook-ceph-tools pod and run ceph command
- PASS: Delete the defined backend flag
- PASS: Using "-o": ceph osd getcrushmap -o crushmap.bin
- PASS: Using "-i": ceph dashboard \
  ac-user-set-password admin -i password.txt

Story: 2011066
Task: 50435

Change-Id: Id40108d28f59c0b7b8a3dfb859ae599557b4c247
Signed-off-by: Erickson Silva de Oliveira <Erickson.SilvadeOliveira@windriver.com>
This commit is contained in:
Erickson Silva de Oliveira 2024-06-26 08:23:04 -03:00
parent cd79d4443a
commit f83e033af1
4 changed files with 190 additions and 0 deletions

View File

@ -4,6 +4,8 @@ export DH_VERBOSE = 1
export ROOT = debian/tmp
export APP_FOLDER = $(ROOT)/usr/local/share/applications/helm
export INITRD_DIR = $(ROOT)/etc/init.d
export LOCAL_BIN_DIR = $(ROOT)/usr/local/bin
export BASH_COMPLETION_DIR = $(ROOT)/etc/bash_completion.d
export DEB_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
export RELEASE = $(shell cat /etc/build.info | grep SW_VERSION | cut -d'"' -f2)
@ -63,11 +65,17 @@ override_dh_auto_install:
# Install the app tar file
install -d -m 755 $(APP_FOLDER)
install -d -m 755 $(INITRD_DIR)
install -d -m 755 $(LOCAL_BIN_DIR)
install -d -m 755 $(BASH_COMPLETION_DIR)
install -p -D -m 755 $(APP_TARBALL) $(APP_FOLDER)
install -m 750 files/rook-mon-exit.sh $(INITRD_DIR)/rook-mon-exit
install -m 750 files/ceph-wrapper.sh $(LOCAL_BIN_DIR)/ceph
install -m 644 files/bash_completion $(BASH_COMPLETION_DIR)/ceph
# Prevents dh_fixperms from changing the permissions defined in this file
override_dh_fixperms:
dh_fixperms --exclude etc/bash_completion.d/ceph
dh_fixperms --exclude etc/init.d/rook-mon-exit
dh_fixperms --exclude usr/local/bin/ceph
override_dh_usrlocal:

View File

@ -1,2 +1,4 @@
usr/local/share/applications/helm/*
usr/local/bin/ceph
etc/bash_completion.d/ceph
etc/init.d/rook-mon-exit

View File

@ -0,0 +1,50 @@
#
# Ceph - scalable distributed file system
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1, as published by the Free Software
# Foundation.
#
_ceph()
{
local options_noarg="-h --help -s --status -w --watch --watch-debug --watch-info --watch-sec --watch-warn --watch-error --version -v --verbose --concise"
local options_arg="-c --conf -i --in-file -o --out-file --id --user -n --name --cluster --admin-daemon --admin-socket -f --format --connect-timeout"
local cnt=${#COMP_WORDS[@]}
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ " -c --conf -i --in-file -o --out-file " =~ " ${prev} " ]]
then
#default autocomplete for options (file autocomplete)
compopt -o default
COMPREPLY=()
return 0
fi
if [[ "${cur:0:1}" == "-" ]] ;
then
COMPREPLY=( $(compgen -W "${options_noarg} ${options_arg}" -- $cur) )
return 0
fi
declare -a hint_args
for (( i=1 ; i<cnt ; i++ ))
do
#skip this word if it is option
if [[ " ${options_noarg} " =~ " ${COMP_WORDS[i]} " ]]
then
continue
fi
#skip this word and next if it is option with arg
if [[ " ${options_arg} " =~ " ${COMP_WORDS[i]} " ]]
then
((i++));
continue
fi
hint_args[$((i-1))]="${COMP_WORDS[i]}"
done
local IFS=$'\n'
COMPREPLY=( $(${COMP_WORDS[0]} --completion "${hint_args[@]}" 2>/dev/null) )
}
complete -F _ceph ceph

View File

@ -0,0 +1,130 @@
#!/bin/bash
# Copyright (c) 2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# This script is wrapper for ceph, providing access to
# bare-metal or rook, according to defined backend.
NODE_ROOK_CONFIGURED_FLAG="/etc/platform/.node_rook_configured"
NODE_CEPH_CONFIGURED_FLAG="/etc/platform/.node_ceph_configured"
PLATFORM_CONF="/etc/platform/platform.conf"
ROOK_CEPH_NAMESPACE="rook-ceph"
ROOK_CEPH_TOOLS_NAME="rook-ceph-tools"
KUBE_CONFIG="/etc/kubernetes/admin.conf"
# Logger setup
LOG_FACILITY=user
LOG_PRIORITY=info
ARGS="$*"
COMPLETION=false
CMD_DIR="/tmp"
function LOG {
if [ "${COMPLETION}" = false ]; then
logger -t "${0##*/}[$$]" -p ${LOG_FACILITY}.${LOG_PRIORITY} "$@"
echo "$@"
fi
}
function process_argument {
local FILENAME
local ARG=$1
FILENAME=$(basename -- "$ARG")
local REMOTE_FILE=${CMD_DIR}/${FILENAME}
ARGS="${ARGS/$ARG/$REMOTE_FILE}"
if [ "$2" = "in" ]; then
kubectl --kubeconfig ${KUBE_CONFIG} cp "${ARG}" \
-n ${ROOK_CEPH_NAMESPACE} "${ROOK_CEPH_TOOLS_POD}":"${CMD_DIR}"
elif [ "$2" = "out" ]; then
OUTPUT_REMOTE=$REMOTE_FILE
fi
}
for ((i=1;i<=$#;i++)); do
if [ "${!i}" = "-i" ] || [ "${!i}" = "--in-file" ]; then
$((i++))
INPUT_ARG=${!i};
fi
if [ "${!i}" = "-o" ] || [ "${!i}" = "--out-file" ]; then
$((i++))
OUTPUT_ARG=${!i};
fi
if [ "${!i}" = "-c" ] || [ "${!i}" = "--conf" ]; then
$((i++))
CONF_ARG=${!i};
fi
done;
if [[ "${ARGS}" == *" " ]]; then
COMPLETION=true
fi
if [ -f "${NODE_ROOK_CONFIGURED_FLAG}" ]; then
. $PLATFORM_CONF
if [[ $nodetype != "controller" ]]; then
LOG "Rook CLI access is only available on control plane hosts."
exit
fi
# Get rook-ceph-tools pod state
ROOK_CEPH_TOOLS_POD=$(kubectl --kubeconfig ${KUBE_CONFIG} \
get pods -n ${ROOK_CEPH_NAMESPACE} \
--selector=app=${ROOK_CEPH_TOOLS_NAME} \
--field-selector=status.phase==Running \
-o jsonpath="{.items[0].metadata.name}" 2>/dev/null)
RETURN_CODE=$?
if [ ${RETURN_CODE} -ne 0 ]; then
LOG "The rook toolbox is not running."
exit ${RETURN_CODE}
fi
if [ -z "${ARGS}" ]; then
# Interactive Rook toolbox
kubectl --kubeconfig ${KUBE_CONFIG} \
exec -it -n ${ROOK_CEPH_NAMESPACE} \
deploy/${ROOK_CEPH_TOOLS_NAME} -- /usr/bin/ceph
else
if [[ $INPUT_ARG ]]; then
process_argument "$INPUT_ARG" "in"
fi
if [[ $CONF_ARG ]]; then
process_argument "$CONF_ARG" "in"
fi
if [[ $OUTPUT_ARG ]]; then
process_argument "$OUTPUT_ARG" "out"
fi
if [ "${COMPLETION}" = true ]; then
ARGS+=" ''"
fi
# Execute single ceph command in Rook toolbox
kubectl --kubeconfig ${KUBE_CONFIG} exec \
-n ${ROOK_CEPH_NAMESPACE} deploy/${ROOK_CEPH_TOOLS_NAME} -- \
bash -c "cd ${CMD_DIR} && /usr/bin/ceph ${ARGS}"
if [[ $OUTPUT_ARG ]]; then
kubectl --kubeconfig ${KUBE_CONFIG} cp -n ${ROOK_CEPH_NAMESPACE} \
"${ROOK_CEPH_TOOLS_POD}":"${OUTPUT_REMOTE:1}" "${OUTPUT_ARG}"
fi
fi
elif [ -f "${NODE_CEPH_CONFIGURED_FLAG}" ]; then
if [ "${COMPLETION}" = true ]; then
/usr/bin/ceph ${ARGS} ''
else
/usr/bin/ceph ${ARGS}
fi
else
LOG "Ceph not enabled."
fi