Skip to content

Instantly share code, notes, and snippets.

@jfontan
Last active March 7, 2020 15:37
Show Gist options
  • Save jfontan/5684171 to your computer and use it in GitHub Desktop.
Save jfontan/5684171 to your computer and use it in GitHub Desktop.
OS=[
ARCH="x86_64",
BOOT="network" ]
CPU="1"
MEMORY="768"
DISK=[
IMAGE="empty_10gb_disk" ]
NIC=[
NETWORK="building" ]
GRAPHICS=[
LISTEN="0.0.0.0",
TYPE="vnc" ]
FOREMAN_OS_ID="2" # in our case this is a Ubuntu 12.10
FOREMAN_SUBNET="building"
#!/usr/bin/env ruby
# Add OpenNebula ruby library path. Alternatively you can install OpenNebula
# ruby gem
$: << '/usr/lib/one/ruby'
require 'rubygems'
require 'foreman_api'
require 'opennebula'
require 'base64'
require 'rexml/document'
# Parameters received by the hook from OpenNebula
ID=ARGV[0]
TEMPLATE_ENCODED=ARGV[1]
# Log file for script debugging
LOG=File.open('/tmp/hook.log', 'w+')
# Change your credentials and endpoint here
CREDENTIALS={
:base_url => 'http://foreman',
:username => 'admin',
:password => 'amazingly_strong_password'
}
# In our infrastructure we have two network, here are the IDs of these networks
# in foreman
SUBNETS={
'building' => 1,
'internal' => 2
}
# There are some values hardcoded for the VMs as we don't use many different
# parameters but these can also be changed
def create_foreman_host(params ={})
host = ForemanApi::Resources::Host.new(CREDENTIALS)
description={
"host" => {
:name => params[:name],
:mac => params[:mac],
:ip => params[:ip],
:architecture_id => 1, # x86_64
:environment_id => 1, # production
:domain_id => 1, # local
:subnet_id => params[:subnet_id],
:operatingsystem_id => params[:os_id].to_i,
:puppet_proxy_id => 1, # Only one proxy
:hostgroup_id => 1, # We only have one hostgroup
:build => 1, # Enable VM building
:ptable_id => params[:ptable_id],
:medium_id => params[:medium_id]
}
}
host.create(description)
end
def get_foreman_os(id)
os = ForemanApi::Resources::OperatingSystem.new(CREDENTIALS)
res = os.index
res[0].select {|o| o["operatingsystem"]["id"]==id }[0]["operatingsystem"]
end
@client=OpenNebula::Client.new
template_decoded=Base64.decode64(TEMPLATE_ENCODED)
xml=Nokogiri::XML(template_decoded)
vm=OpenNebula::VirtualMachine.new(xml, @client)
LOG.puts vm.inspect
os_id=vm['VM/USER_TEMPLATE/FOREMAN_OS_ID']
subnet_name=vm['VM/USER_TEMPLATE/FOREMAN_SUBNET']
# We only execute the hook when FOREMAN_OS_ID is set in the VM template
exit(0) if !os_id
os=get_foreman_os(os_id.to_i)
# We need to fill medium and ptable values from OS parameters as Foreman uses
# the values from the hostgroup
medium=os['media'][0]['medium']['id']
ptable=os['ptables'][0]['ptable']['id']
subnet=1
subnet=SUBNETS[subnet_name] if SUBNETS[subnet_name]
# Fill VM parameters
info={
:name => vm['VM/NAME'],
:ip => vm['VM/TEMPLATE/NIC/IP'],
:mac => vm['VM/TEMPLATE/NIC/MAC'],
:subnet_id => subnet,
:os_id => os_id,
:medium_id => medium,
:ptable_id => ptable
}
LOG.puts create_foreman_host(info).inspect
# Chill out a bit an let foreman do its job
sleep 5
vm=OpenNebula::VirtualMachine.new(
OpenNebula::VirtualMachine.build_xml(ID), @client)
# Release the VM hold so it can start
LOG.puts vm.release.inspect
VM_HOOK = [
name = "foreman-create",
on = "CREATE",
command = "/var/lib/one/foreman_create_hook.rb",
arguments = "$ID $TEMPLATE" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment