Skip to content

Commit 052ed22

Browse files
committed
Prefer Podman on Mac, Docker Engine elsewhere...
- Use --mount instead of -v where possible (on Mac, or Ubuntu Docker). - Don't allow build_docs_to_publish with Podman except on Mac. See #27.
1 parent 8c1dc38 commit 052ed22

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

build_docs_to_publish

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ from doc_builder.build_docs import (
1919
main as build_docs,
2020
)
2121
from doc_builder.build_docs_shared_args import main as build_docs_shared_args
22-
from doc_builder.sys_utils import get_git_head_or_branch, check_permanent_file, get_toplevel_git_dir
22+
from doc_builder.sys_utils import (
23+
get_git_head_or_branch,
24+
check_permanent_file,
25+
get_toplevel_git_dir,
26+
is_mac,
27+
)
28+
from doc_builder.build_commands import get_container_cli_tool
2329

2430
# Change to the parent director of doc-builder and add to Python path
2531
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
@@ -179,6 +185,15 @@ def main():
179185
# Check version list for problems
180186
check_version_list()
181187

188+
# build_docs_to_publish has trouble on Linux (maybe just Ubuntu?) with Podman. Only allow
189+
# Docker. Issue filed: https://github.com/ESMCI/doc-builder/issues/27
190+
if args.container_cli_tool is None:
191+
args.container_cli_tool = get_container_cli_tool()
192+
if args.container_cli_tool == "podman" and not is_mac():
193+
raise RuntimeError(
194+
"build_docs_to_publish is only supported for Podman on Macs. Use Docker instead."
195+
)
196+
182197
# Loop over all documentation versions
183198
for version in VERSION_LIST:
184199
# Check that repo is ready and get our current state

doc_builder/build_commands.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
_CONTAINER_HOME = "/home/user/mounted_home"
1414

1515
# CLI tools we can try to use to run our container, in decreasing order of preference
16-
COMPATIBLE_CLI_TOOLS = ["podman", "docker"]
16+
if sys_utils.is_mac():
17+
# Prefer Podman because Docker Desktop isn't always free
18+
COMPATIBLE_CLI_TOOLS = ["podman", "docker"]
19+
else:
20+
# On Linux, Docker Engine (free) can be obtained without Docker Desktop, and it works better
21+
COMPATIBLE_CLI_TOOLS = ["docker", "podman"]
1722

1823

1924
def get_build_dir(build_dir=None, repo_root=None, version=None):
@@ -81,6 +86,24 @@ def get_container_cli_tool():
8186
raise RuntimeError(f"No compatible container software found: {', '.join(COMPATIBLE_CLI_TOOLS)}")
8287

8388

