Finish Docker Desktop flow

This commit is contained in:
Micah Lee 2021-11-22 15:02:29 -08:00
parent a54e19fe11
commit a7e0c3994d
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
2 changed files with 46 additions and 20 deletions

View file

@ -37,10 +37,6 @@ class GlobalCommon(object):
# Container # Container
self.container_name = "dangerzone.rocks/dangerzone" self.container_name = "dangerzone.rocks/dangerzone"
if platform.system() == "Linux":
self.container_runtime = shutil.which("podman")
else:
self.container_runtime = shutil.which("docker")
# Languages supported by tesseract # Languages supported by tesseract
self.ocr_languages = { self.ocr_languages = {
@ -384,6 +380,12 @@ class GlobalCommon(object):
) )
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
def get_container_runtime(self):
if platform.system() == "Linux":
return shutil.which("podman")
else:
return shutil.which("docker")
def get_resource_path(self, filename): def get_resource_path(self, filename):
if getattr(sys, "dangerzone_dev", False): if getattr(sys, "dangerzone_dev", False):
# Look for resources directory relative to python file # Look for resources directory relative to python file
@ -465,16 +467,19 @@ class GlobalCommon(object):
Make sure the podman container is installed. Linux only. Make sure the podman container is installed. Linux only.
""" """
if self.is_container_installed(): if self.is_container_installed():
print("Dangerzone container is already installed")
return return
# Load the container into podman # Load the container into podman
print("Installing Dangerzone container...") print("Installing Dangerzone container...")
p = subprocess.Popen([self.container_runtime, "load"], stdin=subprocess.PIPE) p = subprocess.Popen(
[self.get_container_runtime(), "load"], stdin=subprocess.PIPE
)
chunk_size = 1024 chunk_size = 1024
compressed_container_path = self.get_resource_path("dangerzone-converter.tar.gz") compressed_container_path = self.get_resource_path(
"dangerzone-converter.tar.gz"
)
with gzip.open(compressed_container_path) as f: with gzip.open(compressed_container_path) as f:
while True: while True:
chunk = f.read(chunk_size) chunk = f.read(chunk_size)
@ -495,8 +500,6 @@ class GlobalCommon(object):
""" """
See if the podman container is installed. Linux only. See if the podman container is installed. Linux only.
""" """
print("Checking if container is already installed")
# Get the image id # Get the image id
with open(self.get_resource_path("image-id.txt")) as f: with open(self.get_resource_path("image-id.txt")) as f:
expected_image_id = f.read().strip() expected_image_id = f.read().strip()
@ -507,7 +510,9 @@ class GlobalCommon(object):
if platform.system() == "Linux": if platform.system() == "Linux":
# Podman # Podman
images = json.loads( images = json.loads(
subprocess.check_output([self.container_runtime, "image", "list", "--format", "json"]) subprocess.check_output(
[self.get_container_runtime(), "image", "list", "--format", "json"]
)
) )
for image in images: for image in images:
if image["Id"] == expected_image_id: if image["Id"] == expected_image_id:
@ -515,10 +520,28 @@ class GlobalCommon(object):
break break
else: else:
# Docker # Docker
found_image_id = subprocess.check_output([self.container_runtime, "image", "list", "--format", "{{.ID}}", self.container_name], text=True) found_image_id = subprocess.check_output(
if found_image_id.strip() == expected_image_id: [
self.get_container_runtime(),
"image",
"list",
"--format",
"{{.ID}}",
self.container_name,
],
text=True,
)
found_image_id = found_image_id.strip()
if found_image_id == expected_image_id:
installed = True installed = True
elif found_image_id == "":
print("Dangerzone container image is not installed")
else: else:
print(f"Image {found_image_id} is installed, not {expected_image_id}") print(f"Image {found_image_id} is installed, not {expected_image_id}")
# Delete the image that exists
subprocess.check_output(
[self.get_container_runtime(), "rmi", found_image_id]
)
return installed return installed

View file

@ -135,6 +135,7 @@ class WaitingWidget(QtWidgets.QWidget):
layout = QtWidgets.QVBoxLayout() layout = QtWidgets.QVBoxLayout()
layout.addStretch() layout.addStretch()
layout.addWidget(self.label) layout.addWidget(self.label)
layout.addStretch()
layout.addWidget(self.buttons) layout.addWidget(self.buttons)
layout.addStretch() layout.addStretch()
self.setLayout(layout) self.setLayout(layout)
@ -150,41 +151,43 @@ class WaitingWidget(QtWidgets.QWidget):
container_runtime = shutil.which("podman") container_runtime = shutil.which("podman")
else: else:
container_runtime = shutil.which("docker") container_runtime = shutil.which("docker")
if container_runtime is None: if container_runtime is None:
print("Docker is not installed") print("Docker is not installed")
state = "not_installed" state = "not_installed"
else: else:
# Can we run `docker image ls` without an error # Can we run `docker image ls` without an error
with subprocess.Popen([container_runtime, "image", "ls"]) as p: with subprocess.Popen(
[container_runtime, "image", "ls"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
) as p:
p.communicate() p.communicate()
if p.returncode != 0: if p.returncode != 0:
print("Docker is not running") print("Docker is not running")
state = "not_running" state = "not_running"
else: else:
# Always try installing the container # Always try installing the container
print("Ensuring the container is installed")
state = "install_container" state = "install_container"
# Update the state # Update the state
print(f"Dangerzone state: {state}")
self.state_change(state) self.state_change(state)
def state_change(self, state): def state_change(self, state):
if state == "not_installed": if state == "not_installed":
self.label.setText( self.label.setText(
"<strong>Dangerzone requires Docker</strong><br><br><a href='https://www.docker.com/products/docker-desktop'>Download Docker Desktop</a> and install it." "<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() self.buttons.show()
elif state == "not_running": elif state == "not_running":
self.label.setText( self.label.setText(
"Docker Desktop is installed, but you must launch it first. Open Docker and make sure it's running in the background." "<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.buttons.show()
else: else:
self.label.setText( self.label.setText(
"Installing the Dangerzone container..." "Installing the Dangerzone container image.<br><br>This might take a few minutes..."
) )
self.buttons.hide() self.buttons.hide()
self.install_container_t = InstallContainerThread(self.global_common) self.install_container_t = InstallContainerThread(self.global_common)