From ae1bc2c031e07f1c1effa9c5d3fb9a585755a60e Mon Sep 17 00:00:00 2001
From: Marc Bonnici <marc.bonnici@arm.com>
Date: Tue, 8 Dec 2020 12:19:18 +0000
Subject: [PATCH] fw/config: Add additional `run_completed` reboot policy

Add an additional `run_completed` reboot policy for when a run
has finished.
This complements the `initial` reboot policy and aims to leave
the device in a fresh state after WA has finished executing.
---
 wa/framework/configuration/core.py | 10 +++++++++-
 wa/framework/execution.py          |  3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/wa/framework/configuration/core.py b/wa/framework/configuration/core.py
index 071fe3f4..e0823565 100644
--- a/wa/framework/configuration/core.py
+++ b/wa/framework/configuration/core.py
@@ -58,10 +58,11 @@ class RebootPolicy(object):
               executing the first workload spec.
     :each_spec: The device will be rebooted before running a new workload spec.
     :each_iteration: The device will be rebooted before each new iteration.
+    :run_completion: The device will be rebooted after the run has been completed.
 
     """
 
-    valid_policies = ['never', 'as_needed', 'initial', 'each_spec', 'each_job']
+    valid_policies = ['never', 'as_needed', 'initial', 'each_spec', 'each_job', 'run_completion']
 
     @staticmethod
     def from_pod(pod):
@@ -92,6 +93,10 @@ class RebootPolicy(object):
     def reboot_on_each_spec(self):
         return self.policy == 'each_spec'
 
+    @property
+    def reboot_on_run_completion(self):
+        return self.policy == 'run_completion'
+
     def __str__(self):
         return self.policy
 
@@ -664,6 +669,9 @@ class RunConfiguration(Configuration):
 
                 .. note:: this acts the same as each_job when execution order
                           is set to by_iteration
+
+            ''"run_completion"''
+                 The device will be reboot after the run has been completed.
             '''),
         ConfigurationPoint(
             'device',
diff --git a/wa/framework/execution.py b/wa/framework/execution.py
index dc700b94..6daf2c64 100644
--- a/wa/framework/execution.py
+++ b/wa/framework/execution.py
@@ -537,6 +537,9 @@ class Runner(object):
             self.pm.process_run_output(self.context)
             self.pm.export_run_output(self.context)
         self.pm.finalize(self.context)
+        if self.context.reboot_policy.reboot_on_run_completion:
+            self.logger.info('Rebooting target on run completion.')
+            self.context.tm.reboot(self.context)
         signal.disconnect(self._error_signalled_callback, signal.ERROR_LOGGED)
         signal.disconnect(self._warning_signalled_callback, signal.WARNING_LOGGED)