89+
def get_mount_arg(container_mountpoint, container_cli_tool=None):
90+
"""
91+
Get the Podman/Docker mount argument depending on which we're using and whether we're on Mac
92+
"""
93+
if container_cli_tool is None:
94+
container_cli_tool = get_container_cli_tool()
95+
96+
# This is preferred for performance reasons (at least on Mac)
97+
mount_option = "--mount"
98+
mount_arg = f"type=bind,source={container_mountpoint},target={_CONTAINER_HOME}"
99+
100+
# This fallback is needed for Podman on Linux machines,
101+
if container_cli_tool == "podman" and not sys_utils.is_mac():
102+
mount_option = "-v"
103+
mount_arg = f"{container_mountpoint}:{_CONTAINER_HOME}:U"
104+
return mount_option, mount_arg
105+
106+
84107
def get_build_command(
85108
*,
86109
build_dir,
@@ -157,15 +180,18 @@ def get_build_command(
157180
if container_cli_tool is None:
158181
container_cli_tool = get_container_cli_tool()
159182

183+
# Get argument for mounting/binding
184+
mount_option, mount_arg = get_mount_arg(container_mountpoint, container_cli_tool)
185+
160186
container_command = [
161187
container_cli_tool,
162188
"run",
163189
"--name",
164190
container_name,
165191
"--user",
166192
f"{uid}:{gid}",
167-
"-v",
168-
f"{container_mountpoint}:{_CONTAINER_HOME}:U",
193+
mount_option,
194+
mount_arg,
169195
"--workdir",
170196
container_workdir,
171197
"-t", # "-t" is needed for colorful output

doc_builder/build_docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import random
99
import string
1010
import sys
11-
import platform
1211
from urllib.parse import urlparse
1312
import signal
1413

@@ -20,6 +19,7 @@
2019
DEFAULT_IMAGE,
2120
)
2221
from doc_builder.build_docs_shared_args import bd_dir_group, bd_parser
22+
from doc_builder.sys_utils import is_mac
2323

2424

2525
def is_web_url(url_string):
@@ -245,7 +245,7 @@ def run_build_command(build_command, version, options):
245245
# Start container software/VM
246246
if "podman" in build_command:
247247
start_container_software("podman machine start")
248-
elif "docker" in build_command and platform.system() == "Darwin": # Darwin means Mac
248+
elif "docker" in build_command and is_mac():
249249
start_container_software("docker desktop start")
250250

251251
print(" ".join(build_command))

doc_builder/sys_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import subprocess
66
import os
7+
import platform
78

89

910
def check_permanent_file(filepath):
@@ -133,6 +134,11 @@ def git_current_branch():
133134
return branch_found, branch_name
134135

135136

137+
def is_mac():
138+
"""Returns True if running on a Mac"""
139+
return platform.system() == "Darwin"
140+
141+
136142
def is_under(child, parent_dir):
137143
"""Check whether child is in parent_dir
138144

test/test_unit_get_build_command.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import os
66
import unittest
77
from unittest.mock import patch
8-
from doc_builder.build_commands import get_build_command # pylint: disable=import-error
8+
# pylint: disable=import-error
9+
from doc_builder.build_commands import get_build_command, get_mount_arg
910

1011
# Allow names that pylint doesn't like, because otherwise I find it hard
1112
# to make readable unit test names
@@ -118,15 +119,16 @@ def test_container(self, mock_get_toplevel_of_doc_builder_parent):
118119
conf_py_path=conf_py_path,
119120
container_cli_tool="abc123",
120121
)
122+
mount_option, mount_arg = get_mount_arg("/path/to/username")
121123
expected = [
122124
"abc123",
123125
"run",
124126
"--name",
125127
"foo",
126128
"--user",
127129
self.uid_gid,
128-
"-v",
129-
"/path/to/username:/home/user/mounted_home:U",
130+
mount_option,
131+
mount_arg,
130132
"--workdir",
131133
"/home/user/mounted_home/foorepos/foocode/doc",
132134
"-t",
@@ -157,15 +159,16 @@ def test_container_relpath(self, mock_get_toplevel_of_doc_builder_parent):
157159
version="None",
158160
container_cli_tool="abc123",
159161
)
162+
mount_option, mount_arg = get_mount_arg("/path/to/username")
160163
expected = [
161164
"abc123",
162165
"run",
163166
"--name",
164167
"foo",
165168
"--user",
166169
self.uid_gid,
167-
"-v",
168-
"/path/to/username:/home/user/mounted_home:U",
170+
mount_option,
171+
mount_arg,
169172
"--workdir",
170173
"/home/user/mounted_home/foorepos/foocode/doc",
171174
"-t",
@@ -197,14 +200,15 @@ def test_container_no_clitool_given(self, mock_get_toplevel_of_doc_builder_paren
197200
conf_py_path=conf_py_path,
198201
container_cli_tool=None,
199202
)
203+
mount_option, mount_arg = get_mount_arg("/path/to/username")
200204
expected = [
201205
"run",
202206
"--name",
203207
"foo",
204208
"--user",
205209
self.uid_gid,
206-
"-v",
207-
"/path/to/username:/home/user/mounted_home:U",
210+
mount_option,
211+
mount_arg,
208212
"--workdir",
209213
"/home/user/mounted_home/foorepos/foocode/doc",
210214
"-t",

0 commit comments

Comments
 (0)