Skip to content

Instantly share code, notes, and snippets.

@msnelling
Created August 21, 2017 15:35
Show Gist options
  • Save msnelling/8d79a1cebd4319df617e5233bb7733eb to your computer and use it in GitHub Desktop.
Save msnelling/8d79a1cebd4319df617e5233bb7733eb to your computer and use it in GitHub Desktop.
k8s-plugin-cifs
usage() {
err "Invalid usage. Usage: "
err "\t$0 init"
err "\t$0 mount <mount dir> <json params>"
err "\t$0 unmount <mount dir>"
exit 1
}
debug() {
date +"%Y-%m-%d %H:%M:%S DEBUG: $*" >>/var/log/solaise-cifs.log
}
err() {
date +"%Y-%m-%d %H:%M:%S ERROR: $*" >>/var/log/solaise-cifs.log
echo -ne $* 1>&2
}
log() {
date +"%Y-%m-%d %H:%M:%S INFO: $*" >>/var/log/solaise-cifs.log
echo -ne $* >&1
}
ismounted() {
debug "ismounted() on ${MNTPATH}"
MOUNT=`findmnt -n ${MNTPATH} 2>/dev/null | cut -d' ' -f1`
if [ "${MOUNT}" == "${MNTPATH}" ]; then
echo "1"
else
echo "0"
fi
}
domount() {
debug "mount() with...\n$1\n$2\n$3"
MNTPATH=$1
if ismounted ; then
log '{"status": "Success"}'
exit 0
fi
VOLUME_SRC=$(echo "$2"|"jq" -r '.source') # this is a json string ie : {"kubernetes.io/fsType":"","kubernetes.io/readwrite":"ro","source":"//xxxx.xxx.com/backup"}
READ_MODE=$(echo "$2"|"jq" -r '.["kubernetes.io/readwrite"]')
MOUNT_OPTIONS=$(echo "$2"|"jq" -r '.mountOptions // empty')
USERNAME=$(echo "$2"|"jq" -r '.["kubernetes.io/secret/username"] // empty'| base64 -d)
PASSWORD=$(echo "$2"|"jq" -r '.["kubernetes.io/secret/password"] // empty'| base64 -d)
ALL_OPTIONS="user=${USERNAME},pass=${PASSWORD},${READ_MODE}"
if [ -n "$MOUNT_OPTIONS" ]; then
ALL_OPTIONS="${ALL_OPTIONS},${MOUNT_OPTIONS}"
fi
mkdir -p ${MNTPATH} &> /dev/null
MOUNTCMD=mount.cifs' -o '${ALL_OPTIONS}' '${VOLUME_SRC}' '${MNTPATH}
debug "Running mount command: ${MOUNTCMD}"
eval $MOUNTCMD
if [ $? -ne 0 ]; then
err '{ "status": "Failure", "message": "Failed to mount at '${MNTPATH}' , user: '${USERNAME}' , '${VOLUME_SRC}' , cmd='${MOUNTCMD}', READ_MODE='${READ_MODE}' "}'
exit 1
fi
exit 0
}
unmount() {
debug "unmount() on $1"
MNTPATH=$1
if [ $(ismounted) -eq 0 ] ; then
log '{"status": "Success"}'
exit 0
fi
umount ${MNTPATH} &> /dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failed\", \"message\": \"Failed to unmount volume at ${MNTPATH}\"}"
exit 1
fi
log '{"status": "Success"}'
exit 0
}
op=$1
if ! command -v jq >/dev/null 2>&1; then
err "{ \"status\": \"Failure\", \"message\": \"'jq' binary not found. Please install jq package before using this driver\"}"
exit 1
fi
if [ "$op" = "init" ]; then
log '{"status": "Success", "capabilities": {"attach": false}}'
exit 0
fi
if [ $# -lt 2 ]; then
usage
fi
shift
case "$op" in
mount)
domount $*
;;
unmount)
unmount $*
;;
*)
#usage
log '{ "status": "Not supported" }'
exit 0
esac
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment