Skip to content

Commit

Permalink
Merge pull request #38 from mfalesni/hwprof
Browse files Browse the repository at this point in the history
Hardware configuration (CPU, RAM) during deployment
  • Loading branch information
jkrocil committed Jun 6, 2016
2 parents 626b632 + b1270f4 commit 8836346
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
23 changes: 14 additions & 9 deletions mgmtsystem/openstack.py
Expand Up @@ -439,6 +439,7 @@ def deploy_template(self, template, *args, **kwargs):
Args:
template: The name of the template to use.
flavour_name: The name of the flavour to use.
flavour_id: UUID of the flavour to use.
vm_name: A name to use for the vm.
network_name: The name of the network if it is a multi network setup (Havanna).
Expand All @@ -448,10 +449,16 @@ def deploy_template(self, template, *args, **kwargs):
power_on = kwargs.pop("power_on", True)
nics = []
timeout = kwargs.pop('timeout', 900)
if 'flavour_name' not in kwargs:
kwargs['flavour_name'] = 'm1.tiny'
if 'flavour_name' in kwargs:
flavour = self.api.flavors.find(name=kwargs['flavour_name'])
elif 'flavour_id' in kwargs:
flavour = self.api.flavors.find(id=kwargs['flavour_id'])
else:
flavour = self.api.flavors.find(name='m1.tiny')
if 'vm_name' not in kwargs:
kwargs['vm_name'] = 'new_instance_name'
vm_name = 'new_instance_name'
else:
vm_name = kwargs['vm_name']
self.logger.info(" Deploying OpenStack template %s to instance %s (%s)" % (
template, kwargs["vm_name"], kwargs["flavour_name"]))
if len(self.list_network()) > 1:
Expand All @@ -462,17 +469,15 @@ def deploy_template(self, template, *args, **kwargs):
nics = [{'net-id': net_id}]

image = self.api.images.find(name=template)
flavour = self.api.flavors.find(name=kwargs['flavour_name'])
instance = self.api.servers.create(kwargs['vm_name'], image, flavour, nics=nics,
*args, **kwargs)
self.wait_vm_running(kwargs['vm_name'], num_sec=timeout)
instance = self.api.servers.create(vm_name, image, flavour, nics=nics, *args, **kwargs)
self.wait_vm_running(vm_name, num_sec=timeout)
if kwargs.get('floating_ip_pool', None):
self.assign_floating_ip(instance, kwargs['floating_ip_pool'])

if power_on:
self.start_vm(kwargs['vm_name'])
self.start_vm(vm_name)

return kwargs['vm_name']
return vm_name

def assign_floating_ip(self, instance_or_name, floating_ip_pool, safety_timer=5):
"""Assigns a floating IP to an instance.
Expand Down
7 changes: 5 additions & 2 deletions mgmtsystem/rhevm.py
Expand Up @@ -387,8 +387,11 @@ def deploy_template(self, template, *args, **kwargs):
policy = params.VmPlacementPolicy(host=host,
affinity=kwargs['placement_policy_affinity'])
vm_kwargs['placement_policy'] = policy
vm = params.VM(**vm_kwargs)
self.api.vms.add(vm)
if 'cpu' in kwargs:
vm_kwargs['cpu'] = params.CPU(topology=params.CpuTopology(cores=int(kwargs['cpu'])))
if 'ram' in kwargs:
vm_kwargs['memory'] = int(kwargs['ram']) * 1024 * 1024 # MB
self.api.vms.add(params.VM(**vm_kwargs))
self.wait_vm_stopped(kwargs['vm_name'], num_sec=timeout)
if power_on:
self.start_vm(kwargs['vm_name'])
Expand Down
7 changes: 6 additions & 1 deletion mgmtsystem/virtualcenter.py
Expand Up @@ -497,7 +497,7 @@ def _pick_datastore(self, allowed_datastores):

def clone_vm(self, source, destination, resourcepool=None, datastore=None, power_on=True,
sparse=False, template=False, provision_timeout=1800, progress_callback=None,
allowed_datastores=None):
allowed_datastores=None, cpu=None, ram=None):
try:
if mobs.VirtualMachine.get(self.api, name=destination).name == destination:
raise Exception("VM already present!")
Expand Down Expand Up @@ -550,6 +550,11 @@ def clone_vm(self, source, destination, resourcepool=None, datastore=None, power
vm_clone_spec.location = vm_reloc_spec
vm_clone_spec.snapshot = None

if cpu is not None:
vm_clone_spec.config.numCPUs = int(cpu)
if ram is not None:
vm_clone_spec.config.memoryMB = int(ram)

try:
folder = source_template.parent.parent.vmParent
except AttributeError:
Expand Down

0 comments on commit 8836346

Please sign in to comment.