Skip to content

Commit 90fc7b7

Browse files
authored
Merge pull request #2 from cloudify-incubator/memory_fixes
Memory fixes
2 parents 360ed2e + f030436 commit 90fc7b7

9 files changed

Lines changed: 139 additions & 19 deletions

File tree

cloudify_libvirt/domain_tasks.py

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
def create(**kwargs):
3030
ctx.logger.info("create")
3131
get_libvirt_params(**kwargs)
32+
# dont need to run anything, we attach disc's in preconfigure state
33+
# so we will define domain later
3234

3335

3436
@operation
@@ -64,18 +66,21 @@ def configure(**kwargs):
6466

6567
if not template_params.get("resource_id"):
6668
template_params["resource_id"] = ctx.instance.id
67-
if (not template_params.get("memmory_minsize") and
68-
template_params.get('memmory_size')):
69-
template_params["memmory_minsize"] = int(
70-
template_params['memmory_size']) / 2
69+
if (not template_params.get("memory_minsize") and
70+
template_params.get('memory_size')):
71+
# if have no minimal memory size, set current as minimum
72+
# and twised memory as maximum
73+
memory_size = int(template_params['memory_size'])
74+
template_params["memory_minsize"] = memory_size
75+
template_params['memory_size'] = memory_size * 2
7176
if not template_params.get("instance_uuid"):
7277
template_params["instance_uuid"] = str(uuid.uuid4())
7378

7479
params = {"ctx": ctx}
7580
params.update(template_params)
7681
xmlconfig = template_engine.render(params)
7782

78-
ctx.logger.info(xmlconfig)
83+
ctx.logger.info(repr(xmlconfig))
7984

8085
dom = conn.defineXML(xmlconfig)
8186
if dom is None:
@@ -96,6 +101,54 @@ def configure(**kwargs):
96101
ctx.instance.runtime_properties['params'] = template_params
97102

98103

104+
@operation
105+
def start(**kwargs):
106+
ctx.logger.info("start")
107+
108+
resource_id = ctx.instance.runtime_properties.get('resource_id')
109+
110+
if not resource_id:
111+
ctx.logger.info("No servers for start")
112+
return
113+
114+
libvirt_auth, _ = get_libvirt_params(**kwargs)
115+
conn = libvirt.open(libvirt_auth)
116+
if conn is None:
117+
raise cfy_exc.NonRecoverableError(
118+
'Failed to open connection to the hypervisor'
119+
)
120+
121+
try:
122+
try:
123+
dom = conn.lookupByName(resource_id)
124+
except Exception as e:
125+
dom = None
126+
ctx.logger.info("Non critical error: {}".format(str(e)))
127+
128+
if dom is None:
129+
raise cfy_exc.NonRecoverableError(
130+
'Failed to find the domain'
131+
)
132+
133+
state, reason = dom.state()
134+
for i in xrange(10):
135+
state, reason = dom.state()
136+
137+
if state == libvirt.VIR_DOMAIN_RUNNING:
138+
ctx.logger.info("Looks as running.")
139+
return
140+
141+
ctx.logger.info("Tring to start vm {}/10".format(i))
142+
if dom.create() < 0:
143+
raise cfy_exc.NonRecoverableError(
144+
'Can not start guest domain.'
145+
)
146+
time.sleep(30)
147+
state, reason = dom.state()
148+
finally:
149+
conn.close()
150+
151+
99152
@operation
100153
def stop(**kwargs):
101154
ctx.logger.info("stop")
@@ -290,3 +343,62 @@ def delete(**kwargs):
290343
)
291344
finally:
292345
conn.close()
346+
347+
348+
def _current_use(dom):
349+
total_list = dom.getCPUStats(True)
350+
if len(total_list):
351+
total = total_list[0]
352+
else:
353+
total = {}
354+
return (
355+
total.get('user_time', 0) + total.get('system_time', 0) +
356+
total.get('cpu_time', 0)
357+
) / 1000000000.0
358+
359+
360+
@operation
361+
def perfomance(**kwargs):
362+
ctx.logger.info("update statistics")
363+
resource_id = ctx.instance.runtime_properties.get('resource_id')
364+
365+
if not resource_id:
366+
ctx.logger.info("No servers for statistics.")
367+
return
368+
369+
libvirt_auth, template_params = get_libvirt_params(**kwargs)
370+
conn = libvirt.open(libvirt_auth)
371+
if conn is None:
372+
raise cfy_exc.NonRecoverableError(
373+
'Failed to open connection to the hypervisor'
374+
)
375+
376+
try:
377+
try:
378+
dom = conn.lookupByName(resource_id)
379+
except Exception as e:
380+
dom = None
381+
ctx.logger.info("Non critical error: {}".format(str(e)))
382+
383+
if dom is None:
384+
raise cfy_exc.NonRecoverableError(
385+
'Failed to find the domain'
386+
)
387+
388+
statistics = ctx.instance.runtime_properties.get('stat', {})
389+
390+
before_usage = _current_use(dom)
391+
392+
# usage generated by compare cpu_time before
393+
# and after sleep for 5 seconds.
394+
ctx.logger.debug("Used: {} seconds.".format(before_usage))
395+
time.sleep(5)
396+
statistics['cpu'] = 100 * (_current_use(dom) - before_usage) / 5
397+
398+
memory = dom.memoryStats()
399+
statistics['memory'] = memory.get('actual', 0) / 1024.0
400+
ctx.instance.runtime_properties['stat'] = statistics
401+
402+
ctx.logger.info("Statistics: {}".format(repr(statistics)))
403+
finally:
404+
conn.close()

