Merge "Small change of tempest_version logic"
diff --git a/cleanup.sh b/cleanup.sh
index 9792873..3b6c112 100755
--- a/cleanup.sh
+++ b/cleanup.sh
@@ -2,66 +2,200 @@
 export OS_INTERFACE='admin'
 mask='rally_\|tempest_\|tempest-'
 
-echo "Starting. Using mask '$mask'"
+### Execute collected commands and flush the temp file
+function _clean_and_flush {
+    if [ -s ${cmds} ]; then
+        echo "Processing $(cat ${cmds} | wc -l) commands"
+        cat ${cmds} | openstack
+        truncate -s 0 ${cmds}
+    fi
+}
 
-echo "Delete users"
-for i in `openstack user list | grep $mask | awk '{print $2}'`; do openstack user delete $i; echo deleted $i; done
+### Users
+function _clean_users {
+    users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    echo "-> ${#users[@]} users containing '${mask}' found"
+    printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete roles"
-for i in `openstack role list | grep $mask | awk '{print $2}'`; do openstack role delete $i; echo deleted $i; done
+### Roles
+function _clean_roles {
+    roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    echo "-> ${#roles[@]} roles containing '${mask}' found"
+    printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
+    _clean_and_flush
+}
 
-#echo "Delete projects"
-#for i in `openstack project list | grep $mask | awk '{print $2}'`; do openstack project delete $i; echo deleted $i; done
+### Projects
+function _clean_projects {
+    projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    echo "-> ${#projects[@]} projects containing '${mask}' found"
+    printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete servers"
-for i in `openstack server list --all | grep $mask | awk '{print $2}'`; do openstack server delete $i; echo deleted $i; done
+### Servers
+function _clean_servers {
+    servers=( $(openstack server list --name ${mask} -c ID -f value) )
+    echo "-> ${#servers[@]} servers containing '${mask}' found"
+    printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Reset snapshot state and delete"
-for i in `openstack volume snapshot list --all | grep $mask | awk '{print $2}'`; do openstack snapshot set --state available $i; echo snapshot reset state is done for $i; done
-for i in `openstack volume snapshot list --all | grep $mask | awk '{print $2}'`; do openstack snapshot set --state available $i; echo deleted $i; done
+### Reset snapshot state and delete
+function _clean_snapshots {
+    snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
+    printf "%s\n" ${snapshots[@]} | xargs -I{} echo snapshot set --state available {} >>${cmds}
+    printf "%s\n" ${snapshots[@]} | xargs -I{} echo snapshot delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Reset volume state and delete"
-for i in `openstack volume list --all | grep $mask | awk '{print $2}'`; do openstack volume set --state available $i; echo reset state is done for $i; done
-for i in `openstack volume list --all | grep $mask | awk '{print $2}'`; do openstack volume delete $i; echo deleted $i; done
+function _clean_volumes {
+    volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
+    echo "-> ${#volumes[@]} volumes containing '${mask}' found"
+    printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
+    printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete volume types"
-for i in `openstack volume type list | grep $mask | awk '{print $2}'`; do openstack volume type delete $i; done
+### Volume types
+function _clean_volume_types {
+    vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
+    printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete images"
-for i in `openstack image list | grep $mask | awk '{print $2}'`; do openstack image delete $i; echo deleted $i; done
+### Images
+function _clean_images {
+    images=( $(openstack image list --name ${mask} -c ID -f value) )
+    echo "-> ${#images[@]} images containing '${mask}' found"
+    printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete sec groups"
-for i in `openstack security group list --all | grep $mask | awk '{print $2}'`; do openstack security group delete $i; echo deleted $i; done
+### Sec groups
+function _clean_sec_groups {
+# openstack project list -c ID -c Name -f value | grep rally | cut -d' ' -f1 | xargs -I{} /bin/bash -c "openstack security group list | grep {}"
+    projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
+    sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
+    echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
+    printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete keypairs"
-for i in `openstack keypair list | grep $mask | awk '{print $2}'`; do openstack keypair delete $i; echo deleted $i; done
+### Keypairs
+function _clean_keypairs {
+    keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
+    echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
+    printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete ports"
-for i in `openstack port list | grep $mask | awk '{print $2}'`; do openstack port delete $i; done
+### Routers and Networks
+function _clean_routers_and_networks {
+    routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
+    if [ ${#routers[@]} -eq 0 ]; then
+        echo "-> No routers containing '${mask}' found"
+    else
+        echo "-> ${#routers[@]} routers containing '${mask}' found"
+        echo "...unsetting gateways"
+        printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} | openstack
+        for router in ${routers[@]}; do
+            r_ports=( $(openstack port list --router ${router} -f value -c ID) )
+            if [ ${#r_ports[@]} -eq 0 ]; then
+                echo "...no ports to unplug for ${router}"
+            else
+                for r_port in ${r_ports[@]}; do
+                    echo "...removing port '${r_port}' from router '${router}'"
+                    openstack router remove port ${router} ${r_port}
+                done
+            fi
+        done
+        printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
+    fi
 
-echo "Delete Router ports (experimental)"
-neutron router-list|grep $mask|awk '{print $2}'|while read line; do echo $line; neutron router-port-list $line|grep subnet_id|awk '{print $11}'|sed 's/^\"//;s/\",//'|while read interface; do neutron router-interface-delete $line $interface; done; done
+    networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
+    if [ ${#networks[@]} -eq 0 ]; then
+        echo "-> No networks containing '${mask}' found"
+    else
+        ports=()
+        subnets=()
+        for((idx=0;idx<${#networks[@]};idx++)) do
+            ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
+            subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
+            echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
+        done
+        printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
+        printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
+        echo network delete ${networks[@]} >>${cmds}
+        echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
+    fi
+    _clean_and_flush
+}
 
-echo "Delete subnets"
-for i in `openstack subnet list | grep $mask | awk '{print $2}'`; do openstack subnet delete $i; done
+### Regions
+function _clean_regions {
+    regions=( $(openstack region list -c Region -f value | grep ${mask}) )
+    echo "-> ${#regions[@]} regions containing '${mask}' found"
+    printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete nets"
-for i in `openstack network list | grep $mask | awk '{print $2}'`; do openstack network delete $i; done
+### Stacks
+function _clean_stacks {
+    # By default openstack denies use of global_index for everyone.
+    # In case you want to have handy cleanup, consider updating policy.json here:
+    # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
+    # 48      "stacks:global_index": "rule:deny_everybody",
+    # 73      "software_configs:global_index": "rule:deny_everybody",
+    # After this you will be able to use --all option
 
-echo "Delete routers"
-for i in `openstack router list | grep $mask | awk '{print $2}'`; do openstack router delete $i; done
+    stacks=( $(openstack stack list --deleted --nested --hidden -c ID -c 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
+}
 
-echo "Delete regions"
-for i in `openstack region list | grep $mask | awk '{print $2}'`; do openstack region delete $i; echo deleted $i; done
+### Containers
+function _clean_containers {
+    containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
+    echo "-> ${#containers[@]} containers containing '${mask}' found"
+    printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
+    _clean_and_flush
+}
 
-echo "Delete stacks"
-for i in `openstack stack list | grep $mask | awk '{print $2}'`; do openstack stack check $i; done
-for i in `openstack stack list | grep $mask | awk '{print $2}'`; do openstack stack delete -y $i; echo deleted $i; done
+###################
+### Main
+###################
+# temp file for commands
+cmds=$(mktemp)
+echo "Using tempfile: '${cmds}'"
 
-#echo "Delete containers"
-#for i in `openstack container list --all | grep $mask | awk '{print $2}'`; do openstack container delete $i; echo deleted $i; done
-echo "Done"
+# Consider cleaning contrail resources carefully
+# ...and only after that - clean projects
 
-# It is not recommended to remove projects until you are sure that there are no other leftovers.
-#echo "Delete projects"
-#for i in `openstack project list | grep $mask | awk '{print $2}'`; do openstack project delete $i; echo deleted $i; done
+_clean_stacks
+_clean_servers
+
+_clean_users
+_clean_roles
+_clean_snapshots
+_clean_volumes
+_clean_volume_types
+_clean_images
+_clean_sec_groups
+_clean_keypairs
+_clean_routers_and_networks
+_clean_regions
+_clean_containers
+
+# project cleaning disabled by default
+# Coz cleaning Contrail with no projects is a hard task
+#_clean_projects
+
+# remove temp file
+rm ${cmds}
diff --git a/cleanup_old.sh b/cleanup_old.sh
new file mode 100644
index 0000000..9792873
--- /dev/null
+++ b/cleanup_old.sh
@@ -0,0 +1,67 @@
+#!/bin/bash
+export OS_INTERFACE='admin'
+mask='rally_\|tempest_\|tempest-'
+
+echo "Starting. Using mask '$mask'"
+
+echo "Delete users"
+for i in `openstack user list | grep $mask | awk '{print $2}'`; do openstack user delete $i; echo deleted $i; done
+
+echo "Delete roles"
+for i in `openstack role list | grep $mask | awk '{print $2}'`; do openstack role delete $i; echo deleted $i; done
+
+#echo "Delete projects"
+#for i in `openstack project list | grep $mask | awk '{print $2}'`; do openstack project delete $i; echo deleted $i; done
+
+echo "Delete servers"
+for i in `openstack server list --all | grep $mask | awk '{print $2}'`; do openstack server delete $i; echo deleted $i; done
+
+echo "Reset snapshot state and delete"
+for i in `openstack volume snapshot list --all | grep $mask | awk '{print $2}'`; do openstack snapshot set --state available $i; echo snapshot reset state is done for $i; done
+for i in `openstack volume snapshot list --all | grep $mask | awk '{print $2}'`; do openstack snapshot set --state available $i; echo deleted $i; done
+
+echo "Reset volume state and delete"
+for i in `openstack volume list --all | grep $mask | awk '{print $2}'`; do openstack volume set --state available $i; echo reset state is done for $i; done
+for i in `openstack volume list --all | grep $mask | awk '{print $2}'`; do openstack volume delete $i; echo deleted $i; done
+
+echo "Delete volume types"
+for i in `openstack volume type list | grep $mask | awk '{print $2}'`; do openstack volume type delete $i; done
+
+echo "Delete images"
+for i in `openstack image list | grep $mask | awk '{print $2}'`; do openstack image delete $i; echo deleted $i; done
+
+echo "Delete sec groups"
+for i in `openstack security group list --all | grep $mask | awk '{print $2}'`; do openstack security group delete $i; echo deleted $i; done
+
+echo "Delete keypairs"
+for i in `openstack keypair list | grep $mask | awk '{print $2}'`; do openstack keypair delete $i; echo deleted $i; done
+
+echo "Delete ports"
+for i in `openstack port list | grep $mask | awk '{print $2}'`; do openstack port delete $i; done
+
+echo "Delete Router ports (experimental)"
+neutron router-list|grep $mask|awk '{print $2}'|while read line; do echo $line; neutron router-port-list $line|grep subnet_id|awk '{print $11}'|sed 's/^\"//;s/\",//'|while read interface; do neutron router-interface-delete $line $interface; done; done
+
+echo "Delete subnets"
+for i in `openstack subnet list | grep $mask | awk '{print $2}'`; do openstack subnet delete $i; done
+
+echo "Delete nets"
+for i in `openstack network list | grep $mask | awk '{print $2}'`; do openstack network delete $i; done
+
+echo "Delete routers"
+for i in `openstack router list | grep $mask | awk '{print $2}'`; do openstack router delete $i; done
+
+echo "Delete regions"
+for i in `openstack region list | grep $mask | awk '{print $2}'`; do openstack region delete $i; echo deleted $i; done
+
+echo "Delete stacks"
+for i in `openstack stack list | grep $mask | awk '{print $2}'`; do openstack stack check $i; done
+for i in `openstack stack list | grep $mask | awk '{print $2}'`; do openstack stack delete -y $i; echo deleted $i; done
+
+#echo "Delete containers"
+#for i in `openstack container list --all | grep $mask | awk '{print $2}'`; do openstack container delete $i; echo deleted $i; done
+echo "Done"
+
+# It is not recommended to remove projects until you are sure that there are no other leftovers.
+#echo "Delete projects"
+#for i in `openstack project list | grep $mask | awk '{print $2}'`; do openstack project delete $i; echo deleted $i; done