Skip to content

Commit cbd49b4

Browse files
authored
Improvements to the pv/create-pv-nfs.sh script
This turns the pv/create-pv-nfs.sh script into a more tunable experience. This includes adding in flags for specifying how to create the NFS PVs, including: - -c / --count -- how many PVs to provision - -m / --nfs-mount -- the NFS mount path - -i / --nfs-ip -- the IP address of the NFS server - -n / --name -- the name format for the PV - -o / --old-name -- the name format of the PVs that are being removed Or you can use `-h` to print it out.
1 parent 5bdf9f2 commit cbd49b4

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

pv/create-pv-nfs.sh

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# use NFS as the persistent volume storage area. It is **not** intended for
1717
# production.
1818
#
19-
# This script makes some assumptions, i.e:
19+
# By default this script makes some assumptions, i.e:
2020
#
2121
# - You have sudo
2222
# - You have your NFS filesystem mounted to the location you are running this
@@ -25,17 +25,90 @@
2525
# - Your PV names will be one of "crunchy-pvNNN" where NNN is a natural number
2626
# - Your NFS UID:GID is "nfsnobody:nfsnobody", which correspunds to "65534:65534"
2727
#
28+
# If you want to modify this script defaults, execute the script with -h argument to see the usage help
2829
# And awaaaay we go...
30+
2931
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
32+
PGO_NFS_IP=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
33+
PV_NAME_INPUT="crunchy-pv"
34+
OLD_PV_NAME_INPUT="crunchy-pv"
35+
SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
36+
PV_COUNT=10
37+
NFS_MOUNT_PATH='/nfsfileshare'
38+
39+
usage(){
40+
echo "Usage: ./${SCRIPT_NAME} <options>"
41+
echo "
42+
Options :
43+
-n|--name persistent volume name. default is crunchy-pv
44+
-v|--old-name old persisten volume name. Useful when you want to delete old PVs and use a new name.
45+
default is crunchy-pv
46+
-c|--count PV count. how many PVs to create. default is 10
47+
-m|--nfs-mount nfs mount path. default is /nfsfileshare
48+
-i|--nfs-ip nfs ip. default is ${PGO_NFS_IP}
49+
-h|--help show usage
50+
"
51+
exit 1
52+
53+
}
54+
55+
if [[ $EUID > 0 ]]
56+
then echo "ERROR: Please run as root or use sudo"
57+
usage
58+
fi
59+
60+
opts=$(getopt \
61+
-o n:o:c:hm:i: \
62+
--long 'name:,old-name:,count:,help,nfs-mount:,nfs-ip: ' \
63+
--name "${SCRIPT_NAME}" \
64+
-- "$@"
65+
)
66+
67+
eval set --$opts
68+
while [[ $# -gt 0 ]]; do
69+
case "$1" in
70+
-h|--help)
71+
usage
72+
;;
73+
-m|--nfs-mount)
74+
NFS_MOUNT_PATH=$2
75+
shift 2
76+
;;
77+
-n|--name)
78+
PV_NAME_INPUT=$2
79+
OLD_PV_NAME_INPUT=$2
80+
shift 2
81+
;;
82+
-o|--old-name)
83+
OLD_PV_NAME_INPUT=$2
84+
shift 2
85+
;;
86+
-c|--count)
87+
PV_COUNT=$2
88+
shift 2
89+
;;
90+
-i|--nfs-ip)
91+
PGO_NFS_IP=$2
92+
shift 2
93+
;;
94+
*)
95+
break
96+
;;
97+
esac
98+
done
99+
100+
101+
PGO_CMD="kubectl"
30102

31103
echo "create the test PV and PVC using the NFS dir"
32-
for i in {1..160}
104+
for i in $(seq 1 $PV_COUNT)
33105
do
34-
PV_NAME="crunchy-pv${i}"
35-
NFS_PV_PATH="/nfsfileshare/${PV_NAME}"
36-
37-
echo "deleting PV ${PV_NAME}"
38-
$PGO_CMD delete pv "${PV_NAME}"
106+
PV_NAME="${PV_NAME_INPUT}${i}"
107+
OLD_PV_NAME="${OLD_PV_NAME_INPUT}${i}"
108+
NFS_PV_PATH="${NFS_MOUNT_PATH}/${PV_NAME}"
109+
110+
echo "deleting PV ${OLD_PV_NAME}"
111+
$PGO_CMD delete pv "${OLD_PV_NAME}"
39112
sudo rm -rf "${NFS_PV_PATH}"
40113

41114
# this is the manifest used to create the persistent volumes
@@ -67,5 +140,6 @@ EOF
67140
sudo chmod ugo=rwx "${NFS_PV_PATH}"
68141

69142
# create the new persistent volume
143+
echo "creating PV ${PV_NAME}"
70144
echo $MANIFEST | $PGO_CMD create -f -
71145
done

0 commit comments

Comments
 (0)