← Back to team overview

livepatch-charmers team mailing list archive

[Merge] ~barryprice/canonical-livepatch-charm/+git/canonical-livepatch-charm:master into canonical-livepatch-charm:master

 

Barry Price has proposed merging ~barryprice/canonical-livepatch-charm/+git/canonical-livepatch-charm:master into canonical-livepatch-charm:master.

Requested reviews:
  Livepatch charm developers (livepatch-charmers)
Related bugs:
  Bug #1715035 in Canonical Livepatch Charm: ""effective kernel" status is misleading"
  https://bugs.launchpad.net/canonical-livepatch-charm/+bug/1715035

For more details, see:
https://code.launchpad.net/~barryprice/canonical-livepatch-charm/+git/canonical-livepatch-charm/+merge/331851
-- 
Your team Livepatch charm developers is requested to review the proposed merge of ~barryprice/canonical-livepatch-charm/+git/canonical-livepatch-charm:master into canonical-livepatch-charm:master.
diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
index bb9d547..fbdaf01 100644
--- a/reactive/canonical_livepatch.py
+++ b/reactive/canonical_livepatch.py
@@ -7,7 +7,6 @@ from subprocess import check_call, check_output, CalledProcessError
 from time import sleep
 from os import path, uname
 from yaml import load, dump
-from platform import release
 from distutils.version import LooseVersion
 
 
@@ -57,37 +56,41 @@ def unit_update(status=None, message=None):
     if status and message:
         hookenv.status_set(status, message)
     else:
-        hookenv.status_set('active', 'Effective kernel {}'.format(
-            get_equiv_kernel_version())
+        patch_details = get_patch_details()
+        hookenv.status_set(
+                'active',
+                'Running kernel {}, patchState: {}'.format(*patch_details)
         )
 
 
-def get_equiv_kernel_version():
-    # default to actual running kernel
-    version_string = release()
-    livepatch_status = ''
+def get_patch_details():
+    kernel = patch_state = 'unknown'
 
     cmd = ['/snap/bin/canonical-livepatch', 'status']
     try:
-        livepatch_status = check_output(cmd, universal_newlines=True)
+        livepatch_state = check_output(cmd, universal_newlines=True)
     except CalledProcessError as e:
         hookenv.log('Unable to get status: {}'.format(str(e)))
-        return version_string
+        return(kernel, patch_state)
 
     # status will usually pass YAML (but not always!)
     try:
-        status_yaml = load(livepatch_status)
+        status_yaml = load(livepatch_state)
     except Exception:
         hookenv.log('Unable to parse status yaml')
-        return version_string
+        return(kernel, patch_state)
 
     # even if we got YAML, be paranoid
     try:
-        version_string = status_yaml['kernel']
+        for k in status_yaml['status']:
+            if k['running'] is True:
+                kernel = k['kernel']
+                patch_state = k['livepatch']['patchState']
     except Exception:
-        hookenv.log('Unable to find kernel line in status yaml')
+        hookenv.log('Unable to find patching details in status yaml')
+        return(kernel, patch_state)
 
-    return version_string
+    return(kernel, patch_state)
 
 
 def get_yaml_if_exists(path_to_yaml):
@@ -214,10 +217,20 @@ def activate_livepatch():
 def livepatch_supported():
     arch = uname()[4]
     opts = layer.options('snap')
-    supported_archs = opts['canonical-livepatch'].pop('supported-architectures', None)
+    supported_archs = opts['canonical-livepatch'].pop(
+        'supported-architectures',
+        None
+    )
     if supported_archs and arch not in supported_archs:
-        hookenv.log('Livepatch does not currently support this architecture: {}'.format(arch))
-        unit_update('blocked', 'Architecture {} is not supported by livepatch'.format(arch))
+        hookenv.log(
+            'Livepatch does not currently support {} architecture'.format(
+                arch
+            )
+        )
+        unit_update(
+            'blocked',
+            'Architecture {} is not supported by livepatch'.format(arch)
+        )
     if is_container():
         hookenv.log('OS container detected, livepatch is not needed')
         unit_update('blocked', 'Livepatch is not needed in OS containers')

Follow ups