#!/bin/bash

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 creates a snapshot of a Pure storage volume, provding it with an
appropriate label and retaining it for the number of days specified
in the source volume's Protection Group setting.

DEFAULT EXPIRY TIME: Source volume's Protection Group value

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

NOTE:
  /opt/pureutils/snapshot is used as a source by 
  "pure_expire_snapshot_deletion" to delete expired snapshots.
  Please make sure the specific path has rwxr-x-r-x permissions.

OPTIONS:
  -h                               Show this message
  -s source_volume                 Name of the volume from which the snapshot 
                                   is to be created, OR
  -w source_volume_wwn             WWN of the volume from which the snapshot 
                                   is to be created
  -l label                         Label for the snapshot
                                   - alphanumeric chars only
                                   - Max 30 characters long
                                   - Min 2 characters long
                                   - No whitespace
 -r retention                      Time for snapshot to be kept, in days
 -k /path/to/secrets.json          Secrets file containing URL and credentials 
                                   to access the Pure storage

E.g.
1. $0 -s TEST123 -l "snap" -r 2  -k /path/to/secrets.json
2. $0 -w 3624a9170a99a32b3267c47150005fa7a -l "snap" -r 2 -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:w:l:r:k:" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    s)
      source=$OPTARG
      ;;
    w)
      source_wwn=$OPTARG
     ;;
    l)
      suffix=$OPTARG
      ;;
    r)
      retention=$OPTARG
      ;;
    k)
      secrets=$OPTARG
      ;;
    ?)
      usage
      exit
      ;;
  esac
done

# check required arguments
if [[ ( -z "$source" && -z "$source_wwn" ) || ( "$source" && "$source_wwn" )  || ( -z "$suffix" ) || ( -z "$retention" ) || ( -z "$secrets" ) ]]
then
   usage
   exit
fi

if [[ ! -d /opt/pureutils/snapshot ]]
then
  mkdir -p /opt/pureutils/snapshot
fi

today=$(date '+%C%y%m%d')
expire_date=$(date '+%C%y%m%d' -d "+"$retention" days")
complete_suffix=$suffix"-"$expire_date

# Call ucampurestorage package
if [[ "$source" ]]
then
  result=$($ucampurestorage --file $secrets snapshot create --srcvol $source --suffix $complete_suffix)
elif  [[ "$source_wwn" ]]
then
  result=$($ucampurestorage --file $secrets snapshot create --srcwwn $source_wwn --suffix $complete_suffix)
  scrwwn=$(echo $source_wwn |rev |cut -c1-24 |rev)
  source=$($ucampurestorage --file $secrets list --object volumes |grep -i $scrwwn |awk -F"'name':" '{print $2}' |cut -d, -f1 |awk '{print $1}' |sed s/\'//g)
fi

if [[ $result == *"[Command succeeded - Returns True]"* ]]
then
    echo "Command succeeded."
    echo $source.$complete_suffix >> /opt/pureutils/snapshot/$today.puresnap
    exit 0
else
    echo "Command failed. Please check the ucampurestorage log file in /var/log/ucampurestorage/."
    exit 1
fi
