summaryrefslogtreecommitdiff
path: root/src/conf_mode/containers.py
blob: 85fc140158ff955b36865baceb9c258ad2cbb005 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
#
# Copyright (C) 2021 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import json

from ipaddress import ip_address
from ipaddress import ip_network

from vyos.config import Config
from vyos.configdict import dict_merge
from vyos.configdict import node_changed
from vyos.util import call
from vyos.util import cmd
from vyos.util import run
from vyos.util import read_file
from vyos.util import write_file
from vyos.template import render
from vyos.template import is_ipv4
from vyos.template import is_ipv6
from vyos.xml import defaults
from vyos import ConfigError
from vyos import airbag
airbag.enable()

config_containers_registry = '/etc/containers/registries.conf'
config_containers_storage = '/etc/containers/storage.conf'

def _cmd(command):
    if os.path.exists('/tmp/vyos.container.debug'):
        print(command)
    return cmd(command)

def network_exists(name):
    # Check explicit name for network, returns True if network exists
    c = _cmd(f'podman network ls --quiet --filter name=^{name}$')
    return bool(c)

# Common functions
def get_config(config=None):
    if config:
        conf = config
    else:
        conf = Config()

    base = ['container']
    container = conf.get_config_dict(base, key_mangling=('-', '_'),
                                     get_first_key=True, no_tag_node_value_mangle=True)
    # We have gathered the dict representation of the CLI, but there are default
    # options which we need to update into the dictionary retrived.
    default_values = defaults(base)
    # container base default values can not be merged here - remove and add them later
    if 'name' in default_values:
        del default_values['name']
    container = dict_merge(default_values, container)

    # Merge per-container default values
    if 'name' in container:
        default_values = defaults(base + ['name'])
        for name in container['name']:
            container['name'][name] = dict_merge(default_values, container['name'][name])

    # Delete container network, delete containers
    tmp = node_changed(conf, ['container', 'network'])
    if tmp: container.update({'network_remove' : tmp})

    tmp = node_changed(conf, ['container', 'name'])
    if tmp: container.update({'container_remove' : tmp})

    return container

def verify(container):
    # bail out early - looks like removal from running config
    if not container:
        return None

    # Add new container
    if 'name' in container:
        for name, container_config in container['name'].items():
            if 'network' in container_config:
                if len(container_config['network']) > 1:
                    raise ConfigError(f'Only one network can be specified for container "{name}"!')

                # Check if the specified container network exists
                network_name = list(container_config['network'])[0]
                if network_name not in container['network']:
                    raise ConfigError('Container network "{network_name}" does not exist!')

                if 'address' in container_config['network'][network_name]:
                    if 'network' not in container_config:
                        raise ConfigError(f'Can not use "address" without "network" for container "{name}"!')

                    address = container_config['network'][network_name]['address']
                    network = None
                    if is_ipv4(address):
                        network = [x for x in container['network'][network_name]['prefix'] if is_ipv4(x)][0]
                    elif is_ipv6(address):
                        network = [x for x in container['network'][network_name]['prefix'] if is_ipv6(x)][0]

                    # Specified container IP address must belong to network prefix
                    if ip_address(address) not in ip_network(network):
                        raise ConfigError(f'Used container address "{address}" not in network "{network}"!')

                    # We can not use the first IP address of a network prefix as this is used by podman
                    if ip_address(address) == ip_network(network)[1]:
                        raise ConfigError(f'IP address "{address}" can not be used for a container, '\
                                          'reserved for the container engine!')

            if 'environment' in container_config:
                for var, cfg in container_config['environment'].items():
                    if 'value' not in cfg:
                        raise ConfigError(f'Environment variable {var} has no value assigned!')

            if 'volume' in container_config:
                for volume, volume_config in container_config['volume'].items():
                    if 'source' not in volume_config:
                        raise ConfigError(f'Volume "{volume}" has no source path configured!')

                    if 'destination' not in volume_config:
                        raise ConfigError(f'Volume "{volume}" has no destination path configured!')

                    source = volume_config['source']
                    if not os.path.exists(source):
                        raise ConfigError(f'Volume "{volume}" source path "{source}" does not exist!')

            # Container image is a mandatory option
            if 'image' not in container_config:
                raise ConfigError(f'Container image for "{name}" is mandatory!')

            # If 'allow-host-networks' or 'network' not set.
            if 'allow_host_networks' not in container_config and 'network' not in container_config:
                raise ConfigError(f'Must either set "network" or "allow-host-networks" for container "{name}"!')

            # Can not set both allow-host-networks and network at the same time
            if {'allow_host_networks', 'network'} <= set(container_config):
                raise ConfigError(f'"allow-host-networks" and "network" for "{name}" cannot be both configured at the same time!')

    # Add new network
    if 'network' in container:
        for network, network_config in container['network'].items():
            v4_prefix = 0
            v6_prefix = 0
            # If ipv4-prefix not defined for user-defined network
            if 'prefix' not in network_config:
                raise ConfigError(f'prefix for network "{net}" must be defined!')

            for prefix in network_config['prefix']:
                if is_ipv4(prefix): v4_prefix += 1
                elif is_ipv6(prefix): v6_prefix += 1

            if v4_prefix > 1:
                raise ConfigError(f'Only one IPv4 prefix can be defined for network "{network}"!')
            if v6_prefix > 1:
                raise ConfigError(f'Only one IPv6 prefix can be defined for network "{network}"!')


    # A network attached to a container can not be deleted
    if {'network_remove', 'name'} <= set(container):
        for network in container['network_remove']:
            for container, container_config in container['name'].items():
                if 'network' in container_config and network in container_config['network']:
                    raise ConfigError(f'Can not remove network "{network}", used by container "{container}"!')

    return None

