Return snapshot slug for snapshot/import (#372)

* Update __init__.py

* Update snapshots.py

* Update API.md

* Update __init__.py

* Update __init__.py
This commit is contained in:
Pascal Vizeli 2018-02-23 10:52:35 +01:00 committed by GitHub
parent 6bc9792248
commit 3b974920d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 16 deletions

21
API.md
View File

@ -124,6 +124,13 @@ Output is the raw docker log.
- POST `/snapshots/new/upload` - POST `/snapshots/new/upload`
return:
```json
{
"slug": ""
}
```
- POST `/snapshots/new/full` - POST `/snapshots/new/full`
```json ```json
@ -133,6 +140,13 @@ Output is the raw docker log.
} }
``` ```
return:
```json
{
"slug": ""
}
```
- POST `/snapshots/new/partial` - POST `/snapshots/new/partial`
```json ```json
@ -144,6 +158,13 @@ Output is the raw docker log.
} }
``` ```
return:
```json
{
"slug": ""
}
```
- POST `/snapshots/reload` - POST `/snapshots/reload`
- GET `/snapshots/{slug}/info` - GET `/snapshots/{slug}/info`

View File

@ -109,16 +109,24 @@ class APISnapshots(CoreSysAttributes):
async def snapshot_full(self, request): async def snapshot_full(self, request):
"""Full-Snapshot a snapshot.""" """Full-Snapshot a snapshot."""
body = await api_validate(SCHEMA_SNAPSHOT_FULL, request) body = await api_validate(SCHEMA_SNAPSHOT_FULL, request)
return await asyncio.shield( snapshot = await asyncio.shield(
self._snapshots.do_snapshot_full(**body), loop=self._loop) self._snapshots.do_snapshot_full(**body), loop=self._loop)
if snapshot:
return {ATTR_SLUG: snapshot.slug}
return False
@api_process @api_process
async def snapshot_partial(self, request): async def snapshot_partial(self, request):
"""Partial-Snapshot a snapshot.""" """Partial-Snapshot a snapshot."""
body = await api_validate(SCHEMA_SNAPSHOT_PARTIAL, request) body = await api_validate(SCHEMA_SNAPSHOT_PARTIAL, request)
return await asyncio.shield( snapshot = await asyncio.shield(
self._snapshots.do_snapshot_partial(**body), loop=self._loop) self._snapshots.do_snapshot_partial(**body), loop=self._loop)
if snapshot:
return {ATTR_SLUG: snapshot.slug}
return False
@api_process @api_process
async def restore_full(self, request): async def restore_full(self, request):
"""Full-Restore a snapshot.""" """Full-Restore a snapshot."""
@ -174,5 +182,9 @@ class APISnapshots(CoreSysAttributes):
except asyncio.CancelledError: except asyncio.CancelledError:
return False return False
return await asyncio.shield( snapshot = await asyncio.shield(
self._snapshots.import_snapshot(tar_file), loop=self._loop) self._snapshots.import_snapshot(tar_file), loop=self._loop)
if snapshot:
return {ATTR_SLUG: snapshot.slug}
return False

View File

@ -90,30 +90,36 @@ class SnapshotManager(CoreSysAttributes):
# Read meta data # Read meta data
if not await snapshot.load(): if not await snapshot.load():
return False return None
# Allready exists? # Allready exists?
if snapshot.slug in self.snapshots_obj: if snapshot.slug in self.snapshots_obj:
_LOGGER.error("Snapshot %s allready exists!", snapshot.slug) _LOGGER.error("Snapshot %s allready exists!", snapshot.slug)
return False return None
# Move snapshot to backup # Move snapshot to backup
tar_origin = Path(self._config.path_backup, f"{snapshot.slug}.tar")
try: try:
snapshot.tarfile.rename( snapshot.tarfile.rename(tar_origin)
Path(self._config.path_backup, f"{snapshot.slug}.tar"))
except OSError as err: except OSError as err:
_LOGGER.error("Can't move snapshot file to storage: %s", err) _LOGGER.error("Can't move snapshot file to storage: %s", err)
return False return None
await self.reload() # Load new snapshot
return True snapshot = Snapshot(self.coresys, tar_origin)
if not await snapshot.load():
return None
_LOGGER.info("Success import %s", snapshot.slug)
self.snapshots_obj[snapshot.slug] = snapshot
return snapshot
async def do_snapshot_full(self, name="", password=None): async def do_snapshot_full(self, name="", password=None):
"""Create a full snapshot.""" """Create a full snapshot."""
if self.lock.locked(): if self.lock.locked():
_LOGGER.error("It is already a snapshot/restore process running") _LOGGER.error("It is already a snapshot/restore process running")
return False return None
snapshot = self._create_snapshot(name, SNAPSHOT_FULL, password) snapshot = self._create_snapshot(name, SNAPSHOT_FULL, password)
_LOGGER.info("Full-Snapshot %s start", snapshot.slug) _LOGGER.info("Full-Snapshot %s start", snapshot.slug)
@ -132,12 +138,12 @@ class SnapshotManager(CoreSysAttributes):
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
_LOGGER.exception("Snapshot %s error", snapshot.slug) _LOGGER.exception("Snapshot %s error", snapshot.slug)
return False return None
else: else:
_LOGGER.info("Full-Snapshot %s done", snapshot.slug) _LOGGER.info("Full-Snapshot %s done", snapshot.slug)
self.snapshots_obj[snapshot.slug] = snapshot self.snapshots_obj[snapshot.slug] = snapshot
return True return snapshot
finally: finally:
self._scheduler.suspend = False self._scheduler.suspend = False
@ -148,7 +154,7 @@ class SnapshotManager(CoreSysAttributes):
"""Create a partial snapshot.""" """Create a partial snapshot."""
if self.lock.locked(): if self.lock.locked():
_LOGGER.error("It is already a snapshot/restore process running") _LOGGER.error("It is already a snapshot/restore process running")
return False return None
addons = addons or [] addons = addons or []
folders = folders or [] folders = folders or []
@ -178,12 +184,12 @@ class SnapshotManager(CoreSysAttributes):
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
_LOGGER.exception("Snapshot %s error", snapshot.slug) _LOGGER.exception("Snapshot %s error", snapshot.slug)
return False return None
else: else:
_LOGGER.info("Partial-Snapshot %s done", snapshot.slug) _LOGGER.info("Partial-Snapshot %s done", snapshot.slug)
self.snapshots_obj[snapshot.slug] = snapshot self.snapshots_obj[snapshot.slug] = snapshot
return True return snapshot
finally: finally:
self._scheduler.suspend = False self._scheduler.suspend = False