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