~townsend/libertine/release-1.2

« back to all changes in this revision

Viewing changes to tools/update-puritine-containers

  • Committer: CI Train Bot
  • Author(s): Chris Townsend, Larry Price, Ted Gould
  • Date: 2016-05-06 13:43:27 UTC
  • mfrom: (125.1.1 libertine-1.0.2)
  • Revision ID: ci-train-bot@canonical.com-20160506134327-xh8636axkh4ui58k
* Make a list of packages to install during container creation and also include the humanity-icon-them as one of them. (LP: #1578353)
* Add a Click package hook for handling bespoke Puritine container installs.  This handles multiple Puritine-type containers installed on the same system.  (LP: #1576356) (LP: #1576406)
* Updating liblibertine to search local applications directory for desktop apps.  This is to make it easier for u-a-l and ContentHub to find a complete set of available launchable applications. Fixes: #1576356, #1576406, #1578353
Approved by: Stephen M. Webb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright (C) 2016 Canonical Ltd.
 
5
# Author: Christopher Townsend <christopher.townsend@canonical.com>
 
6
 
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
 
 
19
import json
 
20
import libertine.utils
 
21
import os
 
22
import shlex
 
23
import shutil
 
24
import subprocess
 
25
 
 
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')
 
29
 
 
30
def puritine_symlink_exists(symlink):
 
31
    for file in os.listdir(puritine_hook_dir):
 
32
        if file == symlink:
 
33
            return True
 
34
 
 
35
    return False
 
36
 
 
37
 
 
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'])
 
43
 
 
44
    return (0, None)
 
45
 
 
46
 
 
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']):
 
50
            continue
 
51
        else:
 
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)
 
56
 
 
57
    return puritine_symlink_farm_list
 
58
 
 
59
 
 
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()]
 
62
 
 
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)
 
66
 
 
67
        with open(config_file_path, 'r') as fd:
 
68
            container_list = json.load(fd)
 
69
 
 
70
        container_id = container_list['containerList'][0]['id']
 
71
 
 
72
        (index, symlink_name) = get_symlink_name(puritine_symlink_farm_list, container_id)
 
73
 
 
74
        if symlink_name:
 
75
            # Package exists and symlink name is the same- no update
 
76
            if symlink_name == symlink.name:
 
77
                continue
 
78
            else:
 
79
                puritine_symlink_farm_list['customContainers'][index]['symlinkName'] = symlink.name
 
80
        else:
 
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]
 
84
            else:
 
85
                puritine_symlink_farm_list['customContainers'].append(symlink_obj)
 
86
 
 
87
        puritine_click_rootfs_path = os.path.join(puritine_click_path, 'libertine-data', 'libertine-container',
 
88
                                                  container_id, 'rootfs')
 
89
 
 
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')
 
93
 
 
94
        if not os.path.exists(puritine_container_path):
 
95
            os.makedirs(puritine_container_path)
 
96
        else:
 
97
            os.remove(puritine_container_rootfs_path)
 
98
 
 
99
        # Link to the click packages container
 
100
        os.symlink(puritine_click_rootfs_path, puritine_container_rootfs_path)
 
101
 
 
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)
 
108
 
 
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()
 
113
 
 
114
    return puritine_symlink_farm_list
 
115
 
 
116
 
 
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)
 
121
 
 
122
puritine_symlink_farm_list = find_new_or_updated_puritine_symlinks(puritine_symlink_farm_list)
 
123
 
 
124
if puritine_symlink_farm_list:
 
125
    puritine_symlink_farm_list = find_removed_puritine_symlinks(puritine_symlink_farm_list)
 
126
 
 
127
with open(puritine_symlink_farm_file, 'w') as fd:
 
128
    json.dump(puritine_symlink_farm_list, fd, sort_keys=True, indent=4)
 
129
    fd.write('\n')