~happyaron/ubiquity/lp-1465530

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8; Mode: Python; indent-tabs-mode: nil; tab-width: 4 -*-

# Copyright (C) 2008, 2009 Canonical Ltd.
# Written by Colin Watson <cjwatson@ubuntu.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# This is a simple frontend that does not perform any filtering; instead, it
# just runs everything using the usual debconf frontend. This is suitable
# for use on a server.
#
# Note that this frontend relies on being run under the control of a debconf
# frontend; the main ubiquity program takes care of this.

from __future__ import print_function

import os
import signal
import sys
import textwrap

import debconf

from ubiquity import i18n
from ubiquity.components import install, plugininstall
from ubiquity.frontend.base import BaseFrontend, Controller
from ubiquity.plugin import Plugin


class PersistentDebconfCommunicator(debconf.Debconf):
    def shutdown(self):
        pass


class Wizard(BaseFrontend):
    def __init__(self, distro):
        BaseFrontend.__init__(self, distro)

        if self.oem_user_config:
            self.db.info('ubiquity/text/oem_user_config_title')
        else:
            self.db.info('ubiquity/text/live_installer')

        self.previous_excepthook = sys.excepthook
        sys.excepthook = self.excepthook

        # Set default language.
        i18n.reset_locale(self)

        self.stop_debconf()

    def excepthook(self, exctype, excvalue, exctb):
        """Crash handler."""

        if (issubclass(exctype, KeyboardInterrupt) or
                issubclass(exctype, SystemExit)):
            return

        self.post_mortem(exctype, excvalue, exctb)

        self.previous_excepthook(exctype, excvalue, exctb)

    def debconf_communicator(self):
        if 'DEBIAN_HAS_FRONTEND' in os.environ:
            # We may only instantiate Debconf once, as it fiddles with
            # sys.stdout. See LP #24727.
            if self.db is None:
                self.db = PersistentDebconfCommunicator()
            return self.db
        else:
            # This needs to be instantiated afresh each time, as normal.
            return BaseFrontend.debconf_communicator(self)

    def stop_debconf(self):
        if 'DEBIAN_HAS_FRONTEND' not in os.environ:
            BaseFrontend.stop_debconf(self)

    def run(self):
        if os.getuid() != 0:
            print(textwrap.fill(
                'This program must be run with administrative privileges, and '
                'cannot continue without them.'), file=sys.stderr)
            sys.exit(1)

        self.pagesindex = 0
        self.pageslen = 0
        self.pages = []
        for mod in self.modules:
            if hasattr(mod.module, 'PageDebconf'):
                mod.ui_class = mod.module.PageDebconf
                mod.controller = Controller(self)
                mod.ui = mod.ui_class(mod.controller)
                title = mod.ui.get('plugin_title')
                if title:
                    mod.title = title
                    self.pageslen += 1
                    self.pages.append(mod)

        while(self.pagesindex >= 0 and self.pagesindex < self.pageslen):
            step = self.pages[self.pagesindex]

            self.db.settitle(step.title)

            if issubclass(self.pages[self.pagesindex].filter_class, Plugin):
                ui = self.pages[self.pagesindex].ui
            else:
                ui = None
            dbfilter = self.pages[self.pagesindex].filter_class(self,
                                                                db=self.db,
                                                                ui=ui)
            ret = dbfilter.run_unfiltered()

            if ret == 10:
                self.pagesindex -= 1
            else:
                self.pagesindex += 1

        # TODO: handle errors
        if self.pagesindex == self.pageslen:
            for install_component in plugininstall, install:
                dbfilter = install_component.Install(self, db=self.db)
                ret = dbfilter.run_unfiltered()
                if ret != 0:
                    self.installing = False
                    if ret == 3:
                        # error already handled by Install
                        sys.exit(ret)
                    elif (os.WIFSIGNALED(ret) and
                          os.WTERMSIG(ret) in (signal.SIGINT, signal.SIGKILL,
                                               signal.SIGTERM)):
                        sys.exit(ret)
                    elif os.path.exists('/var/lib/ubiquity/install.trace'):
                        with open('/var/lib/ubiquity/install.trace') as tbfile:
                            realtb = tbfile.read()
                        raise RuntimeError(
                            "Install failed with exit code %s\n%s" %
                            (ret, realtb))
                    else:
                        raise RuntimeError(
                            "Install failed with exit code %s; see "
                            "/var/log/syslog" % ret)

            return 0
        else:
            return 10