-
Notifications
You must be signed in to change notification settings - Fork 8
/
runcom
71 lines (58 loc) · 2.27 KB
/
runcom
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
#!/bin/bash
# run commands
export LIB_DOCKER_PATH="$(dirname $(find $(readlink -f /lib) -iname libc.so.6))"
function docker(){
local DEBUG && [[ "$-" == *x* ]] && DEBUG="true" && set +x
local ARGS=""
for ARG in "$@"; do
if [[ -n "$(echo "${ARG}" | grep '{.*}' | jq . 2>/dev/null)" ]]; then
ARGS="${ARGS} \"$(echo ${ARG} | jq -c . | sed "s/\"/\\\\\"/g")\""
elif [[ "$(echo "${ARG}" | wc -l)" -gt "1" ]]; then
ARGS="${ARGS} \"$(echo "${ARG}" | sed "s/\"/\\\\\"/g")\""
else
ARGS="${ARGS} ${ARG}"
fi
done
[[ "${DEBUG}" == "true" ]] && set -x
bash -c "LD_LIBRARY_PATH=${LIB_DOCKER_PATH}:/host/lib $(which docker) ${ARGS}"
return "$?"
}
# Convert CIDR to submask format. e.g. 23 => 255.255.254.0
function cidr2mask(){
# Number of args to shift, 255..255, first non-255 byte, zeroes
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
[ $1 -gt 1 ] && shift $1 || shift
echo ${1-0}.${2-0}.${3-0}.${4-0}
}
# Convert IP address from decimal to heximal. e.g. 192.168.1.200 => 0xC0A801C8
function addr2hex(){
local IPADDR="$1"
echo "0x$(printf '%02X' ${IPADDR//./ } ; echo)"
}
# Convert IP/Mask to SubnetID/Mask. e.g. 192.168.1.200/24 => 192.168.1.0/24
function get_subnet_id_and_mask(){
local ADDR_AND_MASK="$1"
local IPMASK_PATTERN="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\/[0-9]\{1,2\}"
echo "${ADDR_AND_MASK}" | grep -o "${IPMASK_PATTERN}" &>/dev/null || { echo "Wrong Address/Mask pattern, exiting..." 1>&2; exit 1; }
local ADDR="$(echo "${ADDR_AND_MASK}" | cut -d '/' -f 1)"
local MASK="$(echo "${ADDR_AND_MASK}" | cut -d '/' -f 2)"
local HEX_ADDR=$(addr2hex "${ADDR}")
local HEX_MASK=$(addr2hex $(cidr2mask "${MASK}"))
local HEX_NETWORK=$(printf '%02X' $((${HEX_ADDR} & ${HEX_MASK})))
local NETWORK=$(printf '%d.' 0x${HEX_NETWORK:0:2} 0x${HEX_NETWORK:2:2} 0x${HEX_NETWORK:4:2} 0x${HEX_NETWORK:6:2})
SUBNET_ID="${NETWORK:0:-1}"
echo "${SUBNET_ID}/${MASK}"
}
function cleanup_old_kubeproxy_iptables(){
local HYPERKUBE_IMAGE_NAME="$1"
docker run \
--net=host \
--rm=true \
--privileged \
--volume=/var/run/dbus \
"${HYPERKUBE_IMAGE_NAME}" \
/hyperkube proxy \
--cleanup-iptables
}
export -f docker
export -f cidr2mask addr2hex get_subnet_id_and_mask