Skip to content

Commit

Permalink
Catch installation errors and display them.
Browse files Browse the repository at this point in the history
Fixes #193
  • Loading branch information
almet committed Oct 3, 2024
1 parent a001b54 commit ee9b8b7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
64 changes: 44 additions & 20 deletions dangerzone/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
from PySide6.QtWidgets import QTextEdit
except ImportError:
from PySide2 import QtCore, QtGui, QtSvg, QtWidgets
from PySide2.QtWidgets import QAction, QTextEdit
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QAction, QTextEdit

from .. import errors
from ..document import SAFE_EXTENSION, Document
from ..isolation_provider.container import Container, NoContainerTechException
from ..isolation_provider.dummy import Dummy
from ..isolation_provider.qubes import Qubes, is_qubes_native_conversion
from ..util import get_resource_path, get_subprocess_startupinfo, get_version
from ..util import get_resource_path, get_subprocess_startupinfo, get_version, format_exception
from .logic import Alert, CollapsibleBox, DangerzoneGui, UpdateDialog
from .updater import UpdateReport

Expand Down Expand Up @@ -388,15 +388,24 @@ def closeEvent(self, e: QtGui.QCloseEvent) -> None:


class InstallContainerThread(QtCore.QThread):
finished = QtCore.Signal()
finished = QtCore.Signal(str)

def __init__(self, dangerzone: DangerzoneGui) -> None:
super(InstallContainerThread, self).__init__()
self.dangerzone = dangerzone

def run(self) -> None:
self.dangerzone.isolation_provider.install()
self.finished.emit()
error = None
try:
installed = self.dangerzone.isolation_provider.install()
except Exception as e:
log.error("Container installation problem")
error = format_exception(e)
else:
if not installed:
error = "The image cannot be found. This can be caused by a faulty container image."
finally:
self.finished.emit(error)


class WaitingWidget(QtWidgets.QWidget):
Expand Down Expand Up @@ -438,7 +447,6 @@ class WaitingWidgetContainer(WaitingWidget):
#
# Linux states
# - "install_container"
finished = QtCore.Signal()

def __init__(self, dangerzone: DangerzoneGui) -> None:
super(WaitingWidgetContainer, self).__init__()
Expand Down Expand Up @@ -509,48 +517,64 @@ def check_state(self) -> None:
# Update the state
self.state_change(state, error)

def show_error(self, msg: str, details: Optional[str] = None) -> None:
self.label.setText(msg)
show_traceback = details is not None
if show_traceback:
self.traceback.set_content(details)
self.traceback.setVisible(show_traceback)
self.buttons.show()

def show_message(self, msg: str) -> None:
self.label.setText(msg)
self.traceback.setVisible(False)
self.buttons.hide()

def installation_finished(self, error:Optional[str] = None):
if error:
msg = (
"During installation of the dangerzone image, <br>"
"the following error occured:"
)
self.show_error(msg, error)
else:
self.finished.emit()

def state_change(self, state: str, error: Optional[str] = None) -> None:
if state == "not_installed":
if platform.system() == "Linux":
self.label.setText(
self.show_error(
"<strong>Dangerzone requires Podman</strong><br><br>"
"Install it and retry."
)
else:
self.label.setText(
self.show_error(
"<strong>Dangerzone requires Docker Desktop</strong><br><br>"
"<a href='https://www.docker.com/products/docker-desktop'>Download Docker Desktop</a>"
", install it, and open it."
)
self.buttons.show()

elif state == "not_running":
if platform.system() == "Linux":
# "not_running" here means that the `podman image ls` command failed.
message = (
"<strong>Dangerzone requires Podman</strong><br><br>"
"Podman is installed but cannot run properly. See errors below"
)
if error:
self.traceback.set_content(error)

self.label.setText(message)

else:
self.label.setText(
message = (
"<strong>Dangerzone requires Docker Desktop</strong><br><br>"
"Docker is installed but isn't running.<br><br>"
"Open Docker and make sure it's running in the background."
)
self.buttons.show()
self.show_error(message, error)
else:
self.label.setText(
self.show_message(
"Installing the Dangerzone container image.<br><br>"
"This might take a few minutes..."
)
self.buttons.hide()
self.traceback.setVisible(False)
self.install_container_t = InstallContainerThread(self.dangerzone)
self.install_container_t.finished.connect(self.finished)
self.install_container_t.finished.connect(self.installation_finished)
self.install_container_t.start()


Expand Down
18 changes: 12 additions & 6 deletions dangerzone/isolation_provider/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def install() -> bool:
startupinfo=get_subprocess_startupinfo(),
)

chunk_size = 10240
chunk_size = 4 << 20
compressed_container_path = get_resource_path("container.tar.gz")
with gzip.open(compressed_container_path) as f:
while True:
Expand All @@ -170,19 +170,20 @@ def install() -> bool:
p.stdin.write(chunk)
else:
break
p.communicate()
_, err = p.communicate()
if p.returncode < 0:
raise RuntimeError(f"Could not install container image: {err}")

if not Container.is_container_installed():
log.error("Failed to install the container image")
if not Container.is_container_installed(raise_on_error=True):
return False

log.info("Container image installed")
return True

@staticmethod
def is_container_installed() -> bool:
def is_container_installed(raise_on_error: bool = False) -> bool:
"""
See if the podman container is installed. Linux only.
See if the container is installed.
"""
# Get the image id
with open(get_resource_path("image-id.txt")) as f:
Expand All @@ -209,6 +210,11 @@ def is_container_installed() -> bool:
elif found_image_id == "":
pass
else:
msg = (f"{Container.CONTAINER_NAME} images found, but IDs do not match."
f"Found: {found_image_id}, Expected: {','.join(expected_image_ids)}")
if raise_on_error:
raise RuntimeError(msg)
log.info(msg)
log.info("Deleting old dangerzone container image")

try:
Expand Down
12 changes: 12 additions & 0 deletions dangerzone/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import unicodedata
from typing import Optional
import traceback

import appdirs

Expand Down Expand Up @@ -97,3 +98,14 @@ def is_safe(chr: str) -> bool:
else:
sanitized_str += "�"
return sanitized_str


def format_exception(e):
# The signature of traceback.format_exception has changed in python 3.10
if sys.version_info < (3, 10):
output = traceback.format_exception(*sys.exc_info())
else:
output = traceback.format_exception(e)

return "".join(output)

0 comments on commit ee9b8b7

Please sign in to comment.