Guest User

Untitled

a guest
Sep 2nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. MYSQL_HOST=$1
  4. MYSQL_USER=$2
  5. MYSQL_PASSWORD=$3
  6.  
  7. pat="\S{8}-\S{4}-\S{4}-\S{4}-\S{12}"
  8.  
  9.  
  10.  
  11. delete_volume(){
  12. volume_uuid=$1
  13. read -d '' DELETE_VOLUME_SQL<<-EOF
  14. update volumes set attach_status='detached',status='available' where id ='$volume_uuid';
  15. update volume_attachment set attach_status='detached',deleted=1,detach_time=now() WHERE volume_id='$volume_uuid';
  16. EOF
  17. RESULT=`echo $DELETE_VOLUME_SQL | mysql --host=$MYSQL_HOST --user=$MYSQAL_USER --password=$MYSQL_PASSWORD --database=cinder`
  18. echo $(openstack volume delete $volume_uuid)
  19. echo "volume: $volume_uuid deleted"
  20. # update volumes set deleted=1,status='deleted',deleted_at=now(),updated_at=now() where deleted=0 and id='$volume_uuid'; (optional)
  21. }
  22.  
  23.  
  24.  
  25. while read -u 3 line
  26. do
  27. volume_id=`echo $line | cut -d " " -f 1`
  28. instance_id=`echo $line | cut -d " " -f 4`
  29. if [[ $instance_id =~ $pat ]];then
  30. read -d '' QUERY_VM_STATE<<-EOF
  31. select vm_state from instances where uuid = '$instance_id';
  32. EOF
  33. QUERY="select vm_state from instances where uuid = instance_id;"
  34. VM_STATE=`echo $QUERY_VM_STATE | mysql --host=$MYSQL_HOST --user=$MYSQAL_USER --password=$MYSQL_PASSWORD --database=nova -ss`
  35. if [[ $VM_STATE == "deleted" ]];
  36. then
  37. read -p "Are you sure to delete volume: $volume_id with instance_id: $instance_id? [y/N] " response
  38. case "$response" in
  39. [yY][eE][sS]|[yY])
  40. delete_volume $volume_id
  41. ;;
  42. *)
  43. echo "Not do anything with volume: $volume_id"
  44. ;;
  45. esac
  46. else
  47. :
  48. fi
  49. else
  50. :
  51. fi
  52.  
  53. #echo "volumeid: $volume_id, instanceid: $instance_id"
  54. done 3< <(openstack volume list -c ID --format value -c "Attached to" --format value)
Add Comment
Please, Sign In to add comment