2929def 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
100153def 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 ()
0 commit comments