Allow users to create snapshots of their virtual machines

This commit is contained in:
Damian Myerscough 2015-03-25 12:44:52 -07:00
parent 2c6753941a
commit b7231a1b3d

View File

@ -867,3 +867,60 @@ def snapshot_list(kwargs=None, call=None):
ret[vm]['snapshots'].append(snap.get_name()) ret[vm]['snapshots'].append(snap.get_name())
return ret return ret
def create_snapshot(kwargs=None, call=None):
'''
Create a snapshot
@name: Name of the virtual machine to snapshot
@snapshot: Name of the snapshot
@description: Description of the snapshot (optional)
@memory: Dump of the internal state of the virtual machine (optional)
CLI Example:
.. code-block:: bash
salt-cloud -f create_snapshot [PROVIDER] name=myvm.example.com snapshot=mysnapshot
salt-cloud -f create_snapshot [PROVIDER] name=myvm.example.com snapshot=mysnapshot description='My Snapshot' memory=True
'''
if call != 'function':
log.error(
'The show_keypair function must be called with -f or --function.'
)
return False
if not kwargs:
kwargs = {}
if 'name' not in kwargs or 'snapshot' not in kwargs:
log.error('name and snapshot are required arguments')
return False
ret = {}
conn = get_conn()
vm = conn.get_vm_by_name(kwargs['name'])
try:
log.info('Creating snapshot')
vm.create_snapshot(
kwargs['snapshot'],
kwargs.get('description', None),
kwargs.get('memory', False)
)
ret['name'] = kwargs['name']
ret['snapshot'] = kwargs['snapshot']
ret['comment'] = 'Snapshot created'
ret['result'] = True
except:
log.error('Unable to create snapshot')
ret['name'] = kwargs['name']
ret['snapshot'] = kwargs['snapshot']
ret['comment'] = 'Failed to create snapshot'
ret['result'] = False
return ret