Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OpenNebula API to automatically start a new VM #1

Open
felipeZ opened this issue Apr 1, 2019 · 0 comments
Open

Use OpenNebula API to automatically start a new VM #1

felipeZ opened this issue Apr 1, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@felipeZ
Copy link
Member

felipeZ commented Apr 1, 2019

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)
@felipeZ felipeZ added the enhancement New feature or request label Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant