OpenStack: server (VM)

Get VM data

openstack server show -c name -c id -c addresses -c OS-EXT-SRV-ATTR:host ${SERVER_ID}
nova interface-list ${SERVER_ID}
nova migration-list --instance-uuid ${SERVER_ID} | grep -v "2019-"

Create VM
https://docs.openstack.org/python-openstackclient/latest/cli/command-objects/server.html#server-create

openstack server create foo-vm1 \
  --image "Ubuntu 18.04" \
  --flavor m1.small \
  --key-name foo-key \
  --network foo-net \
  --security-group default

CLI

# Delete instance
openstack server delete ${SERVER_ID}
 
# list all servers from all projects
openstack server list --all-projects
 
# get IDs only from server
openstack server list -c ID -f value
 
# get all servers using windows images
for IMAGE in $(openstack image list --long --os-cloud=prod-admin -c ID -c Name -f value | grep -i "Windows" | cut -d" " -f 1); do
    openstack server list --long --all-projects --os-cloud=prod-admin --image ${IMAGE} 
done
 
# Add floating IP
openstack server add floating ip vm1 $(openstack floating ip create public -c floating_ip_address -f value)
 
# resize
openstack server resize ...
openstack server resize --confirm ${SERVER_ID}
 
# attach port to VM
openstack server add port VM1 NET1-PORT1
 
# list instances / server
openstack server list
openstack console log show foo-vm1
 
# get hypervisor
openstack server list
ssh compute1 ps axf | grep ${SERVER_ID}
 
# Show console URL
openstack console url show ${SERVER_ID}
 
# Reboot
openstack server reboot --hard  ${SERVER_ID}
 
# get server ports
nova interface-list  ${SERVER_ID}
 
# set VM properties
openstack server set --property "sysadmin=foo" --property "os=Ubuntu" ${SERVER_ID}
 
# remove VM property
openstack server unset --property os ${SERVER_ID}
 
# Update / fix server status
openstack server set --state active ${SEVER_ID}
openstack server show -c id -c name -c status -c OS-EXT-SRV-ATTR:hypervisor_hostname ${SEVER_ID}
 
# allow SSH incomming access
SECURITY_GROUP_ID=$(openstack server show -c security_groups -f value ${SERVER_ID}  | cut -d "'" -f2)
openstack security group rule create --protocol tcp --dst-port 22:22 --remote-ip 0.0.0.0/0  ${SECURITY_GROUP_ID}

Snapshot and restore

openstack server image create --name vm1-snap1 vm1 --os-cloud=dev
openstack image list --os-cloud=dev
openstack server rebuild --image vm1-snap1 vm1 --os-cloud=dev
openstack server show vm1 -c name -c status --os-cloud=dev

Install qemu-guest-agent (run inside of a VM)
https://www.sebastien-han.fr/blog/2015/02/09/openstack-perform-consistent-snapshots-with-qemu-guest-agent/

apt install -y qemu-guest-agent
# hw_qemu_guest_agent=yes

Get instance data

# get flavor
curl http://169.254.169.254/latest/meta-data/instance-type
 
# get security-groups
curl http://169.254.169.254/latest/meta-data/security-groups
 
# get availability-zone
curl http://169.254.169.254/latest/meta-data/placement/availability-zone

List VMs

# list VMs and  tenants
nova list --all-tenants
nova list --all-tenants --fields tenant_id
 
openstack server list --all --long --sort-column Name -f json
openstack server list --all --long --sort-column Name --name foo
 
# show all deleted VMs
openstack server list --deleted --all

Fix task_state "powering-on"

for SERVER_ID in $(openstack server list --all -c ID -f value); do
    openstack server show ${SERVER_ID} -c id -c OS-EXT-STS:task_state -f value | paste - - | grep -v None
done
 
openstack server set --state active ${SERVER_ID}
openstack server reboot ${SERVER_ID} [--hard]https://docs.openstack.org/cinder/latest/cli/cli-manage-volumes.html
openstack console url show ${SERVER_ID}

search for VM on all compute nodes

SERVER_ID=xxxx-xxxx-xxxx-xxxx
for NODE in $(openstack compute service list --service nova-compute -c Host -f value); do
    echo ${NODE}
    ssh ${NODE} ps axf | grep ${SERVER_ID}
    ssh ${NODE} docker exec -i nova_libvirt virsh dominfo ${SERVER_ID}
done

Event

openstack server event list ${SERVER_ID}

Get server / tanent data

SERVER_IDS="
07111f96-c752-4faa-b9dd-1ec962c7b6dc
b41c7bfb-0411-48c9-b133-6ee95c4dc20e
894abf55-2257-469f-b102-987a90342abe
"
 
for SERVER_ID in ${SERVER_IDS}; do
    SERVER_JSON=$(openstack server show ${SERVER_ID} -f json)
    SERVER_NAME=$(echo ${SERVER_JSON} | jq -r .name)
 
    PROJECT_ID=$(echo ${SERVER_JSON} | jq -r .project_id)
    PROJECT_JSON=$(openstack project show ${PROJECT_ID} -f json)
    PROJECT_NAME=$(echo ${PROJECT_JSON} | jq -r .name)
 
    DOMAIN_ID=$(echo ${PROJECT_JSON} | jq -r .domain_id)
    DOMAIN_JSON=$(openstack domain show ${DOMAIN_ID} -f json)
    DOMAIN_NAME=$(echo ${DOMAIN_JSON} | jq -r .name)
 
    echo "${SERVER_ID}  ${SERVER_NAME}  ${DOMAIN_NAME}  ${PROJECT_NAME}"
done

Transfer VM
https://docs.openstack.org/cinder/latest/cli/cli-manage-volumes.html

openstack volume list --all-projects --name docs1-dev
openstack volume transfer request create  4aef794d-9f79-4d24-a220-211dc4863424

Links
https://docs.openstack.org/python-openstackclient/latest/cli/command-objects/server.html