2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2016 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/>.
20
import libertine.utils
26
puritine_hook_dir = os.path.join(os.environ['HOME'], '.cache', 'libertine', 'puritine')
27
puritine_symlink_farm_file = os.path.join(puritine_hook_dir, 'PuritineSymlinkFarm.json')
28
puritine_click_config_file = os.path.join('libertine-config', 'libertine', 'ContainersConfig.json')
30
def puritine_symlink_exists(symlink):
31
for file in os.listdir(puritine_hook_dir):
38
def get_symlink_name(puritine_symlink_farm_list, container_id):
39
if puritine_symlink_farm_list:
40
for container in puritine_symlink_farm_list['customContainers']:
41
if container['id'] == container_id:
42
return (puritine_symlink_farm_list['customContainers'].index(container), container['symlinkName'])
47
def find_removed_puritine_symlinks(puritine_symlink_farm_list):
48
for container in puritine_symlink_farm_list['customContainers']:
49
if puritine_symlink_exists(container['symlinkName']):
52
manager_cmd = "libertine-container-manager destroy -i " + container['id']
53
cmd = shlex.split(manager_cmd)
54
subprocess.Popen(cmd).wait()
55
puritine_symlink_farm_list['customContainers'].remove(container)
57
return puritine_symlink_farm_list
60
def find_new_or_updated_puritine_symlinks(puritine_symlink_farm_list):
61
symlinks = [f for f in os.scandir(puritine_hook_dir) if f.is_symlink()]
63
for symlink in symlinks:
64
puritine_click_path = os.path.join(puritine_hook_dir, symlink.name)
65
config_file_path = os.path.join(puritine_click_path, puritine_click_config_file)
67
with open(config_file_path, 'r') as fd:
68
container_list = json.load(fd)
70
container_id = container_list['containerList'][0]['id']
72
(index, symlink_name) = get_symlink_name(puritine_symlink_farm_list, container_id)
75
# Package exists and symlink name is the same- no update
76
if symlink_name == symlink.name:
79
puritine_symlink_farm_list['customContainers'][index]['symlinkName'] = symlink.name
81
symlink_obj = {'symlinkName': symlink.name, 'id': container_id}
82
if 'customContainers' not in puritine_symlink_farm_list:
83
puritine_symlink_farm_list['customContainers'] = [symlink_obj]
85
puritine_symlink_farm_list['customContainers'].append(symlink_obj)
87
puritine_click_rootfs_path = os.path.join(puritine_click_path, 'libertine-data', 'libertine-container',
88
container_id, 'rootfs')
90
libertine_containers_path = libertine.utils.get_libertine_containers_dir_path()
91
puritine_container_path = os.path.join(libertine_containers_path, container_id)
92
puritine_container_rootfs_path = os.path.join(puritine_container_path, 'rootfs')
94
if not os.path.exists(puritine_container_path):
95
os.makedirs(puritine_container_path)
97
os.remove(puritine_container_rootfs_path)
99
# Link to the click packages container
100
os.symlink(puritine_click_rootfs_path, puritine_container_rootfs_path)
102
# Copy any user data that does not exist
103
libertine_user_data_path = libertine.utils.get_libertine_container_userdata_dir_path(container_id)
104
if not os.path.exists(libertine_user_data_path):
105
puritine_click_user_data_path = os.path.join(puritine_click_path, 'libertine-config', 'libertine-container',
106
'user-data', container_id)
107
shutil.copytree(puritine_click_user_data_path, libertine_user_data_path)
109
# Update the main ContainerConfig.json
110
manager_cmd = "libertine-container-manager merge-configs -f " + config_file_path
111
cmd = shlex.split(manager_cmd)
112
subprocess.Popen(cmd).wait()
114
return puritine_symlink_farm_list
117
with open(puritine_symlink_farm_file, 'r') as fd:
118
puritine_symlink_farm_list = {}
119
if os.path.getsize(puritine_symlink_farm_file) != 0:
120
puritine_symlink_farm_list = json.load(fd)
122
puritine_symlink_farm_list = find_new_or_updated_puritine_symlinks(puritine_symlink_farm_list)
124
if puritine_symlink_farm_list:
125
puritine_symlink_farm_list = find_removed_puritine_symlinks(puritine_symlink_farm_list)
127
with open(puritine_symlink_farm_file, 'w') as fd:
128
json.dump(puritine_symlink_farm_list, fd, sort_keys=True, indent=4)