Skip to content

Commit

Permalink
renaming a_commands to a-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmattil committed Oct 2, 2019
1 parent e5e79f8 commit 4c65630
Show file tree
Hide file tree
Showing 10 changed files with 3,228 additions and 0 deletions.
341 changes: 341 additions & 0 deletions a-delete
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
#!/bin/bash

#default user
user=($USER)

#read static variables
inst_root=$(dirname $(readlink -f $0))
source $inst_root/a_env_conf

#local variables
object_name=("")
print_help=(0)
os_project_name=("none")
input_def=("")
mode=("swift")
show_filelist=(0)
query=("")
object_with_bucket=(0)
remove_bucket=(0)
force=(0)

#Process command line
while [[ $# -ge 1 ]]
do
case "$1" in
'--object' | '-o' )
# query file
object_name=($2)
shift
shift
;;
'--project' | '-p' )
os_project_name=($2)
shift
shift
;;

'--bucket' | '-b' )
object_with_bucket=(1)
shift
;;
'--force' | '-f')
force=(1)
shift
;;
'--user' | '-u')
user=($2)
shift
shift
;;
'--rmb')
remove_bucket=(1)
shift
;;
'-h' | '--help' )
print_help=(1)
shift
;;
*)
if [[ $object_name != "" ]]; then
echo "unknown option: $1"
echo "Object name is: $object"
exit 1
fi
object_name=("$1")
shift # No more switches
;;
esac
done

#if [[ $object_name == "" ]]
#then
# echo "Please give the name of the object to be deleted:"
# read object_name
#fi


if [ $print_help -eq 1 ]; then
cat <<EOF
This tool is used to remove data that has been uploaded to Allas service using the a-put command.
The basic syntax of the comand is:
a-delete object_name
Options:
-p, --project <project_ID> Delete objects form the buckets of the defined project in stead of the currently configured project.
-b --bucket Object name includes bucket name and the command does not try to use the default bucket names.
-u, --user <username> Option allows you to assign a user account that is used to confoirm the object ownership.
-f, --force Don't as confirmation when deleting a file
Related commands: a-put, a-get, a-find, a-info
EOF

fi

#Configure rclone
if [ ! -e $HOME/.config/rclone ]
then
mkdir -p $HOME/.config/rclone
fi
if [ -e $HOME/.config/rclone/rclone.conf ]
then
rc_check=$(grep -c $storage_server $HOME/.config/rclone/rclone.conf)
if [ $rc_check -lt 1 ]
then
echo '[pouta]' >> $HOME/.config/rclone/rclone.conf
echo 'type = swift' >> $HOME/.config/rclone/rclone.conf
echo 'env_auth = true' >> $HOME/.config/rclone/rclone.conf
fi
else
echo '[pouta]' >> $HOME/.config/rclone/rclone.conf
echo 'type = swift' >> $HOME/.config/rclone/rclone.conf
echo 'env_auth = true' >> $HOME/.config/rclone/rclone.conf
fi

#Assign project to be used if not defined
if [ $os_project_name == "none" ]
then
if [ -e $HOME/.allas_default ]
then
source $HOME/.allas_default
#echo $os_project_name $OS_PROJECT_NAME
if [[ $os_project_name != $OS_PROJECT_NAME ]]
then
echo "Switching allas configuration to use project $os_project_name"
source /appl/opt/allas_conf -user $user $os_project_name -keeppasswd
export OS_PROJECT_NAME=$os_project_name
fi
else
echo "Default project is not defined"
source /appl/opt/allas_conf -user $user
echo "os_project_name=$OS_PROJECT_NAME" > $HOME/.allas_default
echo "Default allas project is stored to \$HOME/.allas_default"
echo ""
fi
else
echo "Switching allas configuration to use project $os_project_name"
source /appl/opt/allas_conf -user $user $os_project_name -keeppasswd
export OS_PROJECT_NAME=$os_project_name
fi

#source $HOME/.allas_default
project_label=$(echo ${os_project_name} | sed -e s/"project_"/""/g)

#Check if connection works
if [[ $mode == "swift" ]]
then
test=$(swift stat 2> /dev/null | grep -c "Account:")
if [[ $test -lt 1 ]]
then
echo "No connection to Allas!"
echo "Please try setting the the connection again."
echo "by running command:"
echo ""
echo " source $allas_conf_path"
exit 1
fi
fi

#source /appl/opt/allas_conf
#input=("$1")

