Add customization in maintenance branch
PROD-27329

Change-Id: I55b06df674309ad501e2abd9585d8e428077eb8e
diff --git a/cleanup.sh b/cleanup.sh
index 15e7ef9..9638842 100755
--- a/cleanup.sh
+++ b/cleanup.sh
@@ -1,15 +1,25 @@
 #!/bin/bash
 export OS_INTERFACE='admin'
-mask='rally_\|tempest_\|tempest-'
+mask='s_rally\|rally_\|tempest_\|tempest-'
 dry_run=false
 clean_projects=false
+make_servers_active=false
+
+stack_batch_size=10
+# granularity values: days,hours,minutes,seconds
+stack_granularity=days
+stack_granularity_value=1
 
 function show_help {
-    printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n\t-t\tDry run mode, no cleaning done\n\t-P\tForce cleaning of projects\n" ${mask}
+    printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n" ${mask}
+    printf "\t-t\tDry run mode, no cleaning done\n"
+    printf "\t-P\tForce cleaning of projects\n"
+    printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n"
+    printf "\t-F\tForce purge deleted stacks. Batch size: %s, >%s %s\n" ${stack_batch_size} ${stack_granularity_value} ${stack_granularity}
 }
 
 OPTIND=1 # Reset in case getopts has been used previously in the shell.
-while getopts "h?:tP" opt; do
+while getopts "h?:tSPF" opt; do
     case "$opt" in
     h|\?)
         show_help
@@ -18,9 +28,15 @@
     t)  dry_run=true
         printf "Running in dry-run mode\n"
         ;;
+    S)  make_servers_active=true
+        printf "Servers will be set to ACTIVE before deletion\n"
+        ;;
     P)  clean_projects=true
         printf "Project cleanning enabled\n"
         ;;
+    F)  purge_deleted_stacks=true
+        printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size
+        ;;
     esac
 done
 
@@ -34,7 +50,7 @@
     fi
     if [ -s ${cmds} ]; then
         echo "Processing $(cat ${cmds} | wc -l) commands"
-        #cat ${cmds} | openstack
+        cat ${cmds} | openstack
         truncate -s 0 ${cmds}
     fi
 }
@@ -65,8 +81,11 @@
 
 ### Servers
 function _clean_servers {
-    servers=( $(openstack server list --name ${mask} -c ID -f value) )
+    servers=( $(openstack server list -c ID -c Name -f value --all | grep "${mask}" | cut -d' ' -f1) )
     echo "-> ${#servers[@]} servers containing '${mask}' found"
+    if [ "$make_servers_active" = true ]; then
+        printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds}
+    fi
     printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
     _clean_and_flush
 }
@@ -181,11 +200,14 @@
     # 73      "software_configs:global_index": "rule:deny_everybody",
     # After this you will be able to use --all option
 
-    stacks=( $(openstack stack list --deleted --nested --hidden -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep ${mask} | cut -d' ' -f1) )
     echo "-> ${#stacks[@]} stacks containing '${mask}' found"
     printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
     printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
     _clean_and_flush
+    if [ "$purge_deleted_stacks" = true ]; then
+        heat-manage purge_deleted -g ${stack_granularity} -b ${stack_batch_size} ${stack_granularity_value} | wc -l | xargs -I{} echo "-> Purged {} stacks"
+    fi
 }
 
 ### Containers