2023-04-30 05:37:23 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-08-10 17:08:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-03-08 11:19:12 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
2019-03-04 22:58:25 +01:00
|
|
|
# https://docs.docker.com/compose/compose-file/#service-configuration-reference
|
|
|
|
# https://docs.docker.com/samples/
|
|
|
|
# https://docs.docker.com/compose/gettingstarted/
|
|
|
|
# https://docs.docker.com/compose/django/
|
|
|
|
# https://docs.docker.com/compose/wordpress/
|
2022-07-04 17:21:31 +02:00
|
|
|
# TODO: podman pod logs --color -n -f pod_testlogs
|
2019-03-04 10:30:14 +01:00
|
|
|
import argparse
|
2024-03-08 22:46:19 +01:00
|
|
|
import asyncio.subprocess
|
|
|
|
import getpass
|
|
|
|
import glob
|
2019-06-09 02:26:13 +02:00
|
|
|
import hashlib
|
2024-03-08 22:46:19 +01:00
|
|
|
import itertools
|
2019-10-05 21:37:14 +02:00
|
|
|
import json
|
2022-09-19 21:21:32 +02:00
|
|
|
import logging
|
2024-03-08 22:46:19 +01:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import re
|
2019-10-04 19:36:30 +02:00
|
|
|
import shlex
|
2024-03-08 22:46:19 +01:00
|
|
|
import signal
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-12-11 08:46:20 +01:00
|
|
|
from asyncio import Task
|
2019-10-04 19:36:30 +02:00
|
|
|
|
2019-07-08 22:53:38 +02:00
|
|
|
try:
|
|
|
|
from shlex import quote as cmd_quote
|
|
|
|
except ImportError:
|
2023-04-09 19:49:08 +02:00
|
|
|
from pipes import quote as cmd_quote # pylint: disable=deprecated-module
|
2019-07-08 22:53:38 +02:00
|
|
|
|
2019-05-09 22:15:05 +02:00
|
|
|
# import fnmatch
|
2019-03-04 22:58:25 +01:00
|
|
|
# fnmatch.fnmatchcase(env, "*_HOST")
|
2019-03-04 10:30:14 +01:00
|
|
|
|
|
|
|
import yaml
|
2021-12-10 00:01:45 +01:00
|
|
|
from dotenv import dotenv_values
|
2019-03-04 10:30:14 +01:00
|
|
|
|
2023-04-09 13:08:54 +02:00
|
|
|
__version__ = "1.0.7"
|
2019-09-03 17:38:57 +02:00
|
|
|
|
2022-03-16 13:25:57 +01:00
|
|
|
script = os.path.realpath(sys.argv[0])
|
|
|
|
|
2019-05-09 22:16:40 +02:00
|
|
|
# helper functions
|
2023-04-09 19:05:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_str(string_object):
|
|
|
|
return isinstance(string_object, str)
|
|
|
|
|
|
|
|
|
|
|
|
def is_dict(dict_object):
|
|
|
|
return isinstance(dict_object, dict)
|
|
|
|
|
|
|
|
|
|
|
|
def is_list(list_object):
|
2024-03-07 17:28:06 +01:00
|
|
|
return not is_str(list_object) and not is_dict(list_object) and hasattr(list_object, "__iter__")
|
2023-04-09 19:05:15 +02:00
|
|
|
|
|
|
|
|
2019-08-16 14:29:09 +02:00
|
|
|
# identity filter
|
2023-04-09 19:05:15 +02:00
|
|
|
def filteri(a):
|
|
|
|
return filter(lambda i: i, a)
|
2019-05-09 22:15:05 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-05-09 22:16:40 +02:00
|
|
|
def try_int(i, fallback=None):
|
|
|
|
try:
|
|
|
|
return int(i)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
return fallback
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-06-22 22:30:22 +02:00
|
|
|
def try_float(i, fallback=None):
|
|
|
|
try:
|
|
|
|
return float(i)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
return fallback
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-09-19 21:21:32 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2021-12-24 17:55:30 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-02-25 21:59:15 +01:00
|
|
|
dir_re = re.compile(r"^[~/\.]")
|
2022-03-01 08:53:24 +01:00
|
|
|
propagation_re = re.compile(
|
|
|
|
"^(?:z|Z|O|U|r?shared|r?slave|r?private|r?unbindable|r?bind|(?:no)?(?:exec|dev|suid))$"
|
|
|
|
)
|
|
|
|
norm_re = re.compile("[^-_a-z0-9]")
|
|
|
|
num_split_re = re.compile(r"(\d+|\D+)")
|
2019-08-14 17:22:36 +02:00
|
|
|
|
2021-05-17 14:03:47 +02:00
|
|
|
PODMAN_CMDS = (
|
2022-03-01 08:53:24 +01:00
|
|
|
"pull",
|
|
|
|
"push",
|
|
|
|
"build",
|
|
|
|
"inspect",
|
|
|
|
"run",
|
|
|
|
"start",
|
|
|
|
"stop",
|
|
|
|
"rm",
|
|
|
|
"volume",
|
2021-05-17 14:03:47 +02:00
|
|
|
)
|
|
|
|
|
2023-04-09 11:47:15 +02:00
|
|
|
t_re = re.compile(r"^(?:(\d+)[m:])?(?:(\d+(?:\.\d+)?)s?)?$")
|
2023-03-29 00:31:42 +02:00
|
|
|
STOP_GRACE_PERIOD = "10"
|
2023-03-28 16:33:02 +02:00
|
|
|
|
2023-04-09 11:47:15 +02:00
|
|
|
|
2023-03-28 16:33:02 +02:00
|
|
|
def str_to_seconds(txt):
|
2023-04-09 11:47:15 +02:00
|
|
|
if not txt:
|
|
|
|
return None
|
|
|
|
if isinstance(txt, (int, float)):
|
|
|
|
return txt
|
|
|
|
match = t_re.match(txt.strip())
|
|
|
|
if not match:
|
|
|
|
return None
|
|
|
|
mins, sec = match[1], match[2]
|
|
|
|
mins = int(mins) if mins else 0
|
|
|
|
sec = float(sec) if sec else 0
|
|
|
|
# "podman stop" takes only int
|
2024-03-08 22:46:24 +01:00
|
|
|
# Error: invalid argument "3.0" for "-t, --time" flag: strconv.ParseUint: parsing "3.0":
|
|
|
|
# invalid syntax
|
2023-04-09 11:47:15 +02:00
|
|
|
return int(mins * 60.0 + sec)
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-10-24 16:35:36 +02:00
|
|
|
def ver_as_list(a):
|
|
|
|
return [try_int(i, i) for i in num_split_re.findall(a)]
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-10-24 16:35:36 +02:00
|
|
|
def strverscmp_lt(a, b):
|
2022-03-01 08:53:24 +01:00
|
|
|
a_ls = ver_as_list(a or "")
|
|
|
|
b_ls = ver_as_list(b or "")
|
2021-10-24 16:35:36 +02:00
|
|
|
return a_ls < b_ls
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-06-09 02:26:13 +02:00
|
|
|
def parse_short_mount(mount_str, basedir):
|
2022-03-01 08:53:24 +01:00
|
|
|
mount_a = mount_str.split(":")
|
2019-06-09 02:26:13 +02:00
|
|
|
mount_opt_dict = {}
|
|
|
|
mount_opt = None
|
2020-04-18 17:39:59 +02:00
|
|
|
if len(mount_a) == 1:
|
|
|
|
# Anonymous: Just specify a path and let the engine creates the volume
|
2019-06-09 02:26:13 +02:00
|
|
|
# - /var/lib/mysql
|
2020-04-18 17:39:59 +02:00
|
|
|
mount_src, mount_dst = None, mount_str
|
|
|
|
elif len(mount_a) == 2:
|
2019-06-09 02:26:13 +02:00
|
|
|
mount_src, mount_dst = mount_a
|
2020-04-18 17:39:59 +02:00
|
|
|
# dest must start with / like /foo:/var/lib/mysql
|
|
|
|
# otherwise it's option like /var/lib/mysql:rw
|
2022-03-01 08:53:24 +01:00
|
|
|
if not mount_dst.startswith("/"):
|
2019-06-09 02:26:13 +02:00
|
|
|
mount_dst, mount_opt = mount_a
|
|
|
|
mount_src = None
|
2020-04-18 17:39:59 +02:00
|
|
|
elif len(mount_a) == 3:
|
2019-06-09 02:26:13 +02:00
|
|
|
mount_src, mount_dst, mount_opt = mount_a
|
|
|
|
else:
|
2022-03-01 08:53:24 +01:00
|
|
|
raise ValueError("could not parse mount " + mount_str)
|
2019-06-09 02:26:13 +02:00
|
|
|
if mount_src and dir_re.match(mount_src):
|
|
|
|
# Specify an absolute path mapping
|
|
|
|
# - /opt/data:/var/lib/mysql
|
|
|
|
# Path on the host, relative to the Compose file
|
|
|
|
# - ./cache:/tmp/cache
|
|
|
|
# User-relative path
|
|
|
|
# - ~/configs:/etc/configs/:ro
|
|
|
|
mount_type = "bind"
|
2024-04-07 08:28:45 +02:00
|
|
|
if os.name != 'nt' or (os.name == 'nt' and ".sock" not in mount_src):
|
|
|
|
mount_src = os.path.abspath(os.path.join(basedir, os.path.expanduser(mount_src)))
|
2019-06-09 02:26:13 +02:00
|
|
|
else:
|
|
|
|
# Named volume
|
|
|
|
# - datavolume:/var/lib/mysql
|
|
|
|
mount_type = "volume"
|
2022-03-01 08:53:24 +01:00
|
|
|
mount_opts = filteri((mount_opt or "").split(","))
|
2022-02-11 01:15:02 +01:00
|
|
|
propagation_opts = []
|
2019-06-11 14:07:49 +02:00
|
|
|
for opt in mount_opts:
|
2022-03-01 08:53:24 +01:00
|
|
|
if opt == "ro":
|
|
|
|
mount_opt_dict["read_only"] = True
|
|
|
|
elif opt == "rw":
|
|
|
|
mount_opt_dict["read_only"] = False
|
|
|
|
elif opt in ("consistent", "delegated", "cached"):
|
2020-04-18 17:39:59 +02:00
|
|
|
mount_opt_dict["consistency"] = opt
|
2022-02-11 01:15:02 +01:00
|
|
|
elif propagation_re.match(opt):
|
|
|
|
propagation_opts.append(opt)
|
2019-06-09 02:26:13 +02:00
|
|
|
else:
|
|
|
|
# TODO: ignore
|
2022-03-01 08:53:24 +01:00
|
|
|
raise ValueError("unknown mount option " + opt)
|
2023-04-09 12:10:33 +02:00
|
|
|
mount_opt_dict["bind"] = {"propagation": ",".join(propagation_opts)}
|
|
|
|
return {
|
|
|
|
"type": mount_type,
|
|
|
|
"source": mount_src,
|
|
|
|
"target": mount_dst,
|
|
|
|
**mount_opt_dict,
|
|
|
|
}
|
2019-06-09 02:26:13 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2020-04-18 17:39:59 +02:00
|
|
|
# NOTE: if a named volume is used but not defined it
|
|
|
|
# gives ERROR: Named volume "abc" is used in service "xyz"
|
|
|
|
# but no declaration was found in the volumes section.
|
|
|
|
# unless it's anonymous-volume
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-10-14 00:30:44 +02:00
|
|
|
def fix_mount_dict(compose, mount_dict, proj_name, srv_name):
|
2019-06-09 02:26:13 +02:00
|
|
|
"""
|
2019-08-14 17:22:36 +02:00
|
|
|
in-place fix mount dictionary to:
|
2021-10-14 00:30:44 +02:00
|
|
|
- define _vol to be the corresponding top-level volume
|
|
|
|
- if name is missing it would be source prefixed with project
|
|
|
|
- if no source it would be generated
|
2019-06-09 02:26:13 +02:00
|
|
|
"""
|
2020-04-18 17:39:59 +02:00
|
|
|
# if already applied nothing todo
|
2022-03-01 08:53:24 +01:00
|
|
|
if "_vol" in mount_dict:
|
|
|
|
return mount_dict
|
2020-04-18 17:39:59 +02:00
|
|
|
if mount_dict["type"] == "volume":
|
2021-10-14 00:30:44 +02:00
|
|
|
vols = compose.vols
|
2020-04-18 17:39:59 +02:00
|
|
|
source = mount_dict.get("source", None)
|
2021-10-14 00:30:44 +02:00
|
|
|
vol = (vols.get(source, None) or {}) if source else {}
|
2022-03-01 08:53:24 +01:00
|
|
|
name = vol.get("name", None)
|
2021-10-14 00:30:44 +02:00
|
|
|
mount_dict["_vol"] = vol
|
2022-09-24 16:04:17 +02:00
|
|
|
# handle anonymous or implied volume
|
2019-08-14 17:22:36 +02:00
|
|
|
if not source:
|
|
|
|
# missing source
|
2024-03-07 17:28:06 +01:00
|
|
|
vol["name"] = "_".join([
|
|
|
|
proj_name,
|
|
|
|
srv_name,
|
|
|
|
hashlib.sha256(mount_dict["target"].encode("utf-8")).hexdigest(),
|
|
|
|
])
|
2021-10-14 00:30:44 +02:00
|
|
|
elif not name:
|
2021-10-14 01:11:45 +02:00
|
|
|
external = vol.get("external", None)
|
2022-04-29 14:23:40 +02:00
|
|
|
if isinstance(external, dict):
|
2022-04-29 14:35:35 +02:00
|
|
|
vol["name"] = external.get("name", f"{source}")
|
2022-04-29 14:23:40 +02:00
|
|
|
elif external:
|
2022-04-29 14:35:35 +02:00
|
|
|
vol["name"] = f"{source}"
|
2022-04-29 14:23:40 +02:00
|
|
|
else:
|
2022-04-29 14:35:35 +02:00
|
|
|
vol["name"] = f"{proj_name}_{source}"
|
2019-06-09 02:26:13 +02:00
|
|
|
return mount_dict
|
2019-05-09 22:16:40 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-03-24 00:08:26 +01:00
|
|
|
# docker and docker-compose support subset of bash variable substitution
|
|
|
|
# https://docs.docker.com/compose/compose-file/#variable-substitution
|
|
|
|
# https://docs.docker.com/compose/env-file/
|
|
|
|
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
|
|
|
|
# $VARIABLE
|
|
|
|
# ${VARIABLE}
|
|
|
|
# ${VARIABLE:-default} default if not set or empty
|
|
|
|
# ${VARIABLE-default} default if not set
|
|
|
|
# ${VARIABLE:?err} raise error if not set or empty
|
|
|
|
# ${VARIABLE?err} raise error if not set
|
|
|
|
# $$ means $
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
var_re = re.compile(
|
|
|
|
r"""
|
2021-05-05 23:49:42 +02:00
|
|
|
\$(?:
|
|
|
|
(?P<escaped>\$) |
|
|
|
|
(?P<named>[_a-zA-Z][_a-zA-Z0-9]*) |
|
|
|
|
(?:{
|
|
|
|
(?P<braced>[_a-zA-Z][_a-zA-Z0-9]*)
|
2021-12-29 12:46:45 +01:00
|
|
|
(?:(?P<empty>:)?(?:
|
2021-12-30 11:19:53 +01:00
|
|
|
(?:-(?P<default>[^}]*)) |
|
|
|
|
(?:\?(?P<err>[^}]*))
|
2021-12-29 12:46:45 +01:00
|
|
|
))?
|
2021-05-05 23:49:42 +02:00
|
|
|
})
|
|
|
|
)
|
2022-03-01 08:53:24 +01:00
|
|
|
""",
|
|
|
|
re.VERBOSE,
|
|
|
|
)
|
|
|
|
|
2019-03-24 00:08:26 +01:00
|
|
|
|
2021-11-13 22:27:43 +01:00
|
|
|
def rec_subs(value, subs_dict):
|
2019-04-19 13:44:47 +02:00
|
|
|
"""
|
|
|
|
do bash-like substitution in value and if list of dictionary do that recursively
|
|
|
|
"""
|
2019-05-09 22:15:05 +02:00
|
|
|
if is_dict(value):
|
2022-02-25 23:41:07 +01:00
|
|
|
value = {k: rec_subs(v, subs_dict) for k, v in value.items()}
|
2019-05-09 22:15:05 +02:00
|
|
|
elif is_str(value):
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-05-05 23:49:42 +02:00
|
|
|
def convert(m):
|
|
|
|
if m.group("escaped") is not None:
|
|
|
|
return "$"
|
|
|
|
name = m.group("named") or m.group("braced")
|
2021-11-13 22:27:43 +01:00
|
|
|
value = subs_dict.get(name)
|
2022-03-01 08:53:24 +01:00
|
|
|
if value == "" and m.group("empty"):
|
2021-12-29 12:46:45 +01:00
|
|
|
value = None
|
2021-11-13 22:27:43 +01:00
|
|
|
if value is not None:
|
2022-02-26 00:49:34 +01:00
|
|
|
return str(value)
|
2021-05-05 23:49:42 +02:00
|
|
|
if m.group("err") is not None:
|
|
|
|
raise RuntimeError(m.group("err"))
|
|
|
|
return m.group("default") or ""
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-05-05 23:49:42 +02:00
|
|
|
value = var_re.sub(convert, value)
|
2019-05-05 13:33:07 +02:00
|
|
|
elif hasattr(value, "__iter__"):
|
2021-11-13 22:27:43 +01:00
|
|
|
value = [rec_subs(i, subs_dict) for i in value]
|
2019-03-24 00:08:26 +01:00
|
|
|
return value
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-03-04 22:58:25 +01:00
|
|
|
def norm_as_list(src):
|
|
|
|
"""
|
|
|
|
given a dictionary {key1:value1, key2: None} or list
|
|
|
|
return a list of ["key1=value1", "key2"]
|
|
|
|
"""
|
|
|
|
if src is None:
|
2019-03-20 23:49:17 +01:00
|
|
|
dst = []
|
2019-05-09 22:15:05 +02:00
|
|
|
elif is_dict(src):
|
2022-02-26 00:49:34 +01:00
|
|
|
dst = [(f"{k}={v}" if v is not None else k) for k, v in src.items()]
|
2019-05-09 22:15:05 +02:00
|
|
|
elif is_list(src):
|
2019-03-20 23:49:17 +01:00
|
|
|
dst = list(src)
|
2019-03-04 22:58:25 +01:00
|
|
|
else:
|
2019-03-20 23:49:17 +01:00
|
|
|
dst = [src]
|
2019-03-04 22:58:25 +01:00
|
|
|
return dst
|
2019-03-04 10:30:14 +01:00
|
|
|
|
2019-03-23 20:42:04 +01:00
|
|
|
|
2019-03-04 22:58:25 +01:00
|
|
|
def norm_as_dict(src):
|
|
|
|
"""
|
|
|
|
given a list ["key1=value1", "key2"]
|
|
|
|
return a dictionary {key1:value1, key2: None}
|
|
|
|
"""
|
|
|
|
if src is None:
|
2019-03-20 23:49:17 +01:00
|
|
|
dst = {}
|
2019-05-09 22:15:05 +02:00
|
|
|
elif is_dict(src):
|
2019-03-20 23:49:17 +01:00
|
|
|
dst = dict(src)
|
2019-05-09 22:15:05 +02:00
|
|
|
elif is_list(src):
|
2019-03-23 20:42:04 +01:00
|
|
|
dst = [i.split("=", 1) for i in src if i]
|
2022-02-25 23:41:07 +01:00
|
|
|
dst = [(a if len(a) == 2 else (a[0], None)) for a in dst]
|
|
|
|
dst = dict(dst)
|
2019-09-08 01:20:48 +02:00
|
|
|
elif is_str(src):
|
|
|
|
key, value = src.split("=", 1) if "=" in src else (src, None)
|
|
|
|
dst = {key: value}
|
2019-03-04 22:58:25 +01:00
|
|
|
else:
|
|
|
|
raise ValueError("dictionary or iterable is expected")
|
|
|
|
return dst
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-09-11 17:50:00 +02:00
|
|
|
def norm_ulimit(inner_value):
|
|
|
|
if is_dict(inner_value):
|
|
|
|
if not inner_value.keys() & {"soft", "hard"}:
|
|
|
|
raise ValueError("expected at least one soft or hard limit")
|
2020-04-18 17:39:59 +02:00
|
|
|
soft = inner_value.get("soft", inner_value.get("hard", None))
|
|
|
|
hard = inner_value.get("hard", inner_value.get("soft", None))
|
2022-02-26 00:49:34 +01:00
|
|
|
return f"{soft}:{hard}"
|
2022-03-01 08:53:24 +01:00
|
|
|
if is_list(inner_value):
|
|
|
|
return norm_ulimit(norm_as_dict(inner_value))
|
2019-09-11 17:50:00 +02:00
|
|
|
# if int or string return as is
|
|
|
|
return inner_value
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
# def tr_identity(project_name, given_containers):
|
2021-11-21 00:23:29 +01:00
|
|
|
# pod_name = f'pod_{project_name}'
|
|
|
|
# pod = dict(name=pod_name)
|
|
|
|
# containers = []
|
|
|
|
# for cnt in given_containers:
|
|
|
|
# containers.append(dict(cnt, pod=pod_name))
|
|
|
|
# return [pod], containers
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-03-08 19:13:14 +01:00
|
|
|
def transform(args, project_name, given_containers):
|
2023-04-09 13:02:10 +02:00
|
|
|
if not args.in_pod:
|
2022-03-08 19:13:14 +01:00
|
|
|
pod_name = None
|
|
|
|
pods = []
|
|
|
|
else:
|
|
|
|
pod_name = f"pod_{project_name}"
|
2023-04-09 12:10:33 +02:00
|
|
|
pod = {"name": pod_name}
|
2022-03-08 19:13:14 +01:00
|
|
|
pods = [pod]
|
2019-03-20 23:49:17 +01:00
|
|
|
containers = []
|
2019-03-04 10:30:14 +01:00
|
|
|
for cnt in given_containers:
|
2022-02-15 00:54:43 +01:00
|
|
|
containers.append(dict(cnt, pod=pod_name))
|
2022-03-08 19:13:14 +01:00
|
|
|
return pods, containers
|
2019-03-04 22:58:25 +01:00
|
|
|
|
2019-03-23 20:42:04 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def assert_volume(compose, mount_dict):
|
2019-06-09 02:26:13 +02:00
|
|
|
"""
|
|
|
|
inspect volume to get directory
|
|
|
|
create volume if needed
|
|
|
|
"""
|
2021-10-14 00:30:44 +02:00
|
|
|
vol = mount_dict.get("_vol", None)
|
2021-12-10 21:45:49 +01:00
|
|
|
if mount_dict["type"] == "bind":
|
|
|
|
basedir = os.path.realpath(compose.dirname)
|
|
|
|
mount_src = mount_dict["source"]
|
2024-03-07 17:28:06 +01:00
|
|
|
mount_src = os.path.realpath(os.path.join(basedir, os.path.expanduser(mount_src)))
|
2021-12-10 21:45:49 +01:00
|
|
|
if not os.path.exists(mount_src):
|
|
|
|
try:
|
|
|
|
os.makedirs(mount_src, exist_ok=True)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return
|
2022-05-10 00:30:03 +02:00
|
|
|
if mount_dict["type"] != "volume" or not vol or not vol.get("name", None):
|
2022-03-01 08:53:24 +01:00
|
|
|
return
|
2019-08-10 21:19:16 +02:00
|
|
|
proj_name = compose.project_name
|
2021-10-14 00:30:44 +02:00
|
|
|
vol_name = vol["name"]
|
2022-04-28 15:43:57 +02:00
|
|
|
is_ext = vol.get("external", None)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.debug("podman volume inspect %s || podman volume create %s", vol_name, vol_name)
|
2020-04-18 17:39:59 +02:00
|
|
|
# TODO: might move to using "volume list"
|
2024-03-08 22:46:24 +01:00
|
|
|
# podman volume list --format '{{.Name}}\t{{.MountPoint}}' \
|
|
|
|
# -f 'label=io.podman.compose.project=HERE'
|
2022-03-01 08:53:24 +01:00
|
|
|
try:
|
2023-12-10 07:51:54 +01:00
|
|
|
_ = (await compose.podman.output([], "volume", ["inspect", vol_name])).decode("utf-8")
|
2022-04-28 15:43:57 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if is_ext:
|
2022-05-10 00:30:03 +02:00
|
|
|
raise RuntimeError(f"External volume [{vol_name}] does not exists") from e
|
2021-10-14 01:11:45 +02:00
|
|
|
labels = vol.get("labels", None) or []
|
|
|
|
args = [
|
|
|
|
"create",
|
2022-03-01 08:53:24 +01:00
|
|
|
"--label",
|
|
|
|
f"io.podman.compose.project={proj_name}",
|
|
|
|
"--label",
|
|
|
|
f"com.docker.compose.project={proj_name}",
|
2021-10-14 01:11:45 +02:00
|
|
|
]
|
|
|
|
for item in norm_as_list(labels):
|
|
|
|
args.extend(["--label", item])
|
2021-12-30 23:06:48 +01:00
|
|
|
driver = vol.get("driver", None)
|
2021-12-29 15:49:19 +01:00
|
|
|
if driver:
|
|
|
|
args.extend(["--driver", driver])
|
2021-12-30 23:06:48 +01:00
|
|
|
driver_opts = vol.get("driver_opts", None) or {}
|
2021-12-29 15:49:19 +01:00
|
|
|
for opt, value in driver_opts.items():
|
2022-02-26 00:49:34 +01:00
|
|
|
args.extend(["--opt", f"{opt}={value}"])
|
2021-10-14 01:11:45 +02:00
|
|
|
args.append(vol_name)
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.output([], "volume", args)
|
|
|
|
_ = (await compose.podman.output([], "volume", ["inspect", vol_name])).decode("utf-8")
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-06-09 02:26:13 +02:00
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
def mount_desc_to_mount_args(compose, mount_desc, srv_name, cnt_name): # pylint: disable=unused-argument
|
2020-04-18 17:39:59 +02:00
|
|
|
mount_type = mount_desc.get("type", None)
|
2022-03-01 08:53:24 +01:00
|
|
|
vol = mount_desc.get("_vol", None) if mount_type == "volume" else None
|
2021-10-14 00:30:44 +02:00
|
|
|
source = vol["name"] if vol else mount_desc.get("source", None)
|
2019-06-09 02:26:13 +02:00
|
|
|
target = mount_desc["target"]
|
2020-04-18 17:39:59 +02:00
|
|
|
opts = []
|
|
|
|
if mount_desc.get(mount_type, None):
|
|
|
|
# TODO: we might need to add mount_dict[mount_type]["propagation"] = "z"
|
|
|
|
mount_prop = mount_desc.get(mount_type, {}).get("propagation", None)
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_prop:
|
|
|
|
opts.append(f"{mount_type}-propagation={mount_prop}")
|
|
|
|
if mount_desc.get("read_only", False):
|
|
|
|
opts.append("ro")
|
|
|
|
if mount_type == "tmpfs":
|
2019-06-09 02:26:13 +02:00
|
|
|
tmpfs_opts = mount_desc.get("tmpfs", {})
|
2020-04-18 17:39:59 +02:00
|
|
|
tmpfs_size = tmpfs_opts.get("size", None)
|
2019-06-09 02:26:13 +02:00
|
|
|
if tmpfs_size:
|
2022-02-26 00:49:34 +01:00
|
|
|
opts.append(f"tmpfs-size={tmpfs_size}")
|
2020-04-18 17:39:59 +02:00
|
|
|
tmpfs_mode = tmpfs_opts.get("mode", None)
|
2019-06-09 02:26:13 +02:00
|
|
|
if tmpfs_mode:
|
2022-02-26 00:49:34 +01:00
|
|
|
opts.append(f"tmpfs-mode={tmpfs_mode}")
|
2020-04-18 17:39:59 +02:00
|
|
|
opts = ",".join(opts)
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type == "bind":
|
2022-02-26 00:49:34 +01:00
|
|
|
return f"type=bind,source={source},destination={target},{opts}".rstrip(",")
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type == "volume":
|
2022-02-26 00:49:34 +01:00
|
|
|
return f"type=volume,source={source},destination={target},{opts}".rstrip(",")
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type == "tmpfs":
|
2022-02-26 00:49:34 +01:00
|
|
|
return f"type=tmpfs,destination={target},{opts}".rstrip(",")
|
2022-03-01 08:53:24 +01:00
|
|
|
raise ValueError("unknown mount type:" + mount_type)
|
|
|
|
|
2019-06-09 02:26:13 +02:00
|
|
|
|
2024-04-05 04:59:25 +02:00
|
|
|
def ulimit_to_ulimit_args(ulimit, podman_args):
|
2020-02-27 10:30:53 +01:00
|
|
|
if ulimit is not None:
|
|
|
|
# ulimit can be a single value, i.e. ulimit: host
|
|
|
|
if is_str(ulimit):
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--ulimit", ulimit])
|
2020-02-27 10:30:53 +01:00
|
|
|
# or a dictionary or list:
|
|
|
|
else:
|
|
|
|
ulimit = norm_as_dict(ulimit)
|
2022-03-01 08:53:24 +01:00
|
|
|
ulimit = [
|
|
|
|
"{}={}".format(ulimit_key, norm_ulimit(inner_value))
|
|
|
|
for ulimit_key, inner_value in ulimit.items()
|
|
|
|
]
|
2020-02-27 10:30:53 +01:00
|
|
|
for i in ulimit:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--ulimit", i])
|
|
|
|
|
2020-02-27 10:30:53 +01:00
|
|
|
|
2024-04-05 04:59:25 +02:00
|
|
|
def container_to_ulimit_args(cnt, podman_args):
|
|
|
|
ulimit_to_ulimit_args(cnt.get("ulimits", []), podman_args)
|
|
|
|
|
|
|
|
|
|
|
|
def container_to_ulimit_build_args(cnt, podman_args):
|
|
|
|
build = cnt.get("build", None)
|
|
|
|
|
|
|
|
if build is not None:
|
|
|
|
ulimit_to_ulimit_args(build.get("ulimits", []), podman_args)
|
|
|
|
|
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
def mount_desc_to_volume_args(compose, mount_desc, srv_name, cnt_name): # pylint: disable=unused-argument
|
2020-04-18 17:39:59 +02:00
|
|
|
mount_type = mount_desc["type"]
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type not in ("bind", "volume"):
|
|
|
|
raise ValueError("unknown mount type:" + mount_type)
|
|
|
|
vol = mount_desc.get("_vol", None) if mount_type == "volume" else None
|
2021-10-14 00:30:44 +02:00
|
|
|
source = vol["name"] if vol else mount_desc.get("source", None)
|
|
|
|
if not source:
|
|
|
|
raise ValueError(f"missing mount source for {mount_type} on {srv_name}")
|
|
|
|
target = mount_desc["target"]
|
|
|
|
opts = []
|
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
propagations = set(filteri(mount_desc.get(mount_type, {}).get("propagation", "").split(",")))
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type != "bind":
|
2024-03-07 17:28:06 +01:00
|
|
|
propagations.update(filteri(mount_desc.get("bind", {}).get("propagation", "").split(",")))
|
2020-04-18 17:39:59 +02:00
|
|
|
opts.extend(propagations)
|
|
|
|
# --volume, -v[=[[SOURCE-VOLUME|HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
|
|
|
|
# [rw|ro]
|
|
|
|
# [z|Z]
|
2021-12-21 21:13:40 +01:00
|
|
|
# [[r]shared|[r]slave|[r]private]|[r]unbindable
|
2020-04-18 17:39:59 +02:00
|
|
|
# [[r]bind]
|
|
|
|
# [noexec|exec]
|
|
|
|
# [nodev|dev]
|
|
|
|
# [nosuid|suid]
|
2021-12-21 21:13:40 +01:00
|
|
|
# [O]
|
|
|
|
# [U]
|
2020-04-18 17:39:59 +02:00
|
|
|
read_only = mount_desc.get("read_only", None)
|
|
|
|
if read_only is not None:
|
2022-03-01 08:53:24 +01:00
|
|
|
opts.append("ro" if read_only else "rw")
|
|
|
|
args = f"{source}:{target}"
|
|
|
|
if opts:
|
|
|
|
args += ":" + ",".join(opts)
|
2020-04-18 17:39:59 +02:00
|
|
|
return args
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-01-21 23:15:05 +01:00
|
|
|
def get_mnt_dict(compose, cnt, volume):
|
2020-04-18 17:39:59 +02:00
|
|
|
proj_name = compose.project_name
|
2022-03-01 08:53:24 +01:00
|
|
|
srv_name = cnt["_service"]
|
2020-04-18 17:39:59 +02:00
|
|
|
basedir = compose.dirname
|
2022-01-21 23:15:05 +01:00
|
|
|
if is_str(volume):
|
|
|
|
volume = parse_short_mount(volume, basedir)
|
|
|
|
return fix_mount_dict(compose, volume, proj_name, srv_name)
|
2020-12-21 11:24:17 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def get_mount_args(compose, cnt, volume):
|
2022-01-21 23:15:05 +01:00
|
|
|
volume = get_mnt_dict(compose, cnt, volume)
|
2022-02-25 21:59:15 +01:00
|
|
|
# proj_name = compose.project_name
|
2022-03-01 08:53:24 +01:00
|
|
|
srv_name = cnt["_service"]
|
2022-01-21 23:15:05 +01:00
|
|
|
mount_type = volume["type"]
|
2023-12-10 07:51:54 +01:00
|
|
|
await assert_volume(compose, volume)
|
2022-02-25 21:59:15 +01:00
|
|
|
if compose.prefer_volume_over_mount:
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type == "tmpfs":
|
2020-04-18 17:39:59 +02:00
|
|
|
# TODO: --tmpfs /tmp:rw,size=787448k,mode=1777
|
2022-03-01 08:53:24 +01:00
|
|
|
args = volume["target"]
|
2020-04-18 17:39:59 +02:00
|
|
|
tmpfs_opts = volume.get("tmpfs", {})
|
|
|
|
opts = []
|
|
|
|
size = tmpfs_opts.get("size", None)
|
2022-03-01 08:53:24 +01:00
|
|
|
if size:
|
|
|
|
opts.append(f"size={size}")
|
2020-04-18 17:39:59 +02:00
|
|
|
mode = tmpfs_opts.get("mode", None)
|
2022-03-01 08:53:24 +01:00
|
|
|
if mode:
|
|
|
|
opts.append(f"mode={mode}")
|
|
|
|
if opts:
|
|
|
|
args += ":" + ",".join(opts)
|
|
|
|
return ["--tmpfs", args]
|
|
|
|
args = mount_desc_to_volume_args(compose, volume, srv_name, cnt["name"])
|
|
|
|
return ["-v", args]
|
|
|
|
args = mount_desc_to_mount_args(compose, volume, srv_name, cnt["name"])
|
|
|
|
return ["--mount", args]
|
2020-02-27 10:30:53 +01:00
|
|
|
|
2021-07-21 18:22:07 +02:00
|
|
|
|
2023-09-20 11:29:36 +02:00
|
|
|
def get_secret_args(compose, cnt, secret, podman_is_building=False):
|
|
|
|
"""
|
|
|
|
podman_is_building: True if we are preparing arguments for an invocation of "podman build"
|
|
|
|
False if we are preparing for something else like "podman run"
|
|
|
|
"""
|
2022-03-01 08:53:24 +01:00
|
|
|
secret_name = secret if is_str(secret) else secret.get("source", None)
|
2021-07-21 18:22:07 +02:00
|
|
|
if not secret_name or secret_name not in compose.declared_secrets.keys():
|
2024-03-07 17:28:06 +01:00
|
|
|
raise ValueError(f'ERROR: undeclared secret: "{secret}", service: {cnt["_service"]}')
|
2021-07-21 18:22:07 +02:00
|
|
|
declared_secret = compose.declared_secrets[secret_name]
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
source_file = declared_secret.get("file", None)
|
|
|
|
dest_file = ""
|
|
|
|
secret_opts = ""
|
2021-07-21 18:22:07 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
target = None if is_str(secret) else secret.get("target", None)
|
|
|
|
uid = None if is_str(secret) else secret.get("uid", None)
|
|
|
|
gid = None if is_str(secret) else secret.get("gid", None)
|
|
|
|
mode = None if is_str(secret) else secret.get("mode", None)
|
2021-07-21 18:22:07 +02:00
|
|
|
|
|
|
|
if source_file:
|
2023-09-20 11:29:36 +02:00
|
|
|
# assemble path for source file first, because we need it for all cases
|
2022-02-27 23:19:07 +01:00
|
|
|
basedir = compose.dirname
|
2024-03-07 17:28:06 +01:00
|
|
|
source_file = os.path.realpath(os.path.join(basedir, os.path.expanduser(source_file)))
|
2023-09-20 11:29:36 +02:00
|
|
|
|
|
|
|
if podman_is_building:
|
|
|
|
# pass file secrets to "podman build" with param --secret
|
|
|
|
if not target:
|
|
|
|
secret_id = secret_name
|
|
|
|
elif "/" in target:
|
|
|
|
raise ValueError(
|
|
|
|
f'ERROR: Build secret "{secret_name}" has invalid target "{target}". '
|
|
|
|
+ "(Expected plain filename without directory as target.)"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
secret_id = target
|
|
|
|
volume_ref = ["--secret", f"id={secret_id},src={source_file}"]
|
|
|
|
else:
|
|
|
|
# pass file secrets to "podman run" as volumes
|
|
|
|
if not target:
|
|
|
|
dest_file = f"/run/secrets/{secret_name}"
|
|
|
|
elif not target.startswith("/"):
|
|
|
|
sec = target if target else secret_name
|
|
|
|
dest_file = f"/run/secrets/{sec}"
|
|
|
|
else:
|
|
|
|
dest_file = target
|
|
|
|
volume_ref = ["--volume", f"{source_file}:{dest_file}:ro,rprivate,rbind"]
|
|
|
|
|
2021-07-21 18:22:07 +02:00
|
|
|
if uid or gid or mode:
|
2022-02-26 00:49:34 +01:00
|
|
|
sec = target if target else secret_name
|
2023-08-09 01:12:10 +02:00
|
|
|
log.warning(
|
|
|
|
"WARNING: Service %s uses secret %s with uid, gid, or mode."
|
|
|
|
+ " These fields are not supported by this implementation of the Compose file",
|
|
|
|
cnt["_service"],
|
|
|
|
sec,
|
2021-07-21 18:22:07 +02:00
|
|
|
)
|
|
|
|
return volume_ref
|
|
|
|
# v3.5 and up added external flag, earlier the spec
|
|
|
|
# only required a name to be specified.
|
|
|
|
# docker-compose does not support external secrets outside of swarm mode.
|
|
|
|
# However accessing these via podman is trivial
|
|
|
|
# since these commands are directly translated to
|
2022-09-24 16:04:17 +02:00
|
|
|
# podman-create commands, albeit we can only support a 1:1 mapping
|
2021-07-21 18:22:07 +02:00
|
|
|
# at the moment
|
2022-03-01 08:53:24 +01:00
|
|
|
if declared_secret.get("external", False) or declared_secret.get("name", None):
|
|
|
|
secret_opts += f",uid={uid}" if uid else ""
|
|
|
|
secret_opts += f",gid={gid}" if gid else ""
|
|
|
|
secret_opts += f",mode={mode}" if mode else ""
|
2021-07-21 18:22:07 +02:00
|
|
|
# The target option is only valid for type=env,
|
|
|
|
# which in an ideal world would work
|
|
|
|
# for type=mount as well.
|
|
|
|
# having a custom name for the external secret
|
|
|
|
# has the same problem as well
|
2022-03-01 08:53:24 +01:00
|
|
|
ext_name = declared_secret.get("name", None)
|
2024-03-08 22:46:24 +01:00
|
|
|
err_str = (
|
|
|
|
'ERROR: Custom name/target reference "{}" '
|
|
|
|
'for mounted external secret "{}" is not supported'
|
|
|
|
)
|
2021-07-21 18:22:07 +02:00
|
|
|
if ext_name and ext_name != secret_name:
|
|
|
|
raise ValueError(err_str.format(secret_name, ext_name))
|
2022-02-25 23:41:07 +01:00
|
|
|
if target and target != secret_name:
|
2021-07-21 18:22:07 +02:00
|
|
|
raise ValueError(err_str.format(target, secret_name))
|
2022-02-25 23:41:07 +01:00
|
|
|
if target:
|
2023-08-09 01:12:10 +02:00
|
|
|
log.warning(
|
|
|
|
'WARNING: Service "%s" uses target: "%s" for secret: "%s".'
|
|
|
|
+ " That is un-supported and a no-op and is ignored.",
|
|
|
|
cnt["_service"],
|
|
|
|
target,
|
|
|
|
secret_name,
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
|
|
|
return ["--secret", "{}{}".format(secret_name, secret_opts)]
|
2021-07-21 18:22:07 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
raise ValueError(
|
2024-03-07 17:28:06 +01:00
|
|
|
'ERROR: unparsable secret: "{}", service: "{}"'.format(secret_name, cnt["_service"])
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
2021-07-21 18:22:07 +02:00
|
|
|
|
|
|
|
|
2021-06-22 22:30:22 +02:00
|
|
|
def container_to_res_args(cnt, podman_args):
|
2022-02-12 16:35:04 +01:00
|
|
|
# v2: https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources
|
|
|
|
# cpus, cpu_shares, mem_limit, mem_reservation
|
2022-03-01 08:53:24 +01:00
|
|
|
cpus_limit_v2 = try_float(cnt.get("cpus", None), None)
|
|
|
|
cpu_shares_v2 = try_int(cnt.get("cpu_shares", None), None)
|
|
|
|
mem_limit_v2 = cnt.get("mem_limit", None)
|
|
|
|
mem_res_v2 = cnt.get("mem_reservation", None)
|
2022-02-12 16:35:04 +01:00
|
|
|
# v3: https://docs.docker.com/compose/compose-file/compose-file-v3/#resources
|
|
|
|
# spec: https://github.com/compose-spec/compose-spec/blob/master/deploy.md#resources
|
|
|
|
# deploy.resources.{limits,reservations}.{cpus, memory}
|
2022-03-01 08:53:24 +01:00
|
|
|
deploy = cnt.get("deploy", None) or {}
|
|
|
|
res = deploy.get("resources", None) or {}
|
|
|
|
limits = res.get("limits", None) or {}
|
|
|
|
cpus_limit_v3 = try_float(limits.get("cpus", None), None)
|
|
|
|
mem_limit_v3 = limits.get("memory", None)
|
|
|
|
reservations = res.get("reservations", None) or {}
|
|
|
|
# cpus_res_v3 = try_float(reservations.get('cpus', None), None)
|
|
|
|
mem_res_v3 = reservations.get("memory", None)
|
2021-06-22 22:30:22 +02:00
|
|
|
# add args
|
2021-06-22 22:48:05 +02:00
|
|
|
cpus = cpus_limit_v3 or cpus_limit_v2
|
|
|
|
if cpus:
|
2024-03-07 17:28:06 +01:00
|
|
|
podman_args.extend((
|
|
|
|
"--cpus",
|
|
|
|
str(cpus),
|
|
|
|
))
|
2021-08-18 10:32:01 +02:00
|
|
|
if cpu_shares_v2:
|
2024-03-07 17:28:06 +01:00
|
|
|
podman_args.extend((
|
|
|
|
"--cpu-shares",
|
|
|
|
str(cpu_shares_v2),
|
|
|
|
))
|
2021-06-22 22:30:22 +02:00
|
|
|
mem = mem_limit_v3 or mem_limit_v2
|
2021-06-22 22:48:05 +02:00
|
|
|
if mem:
|
2024-03-07 17:28:06 +01:00
|
|
|
podman_args.extend((
|
|
|
|
"-m",
|
|
|
|
str(mem).lower(),
|
|
|
|
))
|
2021-06-22 22:30:22 +02:00
|
|
|
mem_res = mem_res_v3 or mem_res_v2
|
2021-06-22 22:48:05 +02:00
|
|
|
if mem_res:
|
2024-03-07 17:28:06 +01:00
|
|
|
podman_args.extend((
|
|
|
|
"--memory-reservation",
|
|
|
|
str(mem_res).lower(),
|
|
|
|
))
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-06-22 22:30:22 +02:00
|
|
|
|
2021-10-09 23:43:01 +02:00
|
|
|
def port_dict_to_str(port_desc):
|
|
|
|
# NOTE: `mode: host|ingress` is ignored
|
|
|
|
cnt_port = port_desc.get("target", None)
|
|
|
|
published = port_desc.get("published", None) or ""
|
|
|
|
host_ip = port_desc.get("host_ip", None)
|
|
|
|
protocol = port_desc.get("protocol", None) or "tcp"
|
|
|
|
if not cnt_port:
|
|
|
|
raise ValueError("target container port must be specified")
|
|
|
|
if host_ip:
|
|
|
|
ret = f"{host_ip}:{published}:{cnt_port}"
|
|
|
|
else:
|
|
|
|
ret = f"{published}:{cnt_port}" if published else f"{cnt_port}"
|
2022-03-01 08:53:24 +01:00
|
|
|
if protocol != "tcp":
|
|
|
|
ret += f"/{protocol}"
|
2021-10-09 23:43:01 +02:00
|
|
|
return ret
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-10-09 23:43:01 +02:00
|
|
|
def norm_ports(ports_in):
|
|
|
|
if not ports_in:
|
|
|
|
ports_in = []
|
|
|
|
if isinstance(ports_in, str):
|
|
|
|
ports_in = [ports_in]
|
|
|
|
ports_out = []
|
|
|
|
for port in ports_in:
|
|
|
|
if isinstance(port, dict):
|
|
|
|
port = port_dict_to_str(port)
|
2022-03-13 21:36:25 +01:00
|
|
|
elif isinstance(port, int):
|
|
|
|
port = str(port)
|
2021-10-09 23:43:01 +02:00
|
|
|
elif not isinstance(port, str):
|
|
|
|
raise TypeError("port should be either string or dict")
|
|
|
|
ports_out.append(port)
|
|
|
|
return ports_out
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2024-03-09 00:46:46 +01:00
|
|
|
def get_network_create_args(net_desc, proj_name, net_name):
|
|
|
|
args = [
|
|
|
|
"create",
|
|
|
|
"--label",
|
|
|
|
f"io.podman.compose.project={proj_name}",
|
|
|
|
"--label",
|
|
|
|
f"com.docker.compose.project={proj_name}",
|
|
|
|
]
|
|
|
|
# TODO: add more options here, like dns, ipv6, etc.
|
|
|
|
labels = net_desc.get("labels", None) or []
|
|
|
|
for item in norm_as_list(labels):
|
|
|
|
args.extend(["--label", item])
|
|
|
|
if net_desc.get("internal", None):
|
|
|
|
args.append("--internal")
|
|
|
|
driver = net_desc.get("driver", None)
|
|
|
|
if driver:
|
|
|
|
args.extend(("--driver", driver))
|
|
|
|
driver_opts = net_desc.get("driver_opts", None) or {}
|
|
|
|
for key, value in driver_opts.items():
|
|
|
|
args.extend(("--opt", f"{key}={value}"))
|
|
|
|
ipam = net_desc.get("ipam", None) or {}
|
|
|
|
ipam_driver = ipam.get("driver", None)
|
|
|
|
if ipam_driver:
|
|
|
|
args.extend(("--ipam-driver", ipam_driver))
|
|
|
|
ipam_config_ls = ipam.get("config", None) or []
|
2023-08-23 17:48:32 +02:00
|
|
|
if net_desc.get("enable_ipv6", None):
|
|
|
|
args.append("--ipv6")
|
|
|
|
|
2024-03-09 00:46:46 +01:00
|
|
|
if is_dict(ipam_config_ls):
|
|
|
|
ipam_config_ls = [ipam_config_ls]
|
|
|
|
for ipam_config in ipam_config_ls:
|
|
|
|
subnet = ipam_config.get("subnet", None)
|
|
|
|
ip_range = ipam_config.get("ip_range", None)
|
|
|
|
gateway = ipam_config.get("gateway", None)
|
|
|
|
if subnet:
|
|
|
|
args.extend(("--subnet", subnet))
|
|
|
|
if ip_range:
|
|
|
|
args.extend(("--ip-range", ip_range))
|
|
|
|
if gateway:
|
|
|
|
args.extend(("--gateway", gateway))
|
|
|
|
args.append(net_name)
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def assert_cnt_nets(compose, cnt):
|
2021-11-21 00:23:29 +01:00
|
|
|
"""
|
|
|
|
create missing networks
|
|
|
|
"""
|
2022-01-18 19:40:29 +01:00
|
|
|
net = cnt.get("network_mode", None)
|
|
|
|
if net and not net.startswith("bridge"):
|
|
|
|
return
|
2021-11-21 00:23:29 +01:00
|
|
|
proj_name = compose.project_name
|
|
|
|
nets = compose.networks
|
|
|
|
default_net = compose.default_net
|
2021-12-25 20:06:29 +01:00
|
|
|
cnt_nets = cnt.get("networks", None)
|
|
|
|
if cnt_nets and is_dict(cnt_nets):
|
|
|
|
cnt_nets = list(cnt_nets.keys())
|
|
|
|
cnt_nets = norm_as_list(cnt_nets or default_net)
|
2021-11-21 00:23:29 +01:00
|
|
|
for net in cnt_nets:
|
|
|
|
net_desc = nets[net] or {}
|
2021-12-12 23:21:53 +01:00
|
|
|
is_ext = net_desc.get("external", None)
|
2021-12-13 00:21:34 +01:00
|
|
|
ext_desc = is_ext if is_dict(is_ext) else {}
|
2021-12-12 23:21:53 +01:00
|
|
|
default_net_name = net if is_ext else f"{proj_name}_{net}"
|
2024-03-07 17:28:06 +01:00
|
|
|
net_name = ext_desc.get("name", None) or net_desc.get("name", None) or default_net_name
|
2022-03-01 08:53:24 +01:00
|
|
|
try:
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.output([], "network", ["exists", net_name])
|
2022-02-25 22:48:42 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
2021-12-12 23:21:53 +01:00
|
|
|
if is_ext:
|
2024-03-07 17:28:06 +01:00
|
|
|
raise RuntimeError(f"External network [{net_name}] does not exists") from e
|
2024-03-09 00:46:46 +01:00
|
|
|
args = get_network_create_args(net_desc, proj_name, net_name)
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.output([], "network", args)
|
|
|
|
await compose.podman.output([], "network", ["exists", net_name])
|
2021-11-21 00:23:29 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-11-21 00:23:29 +01:00
|
|
|
def get_net_args(compose, cnt):
|
2021-11-21 11:35:13 +01:00
|
|
|
service_name = cnt["service_name"]
|
2022-06-21 20:48:45 +02:00
|
|
|
net_args = []
|
2022-06-29 09:46:47 +02:00
|
|
|
is_bridge = False
|
2024-03-21 15:36:49 +01:00
|
|
|
mac_address = cnt.get("mac_address", None)
|
2022-01-18 19:40:29 +01:00
|
|
|
net = cnt.get("network_mode", None)
|
|
|
|
if net:
|
2023-02-10 11:37:50 +01:00
|
|
|
if net == "none":
|
|
|
|
is_bridge = False
|
|
|
|
elif net == "host":
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network={net}")
|
2024-03-21 19:41:25 +01:00
|
|
|
elif net.startswith("slirp4netns"): # Note: podman-specific network mode
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network={net}")
|
2024-03-21 19:42:05 +01:00
|
|
|
elif net == "private": # Note: podman-specific network mode
|
|
|
|
net_args.append("--network=private")
|
|
|
|
elif net.startswith("pasta"): # Note: podman-specific network mode
|
|
|
|
net_args.append(f"--network={net}")
|
|
|
|
elif net.startswith("ns:"): # Note: podman-specific network mode
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network={net}")
|
2022-06-21 20:48:45 +02:00
|
|
|
elif net.startswith("service:"):
|
2022-01-18 19:40:29 +01:00
|
|
|
other_srv = net.split(":", 1)[1].strip()
|
|
|
|
other_cnt = compose.container_names_by_service[other_srv][0]
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network=container:{other_cnt}")
|
2022-06-21 20:48:45 +02:00
|
|
|
elif net.startswith("container:"):
|
2022-03-01 08:53:24 +01:00
|
|
|
other_cnt = net.split(":", 1)[1].strip()
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network=container:{other_cnt}")
|
2022-06-29 09:46:47 +02:00
|
|
|
elif net.startswith("bridge"):
|
2022-06-29 10:55:39 +02:00
|
|
|
is_bridge = True
|
2022-06-21 20:48:45 +02:00
|
|
|
else:
|
2023-08-09 01:12:10 +02:00
|
|
|
log.fatal("unknown network_mode [%s]", net)
|
2022-06-21 20:54:44 +02:00
|
|
|
sys.exit(1)
|
2022-06-29 09:46:47 +02:00
|
|
|
else:
|
2022-06-29 10:55:39 +02:00
|
|
|
is_bridge = True
|
2021-12-12 23:24:23 +01:00
|
|
|
proj_name = compose.project_name
|
2021-11-21 00:23:29 +01:00
|
|
|
default_net = compose.default_net
|
|
|
|
nets = compose.networks
|
2021-12-25 20:06:29 +01:00
|
|
|
cnt_nets = cnt.get("networks", None)
|
2024-03-21 19:42:05 +01:00
|
|
|
|
2021-12-25 20:06:29 +01:00
|
|
|
aliases = [service_name]
|
|
|
|
# NOTE: from podman manpage:
|
2023-04-09 19:05:15 +02:00
|
|
|
# NOTE: A container will only have access to aliases on the first network
|
|
|
|
# that it joins. This is a limitation that will be removed in a later
|
|
|
|
# release.
|
2022-01-12 20:48:42 +01:00
|
|
|
ip = None
|
2022-06-28 14:58:19 +02:00
|
|
|
ip6 = None
|
2022-10-16 00:04:25 +02:00
|
|
|
ip_assignments = 0
|
2023-07-15 23:25:03 +02:00
|
|
|
if cnt.get("_aliases", None):
|
|
|
|
aliases.extend(cnt.get("_aliases", None))
|
2021-12-25 20:06:29 +01:00
|
|
|
if cnt_nets and is_dict(cnt_nets):
|
2023-04-09 11:00:45 +02:00
|
|
|
prioritized_cnt_nets = []
|
2022-02-25 22:48:42 +01:00
|
|
|
# cnt_nets is {net_key: net_value, ...}
|
2023-02-09 20:43:25 +01:00
|
|
|
for net_key, net_value in cnt_nets.items():
|
2022-07-04 17:21:31 +02:00
|
|
|
net_value = net_value or {}
|
2021-12-25 20:06:29 +01:00
|
|
|
aliases.extend(norm_as_list(net_value.get("aliases", None)))
|
2023-04-10 12:38:21 +02:00
|
|
|
if net_value.get("ipv4_address", None) is not None:
|
|
|
|
ip_assignments = ip_assignments + 1
|
|
|
|
if net_value.get("ipv6_address", None) is not None:
|
|
|
|
ip_assignments = ip_assignments + 1
|
2022-10-16 00:04:25 +02:00
|
|
|
|
2022-06-28 14:58:19 +02:00
|
|
|
if not ip:
|
|
|
|
ip = net_value.get("ipv4_address", None)
|
|
|
|
if not ip6:
|
|
|
|
ip6 = net_value.get("ipv6_address", None)
|
2023-02-09 20:43:25 +01:00
|
|
|
net_priority = net_value.get("priority", 0)
|
2024-03-07 17:28:06 +01:00
|
|
|
prioritized_cnt_nets.append((
|
|
|
|
net_priority,
|
|
|
|
net_key,
|
|
|
|
))
|
2023-02-09 20:43:25 +01:00
|
|
|
# sort dict by priority
|
2023-04-09 11:00:45 +02:00
|
|
|
prioritized_cnt_nets.sort(reverse=True)
|
2023-04-09 11:47:15 +02:00
|
|
|
cnt_nets = [net_key for _, net_key in prioritized_cnt_nets]
|
2021-12-25 20:06:29 +01:00
|
|
|
cnt_nets = norm_as_list(cnt_nets or default_net)
|
2023-04-09 11:47:15 +02:00
|
|
|
net_names = []
|
2021-11-21 00:23:29 +01:00
|
|
|
for net in cnt_nets:
|
|
|
|
net_desc = nets[net] or {}
|
2021-12-12 23:24:23 +01:00
|
|
|
is_ext = net_desc.get("external", None)
|
2021-12-13 00:21:34 +01:00
|
|
|
ext_desc = is_ext if is_dict(is_ext) else {}
|
2021-12-12 23:24:23 +01:00
|
|
|
default_net_name = net if is_ext else f"{proj_name}_{net}"
|
2024-03-07 17:28:06 +01:00
|
|
|
net_name = ext_desc.get("name", None) or net_desc.get("name", None) or default_net_name
|
2023-02-09 20:43:25 +01:00
|
|
|
net_names.append(net_name)
|
2021-11-21 00:23:29 +01:00
|
|
|
net_names_str = ",".join(net_names)
|
2022-10-16 00:04:25 +02:00
|
|
|
|
2024-03-21 16:43:37 +01:00
|
|
|
# TODO: add support for per-interface aliases
|
|
|
|
# See https://docs.docker.com/compose/compose-file/compose-file-v3/#aliases
|
|
|
|
# Even though podman accepts network-specific aliases (e.g., --network=bridge:alias=foo,
|
|
|
|
# podman currently ignores this if a per-container network-alias is set; as pdoman-compose
|
|
|
|
# always sets a network-alias to the container name, is currently doesn't make sense to
|
|
|
|
# implement this.
|
2024-03-21 15:13:04 +01:00
|
|
|
multiple_nets = cnt.get("networks", None)
|
|
|
|
if multiple_nets and len(multiple_nets) > 1:
|
|
|
|
# networks can be specified as a dict with config per network or as a plain list without
|
|
|
|
# config. Support both cases by converting the plain list to a dict with empty config.
|
|
|
|
if is_list(multiple_nets):
|
|
|
|
multiple_nets = {net: {} for net in multiple_nets}
|
|
|
|
else:
|
|
|
|
multiple_nets = {net: net_config or {} for net, net_config in multiple_nets.items()}
|
2023-04-10 12:38:21 +02:00
|
|
|
|
2024-03-21 15:36:49 +01:00
|
|
|
# if a mac_address was specified on the container level, we need to check that it is not
|
|
|
|
# specified on the network level as well
|
|
|
|
if mac_address is not None:
|
|
|
|
for net_config_ in multiple_nets.values():
|
2024-04-17 19:15:26 +02:00
|
|
|
network_mac = net_config_.get("x-podman.mac_address", None)
|
2024-03-21 15:36:49 +01:00
|
|
|
if network_mac is not None:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"conflicting mac addresses {mac_address} and {network_mac}:"
|
|
|
|
"specifying mac_address on both container and network level "
|
|
|
|
"is not supported"
|
|
|
|
)
|
|
|
|
|
2024-03-21 15:13:04 +01:00
|
|
|
for net_, net_config_ in multiple_nets.items():
|
2023-04-10 12:38:21 +02:00
|
|
|
net_desc = nets[net_] or {}
|
|
|
|
is_ext = net_desc.get("external", None)
|
|
|
|
ext_desc = is_ext if is_dict(is_ext) else {}
|
|
|
|
default_net_name = net_ if is_ext else f"{proj_name}_{net_}"
|
2024-03-07 17:28:06 +01:00
|
|
|
net_name = ext_desc.get("name", None) or net_desc.get("name", None) or default_net_name
|
2022-10-16 00:04:25 +02:00
|
|
|
|
2024-03-21 15:13:04 +01:00
|
|
|
ipv4 = net_config_.get("ipv4_address", None)
|
|
|
|
ipv6 = net_config_.get("ipv6_address", None)
|
2024-03-21 15:36:49 +01:00
|
|
|
# custom extension; not supported by docker-compose v3
|
2024-04-17 19:15:26 +02:00
|
|
|
mac = net_config_.get("x-podman.mac_address", None)
|
2024-03-21 15:36:49 +01:00
|
|
|
|
|
|
|
# if a mac_address was specified on the container level, apply it to the first network
|
|
|
|
# This works for Python > 3.6, because dict insert ordering is preserved, so we are
|
|
|
|
# sure that the first network we encounter here is also the first one specified by
|
|
|
|
# the user
|
|
|
|
if mac is None and mac_address is not None:
|
|
|
|
mac = mac_address
|
|
|
|
mac_address = None
|
2024-03-21 15:13:04 +01:00
|
|
|
|
|
|
|
net_options = []
|
|
|
|
if ipv4:
|
|
|
|
net_options.append(f"ip={ipv4}")
|
|
|
|
if ipv6:
|
|
|
|
net_options.append(f"ip={ipv6}")
|
2024-03-21 15:37:10 +01:00
|
|
|
if mac:
|
|
|
|
net_options.append(f"mac={mac}")
|
2024-03-21 15:13:04 +01:00
|
|
|
|
|
|
|
if net_options:
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network={net_name}:" + ",".join(net_options))
|
2024-03-21 15:13:04 +01:00
|
|
|
else:
|
2024-03-21 16:28:15 +01:00
|
|
|
net_args.append(f"--network={net_name}")
|
2022-10-16 00:04:25 +02:00
|
|
|
else:
|
2023-04-10 12:38:21 +02:00
|
|
|
if is_bridge:
|
2024-03-21 16:28:15 +01:00
|
|
|
if net_names_str:
|
|
|
|
net_args.append(f"--network={net_names_str}")
|
|
|
|
else:
|
|
|
|
net_args.append("--network=bridge")
|
2023-04-10 12:38:21 +02:00
|
|
|
if ip:
|
|
|
|
net_args.append(f"--ip={ip}")
|
|
|
|
if ip6:
|
|
|
|
net_args.append(f"--ip6={ip6}")
|
2024-03-21 15:36:49 +01:00
|
|
|
if mac_address:
|
|
|
|
net_args.append(f"--mac-address={mac_address}")
|
2024-03-21 15:13:04 +01:00
|
|
|
|
|
|
|
if is_bridge:
|
2024-03-21 16:28:15 +01:00
|
|
|
for alias in aliases:
|
|
|
|
net_args.extend([f"--network-alias={alias}"])
|
2024-03-21 15:13:04 +01:00
|
|
|
|
2022-01-12 20:48:42 +01:00
|
|
|
return net_args
|
|
|
|
|
2021-11-21 00:23:29 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def container_to_args(compose, cnt, detached=True):
|
2019-08-17 22:39:42 +02:00
|
|
|
# TODO: double check -e , --add-host, -v, --read-only
|
2019-08-09 15:31:56 +02:00
|
|
|
dirname = compose.dirname
|
2022-03-01 08:53:24 +01:00
|
|
|
pod = cnt.get("pod", None) or ""
|
|
|
|
name = cnt["name"]
|
|
|
|
podman_args = [f"--name={name}"]
|
2019-03-23 20:42:04 +01:00
|
|
|
|
2019-08-10 13:11:28 +02:00
|
|
|
if detached:
|
|
|
|
podman_args.append("-d")
|
|
|
|
|
2019-03-04 23:04:53 +01:00
|
|
|
if pod:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.append(f"--pod={pod}")
|
2022-02-15 00:33:00 +01:00
|
|
|
deps = []
|
2022-03-01 08:53:24 +01:00
|
|
|
for dep_srv in cnt.get("_deps", None) or []:
|
2022-02-15 00:33:00 +01:00
|
|
|
deps.extend(compose.container_names_by_service.get(dep_srv, None) or [])
|
|
|
|
if deps:
|
2022-02-26 00:49:34 +01:00
|
|
|
deps_csv = ",".join(deps)
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.append(f"--requires={deps_csv}")
|
2020-04-18 17:39:59 +02:00
|
|
|
sec = norm_as_list(cnt.get("security_opt", None))
|
2022-02-25 23:41:07 +01:00
|
|
|
for sec_item in sec:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--security-opt", sec_item])
|
2021-02-20 18:21:32 +01:00
|
|
|
ann = norm_as_list(cnt.get("annotations", None))
|
|
|
|
for a in ann:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--annotation", a])
|
|
|
|
if cnt.get("read_only", None):
|
|
|
|
podman_args.append("--read-only")
|
2024-03-09 18:10:09 +01:00
|
|
|
if cnt.get("http_proxy", None) is False:
|
|
|
|
podman_args.append("--http-proxy=false")
|
2022-03-01 08:53:24 +01:00
|
|
|
for i in cnt.get("labels", []):
|
|
|
|
podman_args.extend(["--label", i])
|
|
|
|
for c in cnt.get("cap_add", []):
|
|
|
|
podman_args.extend(["--cap-add", c])
|
|
|
|
for c in cnt.get("cap_drop", []):
|
|
|
|
podman_args.extend(["--cap-drop", c])
|
2023-03-28 17:27:33 +02:00
|
|
|
for item in cnt.get("group_add", []):
|
|
|
|
podman_args.extend(["--group-add", item])
|
2022-03-01 08:53:24 +01:00
|
|
|
for item in cnt.get("devices", []):
|
|
|
|
podman_args.extend(["--device", item])
|
|
|
|
for item in norm_as_list(cnt.get("dns", None)):
|
|
|
|
podman_args.extend(["--dns", item])
|
|
|
|
for item in norm_as_list(cnt.get("dns_opt", None)):
|
|
|
|
podman_args.extend(["--dns-opt", item])
|
|
|
|
for item in norm_as_list(cnt.get("dns_search", None)):
|
|
|
|
podman_args.extend(["--dns-search", item])
|
|
|
|
env_file = cnt.get("env_file", [])
|
|
|
|
if is_str(env_file):
|
|
|
|
env_file = [env_file]
|
2020-09-09 16:19:07 +02:00
|
|
|
for i in env_file:
|
2019-03-20 23:49:17 +01:00
|
|
|
i = os.path.realpath(os.path.join(dirname, i))
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--env-file", i])
|
|
|
|
env = norm_as_list(cnt.get("environment", {}))
|
2020-10-08 13:34:02 +02:00
|
|
|
for e in env:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["-e", e])
|
|
|
|
tmpfs_ls = cnt.get("tmpfs", [])
|
|
|
|
if is_str(tmpfs_ls):
|
|
|
|
tmpfs_ls = [tmpfs_ls]
|
2019-06-11 16:03:24 +02:00
|
|
|
for i in tmpfs_ls:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--tmpfs", i])
|
|
|
|
for volume in cnt.get("volumes", []):
|
2023-12-10 07:51:54 +01:00
|
|
|
podman_args.extend(await get_mount_args(compose, cnt, volume))
|
2022-01-12 14:44:47 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
await assert_cnt_nets(compose, cnt)
|
2022-01-18 19:40:29 +01:00
|
|
|
podman_args.extend(get_net_args(compose, cnt))
|
2022-01-12 14:44:47 +01:00
|
|
|
|
2023-08-09 01:12:10 +02:00
|
|
|
log_config = cnt.get("logging", None)
|
|
|
|
if log_config is not None:
|
|
|
|
podman_args.append(f'--log-driver={log_config.get("driver", "k8s-file")}')
|
|
|
|
log_opts = log_config.get("options") or {}
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args += [f"--log-opt={name}={value}" for name, value in log_opts.items()]
|
|
|
|
for secret in cnt.get("secrets", []):
|
2021-07-21 18:22:07 +02:00
|
|
|
podman_args.extend(get_secret_args(compose, cnt, secret))
|
2022-03-01 08:53:24 +01:00
|
|
|
for i in cnt.get("extra_hosts", []):
|
|
|
|
podman_args.extend(["--add-host", i])
|
|
|
|
for i in cnt.get("expose", []):
|
|
|
|
podman_args.extend(["--expose", i])
|
|
|
|
if cnt.get("publishall", None):
|
|
|
|
podman_args.append("-P")
|
|
|
|
ports = cnt.get("ports", None) or []
|
2021-08-26 11:37:14 +02:00
|
|
|
if isinstance(ports, str):
|
|
|
|
ports = [ports]
|
2021-10-09 23:43:01 +02:00
|
|
|
for port in ports:
|
|
|
|
if isinstance(port, dict):
|
|
|
|
port = port_dict_to_str(port)
|
|
|
|
elif not isinstance(port, str):
|
|
|
|
raise TypeError("port should be either string or dict")
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["-p", port])
|
2021-11-21 00:23:29 +01:00
|
|
|
|
2022-03-18 16:05:57 +01:00
|
|
|
userns_mode = cnt.get("userns_mode", None)
|
|
|
|
if userns_mode is not None:
|
|
|
|
podman_args.extend(["--userns", userns_mode])
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
user = cnt.get("user", None)
|
2019-03-04 10:30:14 +01:00
|
|
|
if user is not None:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["-u", user])
|
|
|
|
if cnt.get("working_dir", None) is not None:
|
|
|
|
podman_args.extend(["-w", cnt["working_dir"]])
|
|
|
|
if cnt.get("hostname", None):
|
|
|
|
podman_args.extend(["--hostname", cnt["hostname"]])
|
|
|
|
if cnt.get("shm_size", None):
|
|
|
|
podman_args.extend(["--shm-size", str(cnt["shm_size"])])
|
|
|
|
if cnt.get("stdin_open", None):
|
|
|
|
podman_args.append("-i")
|
|
|
|
if cnt.get("stop_signal", None):
|
|
|
|
podman_args.extend(["--stop-signal", cnt["stop_signal"]])
|
2023-09-09 08:38:23 +02:00
|
|
|
|
|
|
|
sysctls = cnt.get("sysctls")
|
|
|
|
if sysctls is not None:
|
|
|
|
if isinstance(sysctls, dict):
|
|
|
|
for sysctl, value in sysctls.items():
|
|
|
|
podman_args.extend(["--sysctl", "{}={}".format(sysctl, value)])
|
|
|
|
elif isinstance(sysctls, list):
|
|
|
|
for i in sysctls:
|
|
|
|
podman_args.extend(["--sysctl", i])
|
|
|
|
else:
|
|
|
|
raise TypeError("sysctls should be either dict or list")
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
if cnt.get("tty", None):
|
|
|
|
podman_args.append("--tty")
|
|
|
|
if cnt.get("privileged", None):
|
|
|
|
podman_args.append("--privileged")
|
2023-09-10 00:18:43 +02:00
|
|
|
if cnt.get("pid", None):
|
|
|
|
podman_args.extend(["--pid", cnt["pid"]])
|
2022-03-01 08:53:24 +01:00
|
|
|
pull_policy = cnt.get("pull_policy", None)
|
|
|
|
if pull_policy is not None and pull_policy != "build":
|
|
|
|
podman_args.extend(["--pull", pull_policy])
|
|
|
|
if cnt.get("restart", None) is not None:
|
|
|
|
podman_args.extend(["--restart", cnt["restart"]])
|
2020-02-27 10:30:53 +01:00
|
|
|
container_to_ulimit_args(cnt, podman_args)
|
2021-06-22 22:30:22 +02:00
|
|
|
container_to_res_args(cnt, podman_args)
|
2019-03-04 10:30:14 +01:00
|
|
|
# currently podman shipped by fedora does not package this
|
2022-03-01 08:53:24 +01:00
|
|
|
if cnt.get("init", None):
|
|
|
|
podman_args.append("--init")
|
|
|
|
if cnt.get("init-path", None):
|
|
|
|
podman_args.extend(["--init-path", cnt["init-path"]])
|
|
|
|
entrypoint = cnt.get("entrypoint", None)
|
2019-03-04 10:30:14 +01:00
|
|
|
if entrypoint is not None:
|
2019-05-09 22:15:05 +02:00
|
|
|
if is_str(entrypoint):
|
2021-10-24 23:14:19 +02:00
|
|
|
entrypoint = shlex.split(entrypoint)
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--entrypoint", json.dumps(entrypoint)])
|
2022-04-08 12:58:18 +02:00
|
|
|
platform = cnt.get("platform", None)
|
|
|
|
if platform is not None:
|
|
|
|
podman_args.extend(["--platform", platform])
|
2024-01-03 17:07:14 +01:00
|
|
|
if cnt.get("runtime", None):
|
|
|
|
podman_args.extend(["--runtime", cnt["runtime"]])
|
2022-07-14 15:24:43 +02:00
|
|
|
|
2019-07-08 22:53:38 +02:00
|
|
|
# WIP: healthchecks are still work in progress
|
2022-03-01 08:53:24 +01:00
|
|
|
healthcheck = cnt.get("healthcheck", None) or {}
|
2019-07-08 22:53:38 +02:00
|
|
|
if not is_dict(healthcheck):
|
2022-09-09 22:21:14 +02:00
|
|
|
raise ValueError("'healthcheck' must be a key-value mapping")
|
2022-07-14 15:24:43 +02:00
|
|
|
healthcheck_disable = healthcheck.get("disable", False)
|
2022-03-01 08:53:24 +01:00
|
|
|
healthcheck_test = healthcheck.get("test", None)
|
2022-07-14 15:24:43 +02:00
|
|
|
if healthcheck_disable:
|
|
|
|
healthcheck_test = ["NONE"]
|
2019-07-08 22:53:38 +02:00
|
|
|
if healthcheck_test:
|
2019-08-10 17:08:21 +02:00
|
|
|
# If it's a string, it's equivalent to specifying CMD-SHELL
|
2019-07-08 22:53:38 +02:00
|
|
|
if is_str(healthcheck_test):
|
|
|
|
# podman does not add shell to handle command with whitespace
|
2024-03-07 17:28:06 +01:00
|
|
|
podman_args.extend([
|
|
|
|
"--healthcheck-command",
|
|
|
|
"/bin/sh -c " + cmd_quote(healthcheck_test),
|
|
|
|
])
|
2019-07-08 22:53:38 +02:00
|
|
|
elif is_list(healthcheck_test):
|
2022-01-15 04:51:30 +01:00
|
|
|
healthcheck_test = healthcheck_test.copy()
|
2019-08-10 17:08:21 +02:00
|
|
|
# If it's a list, first item is either NONE, CMD or CMD-SHELL.
|
2019-07-08 22:53:38 +02:00
|
|
|
healthcheck_type = healthcheck_test.pop(0)
|
2022-03-01 08:53:24 +01:00
|
|
|
if healthcheck_type == "NONE":
|
2019-08-09 15:31:56 +02:00
|
|
|
podman_args.append("--no-healthcheck")
|
2022-03-01 08:53:24 +01:00
|
|
|
elif healthcheck_type == "CMD":
|
|
|
|
cmd_q = "' '".join([cmd_quote(i) for i in healthcheck_test])
|
|
|
|
podman_args.extend(["--healthcheck-command", "/bin/sh -c " + cmd_q])
|
|
|
|
elif healthcheck_type == "CMD-SHELL":
|
|
|
|
if len(healthcheck_test) != 1:
|
2019-07-08 22:53:38 +02:00
|
|
|
raise ValueError("'CMD_SHELL' takes a single string after it")
|
2022-02-26 00:49:34 +01:00
|
|
|
cmd_q = cmd_quote(healthcheck_test[0])
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--healthcheck-command", "/bin/sh -c " + cmd_q])
|
2019-07-08 22:53:38 +02:00
|
|
|
else:
|
|
|
|
raise ValueError(
|
2022-02-26 00:49:34 +01:00
|
|
|
f"unknown healthcheck test type [{healthcheck_type}],\
|
2019-07-08 22:53:38 +02:00
|
|
|
expecting NONE, CMD or CMD-SHELL."
|
|
|
|
)
|
2019-07-08 13:47:27 +02:00
|
|
|
else:
|
2019-07-08 22:53:38 +02:00
|
|
|
raise ValueError("'healthcheck.test' either a string or a list")
|
|
|
|
|
|
|
|
# interval, timeout and start_period are specified as durations.
|
2022-03-01 08:53:24 +01:00
|
|
|
if "interval" in healthcheck:
|
|
|
|
podman_args.extend(["--healthcheck-interval", healthcheck["interval"]])
|
|
|
|
if "timeout" in healthcheck:
|
|
|
|
podman_args.extend(["--healthcheck-timeout", healthcheck["timeout"]])
|
|
|
|
if "start_period" in healthcheck:
|
|
|
|
podman_args.extend(["--healthcheck-start-period", healthcheck["start_period"]])
|
2019-07-08 22:53:38 +02:00
|
|
|
|
|
|
|
# convert other parameters to string
|
2022-03-01 08:53:24 +01:00
|
|
|
if "retries" in healthcheck:
|
|
|
|
podman_args.extend(["--healthcheck-retries", str(healthcheck["retries"])])
|
2019-07-08 13:47:27 +02:00
|
|
|
|
2022-06-04 15:31:48 +02:00
|
|
|
# handle podman extension
|
|
|
|
x_podman = cnt.get("x-podman", None)
|
|
|
|
if x_podman is not None:
|
|
|
|
for uidmap in x_podman.get("uidmaps", []):
|
|
|
|
podman_args.extend(["--uidmap", uidmap])
|
|
|
|
for gidmap in x_podman.get("gidmaps", []):
|
|
|
|
podman_args.extend(["--gidmap", gidmap])
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.append(cnt["image"]) # command, ..etc.
|
|
|
|
command = cnt.get("command", None)
|
2019-03-04 10:30:14 +01:00
|
|
|
if command is not None:
|
2019-07-08 08:12:25 +02:00
|
|
|
if is_str(command):
|
2019-10-04 19:36:30 +02:00
|
|
|
podman_args.extend(shlex.split(command))
|
2019-07-08 08:12:25 +02:00
|
|
|
else:
|
2021-11-11 16:47:26 +01:00
|
|
|
podman_args.extend([str(i) for i in command])
|
2019-08-09 15:31:56 +02:00
|
|
|
return podman_args
|
2019-03-04 10:30:14 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-10-05 21:37:14 +02:00
|
|
|
def rec_deps(services, service_name, start_point=None):
|
|
|
|
"""
|
|
|
|
return all dependencies of service_name recursively
|
|
|
|
"""
|
|
|
|
if not start_point:
|
|
|
|
start_point = service_name
|
|
|
|
deps = services[service_name]["_deps"]
|
|
|
|
for dep_name in deps.copy():
|
2019-11-07 18:09:32 +01:00
|
|
|
# avoid A depens on A
|
2020-04-18 17:39:59 +02:00
|
|
|
if dep_name == service_name:
|
2019-11-07 18:09:32 +01:00
|
|
|
continue
|
2020-04-18 17:39:59 +02:00
|
|
|
dep_srv = services.get(dep_name, None)
|
2019-10-05 21:37:14 +02:00
|
|
|
if not dep_srv:
|
2019-03-23 20:42:04 +01:00
|
|
|
continue
|
2019-10-05 21:37:14 +02:00
|
|
|
# NOTE: avoid creating loops, A->B->A
|
|
|
|
if start_point and start_point in dep_srv["_deps"]:
|
|
|
|
continue
|
|
|
|
new_deps = rec_deps(services, dep_name, start_point)
|
|
|
|
deps.update(new_deps)
|
2019-03-09 22:25:32 +01:00
|
|
|
return deps
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-10-05 21:37:14 +02:00
|
|
|
def flat_deps(services, with_extends=False):
|
|
|
|
"""
|
|
|
|
create dependencies "_deps" or update it recursively for all services
|
|
|
|
"""
|
|
|
|
for name, srv in services.items():
|
|
|
|
deps = set()
|
2019-10-25 09:49:51 +02:00
|
|
|
srv["_deps"] = deps
|
2019-10-05 21:37:14 +02:00
|
|
|
if with_extends:
|
|
|
|
ext = srv.get("extends", {}).get("service", None)
|
|
|
|
if ext:
|
2022-03-01 08:53:24 +01:00
|
|
|
if ext != name:
|
|
|
|
deps.add(ext)
|
2019-10-05 21:37:14 +02:00
|
|
|
continue
|
2021-11-14 00:37:22 +01:00
|
|
|
deps_ls = srv.get("depends_on", None) or []
|
2022-03-01 08:53:24 +01:00
|
|
|
if is_str(deps_ls):
|
|
|
|
deps_ls = [deps_ls]
|
|
|
|
elif is_dict(deps_ls):
|
|
|
|
deps_ls = list(deps_ls.keys())
|
2021-11-14 00:37:22 +01:00
|
|
|
deps.update(deps_ls)
|
2019-10-05 21:37:14 +02:00
|
|
|
# parse link to get service name and remove alias
|
2021-11-14 00:37:22 +01:00
|
|
|
links_ls = srv.get("links", None) or []
|
2022-03-01 08:53:24 +01:00
|
|
|
if not is_list(links_ls):
|
|
|
|
links_ls = [links_ls]
|
|
|
|
deps.update([(c.split(":")[0] if ":" in c else c) for c in links_ls])
|
2023-07-15 23:25:03 +02:00
|
|
|
for c in links_ls:
|
|
|
|
if ":" in c:
|
|
|
|
dep_name, dep_alias = c.split(":")
|
2024-03-08 22:46:21 +01:00
|
|
|
if "_aliases" not in services[dep_name]:
|
2023-07-15 23:25:03 +02:00
|
|
|
services[dep_name]["_aliases"] = set()
|
|
|
|
services[dep_name]["_aliases"].add(dep_alias)
|
2019-10-05 21:37:14 +02:00
|
|
|
for name, srv in services.items():
|
|
|
|
rec_deps(services, name)
|
2019-03-09 22:25:32 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2024-02-25 22:34:41 +01:00
|
|
|
async def wait_with_timeout(coro, timeout):
|
|
|
|
"""
|
|
|
|
Asynchronously waits for the given coroutine to complete with a timeout.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
coro: The coroutine to wait for.
|
|
|
|
timeout (int or float): The maximum number of seconds to wait for.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
TimeoutError: If the coroutine does not complete within the specified timeout.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return await asyncio.wait_for(coro, timeout)
|
|
|
|
except asyncio.TimeoutError as exc:
|
|
|
|
raise TimeoutError from exc
|
|
|
|
|
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
###################
|
|
|
|
# podman and compose classes
|
|
|
|
###################
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
class Podman:
|
2024-03-07 17:28:06 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
compose,
|
|
|
|
podman_path="podman",
|
|
|
|
dry_run=False,
|
|
|
|
semaphore: asyncio.Semaphore = asyncio.Semaphore(sys.maxsize),
|
|
|
|
):
|
2019-08-09 15:31:56 +02:00
|
|
|
self.compose = compose
|
|
|
|
self.podman_path = podman_path
|
|
|
|
self.dry_run = dry_run
|
2023-12-11 08:46:20 +01:00
|
|
|
self.semaphore = semaphore
|
2019-11-17 08:09:41 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def output(self, podman_args, cmd="", cmd_args=None):
|
2023-12-11 08:46:20 +01:00
|
|
|
async with self.semaphore:
|
|
|
|
cmd_args = cmd_args or []
|
|
|
|
xargs = self.compose.get_podman_args(cmd) if cmd else []
|
|
|
|
cmd_ls = [self.podman_path, *podman_args, cmd] + xargs + cmd_args
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info(str(cmd_ls))
|
2024-03-09 16:14:34 +01:00
|
|
|
p = await asyncio.create_subprocess_exec(
|
2024-03-07 17:28:06 +01:00
|
|
|
*cmd_ls, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
2023-12-11 08:46:20 +01:00
|
|
|
)
|
2023-12-10 07:51:54 +01:00
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
stdout_data, stderr_data = await p.communicate()
|
|
|
|
if p.returncode == 0:
|
|
|
|
return stdout_data
|
2024-03-09 16:14:34 +01:00
|
|
|
|
|
|
|
raise subprocess.CalledProcessError(p.returncode, " ".join(cmd_ls), stderr_data)
|
2021-05-17 14:03:47 +02:00
|
|
|
|
2022-03-18 13:50:24 +01:00
|
|
|
def exec(
|
|
|
|
self,
|
|
|
|
podman_args,
|
|
|
|
cmd="",
|
|
|
|
cmd_args=None,
|
|
|
|
):
|
|
|
|
cmd_args = list(map(str, cmd_args or []))
|
|
|
|
xargs = self.compose.get_podman_args(cmd) if cmd else []
|
|
|
|
cmd_ls = [self.podman_path, *podman_args, cmd] + xargs + cmd_args
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info(" ".join([str(i) for i in cmd_ls]))
|
2022-03-18 13:50:24 +01:00
|
|
|
os.execlp(self.podman_path, *cmd_ls)
|
|
|
|
|
2024-03-09 16:14:34 +01:00
|
|
|
async def run( # pylint: disable=dangerous-default-value
|
2022-03-01 08:53:24 +01:00
|
|
|
self,
|
|
|
|
podman_args,
|
|
|
|
cmd="",
|
|
|
|
cmd_args=None,
|
|
|
|
log_formatter=None,
|
2023-12-10 07:51:54 +01:00
|
|
|
*,
|
|
|
|
# Intentionally mutable default argument to hold references to tasks
|
2024-03-07 17:28:06 +01:00
|
|
|
task_reference=set(),
|
2023-12-11 08:46:20 +01:00
|
|
|
) -> int:
|
|
|
|
async with self.semaphore:
|
|
|
|
cmd_args = list(map(str, cmd_args or []))
|
|
|
|
xargs = self.compose.get_podman_args(cmd) if cmd else []
|
|
|
|
cmd_ls = [self.podman_path, *podman_args, cmd] + xargs + cmd_args
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info(" ".join([str(i) for i in cmd_ls]))
|
2023-12-11 08:46:20 +01:00
|
|
|
if self.dry_run:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if log_formatter is not None:
|
|
|
|
|
|
|
|
async def format_out(stdout):
|
|
|
|
while True:
|
2024-03-08 22:46:23 +01:00
|
|
|
line = await stdout.readline()
|
|
|
|
if line:
|
|
|
|
print(log_formatter, line.decode('utf-8'), end='')
|
2023-12-11 08:46:20 +01:00
|
|
|
if stdout.at_eof():
|
|
|
|
break
|
|
|
|
|
2024-03-09 16:14:34 +01:00
|
|
|
p = await asyncio.create_subprocess_exec(
|
2023-12-11 08:46:20 +01:00
|
|
|
*cmd_ls, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
|
|
|
) # pylint: disable=consider-using-with
|
|
|
|
|
|
|
|
# This is hacky to make the tasks not get garbage collected
|
|
|
|
# https://github.com/python/cpython/issues/91887
|
|
|
|
out_t = asyncio.create_task(format_out(p.stdout))
|
|
|
|
task_reference.add(out_t)
|
|
|
|
out_t.add_done_callback(task_reference.discard)
|
|
|
|
|
|
|
|
err_t = asyncio.create_task(format_out(p.stderr))
|
|
|
|
task_reference.add(err_t)
|
|
|
|
err_t.add_done_callback(task_reference.discard)
|
|
|
|
|
|
|
|
else:
|
2024-03-09 16:14:34 +01:00
|
|
|
p = await asyncio.create_subprocess_exec(*cmd_ls) # pylint: disable=consider-using-with
|
2022-02-03 21:50:12 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
try:
|
|
|
|
exit_code = await p.wait()
|
2024-03-08 22:46:22 +01:00
|
|
|
except asyncio.CancelledError:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info("Sending termination signal")
|
2023-12-10 07:51:54 +01:00
|
|
|
p.terminate()
|
2023-12-11 08:46:20 +01:00
|
|
|
try:
|
2024-02-25 22:34:41 +01:00
|
|
|
exit_code = await wait_with_timeout(p.wait(), 10)
|
2023-12-11 08:46:20 +01:00
|
|
|
except TimeoutError:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.warning("container did not shut down after 10 seconds, killing")
|
2023-12-11 08:46:20 +01:00
|
|
|
p.kill()
|
|
|
|
exit_code = await p.wait()
|
|
|
|
|
2023-08-09 01:12:10 +02:00
|
|
|
log.info("exit code: %s", exit_code)
|
2023-12-10 07:51:54 +01:00
|
|
|
return exit_code
|
2021-09-06 06:45:50 +02:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def volume_ls(self, proj=None):
|
2021-12-21 21:44:34 +01:00
|
|
|
if not proj:
|
|
|
|
proj = self.compose.project_name
|
2024-03-07 17:28:06 +01:00
|
|
|
output = (
|
|
|
|
await self.output(
|
|
|
|
[],
|
|
|
|
"volume",
|
|
|
|
[
|
|
|
|
"ls",
|
|
|
|
"--noheading",
|
|
|
|
"--filter",
|
|
|
|
f"label=io.podman.compose.project={proj}",
|
|
|
|
"--format",
|
|
|
|
"{{.Name}}",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
).decode("utf-8")
|
2022-01-06 15:54:37 +01:00
|
|
|
volumes = output.splitlines()
|
|
|
|
return volumes
|
2021-11-22 19:34:40 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
def normalize_service(service, sub_dir=""):
|
2023-05-06 07:00:46 +02:00
|
|
|
if "build" in service:
|
|
|
|
build = service["build"]
|
|
|
|
if is_str(build):
|
|
|
|
service["build"] = {"context": build}
|
2022-03-01 08:53:24 +01:00
|
|
|
if sub_dir and "build" in service:
|
|
|
|
build = service["build"]
|
2023-05-06 07:00:46 +02:00
|
|
|
context = build.get("context", None) or ""
|
2022-02-25 16:32:19 +01:00
|
|
|
if context or sub_dir:
|
2022-03-01 08:53:24 +01:00
|
|
|
if context.startswith("./"):
|
2022-02-25 16:32:19 +01:00
|
|
|
context = context[2:]
|
|
|
|
if sub_dir:
|
|
|
|
context = os.path.join(sub_dir, context)
|
2022-03-01 08:53:24 +01:00
|
|
|
context = context.rstrip("/")
|
2022-02-25 16:32:19 +01:00
|
|
|
if not context:
|
2022-03-01 08:53:24 +01:00
|
|
|
context = "."
|
2023-05-06 07:00:46 +02:00
|
|
|
service["build"]["context"] = context
|
2023-05-06 07:56:15 +02:00
|
|
|
for key in ("command", "entrypoint"):
|
|
|
|
if key in service:
|
|
|
|
if is_str(service[key]):
|
|
|
|
service[key] = shlex.split(service[key])
|
2021-12-11 00:50:40 +01:00
|
|
|
for key in ("env_file", "security_opt", "volumes"):
|
2022-03-01 08:53:24 +01:00
|
|
|
if key not in service:
|
|
|
|
continue
|
|
|
|
if is_str(service[key]):
|
|
|
|
service[key] = [service[key]]
|
2021-12-11 00:50:40 +01:00
|
|
|
if "security_opt" in service:
|
|
|
|
sec_ls = service["security_opt"]
|
|
|
|
for ix, item in enumerate(sec_ls):
|
2022-03-01 08:53:24 +01:00
|
|
|
if item in ("seccomp:unconfined", "apparmor:unconfined"):
|
2021-12-11 00:50:40 +01:00
|
|
|
sec_ls[ix] = item.replace(":", "=")
|
2019-10-05 21:37:14 +02:00
|
|
|
for key in ("environment", "labels"):
|
2022-03-01 08:53:24 +01:00
|
|
|
if key not in service:
|
|
|
|
continue
|
2019-10-05 21:37:14 +02:00
|
|
|
service[key] = norm_as_dict(service[key])
|
|
|
|
if "extends" in service:
|
|
|
|
extends = service["extends"]
|
|
|
|
if is_str(extends):
|
|
|
|
extends = {"service": extends}
|
|
|
|
service["extends"] = extends
|
|
|
|
return service
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-09-08 01:20:48 +02:00
|
|
|
def normalize(compose):
|
|
|
|
"""
|
|
|
|
convert compose dict of some keys from string or dicts into arrays
|
|
|
|
"""
|
|
|
|
services = compose.get("services", None) or {}
|
2022-02-25 22:48:42 +01:00
|
|
|
for service in services.values():
|
2019-10-05 21:37:14 +02:00
|
|
|
normalize_service(service)
|
2019-09-08 01:20:48 +02:00
|
|
|
return compose
|
|
|
|
|
2022-03-28 12:40:02 +02:00
|
|
|
|
2023-04-30 05:37:23 +02:00
|
|
|
def normalize_service_final(service: dict, project_dir: str) -> dict:
|
|
|
|
if "build" in service:
|
|
|
|
build = service["build"]
|
|
|
|
context = build if is_str(build) else build.get("context", ".")
|
2023-05-01 08:41:29 +02:00
|
|
|
context = os.path.normpath(os.path.join(project_dir, context))
|
|
|
|
dockerfile = (
|
2024-03-07 17:28:06 +01:00
|
|
|
"Dockerfile" if is_str(build) else service["build"].get("dockerfile", "Dockerfile")
|
2023-05-01 08:41:29 +02:00
|
|
|
)
|
2023-04-30 05:37:23 +02:00
|
|
|
if not is_dict(service["build"]):
|
|
|
|
service["build"] = {}
|
|
|
|
service["build"]["dockerfile"] = dockerfile
|
|
|
|
service["build"]["context"] = context
|
|
|
|
return service
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_final(compose: dict, project_dir: str) -> dict:
|
|
|
|
services = compose.get("services", None) or {}
|
|
|
|
for service in services.values():
|
|
|
|
normalize_service_final(service, project_dir)
|
|
|
|
return compose
|
|
|
|
|
|
|
|
|
2022-03-28 12:32:22 +02:00
|
|
|
def clone(value):
|
|
|
|
return value.copy() if is_list(value) or is_dict(value) else value
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-03-28 12:40:02 +02:00
|
|
|
|
2019-10-05 21:37:14 +02:00
|
|
|
def rec_merge_one(target, source):
|
2019-09-08 01:20:48 +02:00
|
|
|
"""
|
2019-10-05 21:37:14 +02:00
|
|
|
update target from source recursively
|
2019-09-08 01:20:48 +02:00
|
|
|
"""
|
|
|
|
done = set()
|
|
|
|
for key, value in source.items():
|
2022-03-01 08:53:24 +01:00
|
|
|
if key in target:
|
|
|
|
continue
|
2023-05-06 07:00:46 +02:00
|
|
|
target[key] = clone(value)
|
2019-09-08 01:20:48 +02:00
|
|
|
done.add(key)
|
|
|
|
for key, value in target.items():
|
2022-03-01 08:53:24 +01:00
|
|
|
if key in done:
|
|
|
|
continue
|
|
|
|
if key not in source:
|
|
|
|
continue
|
2019-09-08 01:20:48 +02:00
|
|
|
value2 = source[key]
|
2023-05-06 07:56:15 +02:00
|
|
|
if key in ("command", "entrypoint"):
|
2022-03-28 12:32:22 +02:00
|
|
|
target[key] = clone(value2)
|
2022-03-18 16:29:27 +01:00
|
|
|
continue
|
2022-03-02 02:54:19 +01:00
|
|
|
if not isinstance(value2, type(value)):
|
2022-02-26 00:49:34 +01:00
|
|
|
value_type = type(value)
|
|
|
|
value2_type = type(value2)
|
2024-03-07 17:28:06 +01:00
|
|
|
raise ValueError(f"can't merge value of [{key}] of type {value_type} and {value2_type}")
|
2019-10-05 21:37:14 +02:00
|
|
|
if is_list(value2):
|
2022-03-01 08:53:24 +01:00
|
|
|
if key == "volumes":
|
2021-12-10 01:06:43 +01:00
|
|
|
# clean duplicate mount targets
|
2022-06-05 19:44:50 +02:00
|
|
|
pts = {v.split(":", 2)[1] for v in value2 if ":" in v}
|
2022-03-01 08:53:24 +01:00
|
|
|
del_ls = [
|
2024-03-07 17:28:06 +01:00
|
|
|
ix for (ix, v) in enumerate(value) if ":" in v and v.split(":", 2)[1] in pts
|
2022-03-01 08:53:24 +01:00
|
|
|
]
|
2021-12-10 01:06:43 +01:00
|
|
|
for ix in reversed(del_ls):
|
|
|
|
del value[ix]
|
|
|
|
value.extend(value2)
|
|
|
|
else:
|
|
|
|
value.extend(value2)
|
2019-09-08 01:20:48 +02:00
|
|
|
elif is_dict(value2):
|
2019-10-05 21:37:14 +02:00
|
|
|
rec_merge_one(value, value2)
|
2019-09-08 01:20:48 +02:00
|
|
|
else:
|
2022-03-01 08:53:24 +01:00
|
|
|
target[key] = value2
|
2019-09-08 01:20:48 +02:00
|
|
|
return target
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-10-05 21:37:14 +02:00
|
|
|
def rec_merge(target, *sources):
|
|
|
|
"""
|
|
|
|
update target recursively from sources
|
|
|
|
"""
|
|
|
|
for source in sources:
|
|
|
|
ret = rec_merge_one(target, source)
|
|
|
|
return ret
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-11-13 22:27:43 +01:00
|
|
|
def resolve_extends(services, service_names, environ):
|
2019-10-05 21:37:14 +02:00
|
|
|
for name in service_names:
|
|
|
|
service = services[name]
|
|
|
|
ext = service.get("extends", {})
|
2022-03-01 08:53:24 +01:00
|
|
|
if is_str(ext):
|
|
|
|
ext = {"service": ext}
|
2019-10-05 21:37:14 +02:00
|
|
|
from_service_name = ext.get("service", None)
|
2022-03-01 08:53:24 +01:00
|
|
|
if not from_service_name:
|
|
|
|
continue
|
2019-10-05 21:37:14 +02:00
|
|
|
filename = ext.get("file", None)
|
|
|
|
if filename:
|
2022-03-01 08:53:24 +01:00
|
|
|
if filename.startswith("./"):
|
2022-02-25 16:32:19 +01:00
|
|
|
filename = filename[2:]
|
2022-03-01 08:53:24 +01:00
|
|
|
with open(filename, "r", encoding="utf-8") as f:
|
2019-10-05 21:37:14 +02:00
|
|
|
content = yaml.safe_load(f) or {}
|
|
|
|
if "services" in content:
|
|
|
|
content = content["services"]
|
2022-02-25 16:32:19 +01:00
|
|
|
subdirectory = os.path.dirname(filename)
|
2021-11-13 22:27:43 +01:00
|
|
|
content = rec_subs(content, environ)
|
2023-07-26 16:29:39 +02:00
|
|
|
from_service = content.get(from_service_name, {}) or {}
|
2022-02-20 21:53:24 +01:00
|
|
|
normalize_service(from_service, subdirectory)
|
2019-10-05 21:37:14 +02:00
|
|
|
else:
|
|
|
|
from_service = services.get(from_service_name, {}).copy()
|
|
|
|
del from_service["_deps"]
|
2019-10-25 09:49:51 +02:00
|
|
|
try:
|
|
|
|
del from_service["extends"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2019-10-05 21:37:14 +02:00
|
|
|
new_service = rec_merge({}, from_service, service)
|
|
|
|
services[name] = new_service
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-12-10 00:01:45 +01:00
|
|
|
def dotenv_to_dict(dotenv_path):
|
|
|
|
if not os.path.isfile(dotenv_path):
|
|
|
|
return {}
|
|
|
|
return dotenv_values(dotenv_path)
|
2019-10-05 21:37:14 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-12-10 22:26:13 +01:00
|
|
|
COMPOSE_DEFAULT_LS = [
|
|
|
|
"compose.yaml",
|
|
|
|
"compose.yml",
|
|
|
|
"compose.override.yaml",
|
|
|
|
"compose.override.yml",
|
|
|
|
"podman-compose.yaml",
|
|
|
|
"podman-compose.yml",
|
|
|
|
"docker-compose.yml",
|
|
|
|
"docker-compose.yaml",
|
|
|
|
"docker-compose.override.yml",
|
|
|
|
"docker-compose.override.yaml",
|
|
|
|
"container-compose.yml",
|
|
|
|
"container-compose.yaml",
|
|
|
|
"container-compose.override.yml",
|
|
|
|
"container-compose.override.yaml",
|
|
|
|
]
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
class PodmanCompose:
|
|
|
|
def __init__(self):
|
2022-02-25 22:48:42 +01:00
|
|
|
self.podman = None
|
2021-11-13 12:08:32 +01:00
|
|
|
self.podman_version = None
|
2022-02-25 22:48:42 +01:00
|
|
|
self.environ = {}
|
2021-05-06 01:08:48 +02:00
|
|
|
self.exit_code = None
|
2019-08-09 15:31:56 +02:00
|
|
|
self.commands = {}
|
|
|
|
self.global_args = None
|
|
|
|
self.project_name = None
|
|
|
|
self.dirname = None
|
|
|
|
self.pods = None
|
|
|
|
self.containers = None
|
2021-10-14 00:30:44 +02:00
|
|
|
self.vols = None
|
2021-11-21 00:23:29 +01:00
|
|
|
self.networks = {}
|
|
|
|
self.default_net = "default"
|
2021-07-21 18:22:07 +02:00
|
|
|
self.declared_secrets = None
|
2019-08-09 15:31:56 +02:00
|
|
|
self.container_names_by_service = None
|
2019-08-10 13:11:28 +02:00
|
|
|
self.container_by_name = None
|
2022-02-25 22:48:42 +01:00
|
|
|
self.services = None
|
2022-02-25 19:13:10 +01:00
|
|
|
self.all_services = set()
|
2022-02-25 21:59:15 +01:00
|
|
|
self.prefer_volume_over_mount = True
|
2022-02-25 22:48:42 +01:00
|
|
|
self.merged_yaml = None
|
2022-03-01 08:53:24 +01:00
|
|
|
self.yaml_hash = ""
|
|
|
|
self.console_colors = [
|
2024-03-07 17:28:06 +01:00
|
|
|
"\x1b[1;32m",
|
|
|
|
"\x1b[1;33m",
|
|
|
|
"\x1b[1;34m",
|
|
|
|
"\x1b[1;35m",
|
|
|
|
"\x1b[1;36m",
|
2022-03-01 08:53:24 +01:00
|
|
|
]
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-02-25 19:13:10 +01:00
|
|
|
def assert_services(self, services):
|
|
|
|
if is_str(services):
|
|
|
|
services = [services]
|
|
|
|
given = set(services or [])
|
|
|
|
missing = given - self.all_services
|
|
|
|
if missing:
|
|
|
|
missing_csv = ",".join(missing)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.warning("missing services [%s]", missing_csv)
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(1)
|
2022-02-25 19:13:10 +01:00
|
|
|
|
2021-05-17 14:03:47 +02:00
|
|
|
def get_podman_args(self, cmd):
|
|
|
|
xargs = []
|
|
|
|
for args in self.global_args.podman_args:
|
|
|
|
xargs.extend(shlex.split(args))
|
2022-03-01 08:53:24 +01:00
|
|
|
cmd_norm = cmd if cmd != "create" else "run"
|
2021-05-17 14:03:47 +02:00
|
|
|
cmd_args = self.global_args.__dict__.get(f"podman_{cmd_norm}_args", None) or []
|
|
|
|
for args in cmd_args:
|
|
|
|
xargs.extend(shlex.split(args))
|
|
|
|
return xargs
|
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def run(self):
|
2023-08-09 01:12:10 +02:00
|
|
|
log.info("podman-compose version: %s", __version__)
|
2019-08-09 15:31:56 +02:00
|
|
|
args = self._parse_args()
|
2019-08-10 17:08:21 +02:00
|
|
|
podman_path = args.podman_path
|
2022-03-01 08:53:24 +01:00
|
|
|
if podman_path != "podman":
|
2019-08-09 15:31:56 +02:00
|
|
|
if os.path.isfile(podman_path) and os.access(podman_path, os.X_OK):
|
|
|
|
podman_path = os.path.realpath(podman_path)
|
|
|
|
else:
|
|
|
|
# this also works if podman hasn't been installed now
|
2022-02-25 23:41:07 +01:00
|
|
|
if args.dry_run is False:
|
2023-08-09 01:12:10 +02:00
|
|
|
log.fatal("Binary %s has not been found.", podman_path)
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(1)
|
2023-12-11 08:46:20 +01:00
|
|
|
self.podman = Podman(self, podman_path, args.dry_run, asyncio.Semaphore(args.parallel))
|
|
|
|
|
2020-04-18 21:15:55 +02:00
|
|
|
if not args.dry_run:
|
|
|
|
# just to make sure podman is running
|
|
|
|
try:
|
2024-03-07 17:28:06 +01:00
|
|
|
self.podman_version = (await self.podman.output(["--version"], "", [])).decode(
|
|
|
|
"utf-8"
|
|
|
|
).strip() or ""
|
2021-10-24 16:35:36 +02:00
|
|
|
self.podman_version = (self.podman_version.split() or [""])[-1]
|
2020-04-18 21:15:55 +02:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.podman_version = None
|
|
|
|
if not self.podman_version:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.fatal("it seems that you do not have `podman` installed")
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(1)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.info("using podman version: %s", self.podman_version)
|
2019-08-10 17:08:21 +02:00
|
|
|
cmd_name = args.command
|
2022-03-12 21:17:26 +01:00
|
|
|
compose_required = cmd_name != "version" and (
|
|
|
|
cmd_name != "systemd" or args.action != "create-unit"
|
|
|
|
)
|
2022-03-12 21:08:24 +01:00
|
|
|
if compose_required:
|
2020-09-28 22:27:29 +02:00
|
|
|
self._parse_compose_file()
|
2019-08-09 15:31:56 +02:00
|
|
|
cmd = self.commands[cmd_name]
|
2023-12-10 07:51:54 +01:00
|
|
|
retcode = await cmd(self, args)
|
2023-04-12 19:22:14 +02:00
|
|
|
if isinstance(retcode, int):
|
|
|
|
sys.exit(retcode)
|
2019-08-09 15:31:56 +02:00
|
|
|
|
|
|
|
def _parse_compose_file(self):
|
|
|
|
args = self.global_args
|
2022-02-25 22:48:42 +01:00
|
|
|
# cmd = args.command
|
2022-03-12 21:08:24 +01:00
|
|
|
dirname = os.environ.get("COMPOSE_PROJECT_DIR", None)
|
|
|
|
if dirname and os.path.isdir(dirname):
|
|
|
|
os.chdir(dirname)
|
2021-12-10 22:26:13 +01:00
|
|
|
pathsep = os.environ.get("COMPOSE_PATH_SEPARATOR", None) or os.pathsep
|
2019-09-08 01:20:48 +02:00
|
|
|
if not args.file:
|
2021-12-10 22:26:13 +01:00
|
|
|
default_str = os.environ.get("COMPOSE_FILE", None)
|
|
|
|
if default_str:
|
|
|
|
default_ls = default_str.split(pathsep)
|
|
|
|
else:
|
|
|
|
default_ls = COMPOSE_DEFAULT_LS
|
|
|
|
args.file = list(filter(os.path.exists, default_ls))
|
2019-09-08 01:20:48 +02:00
|
|
|
files = args.file
|
|
|
|
if not files:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.fatal(
|
2024-03-08 22:46:24 +01:00
|
|
|
"no compose.yaml, docker-compose.yml or container-compose.yml file found, "
|
|
|
|
"pass files with -f"
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(-1)
|
2019-09-08 01:20:48 +02:00
|
|
|
ex = map(os.path.exists, files)
|
2022-03-01 08:53:24 +01:00
|
|
|
missing = [fn0 for ex0, fn0 in zip(ex, files) if not ex0]
|
2019-09-08 01:20:48 +02:00
|
|
|
if missing:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.fatal("missing files: %s", missing)
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(1)
|
2019-09-08 01:20:48 +02:00
|
|
|
# make absolute
|
2021-03-03 16:30:26 +01:00
|
|
|
relative_files = files
|
2019-09-08 01:20:48 +02:00
|
|
|
files = list(map(os.path.realpath, files))
|
|
|
|
filename = files[0]
|
2019-08-09 15:31:56 +02:00
|
|
|
project_name = args.project_name
|
2022-02-25 22:48:42 +01:00
|
|
|
# no_ansi = args.no_ansi
|
|
|
|
# no_cleanup = args.no_cleanup
|
|
|
|
# dry_run = args.dry_run
|
|
|
|
# host_env = None
|
2021-12-10 21:45:49 +01:00
|
|
|
dirname = os.path.realpath(os.path.dirname(filename))
|
2019-08-09 15:31:56 +02:00
|
|
|
dir_basename = os.path.basename(dirname)
|
|
|
|
self.dirname = dirname
|
|
|
|
|
2023-04-07 22:03:28 +02:00
|
|
|
# env-file is relative to the CWD
|
|
|
|
dotenv_dict = {}
|
|
|
|
if args.env_file:
|
|
|
|
dotenv_path = os.path.realpath(args.env_file)
|
|
|
|
dotenv_dict = dotenv_to_dict(dotenv_path)
|
|
|
|
|
2023-04-10 11:46:47 +02:00
|
|
|
# TODO: remove next line
|
|
|
|
os.chdir(dirname)
|
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
os.environ.update({
|
|
|
|
key: value for key, value in dotenv_dict.items() if key.startswith("PODMAN_")
|
|
|
|
})
|
2022-03-12 21:08:24 +01:00
|
|
|
self.environ = dict(os.environ)
|
|
|
|
self.environ.update(dotenv_dict)
|
2020-11-04 23:08:19 +01:00
|
|
|
# see: https://docs.docker.com/compose/reference/envvars/
|
|
|
|
# see: https://docs.docker.com/compose/env-file/
|
2024-03-07 17:28:06 +01:00
|
|
|
self.environ.update({
|
|
|
|
"COMPOSE_PROJECT_DIR": dirname,
|
|
|
|
"COMPOSE_FILE": pathsep.join(relative_files),
|
|
|
|
"COMPOSE_PATH_SEPARATOR": pathsep,
|
|
|
|
})
|
2021-12-31 00:54:32 +01:00
|
|
|
compose = {}
|
2023-07-14 06:11:29 +02:00
|
|
|
# Iterate over files primitively to allow appending to files in-loop
|
|
|
|
files_iter = iter(files)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
filename = next(files_iter)
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
with open(filename, "r", encoding="utf-8") as f:
|
2019-09-08 01:20:48 +02:00
|
|
|
content = yaml.safe_load(f)
|
2022-03-01 08:53:24 +01:00
|
|
|
# log(filename, json.dumps(content, indent = 2))
|
2020-05-09 17:40:10 +02:00
|
|
|
if not isinstance(content, dict):
|
2022-03-01 08:53:24 +01:00
|
|
|
sys.stderr.write(
|
2024-03-07 17:28:06 +01:00
|
|
|
"Compose file does not contain a top level object: %s\n" % filename
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(1)
|
2019-09-08 01:20:48 +02:00
|
|
|
content = normalize(content)
|
2022-03-01 08:53:24 +01:00
|
|
|
# log(filename, json.dumps(content, indent = 2))
|
2021-11-13 22:27:43 +01:00
|
|
|
content = rec_subs(content, self.environ)
|
2019-09-08 01:20:48 +02:00
|
|
|
rec_merge(compose, content)
|
2023-07-14 06:11:29 +02:00
|
|
|
# If `include` is used, append included files to files
|
2023-07-18 12:23:55 +02:00
|
|
|
include = compose.get("include", None)
|
|
|
|
if include:
|
2023-07-14 06:11:29 +02:00
|
|
|
files.append(*include)
|
|
|
|
# As compose obj is updated and tested with every loop, not deleting `include`
|
|
|
|
# from it, results in it being tested again and again, original values for
|
|
|
|
# `include` be appended to `files`, and, included files be processed for ever.
|
|
|
|
# Solution is to remove 'include' key from compose obj. This doesn't break
|
|
|
|
# having `include` present and correctly processed in included files
|
|
|
|
del compose["include"]
|
2024-03-07 17:28:06 +01:00
|
|
|
resolved_services = self._resolve_profiles(compose.get("services", {}), set(args.profile))
|
2022-11-09 18:21:25 +01:00
|
|
|
compose["services"] = resolved_services
|
2023-06-11 07:20:14 +02:00
|
|
|
if not getattr(args, "no_normalize", None):
|
2023-06-11 07:19:14 +02:00
|
|
|
compose = normalize_final(compose, self.dirname)
|
2021-12-31 00:54:32 +01:00
|
|
|
self.merged_yaml = yaml.safe_dump(compose)
|
2022-03-01 08:53:24 +01:00
|
|
|
merged_json_b = json.dumps(compose, separators=(",", ":")).encode("utf-8")
|
2022-02-25 16:56:36 +01:00
|
|
|
self.yaml_hash = hashlib.sha256(merged_json_b).hexdigest()
|
2022-03-01 08:53:24 +01:00
|
|
|
compose["_dirname"] = dirname
|
2019-08-09 15:31:56 +02:00
|
|
|
# debug mode
|
2022-03-01 08:53:24 +01:00
|
|
|
if len(files) > 1:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.debug(" ** merged:\n%s", json.dumps(compose, indent=2))
|
2022-02-25 22:48:42 +01:00
|
|
|
# ver = compose.get('version', None)
|
2022-08-17 00:48:01 +02:00
|
|
|
|
|
|
|
if not project_name:
|
|
|
|
project_name = compose.get("name", None)
|
|
|
|
if project_name is None:
|
2024-03-08 22:46:24 +01:00
|
|
|
# More strict then actually needed for simplicity:
|
|
|
|
# podman requires [a-zA-Z0-9][a-zA-Z0-9_.-]*
|
2022-08-17 00:48:01 +02:00
|
|
|
project_name = (
|
2024-03-07 17:28:06 +01:00
|
|
|
self.environ.get("COMPOSE_PROJECT_NAME", None) or dir_basename.lower()
|
2022-08-17 00:48:01 +02:00
|
|
|
)
|
|
|
|
project_name = norm_re.sub("", project_name)
|
|
|
|
if not project_name:
|
2024-03-07 17:28:06 +01:00
|
|
|
raise RuntimeError(f"Project name [{dir_basename}] normalized to empty")
|
2022-08-17 00:48:01 +02:00
|
|
|
|
|
|
|
self.project_name = project_name
|
|
|
|
self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name})
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
services = compose.get("services", None)
|
2020-04-04 23:09:37 +02:00
|
|
|
if services is None:
|
|
|
|
services = {}
|
2023-08-09 01:12:10 +02:00
|
|
|
log.warning("WARNING: No services defined")
|
2022-11-09 18:21:25 +01:00
|
|
|
# include services with no profile defined or the selected profiles
|
|
|
|
services = self._resolve_profiles(services, set(args.profile))
|
2021-09-06 06:45:50 +02:00
|
|
|
|
2019-10-05 21:37:14 +02:00
|
|
|
# NOTE: maybe add "extends.service" to _deps at this stage
|
|
|
|
flat_deps(services, with_extends=True)
|
2024-03-07 17:28:06 +01:00
|
|
|
service_names = sorted([(len(srv["_deps"]), name) for name, srv in services.items()])
|
2022-03-01 08:53:24 +01:00
|
|
|
service_names = [name for _, name in service_names]
|
2021-11-13 22:27:43 +01:00
|
|
|
resolve_extends(services, service_names, self.environ)
|
2019-10-05 21:37:14 +02:00
|
|
|
flat_deps(services)
|
2024-03-07 17:28:06 +01:00
|
|
|
service_names = sorted([(len(srv["_deps"]), name) for name, srv in services.items()])
|
2022-03-01 08:53:24 +01:00
|
|
|
service_names = [name for _, name in service_names]
|
2021-11-21 00:23:29 +01:00
|
|
|
nets = compose.get("networks", None) or {}
|
|
|
|
if not nets:
|
|
|
|
nets["default"] = None
|
|
|
|
self.networks = nets
|
2022-03-01 08:53:24 +01:00
|
|
|
if len(self.networks) == 1:
|
2021-11-21 00:23:29 +01:00
|
|
|
self.default_net = list(nets.keys())[0]
|
|
|
|
elif "default" in nets:
|
|
|
|
self.default_net = "default"
|
|
|
|
else:
|
|
|
|
self.default_net = None
|
|
|
|
default_net = self.default_net
|
|
|
|
allnets = set()
|
|
|
|
for name, srv in services.items():
|
2022-01-12 20:48:42 +01:00
|
|
|
srv_nets = srv.get("networks", None) or default_net
|
2024-03-07 17:28:06 +01:00
|
|
|
srv_nets = list(srv_nets.keys()) if is_dict(srv_nets) else norm_as_list(srv_nets)
|
2021-11-21 00:23:29 +01:00
|
|
|
allnets.update(srv_nets)
|
|
|
|
given_nets = set(nets.keys())
|
2022-03-20 15:16:59 +01:00
|
|
|
missing_nets = allnets - given_nets
|
|
|
|
unused_nets = given_nets - allnets - set(["default"])
|
|
|
|
if len(unused_nets):
|
|
|
|
unused_nets_str = ",".join(unused_nets)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.warning("WARNING: unused networks: %s", unused_nets_str)
|
2021-11-21 00:23:29 +01:00
|
|
|
if len(missing_nets):
|
2022-03-01 08:53:24 +01:00
|
|
|
missing_nets_str = ",".join(missing_nets)
|
2021-11-21 00:23:29 +01:00
|
|
|
raise RuntimeError(f"missing networks: {missing_nets_str}")
|
2019-08-09 15:31:56 +02:00
|
|
|
# volumes: [...]
|
2022-03-01 08:53:24 +01:00
|
|
|
self.vols = compose.get("volumes", {})
|
2019-08-09 15:31:56 +02:00
|
|
|
podman_compose_labels = [
|
2022-03-01 08:53:24 +01:00
|
|
|
"io.podman.compose.config-hash=" + self.yaml_hash,
|
2019-08-09 15:31:56 +02:00
|
|
|
"io.podman.compose.project=" + project_name,
|
2022-03-16 13:25:57 +01:00
|
|
|
"io.podman.compose.version=" + __version__,
|
2022-07-29 20:13:44 +02:00
|
|
|
f"PODMAN_SYSTEMD_UNIT=podman-compose@{project_name}.service",
|
2021-03-03 16:30:26 +01:00
|
|
|
"com.docker.compose.project=" + project_name,
|
|
|
|
"com.docker.compose.project.working_dir=" + dirname,
|
2022-03-01 08:53:24 +01:00
|
|
|
"com.docker.compose.project.config_files=" + ",".join(relative_files),
|
2019-08-09 15:31:56 +02:00
|
|
|
]
|
|
|
|
# other top-levels:
|
|
|
|
# networks: {driver: ...}
|
|
|
|
# configs: {...}
|
2022-03-01 08:53:24 +01:00
|
|
|
self.declared_secrets = compose.get("secrets", {})
|
2019-08-09 15:31:56 +02:00
|
|
|
given_containers = []
|
|
|
|
container_names_by_service = {}
|
2021-11-13 23:28:43 +01:00
|
|
|
self.services = services
|
2019-08-09 15:31:56 +02:00
|
|
|
for service_name, service_desc in services.items():
|
2022-03-01 08:53:24 +01:00
|
|
|
replicas = try_int(service_desc.get("deploy", {}).get("replicas", "1"))
|
2019-08-09 15:31:56 +02:00
|
|
|
container_names_by_service[service_name] = []
|
2022-03-01 08:53:24 +01:00
|
|
|
for num in range(1, replicas + 1):
|
2022-02-26 00:49:34 +01:00
|
|
|
name0 = f"{project_name}_{service_name}_{num}"
|
2019-08-09 15:31:56 +02:00
|
|
|
if num == 1:
|
|
|
|
name = service_desc.get("container_name", name0)
|
|
|
|
else:
|
|
|
|
name = name0
|
|
|
|
container_names_by_service[service_name].append(name)
|
2021-12-24 17:55:30 +01:00
|
|
|
# log(service_name,service_desc)
|
2023-04-09 12:10:33 +02:00
|
|
|
cnt = {
|
|
|
|
"name": name,
|
|
|
|
"num": num,
|
|
|
|
"service_name": service_name,
|
|
|
|
**service_desc,
|
|
|
|
}
|
2022-03-01 08:53:24 +01:00
|
|
|
if "image" not in cnt:
|
|
|
|
cnt["image"] = f"{project_name}_{service_name}"
|
|
|
|
labels = norm_as_list(cnt.get("labels", None))
|
2021-10-09 23:43:01 +02:00
|
|
|
cnt["ports"] = norm_ports(cnt.get("ports", None))
|
2019-08-09 15:31:56 +02:00
|
|
|
labels.extend(podman_compose_labels)
|
2024-03-07 17:28:06 +01:00
|
|
|
labels.extend([
|
|
|
|
f"com.docker.compose.container-number={num}",
|
|
|
|
"com.docker.compose.service=" + service_name,
|
|
|
|
])
|
2022-03-01 08:53:24 +01:00
|
|
|
cnt["labels"] = labels
|
|
|
|
cnt["_service"] = service_name
|
|
|
|
cnt["_project"] = project_name
|
2019-08-09 15:31:56 +02:00
|
|
|
given_containers.append(cnt)
|
2022-01-21 23:15:05 +01:00
|
|
|
volumes = cnt.get("volumes", None) or []
|
|
|
|
for volume in volumes:
|
|
|
|
mnt_dict = get_mnt_dict(self, cnt, volume)
|
2022-03-01 08:53:24 +01:00
|
|
|
if (
|
|
|
|
mnt_dict.get("type", None) == "volume"
|
|
|
|
and mnt_dict["source"]
|
|
|
|
and mnt_dict["source"] not in self.vols
|
|
|
|
):
|
2022-01-21 23:15:05 +01:00
|
|
|
vol_name = mnt_dict["source"]
|
2024-03-07 17:28:06 +01:00
|
|
|
raise RuntimeError(f"volume [{vol_name}] not defined in top level")
|
2019-08-09 15:31:56 +02:00
|
|
|
self.container_names_by_service = container_names_by_service
|
2022-02-25 19:13:10 +01:00
|
|
|
self.all_services = set(container_names_by_service.keys())
|
2022-02-25 23:41:07 +01:00
|
|
|
container_by_name = {c["name"]: c for c in given_containers}
|
2022-03-01 08:53:24 +01:00
|
|
|
# log("deps:", [(c["name"], c["_deps"]) for c in given_containers])
|
2019-08-09 15:31:56 +02:00
|
|
|
given_containers = list(container_by_name.values())
|
2022-03-01 08:53:24 +01:00
|
|
|
given_containers.sort(key=lambda c: len(c.get("_deps", None) or []))
|
|
|
|
# log("sorted:", [c["name"] for c in given_containers])
|
2022-03-08 19:13:14 +01:00
|
|
|
pods, containers = transform(args, project_name, given_containers)
|
2019-08-09 15:31:56 +02:00
|
|
|
self.pods = pods
|
|
|
|
self.containers = containers
|
2022-02-25 23:41:07 +01:00
|
|
|
self.container_by_name = {c["name"]: c for c in containers}
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-11-09 18:21:25 +01:00
|
|
|
def _resolve_profiles(self, defined_services, requested_profiles=None):
|
|
|
|
"""
|
2024-03-08 22:46:24 +01:00
|
|
|
Returns a service dictionary (key = service name, value = service config) compatible with
|
|
|
|
the requested_profiles list.
|
2022-11-09 18:21:25 +01:00
|
|
|
|
2024-03-08 22:46:24 +01:00
|
|
|
The returned service dictionary contains all services which do not include/reference a
|
|
|
|
profile in addition to services that match the requested_profiles.
|
2022-11-09 18:21:25 +01:00
|
|
|
|
|
|
|
:param defined_services: The service dictionary
|
|
|
|
:param requested_profiles: The profiles requested using the --profile arg.
|
|
|
|
"""
|
|
|
|
if requested_profiles is None:
|
|
|
|
requested_profiles = set()
|
|
|
|
|
|
|
|
services = {}
|
|
|
|
|
|
|
|
for name, config in defined_services.items():
|
|
|
|
service_profiles = set(config.get("profiles", []))
|
2024-03-07 17:28:06 +01:00
|
|
|
if not service_profiles or requested_profiles.intersection(service_profiles):
|
2022-11-09 18:21:25 +01:00
|
|
|
services[name] = config
|
|
|
|
return services
|
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
def _parse_args(self):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
|
2019-08-09 15:31:56 +02:00
|
|
|
self._init_global_parser(parser)
|
2022-03-01 08:53:24 +01:00
|
|
|
subparsers = parser.add_subparsers(title="command", dest="command")
|
|
|
|
subparser = subparsers.add_parser("help", help="show help")
|
2019-08-10 17:08:21 +02:00
|
|
|
for cmd_name, cmd in self.commands.items():
|
2024-03-07 17:28:06 +01:00
|
|
|
subparser = subparsers.add_parser(cmd_name, help=cmd.desc) # pylint: disable=protected-access
|
2022-02-25 22:48:42 +01:00
|
|
|
for cmd_parser in cmd._parse_args: # pylint: disable=protected-access
|
2019-08-10 17:08:21 +02:00
|
|
|
cmd_parser(subparser)
|
2019-08-09 15:31:56 +02:00
|
|
|
self.global_args = parser.parse_args()
|
2021-09-08 23:33:07 +02:00
|
|
|
if self.global_args.version:
|
|
|
|
self.global_args.command = "version"
|
2022-03-01 08:53:24 +01:00
|
|
|
if not self.global_args.command or self.global_args.command == "help":
|
2019-09-02 23:19:07 +02:00
|
|
|
parser.print_help()
|
2022-02-25 23:41:07 +01:00
|
|
|
sys.exit(-1)
|
2022-09-19 21:21:32 +02:00
|
|
|
|
2023-08-09 01:12:10 +02:00
|
|
|
logging.basicConfig(level=("DEBUG" if self.global_args.verbose else "WARN"))
|
2019-08-10 17:08:21 +02:00
|
|
|
return self.global_args
|
|
|
|
|
2022-02-25 23:41:07 +01:00
|
|
|
@staticmethod
|
|
|
|
def _init_global_parser(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument("-v", "--version", help="show version", action="store_true")
|
2022-03-08 19:13:14 +01:00
|
|
|
parser.add_argument(
|
2023-04-09 13:02:10 +02:00
|
|
|
"--in-pod",
|
|
|
|
help="pod creation",
|
|
|
|
metavar="in_pod",
|
|
|
|
type=bool,
|
2023-04-09 13:08:54 +02:00
|
|
|
default=True,
|
2022-03-08 19:13:14 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--pod-args",
|
2022-04-10 06:41:13 +02:00
|
|
|
help="custom arguments to be passed to `podman pod`",
|
2022-03-08 19:13:14 +01:00
|
|
|
metavar="pod_args",
|
|
|
|
type=str,
|
|
|
|
default="--infra=false --share=",
|
|
|
|
)
|
2022-03-08 08:57:44 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--env-file",
|
|
|
|
help="Specify an alternate environment file",
|
|
|
|
metavar="env_file",
|
|
|
|
type=str,
|
2022-03-08 10:01:02 +01:00
|
|
|
default=".env",
|
2022-03-08 08:57:44 +01:00
|
|
|
)
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--file",
|
|
|
|
help="Specify an alternate compose file (default: docker-compose.yml)",
|
|
|
|
metavar="file",
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
)
|
2022-11-09 18:21:25 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--profile",
|
|
|
|
help="Specify a profile to enable",
|
|
|
|
metavar="profile",
|
|
|
|
action="append",
|
2022-11-09 18:59:28 +01:00
|
|
|
default=[],
|
2022-11-09 18:21:25 +01:00
|
|
|
)
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--project-name",
|
|
|
|
help="Specify an alternate project name (default: directory name)",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--podman-path",
|
|
|
|
help="Specify an alternate path to podman (default: use location in $PATH variable)",
|
|
|
|
type=str,
|
|
|
|
default="podman",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--podman-args",
|
|
|
|
help="custom global arguments to be passed to `podman`",
|
|
|
|
metavar="args",
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
)
|
2021-05-17 14:03:47 +02:00
|
|
|
for podman_cmd in PODMAN_CMDS:
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
f"--podman-{podman_cmd}-args",
|
2021-05-17 14:03:47 +02:00
|
|
|
help=f"custom arguments to be passed to `podman {podman_cmd}`",
|
2022-03-01 08:53:24 +01:00
|
|
|
metavar="args",
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-ansi",
|
|
|
|
help="Do not print ANSI control characters",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-cleanup",
|
|
|
|
help="Do not stop and remove existing pod & containers",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dry-run",
|
|
|
|
help="No action; perform a simulation of commands",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2023-12-11 08:46:20 +01:00
|
|
|
parser.add_argument(
|
2024-03-07 17:28:06 +01:00
|
|
|
"--parallel", type=int, default=os.environ.get("COMPOSE_PARALLEL_LIMIT", sys.maxsize)
|
2023-12-11 08:46:20 +01:00
|
|
|
)
|
2022-09-19 21:21:32 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--verbose",
|
|
|
|
help="Print debugging output",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
|
|
|
|
podman_compose = PodmanCompose()
|
|
|
|
|
2024-03-09 16:14:34 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
###################
|
|
|
|
# decorators to add commands and parse options
|
|
|
|
###################
|
2024-03-09 16:14:34 +01:00
|
|
|
class PodmanComposeError(Exception):
|
|
|
|
pass
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
class cmd_run: # pylint: disable=invalid-name,too-few-public-methods
|
2022-03-12 21:08:24 +01:00
|
|
|
def __init__(self, compose, cmd_name, cmd_desc=None):
|
2019-08-09 15:31:56 +02:00
|
|
|
self.compose = compose
|
|
|
|
self.cmd_name = cmd_name
|
|
|
|
self.cmd_desc = cmd_desc
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
def __call__(self, func):
|
|
|
|
def wrapped(*args, **kw):
|
|
|
|
return func(*args, **kw)
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
if not asyncio.iscoroutinefunction(func):
|
2024-03-09 16:14:34 +01:00
|
|
|
raise PodmanComposeError("Command must be async")
|
2019-08-09 15:31:56 +02:00
|
|
|
wrapped._compose = self.compose
|
2022-05-15 16:03:19 +02:00
|
|
|
# Trim extra indentation at start of multiline docstrings.
|
2022-06-21 20:51:09 +02:00
|
|
|
wrapped.desc = self.cmd_desc or re.sub(r"^\s+", "", func.__doc__)
|
2019-08-09 15:31:56 +02:00
|
|
|
wrapped._parse_args = []
|
|
|
|
self.compose.commands[self.cmd_name] = wrapped
|
|
|
|
return wrapped
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
class cmd_parse: # pylint: disable=invalid-name,too-few-public-methods
|
2019-08-09 15:31:56 +02:00
|
|
|
def __init__(self, compose, cmd_names):
|
|
|
|
self.compose = compose
|
|
|
|
self.cmd_names = cmd_names if is_list(cmd_names) else [cmd_names]
|
|
|
|
|
|
|
|
def __call__(self, func):
|
|
|
|
def wrapped(*args, **kw):
|
|
|
|
return func(*args, **kw)
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
for cmd_name in self.cmd_names:
|
|
|
|
self.compose.commands[cmd_name]._parse_args.append(wrapped)
|
|
|
|
return wrapped
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
###################
|
|
|
|
# actual commands
|
|
|
|
###################
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "version", "show version")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_version(compose, args):
|
2022-03-01 08:53:24 +01:00
|
|
|
if getattr(args, "short", False):
|
2021-12-29 22:23:24 +01:00
|
|
|
print(__version__)
|
|
|
|
return
|
2022-03-01 08:53:24 +01:00
|
|
|
if getattr(args, "format", "pretty") == "json":
|
2021-12-31 00:32:15 +01:00
|
|
|
res = {"version": __version__}
|
|
|
|
print(json.dumps(res))
|
2021-12-29 22:23:24 +01:00
|
|
|
return
|
2022-06-30 16:24:40 +02:00
|
|
|
print("podman-compose version", __version__)
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.podman.run(["--version"], "", [])
|
2019-10-04 19:57:07 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-08-05 11:24:35 +02:00
|
|
|
def is_local(container: dict) -> bool:
|
|
|
|
"""Test if a container is local, i.e. if it is
|
|
|
|
* prefixed with localhost/
|
|
|
|
* has a build section and is not prefixed
|
|
|
|
"""
|
|
|
|
return (
|
2023-04-09 19:05:15 +02:00
|
|
|
"/" not in container["image"]
|
2021-08-05 11:24:35 +02:00
|
|
|
if "build" in container
|
|
|
|
else container["image"].startswith("localhost/")
|
|
|
|
)
|
|
|
|
|
2022-03-12 21:08:24 +01:00
|
|
|
|
2022-03-12 19:16:16 +01:00
|
|
|
@cmd_run(podman_compose, "wait", "wait running containers to stop")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_wait(compose, args): # pylint: disable=unused-argument
|
2022-03-12 19:16:16 +01:00
|
|
|
containers = [cnt["name"] for cnt in compose.containers]
|
|
|
|
cmd_args = ["--"]
|
|
|
|
cmd_args.extend(containers)
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.exec([], "wait", cmd_args)
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-03-12 21:08:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "systemd")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_systemd(compose, args):
|
2022-03-12 21:08:24 +01:00
|
|
|
"""
|
|
|
|
create systemd unit file and register its compose stacks
|
2022-03-12 21:17:26 +01:00
|
|
|
|
2022-05-15 16:03:19 +02:00
|
|
|
When first installed type `sudo podman-compose systemd -a create-unit`
|
|
|
|
later you can add a compose stack by running `podman-compose systemd -a register`
|
2022-03-12 21:08:24 +01:00
|
|
|
then you can start/stop your stack with `systemctl --user start podman-compose@<PROJ>`
|
|
|
|
"""
|
|
|
|
stacks_dir = ".config/containers/compose/projects"
|
2022-03-12 21:17:26 +01:00
|
|
|
if args.action == "register":
|
2022-03-12 21:08:24 +01:00
|
|
|
proj_name = compose.project_name
|
|
|
|
fn = os.path.expanduser(f"~/{stacks_dir}/{proj_name}.env")
|
|
|
|
os.makedirs(os.path.dirname(fn), exist_ok=True)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.debug("writing [%s]: ...", fn)
|
2022-03-13 08:08:48 +01:00
|
|
|
with open(fn, "w", encoding="utf-8") as f:
|
2022-03-12 21:08:24 +01:00
|
|
|
for k, v in compose.environ.items():
|
|
|
|
if k.startswith("COMPOSE_") or k.startswith("PODMAN_"):
|
|
|
|
f.write(f"{k}={v}\n")
|
2023-08-09 01:12:10 +02:00
|
|
|
log.debug("writing [%s]: done.", fn)
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info("\n\ncreating the pod without starting it: ...\n\n")
|
2024-03-09 16:14:34 +01:00
|
|
|
process = await asyncio.create_subprocess_exec(script, ["up", "--no-start"])
|
|
|
|
log.info("\nfinal exit code is %d", process)
|
2022-03-15 20:47:54 +01:00
|
|
|
username = getpass.getuser()
|
2022-03-15 20:50:03 +01:00
|
|
|
print(
|
|
|
|
f"""
|
2022-03-15 20:47:54 +01:00
|
|
|
you can use systemd commands like enable, start, stop, status, cat
|
|
|
|
all without `sudo` like this:
|
|
|
|
|
|
|
|
\t\tsystemctl --user enable --now 'podman-compose@{proj_name}'
|
|
|
|
\t\tsystemctl --user status 'podman-compose@{proj_name}'
|
|
|
|
\t\tjournalctl --user -xeu 'podman-compose@{proj_name}'
|
|
|
|
|
|
|
|
and for that to work outside a session
|
|
|
|
you might need to run the following command *once*
|
|
|
|
|
|
|
|
\t\tsudo loginctl enable-linger '{username}'
|
|
|
|
|
|
|
|
you can use podman commands like:
|
|
|
|
|
|
|
|
\t\tpodman pod ps
|
|
|
|
\t\tpodman pod stats 'pod_{proj_name}'
|
|
|
|
\t\tpodman pod logs --tail=10 -f 'pod_{proj_name}'
|
2022-03-15 20:50:03 +01:00
|
|
|
"""
|
|
|
|
)
|
2022-03-14 12:17:49 +01:00
|
|
|
elif args.action in ("list", "ls"):
|
2022-03-14 12:05:20 +01:00
|
|
|
ls = glob.glob(os.path.expanduser(f"~/{stacks_dir}/*.env"))
|
|
|
|
for i in ls:
|
|
|
|
print(os.path.basename(i[:-4]))
|
2022-03-12 21:17:26 +01:00
|
|
|
elif args.action == "create-unit":
|
2022-05-10 00:13:34 +02:00
|
|
|
fn = "/etc/systemd/user/podman-compose@.service"
|
2022-03-12 21:08:24 +01:00
|
|
|
out = f"""\
|
|
|
|
# {fn}
|
|
|
|
|
|
|
|
[Unit]
|
|
|
|
Description=%i rootless pod (podman-compose)
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Type=simple
|
|
|
|
EnvironmentFile=%h/{stacks_dir}/%i.env
|
2022-03-13 09:20:59 +01:00
|
|
|
ExecStartPre=-{script} up --no-start
|
2022-03-12 21:08:24 +01:00
|
|
|
ExecStartPre=/usr/bin/podman pod start pod_%i
|
|
|
|
ExecStart={script} wait
|
|
|
|
ExecStop=/usr/bin/podman pod stop pod_%i
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=default.target
|
|
|
|
"""
|
|
|
|
if os.access(os.path.dirname(fn), os.W_OK):
|
2023-08-09 01:12:10 +02:00
|
|
|
log.debug("writing [%s]: ...", fn)
|
2022-03-13 08:08:48 +01:00
|
|
|
with open(fn, "w", encoding="utf-8") as f:
|
2022-03-12 21:08:24 +01:00
|
|
|
f.write(out)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.debug("writing [%s]: done.", fn)
|
2022-03-12 21:17:26 +01:00
|
|
|
print(
|
|
|
|
"""
|
2022-03-12 21:08:24 +01:00
|
|
|
while in your project type `podman-compose systemd -a register`
|
2022-03-12 21:17:26 +01:00
|
|
|
"""
|
|
|
|
)
|
2022-03-12 21:08:24 +01:00
|
|
|
else:
|
|
|
|
print(out)
|
2023-08-09 01:12:10 +02:00
|
|
|
log.warning("Could not write to [%s], use 'sudo'", fn)
|
2022-03-12 21:08:24 +01:00
|
|
|
|
|
|
|
|
2021-08-05 11:24:35 +02:00
|
|
|
@cmd_run(podman_compose, "pull", "pull stack images")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_pull(compose, args):
|
2021-08-05 11:24:35 +02:00
|
|
|
img_containers = [cnt for cnt in compose.containers if "image" in cnt]
|
2022-08-29 19:08:19 +02:00
|
|
|
if args.services:
|
|
|
|
services = set(args.services)
|
|
|
|
img_containers = [cnt for cnt in img_containers if cnt["_service"] in services]
|
2021-08-05 11:24:35 +02:00
|
|
|
images = {cnt["image"] for cnt in img_containers}
|
|
|
|
if not args.force_local:
|
|
|
|
local_images = {cnt["image"] for cnt in img_containers if is_local(cnt)}
|
|
|
|
images -= local_images
|
2023-12-10 07:51:54 +01:00
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
await asyncio.gather(*[compose.podman.run([], "pull", [image]) for image in images])
|
2019-04-19 17:24:30 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "push", "push stack images")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_push(compose, args):
|
2019-06-09 03:22:58 +02:00
|
|
|
services = set(args.services)
|
2019-08-09 15:31:56 +02:00
|
|
|
for cnt in compose.containers:
|
2022-03-01 08:53:24 +01:00
|
|
|
if "build" not in cnt:
|
|
|
|
continue
|
|
|
|
if services and cnt["_service"] not in services:
|
|
|
|
continue
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.podman.run([], "push", [cnt["image"]])
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def build_one(compose, args, cnt):
|
2022-03-01 08:53:24 +01:00
|
|
|
if "build" not in cnt:
|
2023-04-10 12:38:21 +02:00
|
|
|
return None
|
2022-03-01 08:53:24 +01:00
|
|
|
if getattr(args, "if_not_exists", None):
|
|
|
|
try:
|
2023-12-10 07:51:54 +01:00
|
|
|
img_id = await compose.podman.output(
|
2022-03-01 08:53:24 +01:00
|
|
|
[], "inspect", ["-t", "image", "-f", "{{.Id}}", cnt["image"]]
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
img_id = None
|
|
|
|
if img_id:
|
2023-04-10 12:38:21 +02:00
|
|
|
return None
|
2022-03-01 08:53:24 +01:00
|
|
|
build_desc = cnt["build"]
|
|
|
|
if not hasattr(build_desc, "items"):
|
2023-04-09 12:10:33 +02:00
|
|
|
build_desc = {"context": build_desc}
|
2022-03-01 08:53:24 +01:00
|
|
|
ctx = build_desc.get("context", ".")
|
2021-11-11 10:27:33 +01:00
|
|
|
dockerfile = build_desc.get("dockerfile", None)
|
|
|
|
if dockerfile:
|
|
|
|
dockerfile = os.path.join(ctx, dockerfile)
|
|
|
|
else:
|
|
|
|
dockerfile_alts = [
|
2022-03-01 08:53:24 +01:00
|
|
|
"Containerfile",
|
|
|
|
"ContainerFile",
|
|
|
|
"containerfile",
|
|
|
|
"Dockerfile",
|
|
|
|
"DockerFile",
|
|
|
|
"dockerfile",
|
2021-11-11 10:27:33 +01:00
|
|
|
]
|
|
|
|
for dockerfile in dockerfile_alts:
|
|
|
|
dockerfile = os.path.join(ctx, dockerfile)
|
2022-03-01 08:53:24 +01:00
|
|
|
if os.path.exists(dockerfile):
|
|
|
|
break
|
2019-08-17 22:39:42 +02:00
|
|
|
if not os.path.exists(dockerfile):
|
2022-03-01 08:53:24 +01:00
|
|
|
raise OSError("Dockerfile not found in " + ctx)
|
2022-07-27 20:05:38 +02:00
|
|
|
build_args = ["-f", dockerfile, "-t", cnt["image"]]
|
2023-03-15 06:49:39 +01:00
|
|
|
for secret in build_desc.get("secrets", []):
|
2023-09-20 11:29:36 +02:00
|
|
|
build_args.extend(get_secret_args(compose, cnt, secret, podman_is_building=True))
|
2022-07-27 20:05:38 +02:00
|
|
|
for tag in build_desc.get("tags", []):
|
|
|
|
build_args.extend(["-t", tag])
|
2020-03-23 19:52:17 +01:00
|
|
|
if "target" in build_desc:
|
2020-04-22 13:44:36 +02:00
|
|
|
build_args.extend(["--target", build_desc["target"]])
|
2024-04-05 04:59:25 +02:00
|
|
|
container_to_ulimit_build_args(cnt, build_args)
|
2022-03-01 08:53:24 +01:00
|
|
|
if getattr(args, "no_cache", None):
|
2020-04-08 16:51:40 +02:00
|
|
|
build_args.append("--no-cache")
|
2022-03-01 08:53:24 +01:00
|
|
|
if getattr(args, "pull_always", None):
|
|
|
|
build_args.append("--pull-always")
|
|
|
|
elif getattr(args, "pull", None):
|
|
|
|
build_args.append("--pull")
|
2023-07-16 01:37:33 +02:00
|
|
|
env = dict(cnt.get("environment", {}))
|
|
|
|
for name, value in env.items():
|
|
|
|
build_args += ["--env", f"{name}" if value is None else f"{name}={value}"]
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
args_list = norm_as_list(build_desc.get("args", {}))
|
2020-04-02 16:18:16 +02:00
|
|
|
for build_arg in args_list + args.build_arg:
|
2024-03-07 17:28:06 +01:00
|
|
|
build_args.extend((
|
|
|
|
"--build-arg",
|
|
|
|
build_arg,
|
|
|
|
))
|
2019-08-17 22:39:42 +02:00
|
|
|
build_args.append(ctx)
|
2023-12-11 08:46:20 +01:00
|
|
|
status = await compose.podman.run([], "build", build_args)
|
2022-11-09 10:33:40 +01:00
|
|
|
return status
|
2019-08-17 22:39:42 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "build", "build stack images")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_build(compose, args):
|
|
|
|
tasks = []
|
2023-04-10 17:25:30 +02:00
|
|
|
|
2020-04-22 14:31:57 +02:00
|
|
|
if args.services:
|
|
|
|
container_names_by_service = compose.container_names_by_service
|
2022-02-25 22:48:42 +01:00
|
|
|
compose.assert_services(args.services)
|
2020-04-22 14:31:57 +02:00
|
|
|
for service in args.services:
|
2022-02-25 22:48:42 +01:00
|
|
|
cnt = compose.container_by_name[container_names_by_service[service][0]]
|
2023-12-11 08:46:20 +01:00
|
|
|
tasks.append(asyncio.create_task(build_one(compose, args, cnt)))
|
2023-12-10 07:51:54 +01:00
|
|
|
|
2020-04-22 14:31:57 +02:00
|
|
|
else:
|
|
|
|
for cnt in compose.containers:
|
2023-12-11 08:46:20 +01:00
|
|
|
tasks.append(asyncio.create_task(build_one(compose, args, cnt)))
|
2023-12-10 07:51:54 +01:00
|
|
|
|
|
|
|
status = 0
|
|
|
|
for t in asyncio.as_completed(tasks):
|
|
|
|
s = await t
|
|
|
|
if s is not None:
|
|
|
|
status = s
|
2023-04-10 17:25:30 +02:00
|
|
|
|
2023-04-12 19:22:14 +02:00
|
|
|
return status
|
2019-03-23 20:42:04 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def create_pods(compose, args): # pylint: disable=unused-argument
|
2019-08-09 15:31:56 +02:00
|
|
|
for pod in compose.pods:
|
|
|
|
podman_args = [
|
2021-05-17 14:03:47 +02:00
|
|
|
"create",
|
2022-03-01 08:53:24 +01:00
|
|
|
"--name=" + pod["name"],
|
2019-03-20 23:49:17 +01:00
|
|
|
]
|
2022-03-08 19:13:14 +01:00
|
|
|
if args.pod_args:
|
|
|
|
podman_args.extend(shlex.split(args.pod_args))
|
2022-03-01 08:53:24 +01:00
|
|
|
# if compose.podman_version and not strverscmp_lt(compose.podman_version, "3.4.0"):
|
2021-11-21 00:23:29 +01:00
|
|
|
# podman_args.append("--infra-name={}_infra".format(pod["name"]))
|
2020-04-18 17:39:59 +02:00
|
|
|
ports = pod.get("ports", None) or []
|
2021-08-26 11:37:14 +02:00
|
|
|
if isinstance(ports, str):
|
|
|
|
ports = [ports]
|
2019-03-20 23:49:17 +01:00
|
|
|
for i in ports:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["-p", str(i)])
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "pod", podman_args)
|
2019-03-23 20:42:04 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-11-13 23:28:43 +01:00
|
|
|
def get_excluded(compose, args):
|
|
|
|
excluded = set()
|
2019-08-17 22:39:42 +02:00
|
|
|
if args.services:
|
2021-11-13 23:28:43 +01:00
|
|
|
excluded = set(compose.services)
|
|
|
|
for service in args.services:
|
2022-03-01 08:53:24 +01:00
|
|
|
excluded -= compose.services[service]["_deps"]
|
2021-11-13 23:28:43 +01:00
|
|
|
excluded.discard(service)
|
2022-09-19 21:21:32 +02:00
|
|
|
log.debug("** excluding: %s", excluded)
|
2021-11-13 23:28:43 +01:00
|
|
|
return excluded
|
2019-08-17 22:39:42 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
@cmd_run(podman_compose, "up", "Create and start the entire stack or some of its services")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_up(compose: PodmanCompose, args):
|
2022-03-16 13:25:57 +01:00
|
|
|
proj_name = compose.project_name
|
2021-11-13 23:28:43 +01:00
|
|
|
excluded = get_excluded(compose, args)
|
2019-08-17 22:39:42 +02:00
|
|
|
if not args.no_build:
|
|
|
|
# `podman build` does not cache, so don't always build
|
2022-03-01 08:53:24 +01:00
|
|
|
build_args = argparse.Namespace(if_not_exists=(not args.build), **args.__dict__)
|
2023-12-11 00:26:58 +01:00
|
|
|
if await compose.commands["build"](compose, build_args) != 0:
|
|
|
|
log("Build command failed")
|
2019-11-17 08:09:41 +01:00
|
|
|
|
2022-03-16 13:27:30 +01:00
|
|
|
hashes = (
|
2024-03-07 17:28:06 +01:00
|
|
|
(
|
|
|
|
await compose.podman.output(
|
|
|
|
[],
|
|
|
|
"ps",
|
|
|
|
[
|
|
|
|
"--filter",
|
|
|
|
f"label=io.podman.compose.project={proj_name}",
|
|
|
|
"-a",
|
|
|
|
"--format",
|
|
|
|
'{{ index .Labels "io.podman.compose.config-hash"}}',
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2022-03-16 13:27:30 +01:00
|
|
|
.decode("utf-8")
|
|
|
|
.splitlines()
|
|
|
|
)
|
|
|
|
diff_hashes = [i for i in hashes if i and i != compose.yaml_hash]
|
2022-03-16 13:25:57 +01:00
|
|
|
if args.force_recreate or len(diff_hashes):
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info("recreating: ...")
|
2021-12-21 21:02:59 +01:00
|
|
|
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.commands["down"](compose, down_args)
|
2022-09-19 21:21:32 +02:00
|
|
|
log.info("recreating: done\n\n")
|
2019-08-17 22:39:42 +02:00
|
|
|
# args.no_recreate disables check for changes (which is not implemented)
|
2019-08-10 13:11:28 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_command = "run" if args.detach and not args.no_start else "create"
|
2019-08-10 13:11:28 +02:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
await create_pods(compose, args)
|
2019-08-09 15:31:56 +02:00
|
|
|
for cnt in compose.containers:
|
2021-11-13 23:28:43 +01:00
|
|
|
if cnt["_service"] in excluded:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.debug("** skipping: %s", cnt["name"])
|
2021-11-13 23:28:43 +01:00
|
|
|
continue
|
2023-12-10 07:51:54 +01:00
|
|
|
podman_args = await container_to_args(compose, cnt, detached=args.detach)
|
|
|
|
subproc = await compose.podman.run([], podman_command, podman_args)
|
2023-12-11 08:46:20 +01:00
|
|
|
if podman_command == "run" and subproc is not None:
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "start", [cnt["name"]])
|
2021-05-17 14:03:47 +02:00
|
|
|
if args.no_start or args.detach or args.dry_run:
|
|
|
|
return
|
2019-09-02 23:19:07 +02:00
|
|
|
# TODO: handle already existing
|
|
|
|
# TODO: if error creating do not enter loop
|
2019-08-17 22:39:42 +02:00
|
|
|
# TODO: colors if sys.stdout.isatty()
|
2022-03-01 08:53:24 +01:00
|
|
|
exit_code_from = args.__dict__.get("exit_code_from", None)
|
2021-11-13 23:59:41 +01:00
|
|
|
if exit_code_from:
|
2022-03-01 08:53:24 +01:00
|
|
|
args.abort_on_container_exit = True
|
2019-03-04 10:30:14 +01:00
|
|
|
|
2022-02-03 21:50:12 +01:00
|
|
|
max_service_length = 0
|
2019-08-17 22:39:42 +02:00
|
|
|
for cnt in compose.containers:
|
2022-02-03 21:50:12 +01:00
|
|
|
curr_length = len(cnt["_service"])
|
2024-03-07 17:28:06 +01:00
|
|
|
max_service_length = curr_length if curr_length > max_service_length else max_service_length
|
2023-12-10 07:51:54 +01:00
|
|
|
|
|
|
|
tasks = set()
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.add_signal_handler(signal.SIGINT, lambda: [t.cancel("User exit") for t in tasks])
|
|
|
|
|
2022-02-03 21:50:12 +01:00
|
|
|
for i, cnt in enumerate(compose.containers):
|
|
|
|
# Add colored service prefix to output by piping output through sed
|
|
|
|
color_idx = i % len(compose.console_colors)
|
|
|
|
color = compose.console_colors[color_idx]
|
2022-03-01 08:53:24 +01:00
|
|
|
space_suffix = " " * (max_service_length - len(cnt["_service"]) + 1)
|
2024-03-07 17:28:06 +01:00
|
|
|
log_formatter = "{}[{}]{}|\x1b[0m".format(color, cnt["_service"], space_suffix)
|
2021-11-13 23:28:43 +01:00
|
|
|
if cnt["_service"] in excluded:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.debug("** skipping: %s", cnt["name"])
|
2021-11-13 23:28:43 +01:00
|
|
|
continue
|
2023-12-10 07:51:54 +01:00
|
|
|
|
2023-12-11 00:26:58 +01:00
|
|
|
tasks.add(
|
|
|
|
asyncio.create_task(
|
2023-12-11 08:46:20 +01:00
|
|
|
compose.podman.run([], "start", ["-a", cnt["name"]], log_formatter=log_formatter),
|
2024-03-07 17:28:06 +01:00
|
|
|
name=cnt["_service"],
|
2023-12-11 00:26:58 +01:00
|
|
|
)
|
|
|
|
)
|
2023-12-10 07:51:54 +01:00
|
|
|
|
2024-03-09 17:06:41 +01:00
|
|
|
def _task_cancelled(task: Task) -> bool:
|
|
|
|
if task.cancelled():
|
|
|
|
return True
|
|
|
|
# Task.cancelling() is new in python 3.11
|
|
|
|
if sys.version_info >= (3, 11) and task.cancelling():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
exit_code = 0
|
2023-12-11 08:46:20 +01:00
|
|
|
exiting = False
|
|
|
|
while tasks:
|
|
|
|
done, tasks = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
|
2023-12-10 07:51:54 +01:00
|
|
|
if args.abort_on_container_exit:
|
2023-12-11 08:46:20 +01:00
|
|
|
if not exiting:
|
2024-03-08 22:46:24 +01:00
|
|
|
# If 2 containers exit at the exact same time, the cancellation of the other ones
|
|
|
|
# cause the status to overwrite. Sleeping for 1 seems to fix this and make it match
|
|
|
|
# docker-compose
|
2023-12-11 08:46:20 +01:00
|
|
|
await asyncio.sleep(1)
|
2024-03-09 17:06:41 +01:00
|
|
|
for t in tasks:
|
|
|
|
if not _task_cancelled(t):
|
|
|
|
t.cancel()
|
2023-12-11 08:46:20 +01:00
|
|
|
t: Task
|
|
|
|
exiting = True
|
|
|
|
for t in done:
|
|
|
|
if t.get_name() == exit_code_from:
|
|
|
|
exit_code = t.result()
|
2023-12-10 07:51:54 +01:00
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
return exit_code
|
2019-03-23 20:42:04 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-12-23 00:17:34 +01:00
|
|
|
def get_volume_names(compose, cnt):
|
|
|
|
proj_name = compose.project_name
|
|
|
|
basedir = compose.dirname
|
2022-03-01 08:53:24 +01:00
|
|
|
srv_name = cnt["_service"]
|
2021-12-23 00:17:34 +01:00
|
|
|
ls = []
|
2022-03-01 08:53:24 +01:00
|
|
|
for volume in cnt.get("volumes", []):
|
|
|
|
if is_str(volume):
|
|
|
|
volume = parse_short_mount(volume, basedir)
|
2021-12-23 00:17:34 +01:00
|
|
|
volume = fix_mount_dict(compose, volume, proj_name, srv_name)
|
|
|
|
mount_type = volume["type"]
|
2022-03-01 08:53:24 +01:00
|
|
|
if mount_type != "volume":
|
|
|
|
continue
|
2021-12-23 00:17:34 +01:00
|
|
|
volume_name = (volume.get("_vol", None) or {}).get("name", None)
|
|
|
|
ls.append(volume_name)
|
|
|
|
return ls
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "down", "tear down entire stack")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_down(compose, args):
|
2021-11-13 23:28:43 +01:00
|
|
|
excluded = get_excluded(compose, args)
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args = []
|
2023-03-29 10:30:24 +02:00
|
|
|
timeout_global = getattr(args, "timeout", None)
|
2021-11-11 10:32:24 +01:00
|
|
|
containers = list(reversed(compose.containers))
|
2020-05-24 16:09:56 +02:00
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
down_tasks = []
|
|
|
|
|
2021-11-11 10:32:24 +01:00
|
|
|
for cnt in containers:
|
2022-03-01 08:53:24 +01:00
|
|
|
if cnt["_service"] in excluded:
|
|
|
|
continue
|
2023-03-28 16:33:02 +02:00
|
|
|
podman_stop_args = [*podman_args]
|
2023-03-29 10:30:24 +02:00
|
|
|
timeout = timeout_global
|
2023-03-28 16:33:02 +02:00
|
|
|
if timeout is None:
|
2023-03-29 00:31:42 +02:00
|
|
|
timeout_str = cnt.get("stop_grace_period", None) or STOP_GRACE_PERIOD
|
2023-03-28 16:33:02 +02:00
|
|
|
timeout = str_to_seconds(timeout_str)
|
|
|
|
if timeout is not None:
|
|
|
|
podman_stop_args.extend(["-t", str(timeout)])
|
2024-03-07 17:28:06 +01:00
|
|
|
down_tasks.append(
|
|
|
|
asyncio.create_task(
|
|
|
|
compose.podman.run([], "stop", [*podman_stop_args, cnt["name"]]), name=cnt["name"]
|
|
|
|
)
|
|
|
|
)
|
2023-12-11 08:46:20 +01:00
|
|
|
await asyncio.gather(*down_tasks)
|
2021-11-11 10:32:24 +01:00
|
|
|
for cnt in containers:
|
2022-03-01 08:53:24 +01:00
|
|
|
if cnt["_service"] in excluded:
|
|
|
|
continue
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.podman.run([], "rm", [cnt["name"]])
|
2023-02-24 12:16:36 +01:00
|
|
|
if args.remove_orphans:
|
|
|
|
names = (
|
2024-03-07 17:28:06 +01:00
|
|
|
(
|
|
|
|
await compose.podman.output(
|
|
|
|
[],
|
|
|
|
"ps",
|
|
|
|
[
|
|
|
|
"--filter",
|
|
|
|
f"label=io.podman.compose.project={compose.project_name}",
|
|
|
|
"-a",
|
|
|
|
"--format",
|
|
|
|
"{{ .Names }}",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2023-02-24 12:16:36 +01:00
|
|
|
.decode("utf-8")
|
|
|
|
.splitlines()
|
|
|
|
)
|
|
|
|
for name in names:
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.podman.run([], "stop", [*podman_args, name])
|
2023-02-24 12:16:36 +01:00
|
|
|
for name in names:
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.podman.run([], "rm", [name])
|
2021-11-22 19:34:40 +01:00
|
|
|
if args.volumes:
|
2021-12-23 00:17:34 +01:00
|
|
|
vol_names_to_keep = set()
|
|
|
|
for cnt in containers:
|
2022-03-01 08:53:24 +01:00
|
|
|
if cnt["_service"] not in excluded:
|
|
|
|
continue
|
2021-12-23 00:17:34 +01:00
|
|
|
vol_names_to_keep.update(get_volume_names(compose, cnt))
|
2023-08-09 01:12:10 +02:00
|
|
|
log.debug("keep %s", vol_names_to_keep)
|
2023-12-11 08:46:20 +01:00
|
|
|
for volume_name in await compose.podman.volume_ls():
|
2022-03-01 08:53:24 +01:00
|
|
|
if volume_name in vol_names_to_keep:
|
|
|
|
continue
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "volume", ["rm", volume_name])
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2021-12-23 00:17:34 +01:00
|
|
|
if excluded:
|
|
|
|
return
|
|
|
|
for pod in compose.pods:
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.podman.run([], "pod", ["rm", pod["name"]])
|
2021-12-23 00:17:34 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "ps", "show status of containers")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_ps(compose, args):
|
2019-10-05 21:47:04 +02:00
|
|
|
proj_name = compose.project_name
|
2023-12-11 08:46:20 +01:00
|
|
|
ps_args = ["-a", "--filter", f"label=io.podman.compose.project={proj_name}"]
|
2022-02-25 23:41:07 +01:00
|
|
|
if args.quiet is True:
|
2023-12-11 08:46:20 +01:00
|
|
|
ps_args.extend(["--format", "{{.ID}}"])
|
|
|
|
elif args.format:
|
|
|
|
ps_args.extend(["--format", args.format])
|
|
|
|
|
|
|
|
await compose.podman.run(
|
|
|
|
[],
|
|
|
|
"ps",
|
|
|
|
ps_args,
|
|
|
|
)
|
2019-10-04 21:56:51 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(
|
|
|
|
podman_compose,
|
|
|
|
"run",
|
|
|
|
"create a container similar to a service to run a one-off command",
|
|
|
|
)
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_run(compose, args):
|
2019-08-10 13:11:28 +02:00
|
|
|
create_pods(compose, args)
|
2022-02-25 19:13:10 +01:00
|
|
|
compose.assert_services(args.service)
|
2022-03-01 08:53:24 +01:00
|
|
|
container_names = compose.container_names_by_service[args.service]
|
|
|
|
container_name = container_names[0]
|
2021-12-21 21:54:27 +01:00
|
|
|
cnt = dict(compose.container_by_name[container_name])
|
2019-08-10 13:11:28 +02:00
|
|
|
deps = cnt["_deps"]
|
2022-03-29 20:17:02 +02:00
|
|
|
if deps and not args.no_deps:
|
2022-03-01 08:53:24 +01:00
|
|
|
up_args = argparse.Namespace(
|
|
|
|
**dict(
|
|
|
|
args.__dict__,
|
|
|
|
detach=True,
|
|
|
|
services=deps,
|
|
|
|
# defaults
|
|
|
|
no_build=False,
|
|
|
|
build=None,
|
|
|
|
force_recreate=False,
|
|
|
|
no_start=False,
|
|
|
|
no_cache=False,
|
|
|
|
build_arg=[],
|
2023-12-11 08:46:20 +01:00
|
|
|
parallel=1,
|
2024-03-07 17:28:06 +01:00
|
|
|
remove_orphans=True,
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
2021-11-14 00:37:22 +01:00
|
|
|
)
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.commands["up"](compose, up_args)
|
2023-04-21 12:15:23 +02:00
|
|
|
|
|
|
|
build_args = argparse.Namespace(
|
2024-03-07 17:28:06 +01:00
|
|
|
services=[args.service], if_not_exists=(not args.build), build_arg=[], **args.__dict__
|
2023-04-21 12:15:23 +02:00
|
|
|
)
|
2023-12-11 08:46:20 +01:00
|
|
|
await compose.commands["build"](compose, build_args)
|
2023-04-21 12:15:23 +02:00
|
|
|
|
2024-03-09 22:17:38 +01:00
|
|
|
compose_run_update_container_from_args(compose, cnt, args)
|
|
|
|
# run podman
|
|
|
|
podman_args = await container_to_args(compose, cnt, args.detach)
|
|
|
|
if not args.detach:
|
|
|
|
podman_args.insert(1, "-i")
|
|
|
|
if args.rm:
|
|
|
|
podman_args.insert(1, "--rm")
|
|
|
|
p = await compose.podman.run([], "run", podman_args)
|
|
|
|
sys.exit(p)
|
|
|
|
|
|
|
|
|
|
|
|
def compose_run_update_container_from_args(compose, cnt, args):
|
2019-08-10 13:11:28 +02:00
|
|
|
# adjust one-off container options
|
2024-03-07 17:28:06 +01:00
|
|
|
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
|
2019-08-10 13:11:28 +02:00
|
|
|
cnt["name"] = args.name or name0
|
2022-03-01 08:53:24 +01:00
|
|
|
if args.entrypoint:
|
|
|
|
cnt["entrypoint"] = args.entrypoint
|
|
|
|
if args.user:
|
|
|
|
cnt["user"] = args.user
|
|
|
|
if args.workdir:
|
|
|
|
cnt["working_dir"] = args.workdir
|
|
|
|
env = dict(cnt.get("environment", {}))
|
2021-05-06 00:17:01 +02:00
|
|
|
if args.env:
|
2024-03-01 22:13:36 +01:00
|
|
|
additional_env_vars = dict(map(lambda each: each.split("=", maxsplit=1), args.env))
|
2021-05-06 00:17:01 +02:00
|
|
|
env.update(additional_env_vars)
|
2022-03-01 08:53:24 +01:00
|
|
|
cnt["environment"] = env
|
2019-08-10 13:11:28 +02:00
|
|
|
if not args.service_ports:
|
|
|
|
for k in ("expose", "publishall", "ports"):
|
2022-03-01 08:53:24 +01:00
|
|
|
try:
|
|
|
|
del cnt[k]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2019-08-10 13:11:28 +02:00
|
|
|
if args.volume:
|
|
|
|
# TODO: handle volumes
|
2022-03-29 20:17:02 +02:00
|
|
|
volumes = clone(cnt.get("volumes", None) or [])
|
|
|
|
volumes.extend(args.volume)
|
|
|
|
cnt["volumes"] = volumes
|
2022-03-01 08:53:24 +01:00
|
|
|
cnt["tty"] = not args.T
|
2019-11-08 20:06:32 +01:00
|
|
|
if args.cnt_command is not None and len(args.cnt_command) > 0:
|
2022-03-01 08:53:24 +01:00
|
|
|
cnt["command"] = args.cnt_command
|
2022-02-20 21:53:24 +01:00
|
|
|
# can't restart and --rm
|
2022-03-01 08:53:24 +01:00
|
|
|
if args.rm and "restart" in cnt:
|
|
|
|
del cnt["restart"]
|
2019-11-17 08:09:41 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "exec", "execute a command in a running container")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_exec(compose, args):
|
2022-02-25 19:13:10 +01:00
|
|
|
compose.assert_services(args.service)
|
2022-03-01 08:53:24 +01:00
|
|
|
container_names = compose.container_names_by_service[args.service]
|
|
|
|
container_name = container_names[args.index - 1]
|
2021-04-26 13:31:09 +02:00
|
|
|
cnt = compose.container_by_name[container_name]
|
2024-03-09 21:52:46 +01:00
|
|
|
podman_args = compose_exec_args(cnt, container_name, args)
|
|
|
|
p = await compose.podman.run([], "exec", podman_args)
|
|
|
|
sys.exit(p)
|
|
|
|
|
|
|
|
|
|
|
|
def compose_exec_args(cnt, container_name, args):
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args = ["--interactive"]
|
|
|
|
if args.privileged:
|
|
|
|
podman_args += ["--privileged"]
|
|
|
|
if args.user:
|
|
|
|
podman_args += ["--user", args.user]
|
|
|
|
if args.workdir:
|
|
|
|
podman_args += ["--workdir", args.workdir]
|
|
|
|
if not args.T:
|
|
|
|
podman_args += ["--tty"]
|
|
|
|
env = dict(cnt.get("environment", {}))
|
2021-04-26 13:31:09 +02:00
|
|
|
if args.env:
|
2023-02-28 17:15:55 +01:00
|
|
|
additional_env_vars = dict(
|
2024-03-01 22:13:36 +01:00
|
|
|
map(lambda each: each.split("=", maxsplit=1) if "=" in each else (each, None), args.env)
|
2023-02-28 17:15:55 +01:00
|
|
|
)
|
2021-04-26 14:07:14 +02:00
|
|
|
env.update(additional_env_vars)
|
|
|
|
for name, value in env.items():
|
2023-02-28 17:15:55 +01:00
|
|
|
podman_args += ["--env", f"{name}" if value is None else f"{name}={value}"]
|
2021-04-26 14:07:14 +02:00
|
|
|
podman_args += [container_name]
|
|
|
|
if args.cnt_command is not None and len(args.cnt_command) > 0:
|
|
|
|
podman_args += args.cnt_command
|
2024-03-09 21:52:46 +01:00
|
|
|
return podman_args
|
2021-04-26 13:31:09 +02:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2023-12-10 07:51:54 +01:00
|
|
|
async def transfer_service_status(compose, args, action):
|
2019-08-09 15:31:56 +02:00
|
|
|
# TODO: handle dependencies, handle creations
|
|
|
|
container_names_by_service = compose.container_names_by_service
|
2021-12-13 21:19:26 +01:00
|
|
|
if not args.services:
|
|
|
|
args.services = container_names_by_service.keys()
|
2022-02-25 19:13:10 +01:00
|
|
|
compose.assert_services(args.services)
|
2019-08-09 15:31:56 +02:00
|
|
|
targets = []
|
|
|
|
for service in args.services:
|
|
|
|
if service not in container_names_by_service:
|
|
|
|
raise ValueError("unknown service: " + service)
|
|
|
|
targets.extend(container_names_by_service[service])
|
2022-03-01 08:53:24 +01:00
|
|
|
if action in ["stop", "restart"]:
|
2021-12-13 21:19:26 +01:00
|
|
|
targets = list(reversed(targets))
|
2023-03-29 10:30:24 +02:00
|
|
|
timeout_global = getattr(args, "timeout", None)
|
2023-12-10 07:51:54 +01:00
|
|
|
tasks = []
|
2019-08-09 15:31:56 +02:00
|
|
|
for target in targets:
|
2023-05-18 18:00:56 +02:00
|
|
|
podman_args = []
|
2023-03-29 00:31:42 +02:00
|
|
|
if action != "start":
|
2023-03-29 10:30:24 +02:00
|
|
|
timeout = timeout_global
|
2023-03-29 00:31:42 +02:00
|
|
|
if timeout is None:
|
2023-04-09 11:47:15 +02:00
|
|
|
timeout_str = (
|
|
|
|
compose.container_by_name[target].get("stop_grace_period", None)
|
|
|
|
or STOP_GRACE_PERIOD
|
|
|
|
)
|
2023-03-29 00:31:42 +02:00
|
|
|
timeout = str_to_seconds(timeout_str)
|
|
|
|
if timeout is not None:
|
|
|
|
podman_args.extend(["-t", str(timeout)])
|
2023-12-11 08:46:20 +01:00
|
|
|
tasks.append(asyncio.create_task(compose.podman.run([], action, podman_args + [target])))
|
2023-12-10 07:51:54 +01:00
|
|
|
await asyncio.gather(*tasks)
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "start", "start specific services")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_start(compose, args):
|
2023-12-11 08:46:20 +01:00
|
|
|
await transfer_service_status(compose, args, "start")
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
@cmd_run(podman_compose, "stop", "stop specific services")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_stop(compose, args):
|
2023-12-11 08:46:20 +01:00
|
|
|
await transfer_service_status(compose, args, "stop")
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "restart", "restart specific services")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_restart(compose, args):
|
2023-12-11 08:46:20 +01:00
|
|
|
await transfer_service_status(compose, args, "restart")
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2019-03-23 21:04:07 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
@cmd_run(podman_compose, "logs", "show logs from services")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_logs(compose, args):
|
2019-11-05 08:42:53 +01:00
|
|
|
container_names_by_service = compose.container_names_by_service
|
2021-12-13 21:41:07 +01:00
|
|
|
if not args.services and not args.latest:
|
2021-12-13 21:20:45 +01:00
|
|
|
args.services = container_names_by_service.keys()
|
2022-02-25 19:13:10 +01:00
|
|
|
compose.assert_services(args.services)
|
2021-12-13 21:20:45 +01:00
|
|
|
targets = []
|
|
|
|
for service in args.services:
|
|
|
|
targets.extend(container_names_by_service[service])
|
2021-05-17 14:03:47 +02:00
|
|
|
podman_args = []
|
2019-11-05 08:42:53 +01:00
|
|
|
if args.follow:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.append("-f")
|
2021-12-13 21:41:07 +01:00
|
|
|
if args.latest:
|
|
|
|
podman_args.append("-l")
|
|
|
|
if args.names:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.append("-n")
|
2021-12-13 21:41:07 +01:00
|
|
|
if args.since:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--since", args.since])
|
2019-11-05 08:42:53 +01:00
|
|
|
# the default value is to print all logs which is in podman = 0 and not
|
|
|
|
# needed to be passed
|
2022-03-01 08:53:24 +01:00
|
|
|
if args.tail and args.tail != "all":
|
|
|
|
podman_args.extend(["--tail", args.tail])
|
2019-11-05 08:42:53 +01:00
|
|
|
if args.timestamps:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.append("-t")
|
2021-12-13 21:41:07 +01:00
|
|
|
if args.until:
|
2022-03-01 08:53:24 +01:00
|
|
|
podman_args.extend(["--until", args.until])
|
2021-12-13 21:20:45 +01:00
|
|
|
for target in targets:
|
|
|
|
podman_args.append(target)
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "logs", podman_args)
|
2019-11-05 08:42:53 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "config", "displays the compose file")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_config(compose, args):
|
2022-04-04 05:33:53 +02:00
|
|
|
if args.services:
|
2022-05-10 00:30:03 +02:00
|
|
|
for service in compose.services:
|
|
|
|
print(service)
|
2022-04-04 05:33:53 +02:00
|
|
|
return
|
2021-12-31 00:54:32 +01:00
|
|
|
print(compose.merged_yaml)
|
2021-12-30 14:39:35 +01:00
|
|
|
|
2022-05-10 00:30:03 +02:00
|
|
|
|
|
|
|
@cmd_run(podman_compose, "port", "Prints the public port for a port binding.")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_port(compose, args):
|
2022-05-10 00:30:03 +02:00
|
|
|
# TODO - deal with pod index
|
2022-04-07 14:29:54 +02:00
|
|
|
compose.assert_services(args.service)
|
|
|
|
containers = compose.container_names_by_service[args.service]
|
2022-05-10 00:30:03 +02:00
|
|
|
container_ports = list(
|
|
|
|
itertools.chain(*(compose.container_by_name[c]["ports"] for c in containers))
|
|
|
|
)
|
2022-04-07 14:29:54 +02:00
|
|
|
|
|
|
|
def _published_target(port_string):
|
2022-05-10 00:30:03 +02:00
|
|
|
published, target = port_string.split(":")[-2:]
|
2022-04-07 14:29:54 +02:00
|
|
|
return int(published), int(target)
|
|
|
|
|
2022-05-10 00:30:03 +02:00
|
|
|
select_udp = args.protocol == "udp"
|
2022-04-07 14:29:54 +02:00
|
|
|
published, target = None, None
|
|
|
|
for p in container_ports:
|
2022-05-10 00:30:03 +02:00
|
|
|
is_udp = p[-4:] == "/udp"
|
|
|
|
|
2022-04-07 14:29:54 +02:00
|
|
|
if select_udp and is_udp:
|
|
|
|
published, target = _published_target(p[-4:])
|
|
|
|
if not select_udp and not is_udp:
|
|
|
|
published, target = _published_target(p)
|
|
|
|
|
|
|
|
if target == args.private_port:
|
|
|
|
print(published)
|
|
|
|
return
|
|
|
|
|
2022-07-13 19:45:16 +02:00
|
|
|
|
2022-07-13 19:13:38 +02:00
|
|
|
@cmd_run(podman_compose, "pause", "Pause all running containers")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_pause(compose, args):
|
2022-07-13 19:13:38 +02:00
|
|
|
container_names_by_service = compose.container_names_by_service
|
|
|
|
if not args.services:
|
|
|
|
args.services = container_names_by_service.keys()
|
|
|
|
targets = []
|
|
|
|
for service in args.services:
|
|
|
|
targets.extend(container_names_by_service[service])
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "pause", targets)
|
2022-06-30 14:04:48 +02:00
|
|
|
|
2022-07-13 19:45:16 +02:00
|
|
|
|
2022-07-13 19:13:38 +02:00
|
|
|
@cmd_run(podman_compose, "unpause", "Unpause all running containers")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_unpause(compose, args):
|
2022-07-13 19:13:38 +02:00
|
|
|
container_names_by_service = compose.container_names_by_service
|
|
|
|
if not args.services:
|
|
|
|
args.services = container_names_by_service.keys()
|
|
|
|
targets = []
|
|
|
|
for service in args.services:
|
|
|
|
targets.extend(container_names_by_service[service])
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "unpause", targets)
|
2022-06-30 14:04:48 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
@cmd_run(podman_compose, "kill", "Kill one or more running containers with a specific signal")
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_kill(compose, args):
|
2022-07-13 23:59:10 +02:00
|
|
|
# to ensure that the user did not execute the command by mistake
|
2022-07-13 23:45:24 +02:00
|
|
|
if not args.services and not args.all:
|
2022-09-19 21:21:32 +02:00
|
|
|
log.fatal(
|
2022-07-13 23:59:10 +02:00
|
|
|
"Error: you must provide at least one service name or use (--all) to kill all services"
|
|
|
|
)
|
2022-07-13 23:45:24 +02:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
container_names_by_service = compose.container_names_by_service
|
|
|
|
podman_args = []
|
|
|
|
|
|
|
|
if args.signal:
|
|
|
|
podman_args.extend(["--signal", args.signal])
|
|
|
|
|
2022-07-13 23:59:10 +02:00
|
|
|
if args.all is True:
|
2022-07-13 23:45:24 +02:00
|
|
|
services = container_names_by_service.keys()
|
|
|
|
targets = []
|
|
|
|
for service in services:
|
|
|
|
targets.extend(container_names_by_service[service])
|
|
|
|
for target in targets:
|
|
|
|
podman_args.append(target)
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "kill", podman_args)
|
|
|
|
elif args.services:
|
2022-07-13 23:45:24 +02:00
|
|
|
targets = []
|
|
|
|
for service in args.services:
|
|
|
|
targets.extend(container_names_by_service[service])
|
|
|
|
for target in targets:
|
|
|
|
podman_args.append(target)
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "kill", podman_args)
|
2022-07-13 23:45:24 +02:00
|
|
|
|
2022-07-13 23:59:10 +02:00
|
|
|
|
2022-07-16 01:48:28 +02:00
|
|
|
@cmd_run(
|
2023-06-23 17:58:43 +02:00
|
|
|
podman_compose,
|
|
|
|
"stats",
|
|
|
|
"Display percentage of CPU, memory, network I/O, block I/O and PIDs for services.",
|
2022-07-16 01:48:28 +02:00
|
|
|
)
|
2023-12-10 07:51:54 +01:00
|
|
|
async def compose_stats(compose, args):
|
2022-07-16 01:48:28 +02:00
|
|
|
container_names_by_service = compose.container_names_by_service
|
|
|
|
if not args.services:
|
|
|
|
args.services = container_names_by_service.keys()
|
|
|
|
targets = []
|
|
|
|
podman_args = []
|
|
|
|
if args.interval:
|
|
|
|
podman_args.extend(["--interval", args.interval])
|
|
|
|
if args.format:
|
|
|
|
podman_args.extend(["--format", args.format])
|
|
|
|
if args.no_reset:
|
|
|
|
podman_args.append("--no-reset")
|
|
|
|
if args.no_stream:
|
|
|
|
podman_args.append("--no-stream")
|
|
|
|
|
|
|
|
for service in args.services:
|
|
|
|
targets.extend(container_names_by_service[service])
|
|
|
|
for target in targets:
|
|
|
|
podman_args.append(target)
|
|
|
|
|
|
|
|
try:
|
2023-12-10 07:51:54 +01:00
|
|
|
await compose.podman.run([], "stats", podman_args)
|
2022-07-16 01:48:28 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
###################
|
|
|
|
# command arguments parsing
|
|
|
|
###################
|
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_parse(podman_compose, "version")
|
2021-12-29 22:23:24 +01:00
|
|
|
def compose_version_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--format",
|
|
|
|
choices=["pretty", "json"],
|
|
|
|
default="pretty",
|
|
|
|
help="Format the output",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--short",
|
|
|
|
action="store_true",
|
|
|
|
help="Shows only Podman Compose's version number",
|
|
|
|
)
|
|
|
|
|
2021-12-29 22:23:24 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
@cmd_parse(podman_compose, "up")
|
2019-08-14 17:49:21 +02:00
|
|
|
def compose_up_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--detach",
|
|
|
|
action="store_true",
|
2023-04-09 19:05:15 +02:00
|
|
|
help="Detached mode: Run container in the background, print new container name. \
|
|
|
|
Incompatible with --abort-on-container-exit.",
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("--no-color", action="store_true", help="Produce monochrome output.")
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--quiet-pull",
|
|
|
|
action="store_true",
|
|
|
|
help="Pull without printing progress information.",
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("--no-deps", action="store_true", help="Don't start linked services.")
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--force-recreate",
|
|
|
|
action="store_true",
|
|
|
|
help="Recreate containers even if their configuration and image haven't changed.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--always-recreate-deps",
|
|
|
|
action="store_true",
|
|
|
|
help="Recreate dependent containers. Incompatible with --no-recreate.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-recreate",
|
|
|
|
action="store_true",
|
2024-03-08 22:46:24 +01:00
|
|
|
help="If containers already exist, don't recreate them. Incompatible with --force-recreate "
|
|
|
|
"and -V.",
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-build",
|
|
|
|
action="store_true",
|
|
|
|
help="Don't build an image, even if it's missing.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-start",
|
|
|
|
action="store_true",
|
|
|
|
help="Don't start the services after creating them.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--build", action="store_true", help="Build images before starting containers."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--abort-on-container-exit",
|
|
|
|
action="store_true",
|
|
|
|
help="Stops all containers if any container was stopped. Incompatible with -d.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--timeout",
|
|
|
|
type=int,
|
2023-03-29 00:31:42 +02:00
|
|
|
default=None,
|
2024-03-08 22:46:24 +01:00
|
|
|
help="Use this timeout in seconds for container shutdown when attached or when containers "
|
|
|
|
"are already running. (default: 10)",
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-V",
|
|
|
|
"--renew-anon-volumes",
|
|
|
|
action="store_true",
|
|
|
|
help="Recreate anonymous volumes instead of retrieving data from the previous containers.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--remove-orphans",
|
|
|
|
action="store_true",
|
|
|
|
help="Remove containers for services not defined in the Compose file.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--scale",
|
|
|
|
metavar="SERVICE=NUM",
|
|
|
|
action="append",
|
2024-03-08 22:46:24 +01:00
|
|
|
help="Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if "
|
|
|
|
"present.",
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--exit-code-from",
|
|
|
|
metavar="SERVICE",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
2024-03-08 22:46:24 +01:00
|
|
|
help="Return the exit code of the selected service container. "
|
|
|
|
"Implies --abort-on-container-exit.",
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@cmd_parse(podman_compose, "down")
|
2021-11-22 19:34:40 +01:00
|
|
|
def compose_down_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-v",
|
|
|
|
"--volumes",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
2021-11-22 19:34:40 +01:00
|
|
|
help="Remove named volumes declared in the `volumes` section of the Compose file and "
|
2022-03-01 08:53:24 +01:00
|
|
|
"anonymous volumes attached to containers.",
|
|
|
|
)
|
2023-02-24 12:16:36 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--remove-orphans",
|
|
|
|
action="store_true",
|
|
|
|
help="Remove containers for services not defined in the Compose file.",
|
|
|
|
)
|
2021-11-22 19:34:40 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_parse(podman_compose, "run")
|
2019-08-10 13:11:28 +02:00
|
|
|
def compose_run_parse(parser):
|
2023-04-21 12:15:23 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--build", action="store_true", help="Build images before starting containers."
|
|
|
|
)
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--detach",
|
|
|
|
action="store_true",
|
|
|
|
help="Detached mode: Run container in the background, print new container name.",
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("--name", type=str, default=None, help="Assign a name to the container")
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--entrypoint",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
help="Override the entrypoint of the image.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-e",
|
|
|
|
"--env",
|
|
|
|
metavar="KEY=VAL",
|
|
|
|
action="append",
|
|
|
|
help="Set an environment variable (can be used multiple times)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--label",
|
|
|
|
metavar="KEY=VAL",
|
|
|
|
action="append",
|
|
|
|
help="Add or override a label (can be used multiple times)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-u", "--user", type=str, default=None, help="Run as specified username or uid"
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("--no-deps", action="store_true", help="Don't start linked services")
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--rm",
|
|
|
|
action="store_true",
|
|
|
|
help="Remove container after run. Ignored in detached mode.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--publish",
|
|
|
|
action="append",
|
|
|
|
help="Publish a container's port(s) to the host (can be used multiple times)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--service-ports",
|
|
|
|
action="store_true",
|
|
|
|
help="Run command with the service's ports enabled and mapped to the host.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-v",
|
|
|
|
"--volume",
|
|
|
|
action="append",
|
|
|
|
help="Bind mount a volume (can be used multiple times)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-T",
|
|
|
|
action="store_true",
|
|
|
|
help="Disable pseudo-tty allocation. By default `podman-compose run` allocates a TTY.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-w",
|
|
|
|
"--workdir",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
help="Working directory inside the container",
|
|
|
|
)
|
|
|
|
parser.add_argument("service", metavar="service", nargs=None, help="service name")
|
|
|
|
parser.add_argument(
|
|
|
|
"cnt_command",
|
|
|
|
metavar="command",
|
|
|
|
nargs=argparse.REMAINDER,
|
|
|
|
help="command and its arguments",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@cmd_parse(podman_compose, "exec")
|
2022-02-25 21:59:15 +01:00
|
|
|
def compose_exec_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--detach",
|
|
|
|
action="store_true",
|
|
|
|
help="Detached mode: Run container in the background, print new container name.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--privileged",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Give the process extended Linux capabilities inside the container",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-u", "--user", type=str, default=None, help="Run as specified username or uid"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-T",
|
|
|
|
action="store_true",
|
|
|
|
help="Disable pseudo-tty allocation. By default `podman-compose run` allocates a TTY.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--index",
|
|
|
|
type=int,
|
|
|
|
default=1,
|
|
|
|
help="Index of the container if there are multiple instances of a service",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-e",
|
|
|
|
"--env",
|
|
|
|
metavar="KEY=VAL",
|
|
|
|
action="append",
|
|
|
|
help="Set an environment variable (can be used multiple times)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-w",
|
|
|
|
"--workdir",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
help="Working directory inside the container",
|
|
|
|
)
|
|
|
|
parser.add_argument("service", metavar="service", nargs=None, help="service name")
|
|
|
|
parser.add_argument(
|
|
|
|
"cnt_command",
|
|
|
|
metavar="command",
|
|
|
|
nargs=argparse.REMAINDER,
|
|
|
|
help="command and its arguments",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@cmd_parse(podman_compose, ["down", "stop", "restart"])
|
2019-08-10 13:11:28 +02:00
|
|
|
def compose_parse_timeout(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--timeout",
|
2019-08-09 15:31:56 +02:00
|
|
|
help="Specify a shutdown timeout in seconds. ",
|
2022-03-01 08:53:24 +01:00
|
|
|
type=int,
|
2023-03-29 00:31:42 +02:00
|
|
|
default=None,
|
2022-03-01 08:53:24 +01:00
|
|
|
)
|
|
|
|
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
@cmd_parse(podman_compose, ["logs"])
|
2019-11-05 08:42:53 +01:00
|
|
|
def compose_logs_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--follow",
|
|
|
|
action="store_true",
|
|
|
|
help="Follow log output. The default is false",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--latest",
|
|
|
|
action="store_true",
|
|
|
|
help="Act on the latest container podman is aware of",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--names",
|
|
|
|
action="store_true",
|
|
|
|
help="Output the container name in the log",
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("--since", help="Show logs since TIMESTAMP", type=str, default=None)
|
|
|
|
parser.add_argument("-t", "--timestamps", action="store_true", help="Show timestamps.")
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--tail",
|
|
|
|
help="Number of lines to show from the end of the logs for each " "container.",
|
|
|
|
type=str,
|
|
|
|
default="all",
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("--until", help="Show logs until TIMESTAMP", type=str, default=None)
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"services", metavar="services", nargs="*", default=None, help="service names"
|
|
|
|
)
|
|
|
|
|
2022-03-12 21:17:26 +01:00
|
|
|
|
2022-03-12 21:08:24 +01:00
|
|
|
@cmd_parse(podman_compose, "systemd")
|
|
|
|
def compose_systemd_parse(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"-a",
|
|
|
|
"--action",
|
2022-03-14 12:05:20 +01:00
|
|
|
choices=["register", "create-unit", "list", "ls"],
|
2022-03-12 21:08:24 +01:00
|
|
|
default="register",
|
|
|
|
help="create systemd unit file or register compose stack to it",
|
|
|
|
)
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2022-03-12 21:17:26 +01:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
@cmd_parse(podman_compose, "pull")
|
2021-08-05 11:24:35 +02:00
|
|
|
def compose_pull_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--force-local",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Also pull unprefixed images for services which have a build section",
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("services", metavar="services", nargs="*", help="services to pull")
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2021-08-05 11:24:35 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
@cmd_parse(podman_compose, "push")
|
2019-08-09 15:31:56 +02:00
|
|
|
def compose_push_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--ignore-push-failures",
|
|
|
|
action="store_true",
|
|
|
|
help="Push what it can and ignores images with push failures. (not implemented)",
|
|
|
|
)
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("services", metavar="services", nargs="*", help="services to push")
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_parse(podman_compose, "ps")
|
2019-10-04 21:56:51 +02:00
|
|
|
def compose_ps_parse(parser):
|
2024-03-07 17:28:06 +01:00
|
|
|
parser.add_argument("-q", "--quiet", help="Only display container IDs", action="store_true")
|
2019-08-09 15:31:56 +02:00
|
|
|
|
2022-03-01 08:53:24 +01:00
|
|
|
|
|
|
|
@cmd_parse(podman_compose, ["build", "up"])
|
2022-02-25 21:59:15 +01:00
|
|
|
def compose_build_up_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--pull",
|
|
|
|
help="attempt to pull a newer version of the image",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--pull-always",
|
2024-03-08 22:46:24 +01:00
|
|
|
help="attempt to pull a newer version of the image, Raise an error even if the image is "
|
|
|
|
"present locally.",
|
2022-03-01 08:53:24 +01:00
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--build-arg",
|
|
|
|
metavar="key=val",
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
help="Set build-time variables for services.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-cache",
|
|
|
|
help="Do not use cache when building the image.",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@cmd_parse(podman_compose, ["build", "up", "down", "start", "stop", "restart"])
|
2021-11-13 23:28:43 +01:00
|
|
|
def compose_build_parse(parser):
|
2022-03-01 08:53:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"services",
|
|
|
|
metavar="services",
|
|
|
|
nargs="*",
|
|
|
|
default=None,
|
|
|
|
help="affected services",
|
|
|
|
)
|
|
|
|
|
2021-11-13 23:28:43 +01:00
|
|
|
|
2022-04-04 05:33:53 +02:00
|
|
|
@cmd_parse(podman_compose, "config")
|
|
|
|
def compose_config_parse(parser):
|
2023-06-11 07:19:14 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-normalize", help="Don't normalize compose model.", action="store_true"
|
|
|
|
)
|
2022-04-04 05:33:53 +02:00
|
|
|
parser.add_argument(
|
2022-05-10 00:30:03 +02:00
|
|
|
"--services", help="Print the service names, one per line.", action="store_true"
|
2022-04-04 05:33:53 +02:00
|
|
|
)
|
|
|
|
|
2022-05-10 00:30:03 +02:00
|
|
|
|
2022-04-07 14:29:54 +02:00
|
|
|
@cmd_parse(podman_compose, "port")
|
|
|
|
def compose_port_parse(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"--index",
|
|
|
|
type=int,
|
|
|
|
default=1,
|
|
|
|
help="index of the container if there are multiple instances of a service",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--protocol",
|
|
|
|
choices=["tcp", "udp"],
|
|
|
|
default="tcp",
|
|
|
|
help="tcp or udp",
|
|
|
|
)
|
|
|
|
parser.add_argument("service", metavar="service", nargs=None, help="service name")
|
2022-05-10 00:30:03 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"private_port",
|
|
|
|
metavar="private_port",
|
|
|
|
nargs=None,
|
|
|
|
type=int,
|
|
|
|
help="private port",
|
|
|
|
)
|
|
|
|
|
2022-07-13 19:45:16 +02:00
|
|
|
|
2022-07-13 19:13:38 +02:00
|
|
|
@cmd_parse(podman_compose, ["pause", "unpause"])
|
|
|
|
def compose_pause_unpause_parse(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"services", metavar="services", nargs="*", default=None, help="service names"
|
|
|
|
)
|
2022-04-04 05:33:53 +02:00
|
|
|
|
2022-07-13 19:45:16 +02:00
|
|
|
|
2022-07-13 23:45:24 +02:00
|
|
|
@cmd_parse(podman_compose, ["kill"])
|
|
|
|
def compose_kill_parse(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"services", metavar="services", nargs="*", default=None, help="service names"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--signal",
|
|
|
|
type=str,
|
|
|
|
help="Signal to send to the container (default 'KILL')",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-a",
|
|
|
|
"--all",
|
|
|
|
help="Signal all running containers",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-16 01:48:28 +02:00
|
|
|
@cmd_parse(podman_compose, ["stats"])
|
|
|
|
def compose_stats_parse(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"services", metavar="services", nargs="*", default=None, help="service names"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-i",
|
|
|
|
"--interval",
|
|
|
|
type=int,
|
|
|
|
help="Time in seconds between stats reports (default 5)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-reset",
|
|
|
|
help="Disable resetting the screen between intervals",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-stream",
|
|
|
|
help="Disable streaming stats and only pull the first result",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
@cmd_parse(podman_compose, ["ps", "stats"])
|
|
|
|
def compose_format_parse(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--format",
|
|
|
|
type=str,
|
|
|
|
help="Pretty-print container statistics to JSON or using a Go template",
|
|
|
|
)
|
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
|
2024-02-05 21:24:32 +01:00
|
|
|
async def async_main():
|
2023-12-10 07:51:54 +01:00
|
|
|
await podman_compose.run()
|
2019-03-23 21:07:06 +01:00
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
|
2024-02-05 21:24:32 +01:00
|
|
|
def main():
|
2024-02-05 22:22:12 +01:00
|
|
|
asyncio.run(async_main())
|
2022-03-01 08:53:24 +01:00
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
|
2019-03-23 21:04:07 +01:00
|
|
|
if __name__ == "__main__":
|
2024-02-05 21:24:32 +01:00
|
|
|
main()
|