@@ -33,34 +33,8 @@ def create(**kwargs):
3333 # so we will define domain later
3434
3535
36- @operation
37- def configure (** kwargs ):
38- ctx .logger .info ("configure" )
39-
40- libvirt_auth , template_params = common .get_libvirt_params (** kwargs )
41- conn = libvirt .open (libvirt_auth )
42- if conn is None :
43- raise cfy_exc .NonRecoverableError (
44- 'Failed to open connection to the hypervisor'
45- )
46-
47- domain_file = kwargs .get ('domain_file' )
48- domain_template = kwargs .get ('domain_template' )
49-
50- if domain_file :
51- domain_template = ctx .get_resource (domain_file )
52-
53- if not domain_file and not domain_template :
54- resource_dir = resource_filename (__name__ , 'templates' )
55- domain_file = '{}/domain.xml' .format (resource_dir )
56- ctx .logger .info ("Will be used internal: %s" % domain_file )
57-
58- if not domain_template :
59- domain_desc = open (domain_file )
60- with domain_desc :
61- domain_template = domain_desc .read ()
62-
63- template_engine = Template (domain_template )
36+ def _update_template_params (template_params ):
37+ # set all params to default values
6438 if not template_params :
6539 template_params = {}
6640
@@ -77,14 +51,66 @@ def configure(**kwargs):
7751 template_params ["instance_uuid" ] = str (uuid .uuid4 ())
7852 if not template_params .get ("domain_type" ):
7953 template_params ["domain_type" ] = "qemu"
54+ return template_params
8055
81- params = {"ctx" : ctx }
82- params .update (template_params )
83- xmlconfig = template_engine .render (params )
8456
85- ctx .logger .debug (repr (xmlconfig ))
57+ @operation
58+ def configure (** kwargs ):
59+ ctx .logger .info ("configure" )
60+
61+ libvirt_auth , template_params = common .get_libvirt_params (** kwargs )
62+ conn = libvirt .open (libvirt_auth )
63+ if conn is None :
64+ raise cfy_exc .NonRecoverableError (
65+ 'Failed to open connection to the hypervisor'
66+ )
67+
68+ template_params = _update_template_params (template_params )
8669
8770 try :
71+ if template_params .get ("use_external_resource" ):
72+ # lookup the default domain by name
73+ try :
74+ dom = conn .lookupByName (template_params ["resource_id" ])
75+ except Exception as e :
76+ dom = None
77+ ctx .logger .info ("Non critical error: {}" .format (str (e )))
78+
79+ if dom is None :
80+ raise cfy_exc .NonRecoverableError (
81+ 'Failed to find the domain'
82+ )
83+
84+ # save settings
85+ ctx .instance .runtime_properties ['params' ] = template_params
86+ ctx .instance .runtime_properties ['resource_id' ] = dom .name ()
87+ ctx .instance .runtime_properties ['use_external_resource' ] = True
88+ return
89+
90+ # templates
91+ domain_file = kwargs .get ('domain_file' )
92+ domain_template = kwargs .get ('domain_template' )
93+
94+ if domain_file :
95+ domain_template = ctx .get_resource (domain_file )
96+
97+ if not domain_file and not domain_template :
98+ resource_dir = resource_filename (__name__ , 'templates' )
99+ domain_file = '{}/domain.xml' .format (resource_dir )
100+ ctx .logger .info ("Will be used internal: %s" % domain_file )
101+
102+ if not domain_template :
103+ domain_desc = open (domain_file )
104+ with domain_desc :
105+ domain_template = domain_desc .read ()
106+
107+ template_engine = Template (domain_template )
108+ params = {"ctx" : ctx }
109+ params .update (template_params )
110+ xmlconfig = template_engine .render (params )
111+
112+ ctx .logger .debug (repr (xmlconfig ))
113+
88114 dom = conn .defineXML (xmlconfig )
89115 if dom is None :
90116 raise cfy_exc .NonRecoverableError (
@@ -234,6 +260,12 @@ def start(**kwargs):
234260 )
235261
236262 time .sleep (30 )
263+
264+ # still no ip
265+ if wait_for_ip :
266+ raise cfy_exc .RecoverableError (
267+ 'No ip for now, try later'
268+ )
237269 finally :
238270 conn .close ()
239271
@@ -248,6 +280,10 @@ def stop(**kwargs):
248280 ctx .logger .info ("No servers for delete" )
249281 return
250282
283+ if ctx .instance .runtime_properties .get ('use_external_resource' ):
284+ ctx .logger .info ("External resource, skip" )
285+ return
286+
251287 libvirt_auth , _ = common .get_libvirt_params (** kwargs )
252288 conn = libvirt .open (libvirt_auth )
253289 if conn is None :
@@ -439,6 +475,10 @@ def delete(**kwargs):
439475 ctx .logger .info ("No servers for delete" )
440476 return
441477
478+ if ctx .instance .runtime_properties .get ('use_external_resource' ):
479+ ctx .logger .info ("External resource, skip" )
480+ return
481+
442482 libvirt_auth , _ = common .get_libvirt_params (** kwargs )
443483 conn = libvirt .open (libvirt_auth )
444484 if conn is None :
0 commit comments