1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
# -*- coding: ascii -*-
#
# Copyright 2019 - 2025
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Containerized Builds
~~~~~~~~~~~~~~~~~~~~
"""
import platform as _platform
class Container(object):
"""Container context"""
_WAIT_COMMAND = ("/bin/sh", "-c", "while sleep 60; do :; done")
def __init__(self, ctx, name, image):
"""
Initialization
Parameters:
ctx (Context):
Context object
name (str):
Container name
image (str):
Image
login (callable):
Login function
"""
self._ctx = ctx
# pylint: disable-next = condition-evals-to-constant
if 0 and _platform.system() == "Darwin" and ctx.which("container"):
self._command = ContainerCommand(ctx)
else:
self._command = DockerCommand(ctx)
self._image = image
self._name = name
self._running = False
def login(self):
"""Login to registry if needed"""
def __enter__(self):
"""
Create packager context
* Login if requested
* remove existing container
* start new container
Returns:
Packager: The instance again
"""
assert not self._running
self.login()
self._run(self._command.delete(self._name), warn=True, hide=True)
self._run(
self._command.run(
self._image,
name=self._name,
detached=True,
command=self._WAIT_COMMAND,
)
)
self._running = True
return self
def __exit__(self, *args):
"""
Exit packager context
* stop the container
* delete the container
"""
try:
kwargs = dict(hide=True, warn=True)
self._run(self._command.stop(self._name), **kwargs)
self._run(self._command.delete(self._name), **kwargs)
finally:
self._running = False
def _run(self, command, **kwargs):
"""
Run command
Parameters:
command (list):
The command to run
**kwargs:
Extra arguments for ``ctx.run()``
Returns:
ctx.run() response
"""
ctx = self._ctx
kwargs.setdefault("echo", True)
return ctx.run(ctx.c(command), **kwargs)
def cp_in(self, src, dest):
"""
cp files into the container
Parameters:
src (str):
Source file or folder
dest (str):
Destination in the container
"""
assert self._running
self._run(self._command.cp_in(self._name, src, dest))
def cp_out(self, src, dest):
"""
cp files out of the container
Parameters:
src (str):
Source file or folder within the container
dest (str):
Destination in the local file system
"""
assert self._running
self._run(self._command.cp_out(self._name, src, dest))
def execute(self, command):
"""
Execute a command in the running container
Parameters:
command (list):
The command to execute
"""
assert self._running
self._run(self._command.execute(self._name, command))
class DockerCommand(object):
"""Docker command creator"""
def __init__(self, ctx):
"""Initialization"""
self._exe = ctx.which("docker")
self._ctx = ctx
self._email_args = (
["-e", "none"]
if "--email"
in ctx.run(
ctx.c([self._exe, "login", "--help"]), hide=True
).stdout
else []
)
def login(self, url, username):
"""
Return login command
Password needs to be passed via stdin
Parameters:
url (str):
Registry URL
username (str):
User name
Returns:
list: The command
"""
cmd = [self._exe, "login"] + self._email_args + ["--password-stdin"]
cmd += ["-u", username]
cmd += ["--", url]
return cmd
def delete(self, container):
"""
Create command to delete a container
Parameters:
container (str):
The container name
Returns:
list: The command
"""
return [self._exe, "rm", "-f", "--", container]
def stop(self, container):
"""
Create command to stop a container
Parameters:
container (str):
The container name
Returns:
list: The command
"""
return [self._exe, "stop", "--", container]
def run(
self,
image,
name=None,
detached=None,
command=None,
remove=None,
privileged=False,
):
"""
Create run command
Parameters:
image (str):
The image to run
name (str):
Optional container name
detached (bool):
Run detached? Default: False
command (list):
Optional in-container command to execute
remove (bool):
Remove after stop? Default: false
privileged (bool):
Run privileged? Default: false
Returns:
list: The command
"""
cmd = [self._exe, "run"]
if detached:
cmd += ["-d"]
if name:
cmd += ["--name", name]
if remove:
cmd += ["--rm"]
if privileged:
cmd += ["--privileged"]
cmd += [image]
if command:
cmd += list(command)
return cmd
def cp_in(self, container, src, dest):
"""
Create command to copy files into a container
Parameters:
container (str):
The container name
src (str):
Source file or folder
dest (str):
Destination in the container
Returns:
list: The command
"""
cmd = [self._exe, "container", "cp"]
cmd += [src]
cmd += ["%s:%s" % (container, dest)]
return cmd
def cp_out(self, container, src, dest):
"""
Create command to copy files out of the container
Parameters:
src (str):
Source file or folder within the container
dest (str):
Destination in the local file system
"""
cmd = [self._exe, "container", "cp"]
cmd += ["%s:%s" % (container, src)]
cmd += [dest]
return cmd
def execute(self, container, command, interactive=True):
"""
Create command to execute a command in the running container
Parameters:
container (str):
Container name
command (list):
The command to execute
interactive (bool):
Run interactively? Default: true
"""
cmd = [self._exe, "exec"]
if interactive:
cmd += ["-i"]
cmd += [container]
cmd.extend(command)
return cmd
class ContainerCommand(object):
"""Apple container command creator"""
def __init__(self, ctx):
"""Initialization"""
self._exe = ctx.which("container")
self._ctx = ctx
def login(self, url, username):
"""
Return login command
Password needs to be passed via stdin
Parameters:
url (str):
Registry URL
username (str):
User name
Returns:
list: The command
"""
cmd = [self._exe, "registry", "login", "--password-stdin"]
cmd += ["-u", username]
cmd += ["--", url]
return cmd
def delete(self, container):
"""
Create command to delete a container
Parameters:
container (str):
The container name
Returns:
list: The command
"""
return [self._exe, "delete", "-f", "--", container]
def stop(self, container):
"""
Create command to stop a container
Parameters:
container (str):
The container name
Returns:
list: The command
"""
return [self._exe, "stop", "--", container]
def run(
self,
image,
name=None,
detached=None,
command=None,
remove=None,
privileged=None,
):
"""
Create run command
Parameters:
image (str):
The image to run
name (str):
Optional container name
detached (bool):
Run detached? Default: False
command (list):
Optional in-container command to execute
remove (bool):
Remove after stop? Default: false
privileged (bool):
Run privileged? Default: false
Returns:
list: The command
"""
cmd = [self._exe, "run"]
if detached:
cmd += ["-d"]
if name:
cmd += ["--name", name]
if remove:
cmd += ["--rm"]
if privileged:
cmd += ["--privileged"]
cmd += [image]
if command:
cmd += list(command)
return cmd
def cp_in(self, container, src, dest):
"""
Copy files into a container
Parameters:
container (str):
The container name
src (str):
Source file or folder
dest (str):
Destination in the container
Returns:
list: The command
"""
# TODO: probably wrong
cmd = [self._exe, "container", "cp"]
cmd += [src]
cmd += ["%s:%s" % (container, dest)]
return cmd
def cp_out(self, container, src, dest):
"""
Create command to copy files out of the container
Parameters:
src (str):
Source file or folder within the container
dest (str):
Destination in the local file system
"""
# TODO: probably wrong
cmd = [self._exe, "container", "cp"]
cmd += ["%s:%s" % (container, src)]
cmd += [dest]
return cmd
def execute(self, container, command, interactive=True):
"""
Create command to execute a command in the running container
Parameters:
container (str):
Container name
command (list):
The command to execute
interactive (bool):
Run interactively? Default: true
"""
cmd = [self._exe, "exec"]
if interactive:
cmd += ["-i"]
cmd += [container]
cmd.extend(command)
return cmd
|