daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Jan 28th, 2016 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ME_CREDS="--os-username=$ME_OS_USERNAME \
  4.     --os-password=$ME_OS_PASSWORD \
  5.     --os-tenant-name=$ME_OS_TENANT_NAME \
  6.     --os-tenant-id=$ME_OS_TENANT_ID \
  7.     --os-auth-url=$ME_OS_AUTH_URL"
  8.  
  9. IAD2_CREDS="--os-username=$IAD2_OS_USERNAME \
  10.     --os-password=$IAD2_OS_PASSWORD \
  11.     --os-tenant-name=$IAD2_OS_TENANT_NAME \
  12.     --os-tenant-id=$IAD2_OS_TENANT_ID \
  13.     --os-auth-url=$IAD2_OS_AUTH_URL"
  14.  
  15. function stop_vm {
  16.         status=$(nova $ME_CREDS show $vm --minimal | grep '^| status ' | awk '{print $4}')
  17.     echo "Stopping ${vm}..."
  18.     nova $ME_CREDS stop $vm
  19.     while [[ $status != "SHUTOFF" ]]; do
  20.         status=$(nova $ME_CREDS show $vm --minimal | grep '^| status ' | awk '{print $4}')
  21.         sleep 10
  22.     done
  23.     echo "Successfully stopped $vm!"
  24. }
  25.  
  26. function convert_to_image {
  27.     volume_id=""
  28.     status=""
  29.     volume_id=$(nova $ME_CREDS show $vm | grep 'os-extended-volumes:volumes_attached' | cut -d\" -f4)
  30.     if [[ $volume_id == "" ]]; then
  31.         echo "Unable to determine backing volume (was this an ephemeral VM?)"
  32.         exit 1
  33.     fi
  34.     echo "Uploading volume to image. This may take a very long time..."
  35.     image=$(cinder $ME_CREDS upload-to-image --force True $volume_id ${vm}-snapshot | grep ' image_id ' | awk '{print $4}')
  36.     while [[ $status != "active" ]]; do
  37.         echo "Current status: $status"
  38.         status=$(glance image-show $image | grep '^| status ' | awk '{print $4}')
  39.         echo "Checking status again in 60 seconds..."
  40.         sleep 60
  41.     done
  42.  
  43. }
  44.  
  45. function download_image {
  46.     echo "Downloading image $image from ME..."
  47.     glance --os-image-api-version 1 $ME_CREDS image-download --file ${vm}-snapshot.raw --progress $image
  48.     echo "Successfully downloaded $image from ME!"
  49.     echo "Removing image snapshot $image from ME..."
  50. #   glance $ME_CREDS image-delete $image
  51.     echo "Successfully delete image ${image} from ME!"
  52. }
  53.  
  54. function upload_image {
  55.     new_image=""
  56.     status=""
  57.     echo "Uploading image $image to IAD2..."
  58.     glance $IAD2_CREDS image-create --name ${vm}-snapshot \
  59.         --disk-format raw \
  60.         --visibility private \
  61.         --container-format bare \
  62.         --file ${vm}-snapshot.raw
  63.     new_image=$(glance $IAD2_CREDS image-list | grep ${vm}-snapshot | awk '{print $2}')
  64.     if [[ $new_image == "" ]]; then
  65.         echo "New image not found (failed in image creation?)."
  66.         exit 1
  67.     fi
  68.     while [[ $status != "active" ]]; do
  69.         status=$(glance $IAD2_CREDS image-show $new_image | grep '^| status ' | awk '{print $4}')
  70.         sleep 10
  71.     done
  72.     echo "Successfully uploaded image $image to IAD2"
  73. }
  74.  
  75. ALL_ME_VMS=$(nova $ME_CREDS list | grep -v '^+' | sed -n '1!p' | awk '{print $2}')
  76.  
  77. for vm in $ALL_ME_VMS; do
  78.     echo "Begin migration of $vm from ME (yes/no)?"
  79.     read answer
  80.     if [[ $answer == "yes" ]]; then
  81.         stop_vm
  82.         convert_to_image
  83.         download_image
  84.         upload_image
  85.     fi
  86. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top