#The method to remove a bucket
if [[ $remove_bucket -eq 1 ]]; then
if [[ $(swift list $object_name | wc -l ) -gt 0 ]]; then
echo "Bucket $object_name is not empty!"
exit 1
else
if [[ $force -eq 1 ]]; then
swift delete $object_name
else
echo ""
echo "Are you sure want to remove bucket:"
echo $object_name
echo "[y/n]"
read ansver
if [[ $ansver == "y" ]] || [[ $ansver == "yes" ]]; then
swift delete ${object_name}
echo "${object_name} was deleted."
else
echo "${object_name} was not deleted."
fi
fi
fi
exit 1
fi
#the method to remove object
#check that object exists

check_os=$(rclone ls ${storage_server}:$object_name 2> /dev/null | wc -l)
if [ $check_os -ne 1 ]; then
echo "Object name: $object_name not found in $storage_server."
echo ""
objects=($(a_find -a | grep "$object_name"))
if [ ${#objects[@]} -eq 1 ]; then
echo "Did you mean this object:"
echo " $objects"
fi
if [ ${#objects[@]} -gt 1 ]; then
echo "Did you mean some of these objects:"
for on in ${objects[@]}
do
echo " $on"
done
fi
if [ ${#objects[@]} -eq 0 ]; then
echo "Try running command:"
echo " a_find $object_name"
fi
exit 1
else
object_with_bucket=(1)
fi




# check if object name contains bucket (if not defined with bucket)
if [[ $object_with_bucket == 0 ]]; then
if [ $(echo $object_name | grep -c "${user}-${project_label}-MISC" ) -eq 1 ]
then
object_with_bucket=(1)
bucket=("${user}-${project_label}-MISC")
fi
if [ $(echo $object_name | grep -c "${user}-${project_label}-HOME" ) -eq 1 ]
then
object_with_bucket=(1)
bucket=("${user}-${project_label}-HOME")
fi
if [ $(echo $object_name | grep -c "${user}-${project_label}-SCRATCH" ) -eq 1 ]
then
object_with_bucket=(1)
bucket=("${user}-${project_label}-HOME")
fi
fi

# check all buckets if the bucket is not deifned
if [ $object_with_bucket == 0 ]
then
buckets=("${user}-${project_label}-MISC ${user}-${project_label}-HOME ${user}-${project_label}-SCRATCH")
num_buckets=(0)

# go through the buckets
for bn in $buckets
do
bucket_found=$(rclone ls ${storage_server}:$bn/$object_name 2> /dev/null | wc -l)
if [ $bucket_found -eq 1 ]; then
echo "Bucket: $bn contains object:$object_name"
(( num_buckets = num_buckets + 1 ))
bucket=("$bn")
fi
done

if [[ $num_buckets -eq 0 ]]; then
echo ""
echo "Could not find object: $object_name "
exit 1
fi
if [[ $num_buckets -gt 1 ]]; then
echo ""
echo "Object $object_name was found in several buckets!"
echo "Please ionclude bucket name in the object name"
exit 1
fi

#add bucket to the object name
object_name=("$bucket/$object_name")
else
#check if object exists
bucket_found=$(rclone ls ${storage_server}:/$object_name 2> /dev/null | wc -l)
if [ $bucket_found -ne 1 ]; then
echo ""
echo "Object $object_name was not found"
echo ""
echo "You can list all the available objects with command:"
echo " a_find"
fi
fi


object_owner=$(rclone cat ${storage_server}:/${object_name}_ameta |grep "^user:" | awk '{print $2}')

if [[ $user != $object_owner ]]; then
echo ""
echo "Your username ($user) is not matching to the username assigned to the object ($object_owner)"
echo "You can use option -user to enforce the deteltion tool to use aspecific user name"
echo "${object_name} was not deleted"
exit 1
fi

#remove object
if [[ $force -eq 1 ]]; then
rclone deletefile ${storage_server}:/${object_name}
rclone deletefile ${storage_server}:/${object_name}_ameta
else
echo ""
echo "Are you sure want to remove object:"
echo $object_name
echo "[y/n]"
read ansver
if [[ $ansver == "y" ]] || [[ $ansver == "yes" ]]; then
rclone deletefile ${storage_server}:/${object_name}
rclone deletefile ${storage_server}:/${object_name}_ameta
echo "${object_name} was deleted."
else
echo "${object_name} was not deleted."
fi
fi

exit


























Loading

0 comments on commit 4c65630

Please sign in to comment.