From 460846cef28a502a51e4d52865743aea73e2f960 Mon Sep 17 00:00:00 2001 From: jichenjc Date: Sat, 20 Feb 2016 22:33:18 +0800 Subject: [compute] Add server backup function Add server backup function There is no return value for this command per following doc http://developer.openstack.org/api-ref-compute-v2.1.html#createBackup, also novaclient can't be updated now due to backward compatible issue http://lists.openstack.org/pipermail/openstack-dev/2016-March/089376.html, so we have to get the information ourselves. The Image tests were not using warlock images, so that needed to be fixed before we could completely test things like --wait. Change-Id: I30159518c4d3fdec89f15963bda641a0b03962d1 --- openstackclient/compute/v2/server_backup.py | 134 ++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 openstackclient/compute/v2/server_backup.py (limited to 'openstackclient/compute') diff --git a/openstackclient/compute/v2/server_backup.py b/openstackclient/compute/v2/server_backup.py new file mode 100644 index 00000000..24d71015 --- /dev/null +++ b/openstackclient/compute/v2/server_backup.py @@ -0,0 +1,134 @@ +# Copyright 2012-2013 OpenStack Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +"""Compute v2 Server action implementations""" + +import sys + +from oslo_utils import importutils +import six + +from openstackclient.common import command +from openstackclient.common import exceptions +from openstackclient.common import utils +from openstackclient.i18n import _ + + +def _show_progress(progress): + if progress: + sys.stderr.write('\rProgress: %s' % progress) + sys.stderr.flush() + + +class CreateServerBackup(command.ShowOne): + """Create a server backup image""" + + IMAGE_API_VERSIONS = { + "1": "openstackclient.image.v1.image", + "2": "openstackclient.image.v2.image", + } + + def get_parser(self, prog_name): + parser = super(CreateServerBackup, self).get_parser(prog_name) + parser.add_argument( + 'server', + metavar='', + help=_('Server to back up (name or ID)'), + ) + parser.add_argument( + '--name', + metavar='', + help=_('Name of the backup image (default: server name)'), + ) + parser.add_argument( + '--type', + metavar='', + help=_( + 'Used to populate the backup_type property of the backup ' + 'image (default: empty)' + ), + ) + parser.add_argument( + '--rotate', + metavar='', + type=int, + help=_('Number of backups to keep (default: 1)'), + ) + parser.add_argument( + '--wait', + action='store_true', + help=_('Wait for backup image create to complete'), + ) + return parser + + def take_action(self, parsed_args): + compute_client = self.app.client_manager.compute + + server = utils.find_resource( + compute_client.servers, + parsed_args.server, + ) + + # Set sane defaults as this API wants all mouths to be fed + if parsed_args.name is None: + backup_name = server.name + else: + backup_name = parsed_args.name + if parsed_args.type is None: + backup_type = "" + else: + backup_type = parsed_args.type + if parsed_args.rotate is None: + backup_rotation = 1 + else: + backup_rotation = parsed_args.rotate + + compute_client.servers.backup( + server.id, + backup_name, + backup_type, + backup_rotation, + ) + + image_client = self.app.client_manager.image + image = utils.find_resource( + image_client.images, + backup_name, + ) + + if parsed_args.wait: + if utils.wait_for_status( + image_client.images.get, + image.id, + callback=_show_progress, + ): + sys.stdout.write('\n') + else: + msg = _('Error creating server backup: %s') % parsed_args.name + raise exceptions.CommandError(msg) + + if self.app.client_manager._api_version['image'] == '1': + info = {} + info.update(image._info) + info['properties'] = utils.format_dict(info.get('properties', {})) + else: + # Get the right image module to format the output + image_module = importutils.import_module( + self.IMAGE_API_VERSIONS[ + self.app.client_manager._api_version['image'] + ] + ) + info = image_module._format_image(image) + return zip(*sorted(six.iteritems(info))) -- cgit v1.2.1