2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2015 Canonical Ltd.
5
# Author: Christopher Townsend <christopher.townsend@canonical.com>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; version 3 of the License.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
25
import xdg.BaseDirectory as basedir
27
home_path = os.environ['HOME']
29
libertine_json_file_path = os.path.join(basedir.xdg_data_home, 'libertine', 'ContainersConfig.json')
30
libertine_container_path = basedir.save_cache_path('libertine-container')
31
libertine_userdata_path = os.path.join(basedir.xdg_data_home, 'libertine-container', 'user-data')
33
def get_container_type(container_id):
36
with open(libertine_json_file_path) as fd:
37
container_list = json.load(fd)
40
for container in container_list["containerList"]:
41
if container["id"] == container_id:
42
return container["type"]
46
def get_container_path(container_id):
47
return os.path.join(libertine_container_path, container_id)
49
def get_userdata_path(container_id):
50
return os.path.join(libertine_userdata_path, container_id)
52
def build_proot_command(container_id):
53
proot_cmd = "proot -R " + get_container_path(container_id)
55
bind_mounts = " -b %s:%s" % (get_userdata_path(container_id), home_path)
57
xdg_user_dirs = ['Documents', 'Music', 'Pictures', 'Videos']
58
for user_dir in xdg_user_dirs:
59
user_dir_path = os.path.join(home_path, user_dir)
60
bind_mounts += " -b %s:%s" % (user_dir_path, user_dir_path)
62
proot_cmd += bind_mounts
65
def launch_lxc_application(container_id, app_exec_line):
66
container = lxc.Container(container_id, libertine_container_path)
68
if not container.running:
69
if not container.start():
70
print("Container failed to start")
72
if not container.wait("RUNNING", 10):
73
print("Container failed to enter the RUNNING state")
76
if not container.get_ips(timeout=30):
77
print("Not able to connect to the network.")
80
container.attach_wait(lxc.attach_run_command, app_exec_line)
82
def launch_chroot_application(container_id, app_exec_line):
83
proot_cmd = build_proot_command(container_id)
85
args = shlex.split(proot_cmd)
86
args.extend(app_exec_line)
88
subprocess.Popen(args).wait()
90
if __name__ == '__main__':
91
container_id = sys.argv[1]
92
app_exec_line = sys.argv[2:]
93
container_type = get_container_type(container_id)
95
if container_type == "lxc":
96
launch_lxc_application(container_id, app_exec_line)
97
elif container_type == "chroot":
98
launch_chroot_application(container_id, app_exec_line)