Skip to content

Use OpenNebula API to automatically start a new VM #1

@felipeZ

Description

@felipeZ

Use the python-oca API to start a VM on demand.
Start the new VM using:

import sys
import argparse
from xml.etree import ElementTree as ET
import oca

ADDRESS = 'https://api.hpccloud.surfsara.nl/RPC2'


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--memory', default=2048, type=int, help="Memory (MB)")
    parser.add_argument('-c', '--cpu', default=2, type=int, help="Number of CPU cores")
    args = parser.parse_args()
    return args


def main(memory=2048, cpu=2):
    options = f"memory={memory} cpu={cpu}"

    client = oca.Client(None, address=ADDRESS)
    # [secret,] ID, name, hold/pending, extra-attrs, persistent
    vmid = client.call('template.instantiate', 10377, 'server', False, options, False)
    print("Started VM with ID =", vmid)


if __name__ == '__main__':
    args = parse_args()
    main(memory=args.memory, cpu=args.cpu)

Then run an action on the VM using something similar to:

import sys
from xml.etree import ElementTree as ET
import oca

ADDRESS = 'https://api.hpccloud.surfsara.nl/RPC2'
client = oca.Client(None, address=ADDRESS)

r = client.call('vmpool.info', -1, -1, -1, -1)
root = ET.fromstring(r)
ET.dump(root)

id = root.find('VM/ID').text
print('VM ID =', id)

r = client.call('vm.action', sys.argv[1], int(id))
print(r)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions