#!/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)