Skip to content

Instantly share code, notes, and snippets.

@hodrigohamalho
Created November 20, 2019 13:14
Show Gist options
  • Save hodrigohamalho/683e042fae320fc85d566a3d3921b189 to your computer and use it in GitHub Desktop.
Save hodrigohamalho/683e042fae320fc85d566a3d3921b189 to your computer and use it in GitHub Desktop.
nfs-handler.sh
# @author Rodrigo Ramalho - hodrigohamalho@gmail.com / rramalho@redhat.com
# This script creates:
# - nfs directory
# - persistence volume on openshift
# - persistence claim on openshift
# Parameters:
# 1. Directory to be create (every names, pv, pvc, nfs will be based on this name)
# 2. Size in Gb
# 3. Project in which pvc will be created
# Usage: oc-nfs-volume jenkis-storage 5 desenvolvimento claim-name
# NOTE: Execute this on your master machine.
DIR=$1
SIZE=$2
PROJECT=$3
ROOT_NFS=/exports
CLAIM_NAME=$4
NFS_CONFIG_FILE=/etc/exports.d/openshift-ansible.exports
# NFS Server Host
HOST=$(hostname)
die () {
echo >&2 "$@"
exit 1
}
create-pv () {
cont=$(oc get pv | grep "^$DIR " | wc -l)
if [ $cont -eq 0 ];then
oc create -f -<< EOF
apiVersion: v1
kind: PersistentVolume
metadata:
name: $DIR
spec:
capacity:
storage: ${SIZE}Gi
accessModes:
- ReadWriteOnce
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
nfs:
server: $HOST
path: $ROOT_NFS/$DIR
EOF
else
echo "PV already exists, skipping creation..."
fi
}
create-pvc () {
cont=$(oc get pvc | grep "^claim-$DIR " | wc -l)
if [ $cont -eq 0 ];then
oc project $PROJECT && \
oc create -f -<< EOF
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: $CLAIM_NAME
spec:
accessModes:
- ReadWriteOnce
- ReadWriteMany
resources:
requests:
storage: ${SIZE}Gi
EOF
else
echo "PVC already exists, skipping creation..."
fi
}
pv-isvalid () {
cont=$(oc get pv | grep "^kratos " | wc -l)
if [ $cont -eq 0 ];then
return 0
else
return 1
fi
}
[ "$#" -eq 4 ] || die "Usage: $0 <directory> <size-in-gb> <project> <claim-name>"
if [ ! -d $ROOT_NFS/$DIR ]; then
mkdir -p "$ROOT_NFS/$DIR"
fi
chown -R nfsnobody: "$ROOT_NFS/$DIR"
chmod 777 "$ROOT_NFS/$DIR"
cont=$(grep "$ROOT_NFS/$DIR" $NFS_CONFIG_FILE|wc -l)
if [ $cont -eq 0 ]; then
echo "$ROOT_NFS/$DIR *(rw,root_squash)" >> $NFS_CONFIG_FILE
exportfs -a
else
echo "Directory $DIR already exists on $NFS_CONFIG_FILE. Skipping creation..."
fi
create-pv
create-pvc
echo "To attach to container use: oc volume dc <DC-NAME> --add --overwrite -t persistentVolumeClaim --claim-name=$CLAIM_NAME --name=<VOLUME_MOUNT_NAME>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment