Skip to content

Commit

Permalink
Allow "Never Delete" setting on previously ignored backups
Browse files Browse the repository at this point in the history
  • Loading branch information
sabeechen committed Apr 1, 2024
1 parent 751a434 commit 87cbb3a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@
if (source.ignored) {
$(".ignore-warning", container).removeClass('default-hidden');
$(".dont-ignore-button", container).removeClass('default-hidden');
$(".never-delete", container).addClass('default-hidden');
} else {
$(".ignore-warning", container).addClass('default-hidden');
$(".dont-ignore-button", container).addClass('default-hidden');
Expand Down
4 changes: 3 additions & 1 deletion hassio-google-drive-backup/backup/ui/uiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ async def retain(self, request: Request):
data = await request.json()
slug = data['slug']

self._coord.getBackup(slug)
backup = self._coord.getBackup(slug)
if (backup.ignore()):
await self._coord.ignore(data['slug'], False)
await self._coord.retain(data['sources'], slug)
return web.json_response({'message': "Updated the backup's settings"})

Expand Down
21 changes: 21 additions & 0 deletions hassio-google-drive-backup/dev/debug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<script type="text/javascript">
// Bind an event handler for the create ignored button on document load, don't use jquery
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('create-ignored').addEventListener('click', function() {
// Create a new ignored backup
fetch('/debug/create_ignored', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
});
});
});
</script>
</head>
<body>
<button id="create-ignored">Create Ignored Backup</button>
</body>
</html>
11 changes: 8 additions & 3 deletions hassio-google-drive-backup/dev/simulated_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def routes(self):
get('/snapshots/new/full', self._newbackup),
get('/snapshots/{slug}/download', self._backupDownload),
get('/snapshots/{slug}/info', self._backupDetail),
post('/debug/create_ignored', self._create_ignored),
]

def getEvents(self):
Expand Down Expand Up @@ -170,7 +171,7 @@ async def toggleBlockBackup(self):
else:
await self._backup_lock.acquire()

async def _verifyHeader(self, request) -> bool:
async def _verifyHeader(self, request):
if request.headers.get("Authorization", None) == "Bearer " + self._auth_token:
return
if request.headers.get("X-Supervisor-Token", None) == self._auth_token:
Expand All @@ -188,7 +189,7 @@ async def _getBackups(self, request: Request):
async def _getMounts(self, request: Request):
await self._verifyHeader(request)
return self._formatDataResponse(self._mounts)

async def _setMounts(self, request: Request):
self._mounts = await request.json()
return self._formatDataResponse({})
Expand Down Expand Up @@ -314,7 +315,11 @@ async def _newbackup(self, request: Request):
input_json = await request.json()
task = asyncio.shield(asyncio.create_task(self._internalNewBackup(request, input_json)))
return self._formatDataResponse({"slug": await task})


async def _create_ignored(self, request: Request):
await self._internalNewBackup(request, {'name': 'Upgrade Backup', 'addons': [all_addons[0]['slug']]}, verify_header=False)
return self._formatDataResponse({"slug": "ignored"})

async def _lock_backups(self, request: Request):
await self._backup_lock.acquire()
return self._formatDataResponse({"message": "locked"})
Expand Down
10 changes: 8 additions & 2 deletions hassio-google-drive-backup/dev/simulationserver.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from pathlib import Path
import re
from typing import Dict
from yarl import URL
import aiohttp
from aiohttp.web import (Application,
HTTPException,
Request, Response, get,
json_response, middleware, post, HTTPSeeOther)
json_response, middleware, post, HTTPSeeOther, FileResponse)
from aiohttp.client import ClientSession
from injector import inject, singleton, Injector, provider

Expand Down Expand Up @@ -115,12 +116,17 @@ def routes(self):
get('/readfile', self.readFile),
post('/uploadfile', self.uploadfile),
get('/ingress/self_slug', self.slugRedirect),
get('/debug/config', self.debug_config)
get('/debug/config', self.debug_config),
get('/debug', self.debug_menu)
] + self.google.routes() + self.supervisor.routes() + self._api_ingress.routes()

async def debug_config(self, request: Request):
return json_response(self.supervisor._options)

async def debug_menu(self, request: Request):
# Render the debug page from debug.html
# first determine the path to the debug.html file, which is a sibling to this file
return FileResponse(Path(__file__).parent/"debug.html")

class SimServerModule(BaseModule):
def __init__(self, base_url: URL):
Expand Down
17 changes: 17 additions & 0 deletions hassio-google-drive-backup/tests/test_uiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,23 @@ async def test_retain(reader: ReaderHelper, config: Config, backup: Backup, coor
assert status['sources'][SOURCE_GOOGLE_DRIVE]['retained'] == 0
assert status['sources'][SOURCE_GOOGLE_DRIVE]['backups'] == 1

@pytest.mark.asyncio
async def test_retain_also_sets_ignore(reader: ReaderHelper, time: FakeTime, coord: Coordinator, config: Config, supervisor: SimulatedSupervisor, ha: HaSource, drive: DriveSource):
"""Verifies that when retaining an ignored backup, it also sets the ignore flag to False."""
config.override(Setting.IGNORE_UPGRADE_BACKUPS, True)
config.override(Setting.DAYS_BETWEEN_BACKUPS, 0)

# make an ignored_backup
slug = await supervisor.createBackup({'name': "Ignore_me", 'folders': ['homeassistant'], 'addons': []}, date=time.now())
await coord.sync()
assert (await ha.get())[slug].ignore() == True

# retain the ignored backups
await reader.getjson("retain", json={'slug': slug, 'sources': {"HomeAssistant": True}})

# verify it is no longer ignored
assert (await ha.get())[slug].ignore() == False


@pytest.mark.asyncio
async def test_note(reader: ReaderHelper, config: Config, backup: Backup, coord: Coordinator, time: FakeTime):
Expand Down

0 comments on commit 87cbb3a

Please sign in to comment.