def __validate_plugin_id(self, plugin_id): is_valid = re.match("^([0-9]|[A-Z]|[a-z]){32}$", plugin_id) if not is_valid: raise APIError(_("Invalid plugin id format.")) if self.is_post(): if Plugin.objects.filter(plugin_id=plugin_id).count() > 0: raise APIError(_("Plugin id already exist."))
def __validate_version(self, version): if not version: raise APIError( _("Invalid plugin config. You must provide Version key in your plugin.json." )) if not re.match("^(\d*)\.(\d*)\.(\d*)$", version): raise APIError(_("Invalid version format."))
def __validate_icon(self, icon_path): if not icon_path: raise APIError( _("Invalid plugin config. You must provide IcoPath key in your plugin.json. " 'It should be a relateive path to your plugin directory, E.g. "IcoPath":"Image\\\\myicon.png"' )) if not self.__is_image_extensions_valid(icon_path): raise APIError( _("Invalid plugin config. IcoPath extension is not allowed"))
def validate_plugin(self, plugin_file, attrs): if not plugin_file.name.endswith(".wox"): raise APIError( _("Invalid plugin file. Wox plugin should ends with .wox")) try: z = zipfile.ZipFile(plugin_file) except Exception, e: raise APIError(_("Invalid plugin file. {}".format(str(e))))
def __validate_language(self, language): if language.upper() not in [ k.upper() for k, v in plugins.models.LANGUAGE_CHOICES ]: raise APIError( _("Invalid plugin file. we didn't support {} language".format( language)))
def do_request(cls, url, headers, payload): try: logger.debug("Requesting %s", url) response = requests.post(url, json=payload, headers=headers) response.raise_for_status() return response.json() except requests.exceptions.HTTPError as http_error: raise APIError.create_from_http_error(http_error)
def create_from_template(cls, request, vm, vm_define_backup, log=logger): """Create backup definitions from vm.template.vm_define_backup list""" if vm_define_backup and isinstance(vm_define_backup, list): request = set_request_method(request, 'POST') for i, data in enumerate(vm_define_backup): try: try: bkpdef = data['bkpdef'] except KeyError: bkpdef = data['name'] disk_id, real_disk_id, zfs_filesystem = get_disk_id(request, vm, data) log.info('Creating backup definition [%d] "%s" for vm=%s, disk_id=%d defined by template %s', i, bkpdef, vm, disk_id, vm.template) define = get_object(request, BackupDefine, {'name': bkpdef, 'vm': vm, 'disk_id': real_disk_id}) res = cls(request, data=data).post(vm, define, vm_template=True) if res.status_code != scode.HTTP_201_CREATED: raise APIError('vm_define_backup error [%s]: %s' % (res.status_code, res.data)) except Exception as ex: log.warn('Failed to create backup definition [%d] for vm=%s defined by template %s with ' 'data="%s". Error: %s', i, vm, vm.template, data, ex)
def validate_preview(self, preview): if not self.__is_image_extensions_valid(preview.name): raise APIError( _("Invalid preview file. Preview image should be either a png or jpg" ))
def validate_step(self, attrs, source): step = attrs[source] if not step: raise APIError(_("step is requried")) return attrs
def get_backup_cmd(action, bkp, bkps=None, define=None, zfs_filesystem=None, fsfreeze=None, vm=None): """Return backup command suitable for execute()""" if bkp.type == Backup.DATASET: action_prefix = 'ds' file_backup = False elif bkp.type == Backup.FILE: action_prefix = 'file' file_backup = True else: raise APIError('Invalid backup type') vm = vm or bkp.vm if vm and bkp.node != vm.node: cmd_args = ['-H %s' % vm.node.address] else: cmd_args = [] if action == 'create': if file_backup: cmd_args.append('-f "%s" -s %s' % (bkp.create_file_path(), zfs_filesystem)) if define.compression: cmd_args.append('-c %s' % define.get_compression_display()) else: cmd_args.append('-d %s -s %s@%s -n "%s"' % (bkp.create_dataset_path(), zfs_filesystem, bkp.snap_name, bkp.name)) if define.bwlimit: cmd_args.append('-l %d' % define.bwlimit) if fsfreeze: cmd_args.append('-F "%s"' % fsfreeze) elif action == 'delete': if bkps: to_delete = ' '.join(['"%s"' % b.file_path for b in bkps]) else: to_delete = '"%s"' % bkp.file_path if file_backup: cmd_args = ['-f %s' % to_delete] else: cmd_args.append('-s %s' % to_delete) if vm: if bkps: last = ' '.join([ '%s@%s' % (b.zfs_filesystem_real, b.snap_name) for b in bkps if b.last ]) elif bkp.last: last = '%s@%s' % (bkp.zfs_filesystem_real, bkp.snap_name) else: last = None if last: cmd_args.append('-r %s' % last) elif action == 'restore': if file_backup: cmd_args.append('-f "%s" -d %s -c %s' % (bkp.file_path, zfs_filesystem, bkp.checksum)) else: cmd_args.append('-s %s -d %s' % (bkp.file_path, zfs_filesystem)) else: raise APIError('Invalid backup action') return 'esbackup %s-%s %s' % (action_prefix, action, ' '.join(cmd_args))
def validate_email(self, attrs, source): email = attrs[source] if User.objects.filter(email = email).count() > 0: raise APIError(u"email already exist") return attrs
def validate_version(self, attrs, source): version = attrs[source] if not re.match("^(\d*)\.(\d*)\.(\d*)$", version): raise APIError(_("Invalid version.")) return attrs
def validate_username(self, attrs, source): username = attrs[source] if User.objects.filter(username = username).count() > 0: raise APIError(u"username already exist") return attrs
class PluginValidator(object): def __init__(self, request): self.request = request def is_post(self): return self.request.method == "POST" def __is_image_extensions_valid(self, path): fileName, fileExtension = os.path.splitext(path) return fileExtension in settings.ALLOWED_IMAGE_EXTENSIONS def __validate_plugin_id(self, plugin_id): is_valid = re.match("^([0-9]|[A-Z]|[a-z]){32}$", plugin_id) if not is_valid: raise APIError(_("Invalid plugin id format.")) if self.is_post(): if Plugin.objects.filter(plugin_id=plugin_id).count() > 0: raise APIError(_("Plugin id already exist.")) def __validate_version(self, version): if not version: raise APIError( _("Invalid plugin config. You must provide Version key in your plugin.json." )) if not re.match("^(\d*)\.(\d*)\.(\d*)$", version): raise APIError(_("Invalid version format.")) def __validate_icon(self, icon_path): if not icon_path: raise APIError( _("Invalid plugin config. You must provide IcoPath key in your plugin.json. " 'It should be a relateive path to your plugin directory, E.g. "IcoPath":"Image\\\\myicon.png"' )) if not self.__is_image_extensions_valid(icon_path): raise APIError( _("Invalid plugin config. IcoPath extension is not allowed")) def __validate_execute_filename(self, executefilename): if not executefilename: raise APIError( _("Invalid plugin config. You must provide ExecuteFileName key in your plugin.json." )) def __validate_name(self, name): if self.is_post(): if Plugin.objects.filter(name=name).count() > 0: raise APIError(_("Plugin name already exist.")) def __validate_language(self, language): if language.upper() not in [ k.upper() for k, v in plugins.models.LANGUAGE_CHOICES ]: raise APIError( _("Invalid plugin file. we didn't support {} language".format( language))) def validate_plugin(self, plugin_file, attrs): if not plugin_file.name.endswith(".wox"): raise APIError( _("Invalid plugin file. Wox plugin should ends with .wox")) try: z = zipfile.ZipFile(plugin_file) except Exception, e: raise APIError(_("Invalid plugin file. {}".format(str(e)))) if not "plugin.json" in z.namelist(): raise APIError( _("Invalid plugin file. we didn't find plugin.json file in your plugin." )) try: config = simplejson.loads(z.read("plugin.json")) except Exception, e: raise APIError(_("Invalid plugin file. {}".format(str(e))))
def __validate_execute_filename(self, executefilename): if not executefilename: raise APIError( _("Invalid plugin config. You must provide ExecuteFileName key in your plugin.json." ))
def validate_icon(self, icon): if not self.__is_image_extensions_valid(icon.name): raise APIError( _("Invalid icon file. Preview image should be either a png or jpg" ))
def __validate_name(self, name): if self.is_post(): if Plugin.objects.filter(name=name).count() > 0: raise APIError(_("Plugin name already exist."))