From 7ce6c483b58eb827da9230bf095896055e979759 Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sat, 15 Aug 2020 19:42:54 +0100 Subject: [PATCH] now able to deprovision IPv4 --- automonty | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/automonty b/automonty index e943a90..731d1a1 100755 --- a/automonty +++ b/automonty @@ -17,6 +17,7 @@ import ipaddress # home - this is the normal home-location of the IP # evacuated - this has been moved from this host to elsewhere # evacuee - this is an evacuated address +# teardown - being destroyed aM1 = re.compile( r"\[aM ?([^]]*)\]" ) def parse_comment( comment ): @@ -180,7 +181,8 @@ def monty_fixup( config, args, routers ): def monty_provision( config, args, routers ): reverse = {} statics = {} - print() + if args.dry_run: + print() for ( name, api ) in routers.items(): if args.dry_run: print( '#' * ( len( name ) + 2 ) ) @@ -244,6 +246,47 @@ def monty_provision( config, args, routers ): if args.dry_run: print() +def monty_teardown( config, args, routers ): + reverse = {} + statics = {} + if args.dry_run: + print() + for ( name, api ) in routers.items(): + if args.dry_run: + print( '#' * ( len( name ) + 2 ) ) + print( '#', name ) + print() + vlans = api.path( 'interface', 'vlan' ) + ip_address = api.path( 'ip', 'address' ) + ipv6_address = api.path( 'ipv6', 'address' ) + + for addr in ip_address: + ( sel, rval, hst ) = parse_comment( addr.get( 'comment', '' ) ) + rval[ 'teardown' ] = True + comment = make_comment( sel, rval, hst ) + if sel == args.hostname or hst == args.hostname: + if args.delete: + if rval.get( 'teardown', False ): + if args.dry_run: + print( '/ip address remove [find where interface="%s" and network="%s" and comment~"teardown" and comment~"%s" ]' % ( addr[ 'interface' ], addr[ 'network' ], hst ) ) + else: + print( "deleting %s %s from %s on %s" % ( addr[ 'address' ], addr[ 'network' ], addr[ 'interface' ], name ) ) + ip_address.remove( addr[ '.id' ] ) + else: + if args.dry_run: + print( "# ignoring %s %s on %s on %s" % ( addr[ 'address' ], addr[ 'network' ], addr[ 'interface' ], name ) ) + else: + print( "ignoring %s %s on %s on %s" % ( addr[ 'address' ], addr[ 'network' ], addr[ 'interface' ], name ) ) + else: + if args.dry_run: + print( '/ip address set [find where interface="%s" and network="%s" and comment~"%s" ] disabled=yes comment="%s"' % ( addr[ 'interface' ], addr[ 'network' ], hst, comment ) ) + else: + print( "disabling %s %s on %s on %s" % ( addr[ 'address' ], addr[ 'network' ], addr[ 'interface' ], name ) ) + ip_address.update( **{ '.id': addr[ '.id' ], 'disabled': 'yes', 'comment': comment } ) + + if args.dry_run: + print() + def load_configuration(): try: config = open( os.path.expanduser( '~/.automonty.yaml' ), 'r' ) @@ -279,6 +322,12 @@ def main(): parser_provision.add_argument( 'address', type = ipaddress.ip_interface, nargs = "+" ) parser_provision.set_defaults( func = monty_provision ) + parser_teardown = subparsers.add_parser( 'teardown' ) + parser_teardown.add_argument( '--delete', action = 'store_true', default = False ) + parser_teardown.add_argument( 'hostname' ) + parser_teardown.add_argument( 'vlan', type = int, default = None, nargs = "?" ) + parser_teardown.set_defaults( func = monty_teardown ) + args = parser.parse_args() routers = connect_routers( config, args ) args.func( config, args, routers )