This project contains a collection of Python scripts used to orchestrate VMware hosts and virtual machines when dealing with power events. It was initially designed to prepare for UPS outages by safely migrating or shutting down VMs and powering hosts back on when power is restored.
- Start and stop ESXi servers through their HP iLO interfaces.
- Start, stop and migrate individual virtual machines.
- Retrieve detailed information and metrics for hosts and VMs.
- Execute a complete migration/shutdown plan defined in YAML.
- Store migration events in Redis in order to roll back the operations afterwards.
- Convenience shell scripts that wrap the Python commands and activate the project virtual environment.
.
├── data_retriever/ # Connection helpers and data transfer objects
├── plans/ # Example migration plan in YAML
├── *.py # Command line utilities (server_start.py, vm_migration.py, ...)
├── *.sh # Shell wrappers for the Python tools
└── requirements.txt # Python dependencies
A recent Python (>3.9) interpreter is required. The init.sh script will create a local virtual environment and install all dependencies:
./init.shAlternatively you may use Docker for an isolated environment:
docker compose up -d --buildEach Python file can be executed directly to perform an action. Below are a few common examples.
./server_start.sh --ip 192.0.2.5 --user admin --password secret
./server_stop.sh --ip 192.0.2.5 --user admin --password secret./vm_start.sh --moid vm-123 --ip 198.51.100.10 --user admin --password secret
./vm_stop.sh --moid vm-123 --ip 198.51.100.10 --user admin --password secret
./vm_migration.sh --vm_moid vm-123 --dist_moid host-200 --ip 198.51.100.10 --user admin --password secretUpdate plans/migration-example.yml (or create your own file) with the information about your vCenter, hosts and vm order, then execute:
./migration_plan.shWhen power comes back, the recorded events can be replayed with:
./restart_plan.shlist_vm.pylists all VMs from the given server.server_info.pyandserver_metrics.pyreturn information or metrics for a host.vm_metrics.pyreturns metrics for a virtual machine.ups_battery.shprints the remaining UPS battery time via SNMP.
All commands accept --help for detailed parameters.
vCenter:
ip: 172.1.2.3
user: vCenterUser
password: vCenterPassword
port: 443
ups:
shutdownGrace: 60
restartGrace: 60
servers:
- server:
host:
name: server_name
moid: srv1_moid
ilo:
ip: 172.2.3.4
user: user
password: password
destination:
name: dist_name
moid: srv2_moid
ilo:
ip: 172.2.3.4
user: user
password: password
vmOrder:
- vmMoId: vm1
- vmMoId: vm2
- vmMoId: vm3
This file is provided as plans/migration-example.yml and can be adapted to your environment.
The scripts print JSON formatted results to stdout and exit with a message describing the executed operation. They can easily be integrated into automation tools or other monitoring systems.