async def _internalNewSnapshot(self, request: Request, input_json, date=None, verify_header=True) -> Response: async with self._snapshot_lock: async with self._snapshot_inner_lock: if 'wait' in input_json: await sleep(input_json['wait']) if verify_header: await self._verifyHeader(request) slug = self.generateId(8) password = input_json.get('password', None) data = createSnapshotTar( slug, input_json.get('name', "Default name"), date=date or self._time.now(), padSize=int( random.uniform(self._min_snapshot_size, self._max_snapshot_size)), included_folders=input_json.get('folders', None), included_addons=input_json.get('addons', None), password=password) snapshot_info = parseSnapshotInfo(data) self._snapshots[slug] = snapshot_info self._snapshot_data[slug] = bytearray(data.getbuffer()) return slug
async def _uploadSnapshot(self, request: Request): await self._verifyHeader(request) try: received_bytes = await self.readAll(request) info = parseSnapshotInfo(io.BytesIO(received_bytes)) self._snapshots[info['slug']] = info self._snapshot_data[info['slug']] = received_bytes return self._formatDataResponse({"slug": info['slug']}) except Exception as e: print(str(e)) return self._formatErrorResponse("Bad snapshot")
async def uploadNewSnapshot(self, request: Request): if self.checkForSupervisorError() is not None: return self.checkForSupervisorError() await self._verifyHassioHeader(request) try: received_bytes = await self.readAll(request) info = parseSnapshotInfo(BytesIO(received_bytes)) self.snapshots[info['slug']] = info self.snapshot_data[info['slug']] = received_bytes return self.formatDataResponse({"slug": info['slug']}) except Exception as e: print(str(e)) return self.formatErrorResponse("Bad snapshot")
async def _uploadSnapshot(self, request: Request): await self._verifyHeader(request) try: reader = await request.multipart() contents = await reader.next() received_bytes = bytearray() while True: chunk = await contents.read_chunk() if not chunk: break received_bytes.extend(chunk) info = parseSnapshotInfo(io.BytesIO(received_bytes)) self._snapshots[info['slug']] = info self._snapshot_data[info['slug']] = received_bytes return self._formatDataResponse({"slug": info['slug']}) except Exception as e: print(str(e)) return self._formatErrorResponse("Bad snapshot")
async def _newSnapshot(self, request: Request): if self._snapshot_lock.locked(): raise HTTPBadRequest() input_json = await request.json() async with self._snapshot_lock: async with self._snapshot_inner_lock: await self._verifyHeader(request) slug = self.generateId(8) password = input_json.get('password', None) data = createSnapshotTar( slug, input_json.get('name', "Default name"), date=self._time.now(), padSize=int(random.uniform(self._min_snapshot_size, self._max_snapshot_size)), included_folders=input_json.get('folders', None), included_addons=input_json.get('addons', None), password=password) snapshot_info = parseSnapshotInfo(data) self._snapshots[slug] = snapshot_info self._snapshot_data[slug] = bytearray(data.getbuffer()) return self._formatDataResponse({"slug": slug})
async def hassioNewFullSnapshot(self, request: Request): if self.checkForSupervisorError() is not None: return self.checkForSupervisorError() if (self.block_snapshots or self.snapshot_in_progress ) and not self.getSetting('always_hard_lock'): raise HTTPBadRequest() input_json = {} try: input_json = await request.json() except: # noqa: E722 pass try: await self._snapshot_lock.acquire() self.snapshot_in_progress = True await self._verifyHassioHeader(request) error = self.getSetting("hassio_snapshot_error") if error is not None: raise HttpMultiException(error) seconds = int( request.query.get('seconds', self.getSetting('snapshot_wait_time'))) date = self._time.now() size = int( random.uniform(float(self.getSetting('snapshot_min_size')), float(self.getSetting('snapshot_max_size')))) slug = self.generateId(8) name = input_json.get('name', "Default name") password = input_json.get('password', None) if seconds > 0: await asyncio.sleep(seconds) data = createSnapshotTar(slug, name, date, size, password=password) snapshot_info = parseSnapshotInfo(data) self.snapshots[slug] = snapshot_info self.snapshot_data[slug] = bytearray(data.getbuffer()) return self.formatDataResponse({"slug": slug}) finally: self.snapshot_in_progress = False self._snapshot_lock.release()
async def hassioNewPartialSnapshot(self, request: Request): if self.checkForSupervisorError() is not None: return self.checkForSupervisorError() if (self.block_snapshots or self.snapshot_in_progress ) and not self.getSetting('always_hard_lock'): raise HTTPBadRequest() input_json = await request.json() try: await self._snapshot_lock.acquire() self.snapshot_in_progress = True await self._verifyHassioHeader(request) seconds = int( request.query.get('seconds', self.getSetting('snapshot_wait_time'))) date = self._time.now() size = int( random.uniform(float(self.getSetting('snapshot_min_size')), float(self.getSetting('snapshot_max_size')))) slug = self.generateId(8) name = input_json['name'] password = input_json.get('password', None) if seconds > 0: await asyncio.sleep(seconds) data = createSnapshotTar(slug, name, date, size, included_folders=input_json['folders'], included_addons=input_json['addons'], password=password) snapshot_info = parseSnapshotInfo(data) self.snapshots[slug] = snapshot_info self.snapshot_data[slug] = bytearray(data.getbuffer()) return self.formatDataResponse({"slug": slug}) finally: self.snapshot_in_progress = False self._snapshot_lock.release()