2021-07-02 08:26:21 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2015-2021 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import argparse
|
2022-10-27 10:43:36 +00:00
|
|
|
import asyncio
|
2021-07-02 08:26:21 +00:00
|
|
|
import fcntl
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2021-07-13 12:39:44 +00:00
|
|
|
from subiquity.cmd.common import LOGDIR, setup_environment
|
2021-07-02 08:26:21 +00:00
|
|
|
from subiquitycore.log import setup_logger
|
2021-07-13 12:39:44 +00:00
|
|
|
from system_setup.cmd.server import make_server_args_parser
|
2021-07-02 08:26:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClickAction(argparse.Action):
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
namespace.scripts.append("c(" + repr(values) + ")")
|
|
|
|
|
|
|
|
|
|
|
|
def make_client_args_parser():
|
2021-08-31 10:11:32 +00:00
|
|
|
# TODO WSL: update this. We have already done it on the past…
|
2021-07-02 08:26:21 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="SUbiquity - Ubiquity for Servers", prog="subiquity"
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
ascii_default = os.ttyname(0) == "/dev/ttysclp0"
|
|
|
|
except OSError:
|
|
|
|
ascii_default = False
|
|
|
|
parser.set_defaults(ascii=ascii_default)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
dest="dry_run",
|
|
|
|
help="menu-only, do not call installer function",
|
|
|
|
)
|
|
|
|
parser.add_argument("--socket")
|
|
|
|
parser.add_argument("--answers")
|
|
|
|
parser.add_argument("--server-pid")
|
2022-04-05 21:12:47 +00:00
|
|
|
parser.add_argument("--autoinstall", action="store", dest="autoinstall")
|
2021-11-04 15:01:00 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--prefill",
|
|
|
|
dest="prefill",
|
|
|
|
help=(
|
|
|
|
"Prefills UI models with data provided in"
|
|
|
|
" a prefill.yaml file yet allowing overrides."
|
|
|
|
),
|
|
|
|
)
|
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",
|
|
|
|
)
|
2021-07-02 08:26:21 +00:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
AUTO_ANSWERS_FILE = "/subiquity_config/answers.yaml"
|
|
|
|
|
|
|
|
|
2022-10-28 09:13:28 +00:00
|
|
|
def main():
|
2021-07-02 08:26:21 +00:00
|
|
|
setup_environment()
|
|
|
|
# setup_environment sets $APPORT_DATA_DIR which must be set before
|
|
|
|
# apport is imported, which is done by this import:
|
2021-07-13 12:39:44 +00:00
|
|
|
from system_setup.client.client import SystemSetupClient
|
2023-07-25 21:26:25 +00:00
|
|
|
|
2021-07-02 08:26:21 +00:00
|
|
|
parser = make_client_args_parser()
|
|
|
|
args = sys.argv[1:]
|
2021-09-20 17:29:10 +00:00
|
|
|
|
|
|
|
need_start_server = False
|
|
|
|
server_args = []
|
|
|
|
server_output_dir = "/var/log/installer"
|
2021-11-03 11:41:54 +00:00
|
|
|
server_state_file = "/run/subiquity/server-state"
|
2021-11-04 15:01:00 +00:00
|
|
|
opts, unknown = parser.parse_known_args(args)
|
2021-07-02 08:26:21 +00:00
|
|
|
if "--dry-run" in args:
|
2022-01-25 21:10:40 +00:00
|
|
|
server_state_file = opts.output_base + "/run/subiquity/server-state"
|
2021-07-02 08:26:21 +00:00
|
|
|
if opts.socket is None:
|
2021-09-20 17:29:10 +00:00
|
|
|
need_start_server = True
|
2022-01-25 21:10:40 +00:00
|
|
|
server_output_dir = opts.output_base
|
2021-09-20 17:29:10 +00:00
|
|
|
sock_path = os.path.join(server_output_dir, "socket")
|
2021-07-02 08:26:21 +00:00
|
|
|
opts.socket = sock_path
|
|
|
|
server_args = ["--dry-run", "--socket=" + sock_path] + unknown
|
2022-03-18 18:01:33 +00:00
|
|
|
server_args += ["--output-base", opts.output_base]
|
2021-11-04 15:01:00 +00:00
|
|
|
|
|
|
|
elif opts.socket is None:
|
|
|
|
need_start_server = True
|
|
|
|
opts.socket = "/run/subiquity/socket"
|
|
|
|
|
|
|
|
if opts.prefill:
|
|
|
|
server_args += ["--prefill=" + opts.prefill]
|
2021-09-20 17:29:10 +00:00
|
|
|
|
2022-04-05 21:12:47 +00:00
|
|
|
if opts.autoinstall:
|
|
|
|
server_args += ["--autoinstall=" + opts.autoinstall]
|
|
|
|
|
2021-09-20 17:29:10 +00:00
|
|
|
os.makedirs(server_output_dir, exist_ok=True)
|
2022-03-08 10:16:31 +00:00
|
|
|
server_stdout = open(os.path.join(server_output_dir, "server-stdout"), "w")
|
|
|
|
server_stderr = open(os.path.join(server_output_dir, "server-stderr"), "w")
|
2021-09-20 17:29:10 +00:00
|
|
|
|
|
|
|
if need_start_server:
|
2021-11-03 11:41:54 +00:00
|
|
|
if os.path.exists(server_state_file):
|
|
|
|
os.unlink(server_state_file)
|
2021-09-20 17:29:10 +00:00
|
|
|
server_parser = make_server_args_parser()
|
|
|
|
server_parser.parse_args(server_args) # just to check
|
|
|
|
server_cmd = [sys.executable, "-m", "system_setup.cmd.server"] + server_args
|
|
|
|
server_proc = subprocess.Popen(
|
2022-03-08 10:16:31 +00:00
|
|
|
server_cmd, stdout=server_stdout, stderr=server_stderr
|
|
|
|
)
|
2021-09-20 17:29:10 +00:00
|
|
|
opts.server_pid = str(server_proc.pid)
|
2021-10-28 18:54:07 +00:00
|
|
|
print("running server pid {} with args: {}".format(server_proc.pid, server_cmd))
|
2021-09-20 17:29:10 +00:00
|
|
|
elif opts.server_pid is not None:
|
|
|
|
print("reconnecting to server pid {}".format(opts.server_pid))
|
|
|
|
|
2021-07-02 08:26:21 +00:00
|
|
|
os.makedirs(os.path.basename(opts.socket), exist_ok=True)
|
|
|
|
logdir = LOGDIR
|
|
|
|
if opts.dry_run:
|
2022-01-25 21:10:40 +00:00
|
|
|
logdir = opts.output_base
|
2021-07-21 09:07:40 +00:00
|
|
|
logfiles = setup_logger(dir=logdir, base="systemsetup-client")
|
2021-07-02 08:26:21 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger("subiquity")
|
2021-07-13 12:39:44 +00:00
|
|
|
version = "unknown"
|
|
|
|
logger.info("Starting System Setup revision {}".format(version))
|
2021-07-02 08:26:21 +00:00
|
|
|
logger.info("Arguments passed: {}".format(sys.argv))
|
|
|
|
|
2021-10-04 02:57:56 +00:00
|
|
|
opts.ssh = False
|
|
|
|
opts.screens = []
|
|
|
|
opts.scripts = []
|
|
|
|
opts.run_on_serial = False
|
2021-07-02 08:26:21 +00:00
|
|
|
if opts.answers is None and os.path.exists(AUTO_ANSWERS_FILE):
|
|
|
|
logger.debug("Autoloading answers from %s", AUTO_ANSWERS_FILE)
|
|
|
|
opts.answers = AUTO_ANSWERS_FILE
|
|
|
|
|
|
|
|
if opts.answers:
|
|
|
|
opts.answers = open(opts.answers)
|
|
|
|
try:
|
|
|
|
fcntl.flock(opts.answers, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
|
|
except OSError:
|
|
|
|
logger.exception("Failed to lock auto answers file, proceding without it.")
|
|
|
|
opts.answers.close()
|
|
|
|
opts.answers = None
|
|
|
|
|
2022-10-28 09:13:28 +00:00
|
|
|
async def run_with_loop():
|
|
|
|
subiquity_interface = SystemSetupClient(opts)
|
2023-10-20 15:47:15 +00:00
|
|
|
subiquity_interface.note_file_for_apport(
|
|
|
|
"InstallerClientLog", logfiles["debug"]
|
|
|
|
)
|
|
|
|
subiquity_interface.note_file_for_apport(
|
|
|
|
"InstallerClientLogInfo", logfiles["info"]
|
|
|
|
)
|
2022-10-28 09:13:28 +00:00
|
|
|
await subiquity_interface.run()
|
2021-07-02 08:26:21 +00:00
|
|
|
|
2022-10-28 09:13:28 +00:00
|
|
|
asyncio.run(run_with_loop())
|
2021-07-02 08:26:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-10-28 09:13:28 +00:00
|
|
|
sys.exit(main())
|