Riaan's SysAdmin Blog

My tips, howtos, gotchas, snippets and stuff. Use at your own risk!

fabricOVM

System Administration with Fabric and OVM

I recently tried fabric for a few automation tasks and could use it to interact with the Oracle VM CLI.  I am using the OVM Rest API whenever possible but in some cases this may work for you.  Also I was having issues with the REST API and POST type calls so needed another method.  I have expect also for CLI interaction but fabric is a lot more python like and powerful.

In addition not in this use case with OVM but if you use fabric to run against many hosts you could pretty easily get it to do parallel execution with a bit of python magic. CHeck here for great example: https://dmsimard.com/2013/11/29/capture-output-from-parallel-execution-with-fabric/

Example fabfile:

import os
from fabric.api import *
from fabric.colors import red, green
import re

def getprop(item,tag):
  item = item + ' '
  s=tag + ":(.*?) "
  ovmprop=re.findall(s, item)
  return ovmprop[0]


env.user = os.getenv('SSH_USER', 'admin')
env.password = os.getenv('SSH_PASSWORD', 'Welcome')

# http://docs.oracle.com/cd/E35328_01/E35336/E35336.pdf
def vm_list_raw():
  vms=run('list vm', shell=False)
  print(green(vms))

def vm_create(name):
  run('create Vm name=' + name + ' domainType=XEN_HVM repository=ovs2 on ServerPool name=ovsPool1', shell=False)

def vm_list():
  with settings(hide('running', 'commands', 'stdout', 'stderr')):
    vms=run('list vm', shell=False).split('\n\r')
    #print(green(vms))

    for line in vms:
      if 'id' in line:
        id=getprop(line,'id')
        name=getprop(line,'name')
        print '{:20} {:20}'.format(id,name)  

Examples:

$ fab -H 192.168.1.223:10000 vm_list
[192.168.1.223:10000] Executing task 'vm_list'
0004fb000006000048e25da9faac0ac6 VM1                 

Done.
Disconnecting from 192.168.1.223:10000... done.

$ fab -H 192.168.1.223:10000 vm_create:"name=VM6"
[192.168.1.223:10000] Executing task 'vm_create'
[192.168.1.223:10000] run: create Vm name=VM6 domainType=XEN_HVM repository=ovs2 on ServerPool name=ovsPool1
[192.168.1.223:10000] out: OVM> create Vm name=VM6 domainType=XEN_HVM repository=ovs2 on ServerPool name=ovsPool1
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out: Command: create Vm name=VM6 domainType=XEN_HVM repository=ovs2 on ServerPool name=ovsPool1
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out: Status: Success
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out: Time: 2017-02-01 09:04:26,212 CST
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out: JobId: 1485961465970
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out: Data: 
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out:   id:0004fb00000600007f77133a8d591c19  name:VM6
[192.168.1.223:10000] out: 
[192.168.1.223:10000] out: OVM> Connection closed.
[192.168.1.223:10000] out: 

admin

Bio Info for Riaan