Created by Kraig Amador, 2012

This code requires the classes @ API Signing, Python

#!/ usr/bin/env python
# By: Kraig Amador, 2012

import hashlib, hmac, string, base64, urllib
import json, urllib, socket, sys

our_ip = socket.gethostbyname(socket.gethostname())
api = CloudStack(api_url, apiKey, secret)

host = None
# First, find the CS host for this machine
request = {'listall': 'true'}
for h in api.listHosts(request)['host']:
    if h['ipaddress'] == our_ip:
        host = h
        break

if host is None:
  print "This node isn't a CS host"
  sys.exit(0)

# we filter the volumes because the hostid filter doesn't seem to work
volumes = []
request = {'listall': 'true', 'hostid': host['id'], 'isrecursive': 'true'}
for volume in api.listVolumes(request)['volume']:
  if volume['storage'] != host['name']:
    continue
  if volume['vmstate'] == 'Stopped':
    volumes.append(volume)

print "Found %d inactive volumes" % len(volumes)

# For each volume, find the VM, start it up.
for volume in volumes:
  request = {'listall': 'true', 'name': volume['vmname'], 'id': volume['virtualmachineid'], 'isrecursive': 'true'}
  result = api.listVirtualMachines(request)
  if result['count'] < 1:
    continue
  for vm in result['virtualmachine']:
    print "Starting up %s" % vm['name']
    request = {'id': vm['id']}
    result = api.startVirtualMachine(request)
  • No labels