Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions one-vm-list.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,60 @@ def get_one_info(one_user, one_password, one_endpoint) -> dict:
return vms


def get_template_info(one_user, one_password, one_endpoint) -> dict:
"""
Return list of templates from OpenNebula.
"""
one = pyone.OneServer(
one_endpoint, session="{}:{}".format(one_user, one_password))

list_templates = one.templatepool.info(
-2, # -4: Resources belonging to the user’s primary group
# -3: Resources belonging to the user
# -2: All resources
# -1: Resources belonging to the user and any of his groups
# >= 0: UID User’s Resources
-1, # >= -1 this is the Range start ID. Can be -1.
# For smaller values this is the offset used for pagination.
-1 # For values >= -1 this is the Range end ID.
# Can be -1 to get until the last ID. For values < -1 this is the page size used for pagination.
# http://docs.opennebula.org/5.6/integration/system_interfaces/api.html#one-templatepool-info
)
tpls = dict()
for tp in list_templates.get_VMTEMPLATE():
tpls[tp.get_NAME()] = tp.get_ID(),tp.get_UNAME()
return tpls



def vm_on_tmpl (one_user, one_password, one_endpoint,tpl_id, vm_name, start_type, addit_params, tpl_copy) -> dict:
tpls = dict()
start_type = False
tpl_copy = False
addit_params = ""
parser = argparse.ArgumentParser()
parser.add_argument(
'command', nargs='*',
help='''
createvm - create new VM based on template,
''')
one = pyone.OneServer(
one_endpoint, session="{}:{}".format(one_user, one_password))
create_vm_based_on_tpl = one.template.instantiate(
tpl_id,
"%s" % vm_name,
False,
" ",
False
)

def run_command(one_vms) -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
'command', nargs='*',
help='''
listvms - show list of VMs,
listtp - show list of all available templates,
ssh vm - connect to vm,
scp vm file [/path/to/file/on/vm] - scp file to vm if path/to not set used same,
rsync vm /path/on/vm - rsync current folder to vm,
Expand All @@ -116,6 +164,22 @@ def run_command(one_vms) -> None:
if args.command[0] == 'listvms':
for k, v in one_vms.items():
print(k, v)
elif args.command[0] == 'listtp':
user, password, endpoint = get_one_auth_data()
one_tpls=get_template_info(user, password, endpoint)
for k in one_tpls.items():
print(k)
elif args.command[0] == 'createvm':
tpl_id = int(args.command[1])
vm_name = args.command[2]
#vm_options = "";
if type(tpl_id) == int:
#vm_on_tmpl
print(tpl_id, "%s" % vm_name)
user, password, endpoint = get_one_auth_data()
vm_on_tmpl(user, password, endpoint, tpl_id, vm_name, False, "", False)
else:
print("please, check that template ID was set propely")
elif args.command[0] == 'ssh':
subprocess.call(['ssh', 'root@{}'.format(one_vms[args.command[1]])])
elif args.command[0] == 'ping':
Expand Down