2
# This file is part of Checkbox.
4
# Copyright 2015 Canonical Ltd.
6
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
8
# Checkbox is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License version 3,
10
# as published by the Free Software Foundation.
12
# Checkbox is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
21
Demonstration of the automatic application restart APIs.
23
This file implements a tiny application based on the plainbox SessionAssistant
24
APIs that supports unattended application restarts. The application runs a
25
simple reboot test. The test reboots the system. Upon completing the reboot,
26
the testing application is re-started and resumed automatically.
28
All that is required to add similar feature to other application is the use of
29
the SessionAssistant.configure_application_restart() and optionally
30
SessionAssistant.use_alternate_resume_strategy() which allows for complete
31
control over the process. Here the second method is not used, defaulting to
32
environment-based auto-detection.
38
from guacamole import Command
40
from plainbox.impl.session.assistant import SA_RESTARTABLE
41
from plainbox.impl.session.assistant import SessionAssistant
44
class RestartDemo(Command):
47
Demonstration application for showcasing application restart support.
51
NOTE: This application will restart the system. If everything else works
52
correctly the application will be re-started after the operating system is
56
def register_arguments(self, parser):
58
'--resume', dest='session_id', metavar="SESSION_ID",
59
help=argparse.SUPPRESS)
61
def invoked(self, ctx):
62
sa = SessionAssistant(
63
"restart-demo", "0.1",
64
# Indicate that we want to use the restart API
65
"0.99", (SA_RESTARTABLE,)
68
sa.select_providers("plainbox-provider-restart-demo")
71
"Please 'develop' the restart-demo provider to use this demo")
72
sa.configure_application_restart(
73
# XXX: This assumes mk-venv and has needless stuff in a realistic
74
# application. If your application has a simple executable to call
75
# just pass that executable name below and don't copy the
76
# PROVIDERPATH trick or the absolute path to executable trick. They
77
# are only needed by this hacky example.
79
'sh', '-c', ' '.join([
80
'PROVIDERPATH={}'.format(os.getenv("PROVIDERPATH")),
81
os.path.expandvars("$VIRTUAL_ENV/bin/python3"),
82
os.path.normpath(os.path.expandvars(
83
"$VIRTUAL_ENV/../tools/restart-demo/restart-demo.py")),
84
"--resume", session_id])
86
# Resume the session if we're asked to do this
87
if ctx.args.session_id:
88
for (session_id, metadata) in sa.get_resumable_sessions():
89
if session_id == ctx.args.session_id:
90
sa.resume_session(session_id)
93
raise RuntimeError("Requested session is not resumable!")
95
sa.start_new_session("Automatic application restart demo")
96
sa.select_test_plan("2015.pl.zygoon.restart-demo::reboot-once")
98
for job_id in sa.get_dynamic_todo_list():
99
print("Running job: ", job_id)
100
result_builder = sa.run_job(job_id, "silent", False)
101
sa.use_job_result(job_id, result_builder.get_result())
102
print("All tests finished, results are printed below")
103
sa.export_to_stream("text", [], sys.stdout.buffer)
104
sa.finalize_session()
105
input("Press enter to continue")
108
if __name__ == '__main__':