Example #1
0
    def _init_snapshot(self,
                       upload,
                       name=None,
                       overwrite=False,
                       background=False,
                       extra_args=None):
        # type: (str, Optional[str], bool, bool, Optional[Dict[str, Any]]) -> Union[str, Dict[str, str]]
        if self.network is None:
            self.set_network()

        if name is None:
            name = Options.default_snapshot_prefix + get_uuid()
        validate_name(name)

        if name in self.list_snapshots():
            if overwrite:
                self.delete_snapshot(name)
            else:
                raise ValueError(
                    'A snapshot named '
                    '{}'
                    ' already exists in network '
                    '{}'
                    '. '
                    'Use overwrite = True if you want to overwrite the '
                    'existing snapshot'.format(name, self.network))

        file_to_send = upload
        tmp_file_name = None  # type: Optional[Text]
        if os.path.isdir(upload):
            # delete=False because we re-open for reading
            with tempfile.NamedTemporaryFile(delete=False) as temp_zip_file:
                zip_dir(upload, temp_zip_file)
                tmp_file_name = file_to_send = temp_zip_file.name

        with open(file_to_send, 'rb') as fd:
            json_data = workhelper.get_data_upload_snapshot(self, name, fd)

            resthelper.get_json_response(self,
                                         CoordConsts.SVC_RSC_UPLOAD_SNAPSHOT,
                                         json_data)
        # Cleanup tmp file if we made one
        if tmp_file_name is not None:
            try:
                os.remove(tmp_file_name)
            except (OSError, IOError):
                # If we can't delete the file for some reason, let it be,
                # no need to crash initialization
                pass

        return self._parse_snapshot(name, background, extra_args)
Example #2
0
 def __init_snapshot_from_io(self, name, fd):
     # type: (str, IO) -> None
     json_data = workhelper.get_data_upload_snapshot(self, name, fd)
     resthelper.get_json_response(
         self, CoordConsts.SVC_RSC_UPLOAD_SNAPSHOT, json_data)