#!/bin/bash

###################################################################
# do_snapshot_volume
# Purpose: create a snapshot of a pure storage volume
#
#    Args: -s -> source volume name
#          -l -> Label for replay
#
#  Output: Log of actions
# Returns: TRUE for success, FALSE for failure
###################################################################
os_level=`cat /etc/os-release |grep REDHAT_SUPPORT_PRODUCT_VERSION |awk -F= '{print $2}' |sed s/\"//g |awk -F. '{print $1}'`
python38="/opt/rh/rh-python38"
if [[ "$os_level" == 7 ]]
then
  if [[ -d  "$python38" ]]
  then
    ucampurestorage="/opt/rh/rh-python38/root/usr/local/bin/ucampurestorage"
  else
    echo "Python 3.8 is not installed as per the recommendatations"
  fi
else
ucampurestorage="/usr/local/bin/ucampurestorage"
fi

echo "Invoked with: $0 $@"
echo "My PID is $$"


###################################################################
# Purpose: usage information
###################################################################
function usage()
{
cat << EOF
usage: $0 options

This script snapshots a pure storage volume, provding it appropriate label
and retaining it for the number of days specified in protection group.

DEFAULT EXPIRY TIME: Protection Groups of Source Volume

Restrictions for use:
  * The volume must be a purestorage volume
  * The volume must be mounted
  * There must be no processes listed by "fuser -cv" for the volume

OPTIONS:
  -h                        Show this message
  -s source volume          Name of the volume for which snapshot has to be taken.
  -l label                  Label for the snapshot
                              - alphanumeric chars only
                              - Max 30 characters long
                              - Min 2 characters long
                              - No whitespace
  -k /path/to/secrets.json  Secret file containing URL and credential to access Pure Storage

E.g.
   $0 -s TEST123 -l "snap01May2023"  -k /path/to/secrets.json

NB.
1. /path/to/secrets.json must be in the following format:
{
  "client_id": "pure_api_client_id",
  "key_id": "pure_api_key_id",
  "client_name": "pure_api_client_name",
  "storage": "purestorage.cam.ac.uk",
  "user": "pureuser",
  "password": "purepassword",
  "keyfile": "private.pem"
}

2. ucampurestorage must be installed and located under /usr/local/bin/
EOF
}

###################################################################
# Parse the args
###################################################################
while getopts "h:s:l:k:" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    s)
      source=$OPTARG
      ;;
    l)
      suffix=$OPTARG
      ;;
    k)
      secrets=$OPTARG
      ;;
    ?)
      usage
      exit
      ;;
  esac
done

# check required arguments
if [ -z "$source" ] || [ -z "$suffix" ] || [ -z "$secrets" ]
then
   usage
   exit
fi

# Call ucampurestorage package
result=$($ucampurestorage --file $secrets snapshot create --srcvol $source --suffix $suffix)

if [[ $result == *"[Command succeeded - Returns True]"* ]]
then
    echo "Command succeeded."
    exit 0
else
    echo "Command failed. Please check the ucampurestorage log file in /var/log/ucampurestorage/."
    exit 1
fi