Add support for non-VM containers again

This commit is contained in:
Micah Lee 2021-08-04 15:02:49 -07:00
parent 588206a9e8
commit 4a4deeb64f
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
4 changed files with 81 additions and 41 deletions

View file

@ -142,12 +142,12 @@ def cli_main(output_filename, ocr_lang, filename):
if returncode != 0: if returncode != 0:
return return
success, error_message = global_common.validate_convert_to_pixel_output( # success, error_message = global_common.validate_convert_to_pixel_output(
common, output # common, output
) # )
if not success: # if not success:
click.echo(error_message) # click.echo(error_message)
return # return
print_header("Safe PDF created successfully") print_header("Safe PDF created successfully")
click.echo(common.save_filename) click.echo(common.output_filename)

View file

@ -7,21 +7,16 @@ import shutil
import json import json
import os import os
import uuid import uuid
import tempfile
# What is the container runtime for this platform? # What container tech is used for this platform?
if platform.system() == "Darwin": if platform.system() == "Darwin":
container_tech = "dangerzone-vm" container_tech = "dangerzone-vm"
container_runtime = shutil.which("docker")
elif platform.system() == "Windows":
container_tech = "docker"
container_runtime = shutil.which("docker.exe")
elif platform.system() == "Linux": elif platform.system() == "Linux":
container_tech = "podman" container_tech = "podman"
container_runtime = shutil.which("podman")
else: else:
print("Unknown operating system, defaulting to Docker") # Windows and unknown use docker for now, dangerzone-vm eventually
container_tech = "docker" container_tech = "docker"
container_runtime = shutil.which("docker")
# Define startupinfo for subprocesses # Define startupinfo for subprocesses
if platform.system() == "Windows": if platform.system() == "Windows":
@ -112,7 +107,7 @@ def vm_download(guest_path, host_path, vm_info):
host_exec(args) host_exec(args)
def exec_container(args, vm_info): def exec_container(args, vm_info=None):
if container_tech == "dangerzone-vm" and vm_info is None: if container_tech == "dangerzone-vm" and vm_info is None:
print("--vm-info-path required on this platform") print("--vm-info-path required on this platform")
return return
@ -121,6 +116,11 @@ def exec_container(args, vm_info):
args = ["/usr/bin/podman"] + args args = ["/usr/bin/podman"] + args
return vm_exec(args, vm_info) return vm_exec(args, vm_info)
else: else:
if container_tech == "podman":
container_runtime = shutil.which("podman")
else:
container_runtime = shutil.which("docker")
args = [container_runtime] + args args = [container_runtime] + args
return host_exec(args) return host_exec(args)
@ -162,31 +162,22 @@ def ls(vm_info_path):
@click.option("--ocr", required=True) @click.option("--ocr", required=True)
@click.option("--ocr-lang", required=True) @click.option("--ocr-lang", required=True)
def convert(vm_info_path, input_filename, output_filename, ocr, ocr_lang): def convert(vm_info_path, input_filename, output_filename, ocr, ocr_lang):
# If there's a VM: container_name = "dangerzone.rocks/dangerzone"
# - make inputdir on VM
# - make pixeldir on VM
# - make safedir on VM
# - scp input file to inputdir
# - run podman documenttopixels
# - run podman pixelstopdf
# - scp output file to host
# - delete inputdir, pixeldir, safedir
#
# If there's not a VM
# - make tmp pixeldir
# - make tmp safedir
# - run podman documenttopixels
# - run podman pixelstopdf
# - delete pixeldir, safedir
vm_info = load_vm_info(vm_info_path) vm_info = load_vm_info(vm_info_path)
if vm_info: if vm_info:
# If there's a VM:
# - make inputdir on VM
# - make pixeldir on VM
# - make safedir on VM
# - scp input file to inputdir
# - run podman documenttopixels
# - run podman pixelstopdf
# - scp output file to host
# - delete inputdir, pixeldir, safedir
ssh_args_str = " ".join(pipes.quote(s) for s in vm_ssh_args(vm_info)) ssh_args_str = " ".join(pipes.quote(s) for s in vm_ssh_args(vm_info))
print("If you want to SSH to the VM: " + ssh_args_str) print("If you want to SSH to the VM: " + ssh_args_str)
container_name = "localhost/dangerzone"
input_dir = vm_mkdir(vm_info) input_dir = vm_mkdir(vm_info)
pixel_dir = vm_mkdir(vm_info) pixel_dir = vm_mkdir(vm_info)
safe_dir = vm_mkdir(vm_info) safe_dir = vm_mkdir(vm_info)
@ -239,4 +230,56 @@ def convert(vm_info_path, input_filename, output_filename, ocr, ocr_lang):
sys.exit(ret) sys.exit(ret)
else: else:
print("not implemented yet") # If there's not a VM
# - make tmp pixeldir
# - make tmp safedir
# - run podman documenttopixels
# - run podman pixelstopdf
# - copy safe PDF to output filename
# - delete pixeldir, safedir
tmpdir = tempfile.TemporaryDirectory()
pixel_dir = os.path.join(tmpdir.name, "pixels")
safe_dir = os.path.join(tmpdir.name, "safe")
os.makedirs(pixel_dir, exist_ok=True)
os.makedirs(safe_dir, exist_ok=True)
container_output_filename = os.path.join(safe_dir, "safe-output-compressed.pdf")
args = [
"run",
"--network",
"none",
"-v",
f"{input_filename}:/tmp/input_file",
"-v",
f"{pixel_dir}:/dangerzone",
container_name,
"document-to-pixels",
]
ret = exec_container(args)
if ret != 0:
print("documents-to-pixels failed")
else:
args = [
"run",
"--network",
"none",
"-v",
f"{pixel_dir}:/dangerzone",
"-v",
f"{safe_dir}:/safezone",
"-e",
f"OCR={ocr}",
"-e",
f"OCR_LANGUAGE={ocr_lang}",
container_name,
"pixels-to-pdf",
]
ret = exec_container(args)
if ret != 0:
print("pixels-to-pdf failed")
else:
os.rename(container_output_filename, output_filename)
shutil.rmtree(tmpdir.name)

View file

@ -1,21 +1,18 @@
import os import os
import stat import stat
import requests import requests
import tempfile
import subprocess import subprocess
import shutil import shutil
import time
import platform import platform
from PySide2 import QtCore, QtGui, QtWidgets from PySide2 import QtCore, QtGui, QtWidgets
from ..container import container_runtime
class AuthorizationFailed(Exception): class AuthorizationFailed(Exception):
pass pass
def is_docker_installed(): def is_docker_installed():
container_runtime = shutil.which("docker.exe")
if platform.system() == "Darwin": if platform.system() == "Darwin":
# Does the docker binary exist? # Does the docker binary exist?
if os.path.isdir("/Applications/Docker.app") and os.path.exists( if os.path.isdir("/Applications/Docker.app") and os.path.exists(

View file

@ -9,7 +9,7 @@ sudo -u user podman system prune -a -f
# Build the podman container # Build the podman container
cd /opt/dangerzone-converter cd /opt/dangerzone-converter
sudo -u user podman build . --tag dangerzone sudo -u user podman build . --tag dangerzone.rocks/dangerzone
# Setup aports # Setup aports
cd ~/ cd ~/