1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515import os
16+ import uuid
17+ from jinja2 import Template
18+ from pkg_resources import resource_filename
19+
1620from cloudify import ctx
1721from cloudify import exceptions as cfy_exc
1822
@@ -32,6 +36,12 @@ def get_libvirt_params(**kwargs):
3236 template_params .update (kwargs .get ('params' , {}))
3337 ctx .instance .runtime_properties ['params' ] = template_params
3438
39+ # set default names and instance_uuid
40+ if not template_params .get ("name" ):
41+ template_params ["name" ] = ctx .instance .id
42+ if not template_params .get ("instance_uuid" ):
43+ template_params ["instance_uuid" ] = str (uuid .uuid4 ())
44+
3545 # update 'resource_id', 'use_external_resource' from kwargs
3646 for field in ['resource_id' , 'use_external_resource' ]:
3747 if field in kwargs :
@@ -40,12 +50,38 @@ def get_libvirt_params(**kwargs):
4050 return libvirt_auth , template_params
4151
4252
53+ def gen_xml_template (kwargs , template_params , default_template ):
54+ # templates
55+ template_resource = kwargs .get ('template_resource' )
56+ template_content = kwargs .get ('template_content' )
57+
58+ if template_resource :
59+ template_content = ctx .get_resource (template_resource )
60+
61+ if not (template_resource or template_content ):
62+ resource_dir = resource_filename (__name__ , 'templates' )
63+ template_resource = '{}/{}.xml' .format (resource_dir , default_template )
64+ ctx .logger .info ("Will be used internal: %s" % template_resource )
65+
66+ if not template_content :
67+ with open (template_resource ) as object_desc :
68+ template_content = object_desc .read ()
69+
70+ template_engine = Template (template_content )
71+ params = {"ctx" : ctx }
72+ if template_params :
73+ params .update (template_params )
74+ xmlconfig = template_engine .render (params )
75+ ctx .logger .debug (repr (xmlconfig ))
76+ return xmlconfig
77+
78+
4379def get_backupname (kwargs ):
4480 if not kwargs .get ("snapshot_name" ):
4581 raise cfy_exc .NonRecoverableError (
4682 'Backup name must be provided.'
4783 )
48- return kwargs ["snapshot_name" ]
84+ return "{}-{}" . format ( ctx . instance . id , kwargs ["snapshot_name" ])
4985
5086
5187def get_backupdir (kwargs ):
@@ -99,3 +135,71 @@ def delete_binary_place(backup_dir, object_name):
99135 full_path = get_binary_place (backup_dir , object_name )
100136 if os .path .isfile (full_path ):
101137 os .remove (full_path )
138+
139+
140+ def xml_snapshot_create (kwargs , resource_id , current_xmldump ):
141+ snapshot_name = get_backupname (kwargs )
142+ if kwargs .get ("snapshot_incremental" ):
143+ backups = ctx .instance .runtime_properties .get ("backups" , {})
144+ if snapshot_name in backups :
145+ raise cfy_exc .NonRecoverableError (
146+ "Snapshot {snapshot_name} already exists."
147+ .format (snapshot_name = snapshot_name ,))
148+ backups [snapshot_name ] = current_xmldump
149+ ctx .instance .runtime_properties ["backups" ] = backups
150+ ctx .logger .info ("Snapshot {snapshot_name} is created."
151+ .format (snapshot_name = snapshot_name ,))
152+ else :
153+ if read_node_state (get_backupdir (kwargs ), resource_id ):
154+ raise cfy_exc .NonRecoverableError (
155+ "Backup {snapshot_name} already exists."
156+ .format (snapshot_name = snapshot_name ,))
157+ save_node_state (get_backupdir (kwargs ), resource_id , current_xmldump )
158+ ctx .logger .info ("Backup {snapshot_name} is created."
159+ .format (snapshot_name = snapshot_name ,))
160+ ctx .logger .debug ("Current config {}" .format (repr (current_xmldump )))
161+
162+
163+ def xml_snapshot_apply (kwargs , resource_id , current_xmldump ):
164+ snapshot_name = get_backupname (kwargs )
165+ if kwargs .get ("snapshot_incremental" ):
166+ backups = ctx .instance .runtime_properties .get ("backups" , {})
167+ if snapshot_name not in backups :
168+ raise cfy_exc .NonRecoverableError (
169+ "No snapshots found with name: {snapshot_name}."
170+ .format (snapshot_name = snapshot_name ,))
171+ xml_backup = backups [snapshot_name ]
172+ else :
173+ xml_backup = read_node_state (get_backupdir (kwargs ), resource_id )
174+ if not xml_backup :
175+ raise cfy_exc .NonRecoverableError (
176+ "No backups found with name: {snapshot_name}."
177+ .format (snapshot_name = snapshot_name ,))
178+
179+ if xml_backup .strip () != current_xmldump .strip ():
180+ ctx .logger .info ("We have different configs,\n {}\n vs\n {}\n "
181+ .format (
182+ repr (xml_backup .strip ()),
183+ repr (current_xmldump .strip ())))
184+ else :
185+ ctx .logger .info ("Already used such configuration: {}"
186+ .format (snapshot_name ))
187+
188+
189+ def xml_snapshot_delete (kwargs , resource_id ):
190+ snapshot_name = get_backupname (kwargs )
191+ if kwargs .get ("snapshot_incremental" ):
192+ backups = ctx .instance .runtime_properties .get ("backups" , {})
193+ if snapshot_name not in backups :
194+ raise cfy_exc .NonRecoverableError (
195+ "No snapshots found with name: {snapshot_name}."
196+ .format (snapshot_name = snapshot_name ,))
197+ del backups [snapshot_name ]
198+ ctx .instance .runtime_properties ["backups" ] = backups
199+ else :
200+ if not read_node_state (get_backupdir (kwargs ), resource_id ):
201+ raise cfy_exc .NonRecoverableError (
202+ "No backups found with name: {snapshot_name}."
203+ .format (snapshot_name = snapshot_name ,))
204+ delete_node_state (get_backupdir (kwargs ), resource_id )
205+ ctx .logger .info ("Backup deleted: {}" .format (snapshot_name ))
0 commit comments