Guest User

Untitled

a guest
Jul 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #!/bin/bash
  2. # Author: Aries Youssefian
  3. # This script will take a volume ID, clone it, attach it to a host, convert that volume to a qcow2, detach, then using Symp CLI will upload it to a remote cluster
  4. # Upon completion it should then delete the volume.
  5. #
  6. # Must be run from a Symphony node.
  7. # SCRIPT ASSUMES SOURCE NODE HAS ENOUGH SPACE ON mancala0
  8. # Script also assumes only 1 project exists per source and destination domain (eg, default)
  9. # if more projects exist, specify -r flag for the project
  10. #
  11. # Todo: Add flags for SSL
  12. #
  13.  
  14.  
  15. display_usage() {
  16. echo "This script will clone a volume, attach it to a host, convert it to a qcow2, and push it to a remote Symp cluster"
  17. echo -e "\nUsage:\nsource-user-name source-domain source-password source-cluster-address source-volume-id destination-user destination-domain destination-password destination-cluster destination-image-name\n"
  18. }
  19.  
  20. # if less than 9 arguments supplied, display usage
  21. if [ $# -le 9 ]
  22. then
  23. display_usage
  24. exit 1
  25. fi
  26.  
  27. # check whether user had supplied -h or --help . If yes display usage
  28. if [[ ( $# == "--help") || $# == "-h" ]]
  29. then
  30. display_usage
  31. exit 0
  32. fi
  33.  
  34. # Variables
  35.  
  36. sourceuser=$1
  37. sourcedomain=$2
  38. sourcepassword=$3
  39. sourcecluster=$4
  40. sourcevolid=$5
  41. destuser=$6
  42. destdomain=$7
  43. destpassword=$8
  44. destcluster=$9
  45. destimagename=${10}
  46. sourcehostname="$(hostname)"
  47.  
  48. echo Cloning $sourcevolid
  49.  
  50. sourceclonedvolid="$(symp -k --url $sourcecluster -d $sourcedomain -u $sourceuser -p $sourcepassword volume create --source-id $sourcevolid tempvol_toexport -f value -c id)"
  51.  
  52. echo Cloning successful, cloned volume ID is $sourceclonedvolid
  53.  
  54. echo Attaching cloned volume $sourceclonedvolid to host $sourcehostname
  55.  
  56. sourcedevaddress="$(mancala volumes attach-to-host $sourceclonedvolid $sourcehostname --json | jq -r .attachments[].mountpoint)"
  57.  
  58. echo Successfully mounted cloned volume $sourceclonedvolid to host $sourcehostname at $sourcedevaddress
  59.  
  60. echo Beginning qemu-img conversion
  61.  
  62. qemu-img convert -f raw -O qcow2 $sourcedevaddress /mnt/mancala0/exportedvm.qcow2
  63.  
  64. echo Successfully completed conversion to /mnt/mancala0
  65.  
  66. echo Unattaching cloned volume $sourceclonevolid from host $sourcehostname
  67.  
  68. mancala volumes detach-from-host $sourceclonedvolid $sourcehostname
  69.  
  70. echo Successfully detached cloned volume $sourceclonevolid from host $sourcehostname
  71.  
  72. echo Deleting cloned volume $sourceclonedvolid
  73.  
  74. symp -k --url $sourcecluster -d $sourcedomain -u $sourceuser -p $sourcepassword volume remove $sourceclonedvolid
  75.  
  76. echo Successfully deleted cloned volume
  77.  
  78. echo Creating image $destimagename on destination cluster $destcluster
  79.  
  80. destimageid="$(symp -k --url $destcluster -d $destdomain -u $destuser -p $destpassword image create $destimagename -f value -c id)"
  81.  
  82. echo Successfully created image $destimagename with ID $destimageid
  83.  
  84. echo Uploading image..
  85.  
  86. symp -k --url $destcluster -d $destdomain -u $destuser -p $destpassword image upload $destimageid /mnt/mancala0/exportedvm.qcow2
  87.  
  88. echo Successfully uploaded image.
  89.  
  90. echo Deleting qcow2 file
  91.  
  92. rm -f /mnt/mancala0/exportedvm.qcow2
  93.  
  94. echo Deleted qcow2 file.
  95.  
  96. echo All done.
Add Comment
Please, Sign In to add comment