OpenStack: server

List

# List instances / VMs
openstack server list
openstack server list -c ID -c Name -c Status -c Networks -c Host --long

Create

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

delete instance

openstack server delete "vm-u1804"
# list all servers from all projects
openstack server list --all-projects --os-cloud=stage-admin
# get IDs only from server
openstack server list --os-cloud=dev-foo -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

# 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

Add floating IP

openstack server add floating ip vm1 $(openstack floating ip create public -c floating_ip_address -f value)

# get VM count by hypervisor
openstack server list --all --long -c Host -f value --os-cloud=stage-admin | sort | uniq -c

# 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 e107c4a4-2812-407e-a6ee-c293b6f78e44

Install qemu-guest-agent
apt install -y qemu-guest-agent

Show console URL
openstack console url show ${VM_ID}

Resize / change flavor
# Resize
https://docs.openstack.org/mitaka/user-guide/cli_change_the_size_of_your_server.html

nova resize ${VMID} 4
nova resize-confirm ${VMID}
# or
nova resize-revert ${VMID}

Reboot
openstack server reboot --hard ${VMID}

Search for server witch status error
openstack server list --all --status ERROR

Search for server with status resizing
openstack server list --all --status=VERIFY_RESIZE

Search for server processes on wrong compute node

for COMPUTE_NODE in $(openstack compute service list --service nova-compute -c Host -f value); do
    for UUID in $(ssh ${COMPUTE_NODE} pgrep qemu -a | grep -o -P '(?<=-uuid ).*(?= -smbios)'); do
        VM_HOST=$(openstack server show -c "OS-EXT-SRV-ATTR:host" -f value ${UUID})
        if [ -z "${VM_HOST}" ]; then
            echo "Server process ${UUID} on ${COMPUTE_NODE} not available in OpenStack"
        else
            if [ "${VM_HOST}" != "${COMPUTE_NODE}" ]; then
                echo "VM ${UUID} on wrong compute node ${COMPUTE_NODE}"
            fi
        fi
    done
done

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