Skip to content

Instantly share code, notes, and snippets.

@antaflos
Forked from rbirnie/10_autostart_libvirt.sh
Last active August 29, 2015 14:27
Show Gist options
  • Save antaflos/5829c49fbe0d847a7985 to your computer and use it in GitHub Desktop.
Save antaflos/5829c49fbe0d847a7985 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Autostart Libvirt VM's created with Foreman
# /usr/share/foreman/config/hooks/host/managed/create/10_autostart_libvirt.sh
# Source: http://www.uberobert.com/autostart-libvirt-vms-in-foreman/
. $(dirname $0)/hook_functions.sh
username='admin'
password='changeme'
# event name (create, before_destroy etc.)
# orchestration hooks must obey this to support rollbacks (create/update/destroy)
event=${HOOK_EVENT}
# to_s representation of the object, e.g. host's fqdn
object=${HOOK_OBJECT}
hostname=$(hook_data host.name)
echo "$(date): setting ${object} to autostart" >> /tmp/hook.log
# find our compute_resource
compute_resource=$(hook_data host.compute_resource_id)
# get compute_resource's url from foreman
compute_resource_url=$(curl -k -u ${username}:${password} https://$(hostname)/api/compute_resources/${compute_resource} | grep -Po '"url":.*?[^\\]",' | awk -F\" '{print $4}') >> /tmp/hook.log
# autostart our host
virsh -c $compute_resource_url autostart $hostname
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment