forked from longhorn/longhorn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlhexec
executable file
·70 lines (57 loc) · 2.1 KB
/
lhexec
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
#!/usr/bin/env bash
NS="longhorn-system"
print_usage() {
echo "Usage: ${0} [|-h|--help] volume_name longhorn_commands_arguments"
echo ""
echo "Examples:"
echo " ${0} test-vol snapshot ls"
echo " ${0} test-vol info"
echo ""
echo "Note: Must have Longhorn installed in "longhorn-system" namespace and have access to "kubectl" and the namespace"
echo ""
exit 0
}
check_volume_exist(){
VOLUME_NAME=${1}
kubectl -n ${NS} get lhv ${VOLUME_NAME} > /dev/null 2>&1
if [[ ${?} -ne 0 ]]; then
echo "Err: Volume ${VOLUME_NAME} not found"
exit 1
fi
}
check_engine_state(){
VOLUME_NAME=${1}
LHE_STATE_FILTER="{.items[?(@.spec.volumeName==\"${VOLUME_NAME}\")].status.currentState}"
LHE_STATE=`kubectl -n ${NS} get lhe --output=jsonpath="${LHE_STATE_FILTER}"`
if [[ ${LHE_STATE} != "running" ]]; then
echo "Err: Longhorn engine for volume ${VOLUME_NAME} is not running"
exit 1
fi
}
exec_command() {
VOLUME_NAME=${1}
COMMAND_ARGS="${@:2}"
INSTANCE_MANAGER_NAME_FILTER="{.items[?(@.spec.volumeName==\"${VOLUME_NAME}\")].status.instanceManagerName}"
INSTANCE_MANAGER_NAME=`kubectl -n ${NS} get lhe --output=jsonpath="${INSTANCE_MANAGER_NAME_FILTER}"`
ENGINE_PORT_FILTER="{.items[?(@.spec.volumeName==\"${VOLUME_NAME}\")].status.port}"
ENGINE_PORT=`kubectl -n ${NS} get lhe --output=jsonpath="${ENGINE_PORT_FILTER}"`
LONGHORN_BIN_PATH=`kubectl -n ${NS} exec -it ${INSTANCE_MANAGER_NAME} -- bash -c "ps -eo command | grep \" ${VOLUME_NAME} \" | grep -v grep | awk '{ printf(\"%s\", \\$1)}'"`
kubectl -n ${NS} exec -it ${INSTANCE_MANAGER_NAME} -- bash -c "${LONGHORN_BIN_PATH} --url localhost:${ENGINE_PORT} ${COMMAND_ARGS}"
}
ARG=$1
case $ARG in
"" | "-h" | "--help")
print_usage
;;
*)
VOLUME_NAME=${ARG}
shift
COMMAND_ARGS="${@}"
if [[ ${COMMAND_ARGS} == "" ]]; then
COMMAND_ARGS="help"
fi
check_volume_exist ${VOLUME_NAME}
check_engine_state ${VOLUME_NAME}
exec_command ${VOLUME_NAME} ${COMMAND_ARGS}
;;
esac