How to bulk rebuild VM - useful for a maintenance

Premise

There is a problem happening with VMs having the virtual disk on a cinder volume with ceph backend when upgrading to Ocata O~S release Due to a nova compute version upgrade VMs that have never been rebooted since some upgrades will fail to attach the volume and fail the boot This problem is due to a change in the xml virsh format that causes a mismatch and is healed with a rebuild+restart.

Warning

Before proceeding, make sure that all VMs are based on non-ephemeral disk

Recipe

this single line script can be used to automate the process starting from a list of machine IDs affected:

cat LIST_VM.txt |while read  SRV_ID; \
do echo "openstack server rebuild "$SRV_ID" --image \\"; \
openstack volume show `openstack server show $SRV_ID|grep os-extended-volumes:volumes_attached|awk '{print $5}'|sed -e s%u\'%% -e s%\'}]%%` | grep --color=auto volume_image_metadata |awk -Fimage_id '{print $2}'|awk -F\' '{print $3}'; \
echo ""; \
done |tee -a rebuild.sh
#
# WARNING: make sure all servers in LIST_VM have non-ephemeral boot volume,
#         by verifying that volumes in volumes_attached line in "server show"
#         exist and are mounted as /dev/vda
#
sh ./rebuild.sh