Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have a kind of tweeky script that could move VMs between projects and update quotas of the project according to the actual servers in the project because moving the VMs will not update the quotas of the project both in source and destination project.

I don't have karma to upload file so pasting here

vm_move.sh - used to move VM to another project

usage: ./vm_move.sh "destination_tenant_id" "VM-1_id" "VM-2_id" ..... "VM-x_id"

#!/bin/bash
for i
do
if [ "$i" != "$1" ]; then
echo "moving instance id " $i " to project id" $1;
mysql -uroot -pxxxx -s -N <<query
use nova;
update instances set project_id="$1" where uuid="$i";
query
else
#get project id of the instance before update
old_proj_id=$(mysql -uroot -pxxxx -s -N <<query
use nova;
select project_id from instances where uuid="$2";
query
)
#get user id of the instance before update
user_id=$(mysql -uroot -pxxxx -s -N <<query
use nova;
select user_id from instances where uuid="$2";
query
)

fi
done
echo "quota_resync " $old_proj_id;
./quota_resync.sh $old_proj_id $user_id;
echo "quota_resync " $1;
./quota_resync.sh $1 $user_id;

quota_resync.sh

usage: ./quota_resync.sh "project_id" "user_id"

#!/bin/bash
count=$(mysql -uroot -pxxxx -s -N <<query
use nova;
select count(*) from instances where project_id="$1" and vm_state="active";
query
)

memory=$(mysql -uroot -pxxxx -s -N <<query
use nova;
select sum(memory_mb) from instances where project_id="$1" and vm_state="active";
query
)

vcpu=$(mysql -uroot -pxxxx -s -N <<query
use nova;
select sum(vcpus) from instances where project_id="$1" and vm_state="active";
query
)

echo "count" $count;
echo "vcpus" $vcpu;
echo "Ram" $memory;

update=$(mysql -uroot -pxxxx -s -N <<query
use nova;
update quota_usages set in_use=$count where project_id="$1" and user_id="$2" and resource="instances";
update quota_usages set in_use=$memory where project_id="$1" and user_id="$2" and resource="ram";
update quota_usages set in_use=$vcpu where project_id="$1" and user_id="$2" and resource="cores";
query
)

Note: both the above scripts must be in same folder and should be in controller node