use context stuff for the refresh snapd requests

This commit is contained in:
Michael Hudson-Doyle 2019-12-20 00:25:43 +13:00
parent eb3851bc16
commit dacd77f2be
1 changed files with 49 additions and 41 deletions

View File

@ -72,30 +72,32 @@ class RefreshController(BaseController):
return task.result() return task.result()
async def configure_snapd(self): async def configure_snapd(self):
log.debug("configure_snapd") with self.context.child("configure_snapd") as context:
try: with context.child("get_details") as subcontext:
r = await self.app.snapd.get( try:
'v2/snaps/{snap_name}'.format(snap_name=self.snap_name)) r = await self.app.snapd.get(
except requests.exceptions.RequestException: 'v2/snaps/{snap_name}'.format(
log.exception("getting snap details") snap_name=self.snap_name))
return except requests.exceptions.RequestException:
self.current_snap_version = r['result']['version'] log.exception("getting snap details")
for k in 'channel', 'revision', 'version': return
self.app.note_data_for_apport( self.current_snap_version = r['result']['version']
"Snap" + k.title(), r['result'][k]) for k in 'channel', 'revision', 'version':
log.debug( self.app.note_data_for_apport(
"current version of snap is: %r", "Snap" + k.title(), r['result'][k])
self.current_snap_version) subcontext.description = "current version of snap is: %r" % (
channel = self.get_refresh_channel() self.current_snap_version)
log.debug("switching %s to %s", self.snap_name, channel) channel = self.get_refresh_channel()
try: desc = "switching {} to {}".format(self.snap_name, channel)
await self.app.snapd.post_and_wait( with context.child("switching", desc) as subcontext:
'v2/snaps/{}'.format(self.snap_name), try:
{'action': 'switch', 'channel': channel}) await self.app.snapd.post_and_wait(
except requests.exceptions.RequestException: 'v2/snaps/{}'.format(self.snap_name),
log.exception("switching channels") {'action': 'switch', 'channel': channel})
return except requests.exceptions.RequestException:
log.debug("snap switching completed") log.exception("switching channels")
return
subcontext.description = "switched to " + channel
def get_refresh_channel(self): def get_refresh_channel(self):
"""Return the channel we should refresh subiquity to.""" """Return the channel we should refresh subiquity to."""
@ -134,28 +136,34 @@ class RefreshController(BaseController):
async def check_for_update(self): async def check_for_update(self):
await asyncio.shield(self.configure_task) await asyncio.shield(self.configure_task)
# If we restarted into this version, don't check for a new version. with self.context.child("check_for_update") as context:
if self.app.updated: if self.app.updated:
context.description = (
"not offered update when already updated")
return CheckState.UNAVAILABLE
result = await self.app.snapd.get('v2/find', select='refresh')
log.debug("check_for_update received %s", result)
for snap in result["result"]:
if snap["name"] == self.snap_name:
self.new_snap_version = snap["version"]
context.description = (
"new version of snap available: %r"
% self.new_snap_version)
return CheckState.AVAILABLE
else:
context.description = (
"no new version of snap available")
return CheckState.UNAVAILABLE return CheckState.UNAVAILABLE
result = await self.app.snapd.get('v2/find', select='refresh')
log.debug("check_for_update received %s", result)
for snap in result["result"]:
if snap["name"] == self.snap_name:
self.new_snap_version = snap["version"]
log.debug(
"new version of snap available: %r",
self.new_snap_version)
return CheckState.AVAILABLE
return CheckState.UNAVAILABLE
async def start_update(self): async def start_update(self):
update_marker = os.path.join(self.app.state_dir, 'updating') update_marker = os.path.join(self.app.state_dir, 'updating')
open(update_marker, 'w').close() open(update_marker, 'w').close()
change = await self.app.snapd.post( with self.context.child("starting_update") as context:
'v2/snaps/{}'.format(self.snap_name), change = await self.app.snapd.post(
{'action': 'refresh'}) 'v2/snaps/{}'.format(self.snap_name),
log.debug("refresh requested: %s", change) {'action': 'refresh'})
return change context.description = "change id: {}".format(change)
return change
async def get_progress(self, change): async def get_progress(self, change):
result = await self.app.snapd.get('v2/changes/{}'.format(change)) result = await self.app.snapd.get('v2/changes/{}'.format(change))