Created
May 22, 2014 15:43
-
-
Save diegoparrilla/6288e1521bffe741f71a to your computer and use it in GitHub Desktop.
Migrate Openstack VM from Private to Public Cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Migrate a VM to another StackOps Cloud | |
# This is the kind of rubbish written by a CEO. Don't be cruel | |
# ENTER THE USERNAME, PASSWORD AND TENANT OF THE SOURCE AND DESTINATION TENANTS | |
# ALSO CHECK THE KEYSTONE ENDPOINT | |
# | |
#!/usr/bin/env bash | |
#set -x | |
SOURCE_KEYSTONE_HOST=<<CHANGE_ME>> | |
SOURCE_OS_USERNAME=<<CHANGE_ME>> | |
SOURCE_OS_PASSWORD=<<CHANGE_ME>> | |
SOURCE_OS_TENANT_NAME=<<CHANGE_ME>> | |
DEST_KEYSTONE_HOST=<<CHANGE_ME>> | |
DEST_OS_USERNAME=<<CHANGE_ME>> | |
DEST_OS_PASSWORD=<<CHANGE_ME>> | |
DEST_OS_TENANT_NAME=<<CHANGE_ME>> | |
VM_NAME=$1 | |
FLAVOR=$2 | |
SNAPSHOT_NAME=$1"-snapshot-"`date "+%d%m%Y"` | |
EXT_NET=ext-net | |
SOURCE_NOVA_CMD="nova --os-auth-url https://$SOURCE_KEYSTONE_HOST/v2.0 --os-username $SOURCE_OS_USERNAME --os-password $SOURCE_OS_PASSWORD --os-tenant-name $SOURCE_OS_TENANT_NAME --insecure" | |
SOURCE_GLANCE_CMD="glance --os-auth-url https://$SOURCE_KEYSTONE_HOST/v2.0 --os-username $SOURCE_OS_USERNAME --os-password $SOURCE_OS_PASSWORD --os-tenant-name $SOURCE_OS_TENANT_NAME --insecure" | |
DEST_NOVA_CMD="nova --os-auth-url https://$DEST_KEYSTONE_HOST/v2.0 --os-username $DEST_OS_USERNAME --os-password $DEST_OS_PASSWORD --os-tenant-name $DEST_OS_TENANT_NAME --insecure" | |
DEST_GLANCE_CMD="glance --os-auth-url https://$DEST_KEYSTONE_HOST/v2.0 --os-username $DEST_OS_USERNAME --os-password $DEST_OS_PASSWORD --os-tenant-name $DEST_OS_TENANT_NAME --insecure" | |
# Get the tenant Id | |
get_net_id() | |
{ | |
echo "`neutron --os-auth-url https://$DEST_KEYSTONE_HOST/v2.0 --os-username $DEST_OS_USERNAME --os-password $DEST_OS_PASSWORD --os-tenant-name $DEST_OS_TENANT_NAME --insecure net-list | grep "$DEST_OS_TENANT_NAME" | awk '/ | / { prin | |
t $2 }' | head -n 1`" | |
} | |
# Get the vm Id | |
get_vm_id() | |
{ | |
echo "`$DEST_NOVA_CMD list | grep "$1" | awk '/ | / { print $2 }'`" | |
} | |
# Take a snapshot of the source VM | |
echo "Start snapshot..." | |
$SOURCE_NOVA_CMD image-create --poll $VM_NAME $SNAPSHOT_NAME | |
echo "Snapshot done." | |
echo "Start download of source snapshot..." | |
$SOURCE_GLANCE_CMD image-download --progress $SNAPSHOT_NAME --file $SNAPSHOT_NAME.qcow2 | |
echo "Download done." | |
echo "Delete source snapshot..." | |
$SOURCE_NOVA_CMD image-delete $SNAPSHOT_NAME | |
echo "Deleted source snapshot." | |
echo "Upload snapshot to destination cloud..." | |
$DEST_GLANCE_CMD image-create --progress --name $SNAPSHOT_NAME --disk-format qcow2 --container-format bare --min-disk 10 --min-ram 1024 --is-public False --file $SNAPSHOT_NAME.qcow2 | |
echo "Uploaded snapshot" | |
echo "Delete local file..." | |
rm $SNAPSHOT_NAME.qcow2 | |
echo "Deleted local file." | |
echo "Boot uploaded snapshot..." | |
NET_ID=`get_net_id` | |
$DEST_NOVA_CMD boot $VM_NAME --flavor $FLAVOR --image $SNAPSHOT_NAME --nic net-id=$NET_ID --poll | |
VM_ID=`get_vm_id $VM_NAME` | |
echo "Snapshot booted." | |
echo "Ask for new public IP" | |
FLOATING_IP=`$DEST_NOVA_CMD floating-ip-create $EXT_NET | grep "$EXT_NET" | awk '/ | / { print $2 }'` | |
$DEST_NOVA_CMD add-floating-ip $VM_ID $FLOATING_IP | |
echo "THE NEW SERVER IS RUNNING IN: $FLOATING_IP" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment