Add --custom-container option, to run a container built locally instead of requiring download from dockerhub

This commit is contained in:
Micah Lee 2020-03-06 18:08:15 +05:30
parent 8845ac6440
commit d519303150
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
4 changed files with 45 additions and 17 deletions

View file

@ -6,6 +6,7 @@ import platform
import click
import time
import uuid
import subprocess
from .global_common import GlobalCommon
from .main_window import MainWindow
@ -39,12 +40,10 @@ class Application(QtWidgets.QApplication):
@click.command()
@click.option("--custom-container") # Use this container instead of flmcode/dangerzone
@click.argument("filename", required=False)
def main(filename):
print(f"dangerzone {dangerzone_version}")
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
signal.signal(signal.SIGINT, signal.SIG_DFL)
def main(custom_container, filename):
click.echo(f"dangerzone {dangerzone_version}")
# Create the Qt app
app = Application()
@ -53,20 +52,35 @@ def main(filename):
# GlobalCommon object
global_common = GlobalCommon(app)
if custom_container:
# Do we have this container?
output = subprocess.check_output(
[global_common.container_runtime, "image", "ls", custom_container],
startupinfo=global_common.get_subprocess_startupinfo(),
)
if custom_container.encode() not in output:
click.echo(f"Container '{container}' not found")
return
global_common.custom_container = custom_container
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
signal.signal(signal.SIGINT, signal.SIG_DFL)
# If we're using Linux and docker, see if we need to add the user to the docker group
if (
platform.system() == "Linux"
and global_common.container_runtime == "/usr/bin/docker"
):
if not global_common.ensure_user_is_in_docker_group():
print("Failed to add user to docker group")
click.echo("Failed to add user to docker group")
return
# See if we need to install Docker...
if (platform.system() == "Darwin" or platform.system() == "Windows") and (
not is_docker_installed(global_common) or not is_docker_ready(global_common)
):
print("Docker is either not installed or not running")
click.echo("Docker is either not installed or not running")
docker_installer = DockerInstaller(global_common)
docker_installer.start()
return
@ -97,10 +111,10 @@ def main(filename):
try:
open(filename, "rb")
except FileNotFoundError:
print("File not found")
click.echo("File not found")
return False
except PermissionError:
print("Permission denied")
click.echo("Permission denied")
return False
window.common.document_filename = filename
window.doc_selection_widget.document_selected.emit()

View file

@ -73,6 +73,9 @@ class GlobalCommon(object):
else:
self.container_runtime = "/usr/bin/docker"
# In case we have a custom container
self.custom_container = None
# Preload list of PDF viewers on computer
self.pdf_viewers = self._find_pdf_viewers()
@ -243,6 +246,12 @@ class GlobalCommon(object):
# Load settings
self.settings = Settings(self)
def get_container_name(self):
if self.custom_container:
return self.custom_container
else:
return "flmcode/dangerzone"
def get_resource_path(self, filename):
if getattr(sys, "dangerzone_dev", False):
# Look for resources directory relative to python file

View file

@ -135,13 +135,18 @@ class SettingsWidget(QtWidgets.QWidget):
self.update_checkbox.setCheckState(QtCore.Qt.Unchecked)
# Is update containers required?
output = subprocess.check_output(
[self.global_common.container_runtime, "image", "ls", "flmcode/dangerzone"],
startupinfo=self.global_common.get_subprocess_startupinfo(),
)
if b"dangerzone" not in output:
self.update_checkbox.setCheckState(QtCore.Qt.Checked)
if self.global_common.custom_container:
self.update_checkbox.setCheckState(QtCore.Qt.Unchecked)
self.update_checkbox.setEnabled(False)
self.update_checkbox.hide()
else:
output = subprocess.check_output(
[self.global_common.container_runtime, "image", "ls", self.global_common.get_container_name()],
startupinfo=self.global_common.get_subprocess_startupinfo(),
)
if b"dangerzone" not in output:
self.update_checkbox.setCheckState(QtCore.Qt.Checked)
self.update_checkbox.setEnabled(False)
def update_ui(self):
if platform.system() == "Windows":

View file

@ -85,7 +85,7 @@ class ConvertToPixels(TaskBase):
f"{self.common.document_filename}:/tmp/input_file",
"-v",
f"{self.common.pixel_dir.name}:/dangerzone",
"flmcode/dangerzone",
self.global_common.get_container_name(),
"document-to-pixels",
]
returncode, output = self.exec_container(args)
@ -194,7 +194,7 @@ class ConvertToPDF(TaskBase):
f"{self.common.safe_dir.name}:/safezone",
]
+ envs
+ ["flmcode/dangerzone", "pixels-to-pdf",]
+ [self.global_common.get_container_name(), "pixels-to-pdf",]
)
returncode, output = self.exec_container(args)