summaryrefslogtreecommitdiff
path: root/sandboxlib/load/appc.py
blob: 7cbafc1b0bc5ac4a48327be5de3e4338dc1bfb95 (plain)
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
# Copyright (C) 2015  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


'''Sandbox loader module for App Container images.'''


import contextlib
import json
import logging
import os
import shutil
import tarfile
import tempfile


# Mandated by https://github.com/appc/spec/blob/master/SPEC.md#execution-environment
BASE_ENVIRONMENT = {
    'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
}


def is_app_container_image(path):
    return path.endswith('.aci')


@contextlib.contextmanager
def unpack_app_container_image(image_file):
    tempdir = tempfile.mkdtemp()
    try:
        # FIXME: you gotta be root, sorry.
        with tarfile.open(image_file, 'r') as tf:
            tf.extractall(path=tempdir)

        manifest_path = os.path.join(tempdir, 'manifest')
        rootfs_path = os.path.join(tempdir, 'rootfs')

        with open(manifest_path, 'r') as f:
            manifest_data = json.load(f)

        yield rootfs_path, manifest_data
    finally:
        shutil.rmtree(tempdir)