2016-06-30 18:18:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2024-04-28 21:28:40 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3.
|
2016-06-30 18:18:54 +00:00
|
|
|
#
|
|
|
|
# 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
|
2024-04-28 21:28:40 +00:00
|
|
|
# GNU General Public License for more details.
|
2016-06-30 18:18:54 +00:00
|
|
|
#
|
2024-04-28 21:28:40 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2016-06-30 18:18:54 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import argparse
|
2022-10-27 10:43:36 +00:00
|
|
|
import asyncio
|
2016-06-30 18:18:54 +00:00
|
|
|
import logging
|
2016-07-27 23:13:19 +00:00
|
|
|
import os
|
2016-06-30 18:18:54 +00:00
|
|
|
import sys
|
2023-07-25 21:26:25 +00:00
|
|
|
|
2020-03-24 10:51:43 +00:00
|
|
|
from console_conf.core import ConsoleConf, RecoveryChooser
|
2016-06-30 18:18:54 +00:00
|
|
|
from subiquitycore import __version__ as VERSION
|
2016-07-27 23:13:19 +00:00
|
|
|
from subiquitycore.log import setup_logger
|
2016-07-26 03:09:44 +00:00
|
|
|
|
2018-05-24 19:26:43 +00:00
|
|
|
|
2016-06-30 18:18:54 +00:00
|
|
|
def parse_options(argv):
|
|
|
|
parser = argparse.ArgumentParser(
|
2018-05-24 19:26:43 +00:00
|
|
|
description="console-conf - Pre-Ownership Configuration for Ubuntu Core",
|
2016-06-30 18:18:54 +00:00
|
|
|
prog="console-conf",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
dest="dry_run",
|
|
|
|
help="menu-only, do not call installer function",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--serial",
|
|
|
|
action="store_true",
|
|
|
|
dest="run_on_serial",
|
|
|
|
help="Run the installer over serial console.",
|
|
|
|
)
|
2019-12-14 03:00:17 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--ascii",
|
|
|
|
action="store_true",
|
|
|
|
dest="ascii",
|
|
|
|
help="Run the installer in ascii mode.",
|
|
|
|
)
|
2016-06-30 18:18:54 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--machine-config",
|
|
|
|
metavar="CONFIG",
|
|
|
|
dest="machine_config",
|
2021-07-14 22:40:22 +00:00
|
|
|
type=argparse.FileType(),
|
2016-06-30 18:18:54 +00:00
|
|
|
help="Don't Probe. Use probe data file",
|
|
|
|
)
|
2018-05-24 19:26:43 +00:00
|
|
|
parser.add_argument("--screens", action="append", dest="screens", default=[])
|
2018-03-08 03:34:25 +00:00
|
|
|
parser.add_argument("--answers")
|
2020-03-24 10:51:43 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--recovery-chooser-mode",
|
|
|
|
action="store_true",
|
|
|
|
dest="chooser_systems",
|
|
|
|
help=(
|
|
|
|
"Run as a recovery chooser interacting with the "
|
|
|
|
"calling process over stdin/stdout streams"
|
2023-07-25 21:26:25 +00:00
|
|
|
),
|
2020-03-24 10:51:43 +00:00
|
|
|
)
|
2022-01-25 21:10:40 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--output-base",
|
|
|
|
action="store",
|
|
|
|
dest="output_base",
|
|
|
|
default=".subiquity",
|
|
|
|
help="in dryrun, control basedir of files",
|
|
|
|
)
|
2016-06-30 18:18:54 +00:00
|
|
|
return parser.parse_args(argv)
|
|
|
|
|
|
|
|
|
2016-08-05 02:41:50 +00:00
|
|
|
LOGDIR = "/var/log/console-conf/"
|
2016-06-30 18:18:54 +00:00
|
|
|
|
2018-05-24 19:26:43 +00:00
|
|
|
|
2022-10-28 09:13:28 +00:00
|
|
|
def main():
|
2016-06-30 18:18:54 +00:00
|
|
|
opts = parse_options(sys.argv[1:])
|
2016-07-27 23:13:19 +00:00
|
|
|
global LOGDIR
|
|
|
|
if opts.dry_run:
|
2022-01-25 21:10:40 +00:00
|
|
|
LOGDIR = opts.output_base
|
2019-09-09 02:32:10 +00:00
|
|
|
setup_logger(dir=LOGDIR)
|
2016-06-30 18:51:39 +00:00
|
|
|
logger = logging.getLogger("console_conf")
|
2016-06-30 18:18:54 +00:00
|
|
|
logger.info("Starting console-conf v{}".format(VERSION))
|
|
|
|
logger.info("Arguments passed: {}".format(sys.argv))
|
|
|
|
|
2022-10-28 09:13:28 +00:00
|
|
|
async def run_with_loop():
|
|
|
|
if opts.chooser_systems:
|
|
|
|
# when running as a chooser, the stdin/stdout streams are set up by
|
|
|
|
# the process that runs us, attempt to restore the tty in/out by
|
|
|
|
# looking at stderr
|
|
|
|
chooser_input, chooser_output = restore_std_streams_from(sys.stderr)
|
|
|
|
interface = RecoveryChooser(opts, chooser_input, chooser_output)
|
|
|
|
else:
|
|
|
|
interface = ConsoleConf(opts)
|
|
|
|
await interface.run()
|
2020-03-24 10:51:43 +00:00
|
|
|
|
2022-10-28 09:13:28 +00:00
|
|
|
asyncio.run(run_with_loop())
|
2016-06-30 18:18:54 +00:00
|
|
|
|
2018-05-24 19:26:43 +00:00
|
|
|
|
2020-03-24 10:51:43 +00:00
|
|
|
def restore_std_streams_from(from_file):
|
|
|
|
"""
|
|
|
|
Attempt to restore the original sys.std{in,out} streams by inspecting the
|
|
|
|
tty that stderr is hooked up to. Returns the chooser input/output streams.
|
|
|
|
"""
|
|
|
|
tty = os.ttyname(from_file.fileno())
|
|
|
|
# we have tty now
|
|
|
|
chooser_input, chooser_output = sys.stdin, sys.stdout
|
|
|
|
sys.stdin = open(tty, "r")
|
|
|
|
sys.stdout = open(tty, "w")
|
|
|
|
return chooser_input, chooser_output
|
|
|
|
|
|
|
|
|
2016-06-30 18:18:54 +00:00
|
|
|
if __name__ == "__main__":
|
2022-10-28 09:13:28 +00:00
|
|
|
sys.exit(main())
|