def generate(container):
    # bail out early - looks like removal from running config
    if not container:
        return None

    render(config_containers_registry, 'containers/registry.tmpl', container)
    render(config_containers_storage, 'containers/storage.tmpl', container)

    return None

def apply(container):
    # Delete old containers if needed. We can't delete running container
    # Option "--force" allows to delete containers with any status
    if 'container_remove' in container:
        for name in container['container_remove']:
            call(f'podman stop {name}')
            call(f'podman rm --force {name}')

    # Delete old networks if needed
    if 'network_remove' in container:
        for network in container['network_remove']:
            call(f'podman network rm --force {network}')

    # Add network
    if 'network' in container:
        for network, network_config in container['network'].items():
            # Check if the network has already been created
            if not network_exists(network) and 'prefix' in network_config:
                tmp = f'podman network create {network}'
                # we can not use list comprehension here as the --ipv6 option
                # must immediately follow the specified subnet!!!
                for prefix in sorted(network_config['prefix']):
                    tmp += f' --subnet={prefix}'
                    if is_ipv6(prefix):
                      tmp += ' --ipv6'
                _cmd(tmp)

                # Disable masquerading and use traditional bridging so VyOS
                # can control firewalling/NAT by the real VyOS CLI
                cni_network_config = f'/etc/cni/net.d/{network}.conflist'
                tmp = read_file(cni_network_config)
                config = json.loads(tmp)
                if 'plugins' in config:
                    for count in range(0, len(config['plugins'])):
                        if 'ipMasq' in config['plugins'][count]:
                            config['plugins'][count]['ipMasq'] = False
                        if 'hairpinMode' in config['plugins'][count]:
                            config['plugins'][count]['hairpinMode'] = False

                write_file(cni_network_config, json.dumps(config, indent=4))

    # Add container
    if 'name' in container:
        for name, container_config in container['name'].items():
            image = container_config['image']

            if 'disable' in container_config:
                # check if there is a container by that name running
                tmp = _cmd('podman ps -a --format "{{.Names}}"')
                if name in tmp:
                    _cmd(f'podman stop {name}')
                continue

            memory = container_config['memory']
            restart = container_config['restart']

            # Check if requested container image exists locally. If it does not, we
            # pull it. print() is the best way to have a good response from the
            # polling process to the user to display progress. If the image exists
            # locally, a user can update it running `update container image <name>`
            tmp = run(f'podman image exists {image}')
            if tmp != 0: print(os.system(f'podman pull {image}'))

            # Check/set environment options "-e foo=bar"
            env_opt = ''
            if 'environment' in container_config:
                for k, v in container_config['environment'].items():
                    env_opt += f" -e \"{k}={v['value']}\""

            # Publish ports
            port = ''
            if 'port' in container_config:
                protocol = ''
                for portmap in container_config['port']:
                    if 'protocol' in container_config['port'][portmap]:
                        protocol = container_config['port'][portmap]['protocol']
                        protocol = f'/{protocol}'
                    else:
                        protocol = '/tcp'
                    sport = container_config['port'][portmap]['source']
                    dport = container_config['port'][portmap]['destination']
                    port += f' -p {sport}:{dport}{protocol}'

            # Bind volume
            volume = ''
            if 'volume' in container_config:
                for vol, vol_config in container_config['volume'].items():
                    svol = vol_config['source']
                    dvol = vol_config['destination']
                    volume += f' -v {svol}:{dvol}'

            container_base_cmd = f'podman run --detach --interactive --tty --replace ' \
                                 f'--memory {memory}m --memory-swap 0 --restart {restart} ' \
                                 f'--name {name} {port} {volume} {env_opt}'
            if 'allow_host_networks' in container_config:
                _cmd(f'{container_base_cmd} --net host {image}')
            else:
                for network in container_config['network']:
                    ipparam = ''
                    if 'address' in container_config['network'][network]:
                        ipparam = '--ip ' + container_config['network'][network]['address']
                    _cmd(f'{container_base_cmd} --net {network} {ipparam} {image}')

    return None

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)