OpenStack: server (VM)

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 af579f92-8d71-42f1-9d6c-29e45ec7adb2
 
# 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 e107c4a4-2812-407e-a6ee-c293b6f78e44
 
# Show console URL
openstack console url show ${VM_ID}
 
# Reboot
openstack server reboot --hard  ${VMID}
 
# get server ports
nova interface-list  ${VM_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)

apt install -y qemu-guest-agent

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

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