2021-08-31 10:07:20 +00:00
|
|
|
# Copyright 2020-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/>.
|
|
|
|
|
2021-10-15 08:29:35 +00:00
|
|
|
import enum
|
2021-10-07 08:53:43 +00:00
|
|
|
import os
|
2021-08-31 10:07:20 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from subiquitycore.context import with_context
|
|
|
|
from subiquity.common.types import ShutdownMode
|
|
|
|
from subiquity.server.controllers import ShutdownController
|
|
|
|
|
2021-09-27 12:36:25 +00:00
|
|
|
log = logging.getLogger("system_setup.server.controllers.restart")
|
2021-08-31 10:07:20 +00:00
|
|
|
|
|
|
|
|
2021-10-15 08:29:35 +00:00
|
|
|
class WSLShutdownMode(enum.Enum):
|
|
|
|
COMPLETE = -1
|
|
|
|
|
|
|
|
|
2021-08-31 10:07:20 +00:00
|
|
|
class SetupShutdownController(ShutdownController):
|
|
|
|
|
|
|
|
def __init__(self, app):
|
2021-08-31 11:26:49 +00:00
|
|
|
# This isn't the most beautiful way, but the shutdown controller
|
|
|
|
# depends on Install, override with our configure one.
|
2021-08-31 10:07:20 +00:00
|
|
|
super().__init__(app)
|
2021-10-07 08:53:43 +00:00
|
|
|
self.root_dir = app.base_model.root
|
2021-08-31 10:07:20 +00:00
|
|
|
self.app.controllers.Install = self.app.controllers.Configure
|
2021-10-15 08:29:35 +00:00
|
|
|
self.mode = WSLShutdownMode.COMPLETE # allow the complete mode
|
2021-08-31 10:07:20 +00:00
|
|
|
|
|
|
|
def start(self):
|
2021-09-09 13:01:47 +00:00
|
|
|
self.app.aio_loop.create_task(self._wait_install())
|
2021-08-31 10:07:20 +00:00
|
|
|
self.app.aio_loop.create_task(self._run())
|
|
|
|
|
2021-09-09 13:01:47 +00:00
|
|
|
async def _wait_install(self):
|
|
|
|
await self.app.controllers.Install.install_task
|
|
|
|
await self.app.controllers.Late.run_event.wait()
|
|
|
|
self.server_reboot_event.set()
|
|
|
|
|
2021-08-31 10:07:20 +00:00
|
|
|
@with_context(description='mode={self.mode.name}')
|
|
|
|
def shutdown(self, context):
|
|
|
|
self.shuttingdown_event.set()
|
2021-12-17 13:35:38 +00:00
|
|
|
comments = ["# This file was auto generated by system-setup.",
|
2021-12-14 15:07:40 +00:00
|
|
|
"# Don't edit it. It will be overwritten at next run."]
|
|
|
|
launcher_status = []
|
2021-10-07 08:53:43 +00:00
|
|
|
|
|
|
|
if self.mode == ShutdownMode.REBOOT:
|
2021-12-14 15:07:40 +00:00
|
|
|
log.debug("Setting launcher for reboot")
|
|
|
|
launcher_status += ["action=reboot"]
|
2021-10-07 08:53:43 +00:00
|
|
|
elif self.mode == ShutdownMode.POWEROFF:
|
2021-12-14 15:07:40 +00:00
|
|
|
log.debug("Setting launcher for shut down")
|
|
|
|
launcher_status += ["action=shutdown"]
|
|
|
|
|
|
|
|
default_uid = self.app.controllers.Install.get_default_uid()
|
|
|
|
if default_uid is not None and default_uid != 0:
|
|
|
|
launcher_status += ["defaultUid={}".format(default_uid)]
|
|
|
|
|
|
|
|
if len(launcher_status) > 0:
|
|
|
|
status_file = os.path.join(self.root_dir, "run/launcher-command")
|
|
|
|
with open(status_file, "w+") as f:
|
|
|
|
f.write("\n".join(comments + launcher_status))
|
2021-10-07 08:53:43 +00:00
|
|
|
|
2021-09-01 15:42:19 +00:00
|
|
|
self.app.exit()
|