container 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # Copyright (C) 2025 Guillaume Tucker
  4. """Containerized builds"""
  5. import abc
  6. import argparse
  7. import logging
  8. import os
  9. import pathlib
  10. import shutil
  11. import subprocess
  12. import sys
  13. import uuid
  14. class ContainerRuntime(abc.ABC):
  15. """Base class for a container runtime implementation"""
  16. name = None # Property defined in each implementation class
  17. def __init__(self, args, logger):
  18. self._uid = args.uid or os.getuid()
  19. self._gid = args.gid or args.uid or os.getgid()
  20. self._env_file = args.env_file
  21. self._shell = args.shell
  22. self._logger = logger
  23. @classmethod
  24. def is_present(cls):
  25. """Determine whether the runtime is present on the system"""
  26. return shutil.which(cls.name) is not None
  27. @abc.abstractmethod
  28. def _do_run(self, image, cmd, container_name):
  29. """Runtime-specific handler to run a command in a container"""
  30. @abc.abstractmethod
  31. def _do_abort(self, container_name):
  32. """Runtime-specific handler to abort a running container"""
  33. def run(self, image, cmd):
  34. """Run a command in a runtime container"""
  35. container_name = str(uuid.uuid4())
  36. self._logger.debug("container: %s", container_name)
  37. try:
  38. return self._do_run(image, cmd, container_name)
  39. except KeyboardInterrupt:
  40. self._logger.error("user aborted")
  41. self._do_abort(container_name)
  42. return 1
  43. class CommonRuntime(ContainerRuntime):
  44. """Common logic for Docker and Podman"""
  45. def _do_run(self, image, cmd, container_name):
  46. cmdline = [self.name, 'run']
  47. cmdline += self._get_opts(container_name)
  48. cmdline.append(image)
  49. cmdline += cmd
  50. self._logger.debug('command: %s', ' '.join(cmdline))
  51. return subprocess.call(cmdline)
  52. def _get_opts(self, container_name):
  53. opts = [
  54. '--name', container_name,
  55. '--rm',
  56. '--volume', f'{pathlib.Path.cwd()}:/src',
  57. '--workdir', '/src',
  58. ]
  59. if self._env_file:
  60. opts += ['--env-file', self._env_file]
  61. if self._shell:
  62. opts += ['--interactive', '--tty']
  63. return opts
  64. def _do_abort(self, container_name):
  65. subprocess.call([self.name, 'kill', container_name])
  66. class DockerRuntime(CommonRuntime):
  67. """Run a command in a Docker container"""
  68. name = 'docker'
  69. def _get_opts(self, container_name):
  70. return super()._get_opts(container_name) + [
  71. '--user', f'{self._uid}:{self._gid}'
  72. ]
  73. class PodmanRuntime(CommonRuntime):
  74. """Run a command in a Podman container"""
  75. name = 'podman'
  76. def _get_opts(self, container_name):
  77. return super()._get_opts(container_name) + [
  78. '--userns', f'keep-id:uid={self._uid},gid={self._gid}',
  79. ]
  80. class Runtimes:
  81. """List of all supported runtimes"""
  82. runtimes = [PodmanRuntime, DockerRuntime]
  83. @classmethod
  84. def get_names(cls):
  85. """Get a list of all the runtime names"""
  86. return list(runtime.name for runtime in cls.runtimes)
  87. @classmethod
  88. def get(cls, name):
  89. """Get a single runtime class matching the given name"""
  90. for runtime in cls.runtimes:
  91. if runtime.name == name:
  92. if not runtime.is_present():
  93. raise ValueError(f"runtime not found: {name}")
  94. return runtime
  95. raise ValueError(f"unknown runtime: {name}")
  96. @classmethod
  97. def find(cls):
  98. """Find the first runtime present on the system"""
  99. for runtime in cls.runtimes:
  100. if runtime.is_present():
  101. return runtime
  102. raise ValueError("no runtime found")
  103. def _get_logger(verbose):
  104. """Set up a logger with the appropriate level"""
  105. logger = logging.getLogger('container')
  106. handler = logging.StreamHandler()
  107. handler.setFormatter(logging.Formatter(
  108. fmt='[container {levelname}] {message}', style='{'
  109. ))
  110. logger.addHandler(handler)
  111. logger.setLevel(logging.DEBUG if verbose is True else logging.INFO)
  112. return logger
  113. def main(args):
  114. """Main entry point for the container tool"""
  115. logger = _get_logger(args.verbose)
  116. try:
  117. cls = Runtimes.get(args.runtime) if args.runtime else Runtimes.find()
  118. except ValueError as ex:
  119. logger.error(ex)
  120. return 1
  121. logger.debug("runtime: %s", cls.name)
  122. logger.debug("image: %s", args.image)
  123. return cls(args, logger).run(args.image, args.cmd)
  124. if __name__ == '__main__':
  125. parser = argparse.ArgumentParser(
  126. 'container',
  127. description="See the documentation for more details: "
  128. "https://docs.kernel.org/dev-tools/container.html"
  129. )
  130. parser.add_argument(
  131. '-e', '--env-file',
  132. help="Path to an environment file to load in the container."
  133. )
  134. parser.add_argument(
  135. '-g', '--gid',
  136. help="Group ID to use inside the container."
  137. )
  138. parser.add_argument(
  139. '-i', '--image', required=True,
  140. help="Container image name."
  141. )
  142. parser.add_argument(
  143. '-r', '--runtime', choices=Runtimes.get_names(),
  144. help="Container runtime name. If not specified, the first one found "
  145. "on the system will be used i.e. Podman if present, otherwise Docker."
  146. )
  147. parser.add_argument(
  148. '-s', '--shell', action='store_true',
  149. help="Run the container in an interactive shell."
  150. )
  151. parser.add_argument(
  152. '-u', '--uid',
  153. help="User ID to use inside the container. If the -g option is not "
  154. "specified, the user ID will also be set as the group ID."
  155. )
  156. parser.add_argument(
  157. '-v', '--verbose', action='store_true',
  158. help="Enable verbose output."
  159. )
  160. parser.add_argument(
  161. 'cmd', nargs='+',
  162. help="Command to run in the container"
  163. )
  164. sys.exit(main(parser.parse_args(sys.argv[1:])))