Ubuntu Pastebin

Paste from roadmr at Fri, 16 Jan 2015 15:26:35 +0000

Download as text
 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
#!/usr/bin/env python3

import lxc
import os
import logging

# Create_container will do just that, but also add a mount from the current
# directory in the host to /some-directory inside the container.
def create_container(devcontainer):
    if not devcontainer.create("ubuntu", 0,
                               {"release": "trusty",
                                "arch": "amd64"}):
        return False
    dest_mount = os.path.join(devcontainer.get_config_item('lxc.rootfs'),
                              "some-directory")
    src_mount = os.getcwd()
    try:
        os.mkdir(dest_mount)
    except OSError as err:
        logging.error("Unable to create %s: %s ", dest_mount, err)
        return False
    mount_entry = "{src} {dest} none bind 0 0".format(src=src_mount,
                                                      dest=dest_mount)
    devcontainer.append_config_item('lxc.mount.entry', mount_entry)
    devcontainer.save_config()
    return True


# This will run some provisioning commands inside the container.
# As part of the result, I expected to see some-directory but instead
# I see the host's root (/).
def provision_container(container):
    command = ["ls", "-la", "/"]
    result = container.attach_wait(lxc.attach_run_command(command))
    print(result)


def start_container(container):
    if not container.start():
        return False
    if not container.wait("RUNNING", 5):
        return False
    return True


devcontainer = lxc.Container("test-container")
create_container(devcontainer)
start_container(devcontainer)
provision_container(devcontainer)
Download as text