~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to tools/restart-demo/restart-demo.py

  • Committer: Sylvain Pineau
  • Date: 2014-07-29 16:05:54 UTC
  • mto: This revision was merged to the branch mainline in revision 3149.
  • Revision ID: sylvain.pineau@canonical.com-20140729160554-qev8887xbunn9tmi
checkbox-ng:launchers:checkbox-cli: The checkbox-cli launcher

Running the default whitelist (with the suite selection screen skipped)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
2
 
# This file is part of Checkbox.
3
 
#
4
 
# Copyright 2015 Canonical Ltd.
5
 
# Written by:
6
 
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
7
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
 
20
 
"""
21
 
Demonstration of the automatic application restart APIs.
22
 
 
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.
27
 
 
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.
33
 
"""
34
 
import argparse
35
 
import os
36
 
import sys
37
 
 
38
 
from guacamole import Command
39
 
 
40
 
from plainbox.impl.session.assistant import SA_RESTARTABLE
41
 
from plainbox.impl.session.assistant import SessionAssistant
42
 
 
43
 
 
44
 
class RestartDemo(Command):
45
 
 
46
 
    """
47
 
    Demonstration application for showcasing application restart support.
48
 
 
49
 
    @EPILOG@
50
 
 
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
53
 
    running again.
54
 
    """
55
 
 
56
 
    def register_arguments(self, parser):
57
 
        parser.add_argument(
58
 
            '--resume', dest='session_id', metavar="SESSION_ID",
59
 
            help=argparse.SUPPRESS)
60
 
 
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,)
66
 
        )
67
 
        try:
68
 
            sa.select_providers("plainbox-provider-restart-demo")
69
 
        except ValueError:
70
 
            raise SystemExit(
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.
78
 
            lambda session_id: [
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])
85
 
            ])
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)
91
 
                    break
92
 
            else:
93
 
                raise RuntimeError("Requested session is not resumable!")
94
 
        else:
95
 
            sa.start_new_session("Automatic application restart demo")
96
 
        sa.select_test_plan("2015.pl.zygoon.restart-demo::reboot-once")
97
 
        sa.bootstrap()
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")
106
 
 
107
 
 
108
 
if __name__ == '__main__':
109
 
    RestartDemo().main()