def plug_bridge(self, instance, vif): if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] bridge = vif['network']['bridge'] gateway = network.find_gateway(instance, vif['network']) vlan = vif.get('vlan') if vlan is not None: iface = (CONF.vlan_interface or vif['network'].get_meta('bridge_interface')) linux_net.LinuxBridgeInterfaceDriver.ensure_vlan_bridge( vlan, bridge, iface, net_attrs=vif, mtu=vif.get('mtu')) iface = 'vlan%s' % vlan else: iface = (CONF.flat_interface or vif['network'].get_meta('bridge_interface')) LOG.debug('Ensuring bridge for %s - %s' % (iface, bridge)) linux_net.LinuxBridgeInterfaceDriver.ensure_bridge( bridge, iface, net_attrs=vif, gateway=gateway) # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: mac_addr = vif["network"]["mac_addr"] utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all # associated resources (remove from the bridge, its pair, etc...) utils.execute('ip', 'link', 'set', if_local_name, 'address', mac_addr, run_as_root=True) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
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
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")