| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2018 Matthias Klumpp <[email protected]> |
| 4 |
# |
| 5 |
# Licensed under the GNU Lesser General Public License Version 3 |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the license, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This software is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with this software. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import os |
| 21 |
import shlex |
| 22 |
import logging as log |
| 23 |
from enum import StrEnum |
| 24 |
from typing import Optional |
| 25 |
from tempfile import NamedTemporaryFile |
| 26 |
from contextlib import contextmanager |
| 27 |
|
| 28 |
from spark import __appname__, __version__ |
| 29 |
from spark.utils.command import run_logged |
| 30 |
|
| 31 |
|
| 32 |
class RunnerResult(StrEnum): |
| 33 |
"""Result of the run action of a job runner""" |
| 34 |
|
| 35 |
SUCCESS = 'success' |
| 36 |
FAILURE = 'failure' |
| 37 |
DEPWAIT = 'depwait' |
| 38 |
|
| 39 |
|
| 40 |
class RunnerError(Exception): |
| 41 |
"""Emitted by runners when execution fails unexpectedly.""" |
| 42 |
|
| 43 |
|
| 44 |
@contextmanager |
| 45 |
def lkworkspace(wsdir): |
| 46 |
import shutil |
| 47 |
|
| 48 |
artifacts_dir = os.path.join(wsdir, 'artifacts') |
| 49 |
if not os.path.exists(artifacts_dir): |
| 50 |
os.makedirs(artifacts_dir) |
| 51 |
|
| 52 |
ncwd = os.getcwd() |
| 53 |
try: |
| 54 |
yield os.chdir(wsdir) |
| 55 |
finally: |
| 56 |
os.chdir(ncwd) |
| 57 |
try: |
| 58 |
shutil.rmtree(wsdir) |
| 59 |
except Exception as e: |
| 60 |
log.warning('Unable to remove stale workspace {0}: {1}'.format(wsdir, str(e))) |
| 61 |
|
| 62 |
|
| 63 |
@contextmanager |
| 64 |
def make_commandfile(job_id, commands): |
| 65 |
f = NamedTemporaryFile('w', suffix='.sh', prefix='{}-'.format(job_id)) |
| 66 |
f.write('#!/bin/sh\n') |
| 67 |
f.write('set -e\n') |
| 68 |
f.write('export SPARK_ID="{}"\n'.format(shlex.quote(__appname__ + '-' + __version__))) |
| 69 |
f.write('set -x\n') |
| 70 |
f.write('\n') |
| 71 |
for cmd in commands: |
| 72 |
f.write(cmd + '\n') |
| 73 |
f.flush() |
| 74 |
yield f.name |
| 75 |
f.close() |
| 76 |
|
| 77 |
|
| 78 |
def debspawn_run_commandfile( |
| 79 |
jlog, |
| 80 |
suite: str, |
| 81 |
arch: str, |
| 82 |
*, |
| 83 |
build_dir: str, |
| 84 |
artifacts_dir: str, |
| 85 |
init_script: Optional[str] = None, |
| 86 |
command_script: str, |
| 87 |
header=None, |
| 88 |
allow_kvm=False, |
| 89 |
cache_key: Optional[str] = None, |
| 90 |
): |
| 91 |
''' |
| 92 |
Execute a command-script file in a debspawn container with optional initial environment caching. |
| 93 |
''' |
| 94 |
|
| 95 |
ds_cmd = ['debspawn', 'run', '--external-command', '--arch={}'.format(arch)] |
| 96 |
if artifacts_dir: |
| 97 |
ds_cmd.extend(['--artifacts-out', artifacts_dir]) |
| 98 |
if build_dir: |
| 99 |
ds_cmd.extend(['--build-dir', build_dir]) |
| 100 |
if allow_kvm: |
| 101 |
ds_cmd.append('--allow={}'.format('kvm,read-kmods')) |
| 102 |
if cache_key: |
| 103 |
ds_cmd.extend(['--cachekey', '{}-{}'.format(suite, cache_key)]) |
| 104 |
if init_script: |
| 105 |
ds_cmd.extend(['--init-command', init_script]) |
| 106 |
|
| 107 |
if header: |
| 108 |
ds_cmd.append('--header={}'.format(header)) |
| 109 |
ds_cmd.append(suite) |
| 110 |
ds_cmd.append(command_script) |
| 111 |
|
| 112 |
return run_logged(jlog, ds_cmd, True) |