cloudify_libvirt/templates/domain.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<partition>/machine</partition>
44
</resource>
55
<uuid>{{ instance_uuid }}</uuid>
6-
<currentMemory unit="KiB">{{ memmory_minsize }}</currentMemory>
6+
<currentMemory unit="KiB">{{ memory_minsize }}</currentMemory>
77
<on_poweroff>destroy</on_poweroff>
88
<devices>
99
<console type="pty" tty="/dev/pts/1">
@@ -56,7 +56,7 @@
5656
<acpi/>
5757
<apic/>
5858
</features>
59-
<memory unit="KiB">{{ memmory_size }}</memory>
59+
<memory unit="KiB">{{ memory_size }}</memory>
6060
<os>
6161
<type machine="pc" arch="x86_64">hvm</type>
6262
<boot dev="hd"/>

examples/cluster.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ node_templates:
110110
inputs:
111111
params:
112112
vcpu: 2
113-
memmory_size: 1048576
113+
memory_size: 1048576
114114
networks:
115115
- network: { get_attribute: [common_network, resource_id] }
116116
dev: vnet0

examples/templates/domain-arm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<partition>/machine</partition>
44
</resource>
55
<uuid>{{ instance_uuid }}</uuid>
6-
<currentMemory unit="KiB">{{ memmory_minsize }}</currentMemory>
6+
<currentMemory unit="KiB">{{ memory_minsize }}</currentMemory>
77
<on_poweroff>destroy</on_poweroff>
88
<devices>
99
<emulator>/usr/bin/qemu-system-aarch64</emulator>
@@ -63,7 +63,7 @@
6363
<features>
6464
<gic version='2'/>
6565
</features>
66-
<memory unit="KiB">{{ memmory_size }}</memory>
66+
<memory unit="KiB">{{ memory_size }}</memory>
6767
<os>
6868
<type arch='aarch64' machine='virt'>hvm</type>
6969
<loader readonly='yes' type='pflash'>/usr/share/AAVMF/AAVMF_CODE.fd</loader>

examples/vm_agent.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ node_templates:
7777
create:
7878
inputs:
7979
params:
80-
memmory_size: 524288
80+
memory_size: 524288
8181
networks:
8282
- network: { get_attribute: [common_network, resource_id] }
8383
dev: vnet0

examples/vm_centos.amd64.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ node_templates:
7474
inputs:
7575
params:
7676
vcpu: 2
77-
memmory_size: 524288
77+
memory_size: 524288
7878
networks:
7979
- network: { get_attribute: [common_network, resource_id] }
8080
dev: vnet0

examples/vm_fabric.amd64.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ node_templates:
7474
inputs:
7575
params:
7676
vcpu: 2
77-
memmory_size: 524288
77+
memory_size: 524288
7878
networks:
7979
- network: { get_attribute: [common_network, resource_id] }
8080
dev: vnet0

examples/vm_fabric.arm64.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ node_templates:
6868
create:
6969
inputs:
7070
params:
71-
memmory_size: 524288
71+
memory_size: 524288
7272
nvram: { get_attribute: [disk_clone, bios_flash] }
7373
disks:
7474
- bus: scsi

plugin.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@ node_types:
2020
configure:
2121
implementation: libvirt.cloudify_libvirt.domain_tasks.configure
2222
inputs: {}
23-
suspend:
24-
implementation: libvirt.cloudify_libvirt.domain_tasks.suspend
25-
inputs: {}
26-
resume:
27-
implementation: libvirt.cloudify_libvirt.domain_tasks.resume
23+
start:
24+
implementation: libvirt.cloudify_libvirt.domain_tasks.start
2825
inputs: {}
2926
stop:
3027
implementation: libvirt.cloudify_libvirt.domain_tasks.stop
3128
inputs: {}
3229
delete:
3330
implementation: libvirt.cloudify_libvirt.domain_tasks.delete
3431
inputs: {}
32+
# suspend/resume
33+
suspend:
34+
implementation: libvirt.cloudify_libvirt.domain_tasks.suspend
35+
inputs: {}
36+
resume:
37+
implementation: libvirt.cloudify_libvirt.domain_tasks.resume
38+
inputs: {}
39+
cloudify.interfaces.statistics:
40+
perfomance:
41+
implementation: libvirt.cloudify_libvirt.domain_tasks.perfomance
42+
inputs: {}
3543

3644
cloudify.libvirt.network:
3745
derived_from: cloudify.nodes.Network

0 commit comments

Comments
 (0)