Last active
November 3, 2024 09:50
-
-
Save SMAK1993/120f337545d97907860c04110ba3d118 to your computer and use it in GitHub Desktop.
Script for transferring VMs from one Openstack project to another
This file contains 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
#!/bin/bash | |
# Requires Bash version 4+ because it uses dictionaries/arrays | |
# Define the 2 project IDs | |
# I assume the same user has access to both projects | |
OLD_PROJECT=INSERT_OLD_PROJECT_ID; | |
NEW_PROJECT=INSERT_NEW_PROJECT_ID; | |
# Maintain a dictionary of volume IDs to VM names so that we can attach to the correct VM later | |
# Detach the non-boot volumes from the VMs in the old project and transfer them to the new project | |
export OS_PROJECT_ID=$OLD_PROJECT; | |
VOLUMES=$(openstack volume list | egrep -v "ID|/dev/vda" | awk '{print $2}'); | |
declare -A volumesToInstanceNames; | |
for VOLUME_ID in $VOLUMES; | |
do | |
export OS_PROJECT_ID=$OLD_PROJECT; | |
echo $VOLUME_ID; | |
SERVER_ID=$(openstack volume show $VOLUME_ID -f json | jq '.attachments[0].server_id' -r); | |
echo $SERVER_ID; | |
SERVER_NAME=$(openstack server show $SERVER_ID -f json | jq '. | .name' -r); | |
echo $SERVER_NAME; | |
volumesToInstanceNames[$VOLUME_ID]=$SERVER_NAME; | |
openstack server remove volume $SERVER_ID $VOLUME_ID; | |
VOLUME_TRANSFER_DATA=$(openstack volume transfer request create $VOLUME_ID -f json); | |
VOLUME_TRANSFER_ID=$(echo $VOLUME_TRANSFER_DATA | jq '.id' -r); | |
VOLUME_TRANSFER_AUTH_KEY=$(echo $VOLUME_TRANSFER_DATA | jq '.auth_key' -r); | |
export OS_PROJECT_ID=$NEW_PROJECT; | |
openstack volume transfer request accept --auth-key $VOLUME_TRANSFER_AUTH_KEY $VOLUME_TRANSFER_ID; | |
done | |
# Create a snapshot image of a VM and the delete the VM | |
# Create a new VM with the same name using the snapshot image and then delete the snapshot image | |
export OS_PROJECT_ID=$OLD_PROJECT; | |
INSTANCES=$(openstack server list | egrep -v ID | awk '{print $2}'); | |
for INSTANCE_ID in $INSTANCES; | |
do | |
export OS_PROJECT_ID=$OLD_PROJECT; | |
echo $INSTANCE_ID; | |
INSTANCE_NAME=$(openstack server show $INSTANCE_ID -f json | jq '. | .name' -r); | |
echo $INSTANCE_NAME; | |
INSTANCE_FLAVOR=$(openstack server show $INSTANCE_ID -f json | jq '. | .flavor' -r | awk '{print $1}'); | |
echo $INSTANCE_FLAVOR; | |
openstack server image create --name $INSTANCE_NAME $INSTANCE_ID; | |
openstack server delete $INSTANCE_ID; | |
export OS_PROJECT_ID=$NEW_PROJECT; | |
openstack server create --flavor $INSTANCE_FLAVOR --image $INSTANCE_NAME $INSTANCE_NAME; | |
openstack image delete $INSTANCE_NAME | |
done | |
# Attach the additional volumes that were transferred to the appropriate VM using the dictionary | |
export OS_PROJECT_ID=$NEW_PROJECT; | |
for volume in ${!volumesToInstanceNames[@]}; | |
do | |
echo ${volume} ${volumesToInstanceNames[${volume}]}; | |
openstack server add volume ${volumesToInstanceNames[${volume}]} $volume | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment