def add_corporation(): ''' Create and Activate the network corporation ''' domain_name = action_get('domain-name') iface_name = action_get('iface-name') # HACK: python's list, used deeper, throws an exception on ints in a tuple vlan_id = str(action_get('vlan-id')) cidr = action_get('cidr') area = action_get('area') subnet_cidr = action_get('subnet-cidr') subnet_area = action_get('subnet-area') iface_vlanid = '%s.%s' % (iface_name, vlan_id) status_set('maintenance', 'adding corporation {}'.format(domain_name)) """ Attempt to run all commands to add the network corporation. If any step fails, abort and call `delete_corporation()` to undo. """ try: """ $ ip link add link eth3 name eth3.103 type vlan id 103 """ router.ip('link', 'add', 'link', iface_name, 'name', iface_vlanid, 'type', 'vlan', 'id', vlan_id) """ $ ip netns add domain """ router.ip('netns', 'add', domain_name) """ $ ip link set dev eth3.103 netns corpB """ router.ip('link', 'set', 'dev', iface_vlanid, 'netns', domain_name) """ $ ifconfig eth3 up """ router._run(['ifconfig', iface_name, 'up']) """ $ ip netns exec corpB ip link set dev eth3.103 up """ router.ip('netns', 'exec', domain_name, 'ip', 'link', 'set', 'dev', iface_vlanid, 'up') """ $ ip netns exec corpB ip address add 10.0.1.1/24 dev eth3.103 """ mask = cidr.split("/")[1] ip = '%s/%s' % (area, mask) router.ip('netns', 'exec', domain_name, 'ip', 'address', 'add', ip, 'dev', iface_vlanid) configure_ospf(domain_name, cidr, area, subnet_cidr, subnet_area, True) except subprocess.CalledProcessError as e: delete_corporation() action_fail('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) finally: remove_state('vpe.add-corporation') status_set('active', 'ready!')
def start_ospfd(): # We may want to make this configurable via config setting ospfd = '/usr/local/bin/ospfd' try: (stdout, stderr) = router._run(['touch', '/usr/admin/global/ospfd.conf']) (stdout, stderr) = router._run( [ospfd, '-d', '-f', '/usr/admin/global/ospfd.conf']) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output)))
def start_ospfd(): # We may want to make this configurable via config setting ospfd = '/usr/local/bin/ospfd' try: (stdout, stderr) = router._run(['touch', '/usr/admin/global/ospfd.conf']) (stdout, stderr) = router._run([ospfd, '-d', '-f', '/usr/admin/global/ospfd.conf']) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output)))
def configure_ospf(domain, cidr, area, subnet_cidr, subnet_area, enable=True): """Configure the OSPF service""" # Check to see if the OSPF daemon is running, and start it if not try: (stdout, stderr) = router._run(['pgrep', 'ospfd']) except subprocess.CalledProcessError as e: # If pgrep fails, the process wasn't found. start_ospfd() log('Command failed (ospfd not running): %s (%s)' % (' '.join(e.cmd), str(e.output))) upordown = '' if not enable: upordown = 'no' try: vrfctl = '/usr/local/bin/vrfctl' vtysh = '/usr/local/bin/vtysh' (stdout, stderr) = router._run([vrfctl, 'list']) domain_id = 0 for line in stdout.split('\n'): if domain in line: domain_id = int(line[3:5]) if domain_id > 0: router._run([ vtysh, '-c', '"configure terminal"', '-c', '"router ospf %d vr %d"' % (domain_id, domain_id), '-c', '"%s network %s area %s"' % (upordown, cidr, area), '-c', '"%s network %s area %s"' % (upordown, subnet_cidr, subnet_area), ]) else: log("Invalid domain id") except subprocess.CalledProcessError as e: action_fail('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) finally: remove_state('vpe.configure-interface') status_set('active', 'ready!')
def configure_ospf(domain, cidr, area, subnet_cidr, subnet_area, enable=True): """Configure the OSPF service""" # Check to see if the OSPF daemon is running, and start it if not try: (stdout, stderr) = router._run(['pgrep', 'ospfd']) except subprocess.CalledProcessError as e: # If pgrep fails, the process wasn't found. start_ospfd() log('Command failed (ospfd not running): %s (%s)' % (' '.join(e.cmd), str(e.output))) upordown = '' if not enable: upordown = 'no' try: vrfctl = '/usr/local/bin/vrfctl' vtysh = '/usr/local/bin/vtysh' (stdout, stderr) = router._run([vrfctl, 'list']) domain_id = 0 for line in stdout.split('\n'): if domain in line: domain_id = int(line[3:5]) if domain_id > 0: router._run([vtysh, '-c', '"configure terminal"', '-c', '"router ospf %d vr %d"' % (domain_id, domain_id), '-c', '"%s network %s area %s"' % (upordown, cidr, area), '-c', '"%s network %s area %s"' % (upordown, subnet_cidr, subnet_area), ]) else: log("Invalid domain id") except subprocess.CalledProcessError as e: action_fail('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) finally: remove_state('vpe.configure-interface') status_set('active', 'ready!')
def delete_corporation(): domain_name = action_get('domain-name') cidr = action_get('cidr') area = action_get('area') subnet_cidr = action_get('subnet-cidr') subnet_area = action_get('subnet-area') status_set('maintenance', 'deleting corporation {}'.format(domain_name)) try: """ Remove all tunnels defined for this domain $ ip netns exec domain_name ip tun show | grep gre | grep -v "remote any" | cut -d":" -f1 """ p = router.ip('netns', 'exec', domain_name, 'ip', 'tun', 'show', '|', 'grep', 'gre', '|', 'grep', '-v', '"remote any"', '|', 'cut -d":" -f1') # `p` should be a tuple of (stdout, stderr) tunnels = p[0].split('\n') for tunnel in tunnels: try: """ $ ip netns exec domain_name ip link set $tunnel_name down """ router.ip('netns', 'exec', domain_name, 'ip', 'link', 'set', tunnel, 'down') except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: """ $ ip netns exec domain_name ip tunnel del $tunnel_name """ router.ip('netns', 'exec', domain_name, 'ip', 'tunnel', 'del', tunnel) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass """ Remove all interfaces associated to the domain $ ip netns exec domain_name ifconfig | grep mtu | cut -d":" -f1 """ p = router.ip('netns', 'exec', domain_name, 'ifconfig', '|', 'grep mtu', '|', 'cut -d":" -f1') ifaces = p[0].split('\n') for iface in ifaces: try: """ $ ip netns exec domain_name ip link set $iface down """ router.ip('netns', 'exec', domain_name, 'ip', 'link', 'set', iface, 'down') except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) try: """ $ ifconfig eth3 down """ router._run(['ifconfig', iface, 'down']) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: """ $ ip link del dev $iface """ router.ip('link', 'del', 'dev', iface) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: """ Remove the domain $ ip netns del domain_name """ router.ip('netns', 'del', domain_name) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: configure_ospf(domain_name, cidr, area, subnet_cidr, subnet_area, False) except subprocess.CalledProcessError as e: action_fail('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) except: # Do nothing log('delete-corporation failed.') pass finally: remove_state('vpe.delete-corporation') status_set('active', 'ready!')
def delete_corporation(): domain_name = action_get('domain-name') cidr = action_get('cidr') area = action_get('area') subnet_cidr = action_get('subnet-cidr') subnet_area = action_get('subnet-area') status_set('maintenance', 'deleting corporation {}'.format(domain_name)) try: """ Remove all tunnels defined for this domain $ ip netns exec domain_name ip tun show | grep gre | grep -v "remote any" | cut -d":" -f1 """ p = router.ip( 'netns', 'exec', domain_name, 'ip', 'tun', 'show', '|', 'grep', 'gre', '|', 'grep', '-v', '"remote any"', '|', 'cut -d":" -f1' ) # `p` should be a tuple of (stdout, stderr) tunnels = p[0].split('\n') for tunnel in tunnels: try: """ $ ip netns exec domain_name ip link set $tunnel_name down """ router.ip( 'netns', 'exec', domain_name, 'ip', 'link', 'set', tunnel, 'down' ) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: """ $ ip netns exec domain_name ip tunnel del $tunnel_name """ router.ip( 'netns', 'exec', domain_name, 'ip', 'tunnel', 'del', tunnel ) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass """ Remove all interfaces associated to the domain $ ip netns exec domain_name ifconfig | grep mtu | cut -d":" -f1 """ p = router.ip( 'netns', 'exec', domain_name, 'ifconfig', '|', 'grep mtu', '|', 'cut -d":" -f1' ) ifaces = p[0].split('\n') for iface in ifaces: try: """ $ ip netns exec domain_name ip link set $iface down """ router.ip( 'netns', 'exec', domain_name, 'ip', 'link', 'set', iface, 'down' ) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) try: """ $ ifconfig eth3 down """ router._run(['ifconfig', iface, 'down']) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: """ $ ip link del dev $iface """ router.ip( 'link', 'del', 'dev', iface ) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: """ Remove the domain $ ip netns del domain_name """ router.ip( 'netns', 'del', domain_name ) except subprocess.CalledProcessError as e: log('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) pass try: configure_ospf(domain_name, cidr, area, subnet_cidr, subnet_area, False) except subprocess.CalledProcessError as e: action_fail('Command failed: %s (%s)' % (' '.join(e.cmd), str(e.output))) except: # Do nothing log('delete-corporation failed.') pass finally: remove_state('vpe.delete-corporation') status_set('active', 'ready!')