Пример #1
0
    def create_pod(self, image_name, name, cpu_shares, command, sshdir, network_info, instance, host_config):
        obj = {
            "id": name,
            "tty": True,
            "resource":{
                "vcpu": cpu_shares,
                "memory": host_config['mem_limit'],
            },
            "containers": [{
                "image": image_name,
                "command": ["/bin/sh"], #todo: change to command
                "files": [],
                "volumes": [],
            }],
            "files": [],
            "volumes": [],
        }
        if sshdir:
            obj["volumes"].append({
                "name": "sshdir",
                "source": sshdir,
                "driver": "vfs"
            })
            for container in obj["containers"]:
                container["volumes"].append({
                    "volume": "sshdir",
                    "path": "/root/.ssh",
                    "readOnly": True
                })
        if network_info:
            obj["interfaces"] = []
            for vif in network_info:
                obj["interfaces"].append({
                    "bridge": vif['network']['bridge'],
                    "ifname": vif['network']['if_local_name'],
                    "mac": vif["network"].get("mac_addr"),
                    "ip": network.find_fixed_ip(instance, vif['network']),
                    "gateway": network.find_gateway(instance, vif['network'])
                })

        result = self._result(self._post_json(url=self._url('/pod/create'),
                                              data=obj),
                              json=True)
        return result
Пример #2
0
    def attach(self, instance, vif, pod_id):
        vif_type = vif['type']
        if_remote_name = 'ns%s' % vif['id'][:11]
        gateway = network.find_gateway(instance, vif['network'])
        ip = network.find_fixed_ip(instance, vif['network'])

        LOG.debug('attach vif_type=%(vif_type)s instance=%(instance)s '
                  'vif=%(vif)s',
                  {'vif_type': vif_type, 'instance': instance,
                   'vif': vif})

        try:
            utils.execute('ip', 'link', 'set', if_remote_name, 'netns',
                          pod_id, run_as_root=True)
            utils.execute('ip', 'netns', 'exec', pod_id, 'ip', 'link',
                          'set', if_remote_name, 'address', vif['address'],
                          run_as_root=True)
            utils.execute('ip', 'netns', 'exec', pod_id, 'ip', 'addr',
                          'add', ip, 'dev', if_remote_name, run_as_root=True)
            utils.execute('ip', 'netns', 'exec', pod_id, 'ip', 'link',
                          'set', if_remote_name, 'up', run_as_root=True)

            # Setup MTU on if_remote_name is required if it is a non
            # default value
            mtu = CONF.network_device_mtu
            if vif.get('mtu') is not None:
                mtu = vif.get('mtu')
            if mtu is not None:
                utils.execute('ip', 'netns', 'exec', pod_id, 'ip',
                              'link', 'set', if_remote_name, 'mtu', mtu,
                              run_as_root=True)

            if gateway is not None:
                utils.execute('ip', 'netns', 'exec', pod_id,
                              'ip', 'route', 'replace', 'default', 'via',
                              gateway, 'dev', if_remote_name, run_as_root=True)
        except Exception:
            LOG.exception("Failed to attach vif")