Changeset 14374 in genesis for tools


Ignore:
Timestamp:
Jun 11, 2019, 10:41:16 AM (6 years ago)
Author:
rick
Message:

Update gformat to FreeBSD new default python3

Location:
tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tools/gformat.py

    r14335 r14374  
    1 #!/usr/local/bin/python
     1#!/usr/bin/env python3
    22#
    33# vim:ts=2:et:sw=2:ai
     
    3232sys.path.append(os.path.dirname(__file__))
    3333
    34 SVN = filter(os.path.isfile, ('/usr/local/bin/svn', '/usr/bin/svn'))[0]
    35 SVNVERSION = filter(os.path.isfile, ('/usr/local/bin/svnversion', '/usr/bin/svnversion'))[0]
     34SVN = next(filter(os.path.isfile, ('/usr/local/bin/svn', '/usr/bin/svn')))
     35SVNVERSION = next(filter(os.path.isfile, ('/usr/local/bin/svnversion', '/usr/bin/svnversion')))
    3636
    3737import argparse
    3838import cgi
    3939import cgitb
     40import codecs
    4041import copy
    4142import glob
     
    5152import time
    5253import traceback
    53 import urlparse
     54import urllib
    5455
    5556from pprint import pprint
    5657from collections import defaultdict, OrderedDict
     58from operator import itemgetter, attrgetter
    5759from sys import stderr
    5860try:
    5961  import yaml
    60 except ImportError, e:
    61   print e
    62   print "[ERROR] Please install the python-yaml or devel/py-yaml package"
     62except ImportError as e:
     63  print(e)
     64  print("[ERROR] Please install the python-yaml or devel/py-yaml package")
    6365  exit(1)
    6466
     
    9294
    9395
    94 if os.environ.has_key('CONFIGROOT'):
     96if os.environ.get('CONFIGROOT', None):
    9597  NODE_DIR = os.environ['CONFIGROOT']
    9698else:
     
    131133DHCP_SERVER = 20
    132134def dhcp_type(item):
    133   if not item.has_key('dhcp'):
     135  if not 'dhcp' in item:
    134136    return NO_DHCP
    135137  elif not item['dhcp']:
     
    159161  try:
    160162    """ Get configuration yaml for 'item'"""
    161     if datadump_cache.has_key(item):
     163    if item in datadump_cache:
    162164      return datadump_cache[item].copy()
    163165
     
    178180    if datadump['nodetype'] == 'Hybrid':
    179181      # Some values are defined implicitly
    180       if datadump.has_key('rdr_host') and datadump['rdr_host'] and not datadump.has_key('service_incoming_rdr'):
     182      if 'rdr_host' in datadump and datadump['rdr_host'] and not 'service_incoming_rdr' in datadump:
    181183        datadump['service_incoming_rdr'] = True
    182184      # Use some boring defaults
     
    188190        'monitoring_group' : 'wleiden',
    189191      }
    190       for (key,value) in defaults.iteritems():
    191         if not datadump.has_key(key):
     192      for (key,value) in defaults.items():
     193        if not key in datadump:
    192194          datadump[key] = value
    193195    f.close()
     
    195197    # Sometimes getting version information is useless or harmfull, like in the pre-commit hooks
    196198    if add_version_info:
    197       p = subprocess.Popen([SVN, 'info', datadump['autogen_gfile']], stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
     199      p = subprocess.Popen([SVN, 'info', datadump['autogen_gfile']], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, universal_newlines=True)
    198200      lines = p.communicate()[0].split('\n')
    199201      if p.returncode == 0:
     
    213215        datadump[key]['autogen_vlan_alias'] = False
    214216
    215         datadump[key]['autogen_bridge_member'] = datadump[key].has_key('parent')
     217        datadump[key]['autogen_bridge_member'] = 'parent' in datadump[key]
    216218        datadump[key]['autogen_bridge'] = datadump[key]['autogen_ifbase'].startswith('bridge')
    217219        datadump[key]['autogen_bridge_alias'] = datadump[key]['autogen_ifbase'].startswith('bridge') and '_alias' in key
    218220
    219         if datadump[key].has_key('parent'):
    220             if datadump[key].has_key('ip'):
     221        if 'parent' in datadump[key]:
     222            if 'ip' in datadump[key]:
    221223                raise ValueError("Interface bridge member cannot have IP assigned")
    222             if datadump[key].has_key('dhcp') and datadump[key]['dhcp'] != False:
     224            if 'dhcp' in datadump[key] and datadump[key]['dhcp'] != False:
    223225                raise ValueError("Interface bridge member cannot have DHCP set")
    224226
    225         if datadump[key].has_key('ip'):
     227        if 'ip' in datadump[key]:
    226228          datadump[key]['autogen_gateway'] = datadump[key]['ip'].split('/')[0]
    227229
     
    252254    datadump['autogen_item'] = item
    253255
    254     datadump['autogen_domain'] = datadump['domain'] if datadump.has_key('domain') else 'wleiden.net.'
     256    datadump['autogen_domain'] = datadump['domain'] if 'domain' in datadump else 'wleiden.net.'
    255257    datadump['autogen_fqdn'] = datadump['nodename'] + '.' + datadump['autogen_domain']
    256258    datadump_cache[item] = datadump.copy()
     
    445447  output += "<h2>Connected To:</h2><ul>"
    446448  (poel, errors) = make_relations()
    447   for network, hosts in poel.iteritems():
     449  for network, hosts in poel.items():
    448450    if host in [x[0] for x in hosts]:
    449451      if len(hosts) == 1:
     
    474476def parseaddr(s):
    475477  """ Process IPv4 CIDR notation addr to a (binary) number """
    476   f = s.split('.')
    477   return (long(f[0]) << 24L) + \
    478     (long(f[1]) << 16L) + \
    479     (long(f[2]) << 8L) + \
    480     long(f[3])
     478  f = list(map(int, s.split('.')))
     479  return ((f[0] << 24) +  (f[1] << 16) + (f[2] << 8) + (f[3]))
    481480
    482481
     
    554553    ifname = datadump[iface_key]['autogen_ifbase']
    555554    groupif = datadump[iface_key]['autogen_if_dhcp']
    556     if not datadump[iface_key].has_key('comment'):
     555    if not 'comment' in datadump[iface_key]:
    557556      datadump[iface_key]['comment'] = None
    558557
    559     if not datadump[iface_key].has_key('ip'):
     558    if not 'ip' in datadump[iface_key]:
    560559      continue
    561560
     
    613612
    614613  # Output the blocks in groups
    615   for ifname,value in sorted(dhcp_out.iteritems()):
     614  for ifname,value in sorted(dhcp_out.items()):
    616615      output += ("shared-network \"%s\" {\n" % ifname) + indent(''.join(value), 2).rstrip()  + '\n}\n\n'
    617616  return output
     
    637636
    638637  for iface_key in get_interface_keys(datadump):
    639     if not datadump[iface_key].has_key('comment'):
     638    if not 'comment' in datadump[iface_key]:
    640639      datadump[iface_key]['comment'] = None
    641640    output += "## %(autogen_ifname)s - %(comment)s\n" % datadump[iface_key]
     
    667666
    668667def make_interface_list(datadump):
    669   if interface_list_cache.has_key(datadump['autogen_item']):
     668  if datadump['autogen_item'] in interface_list_cache:
    670669    return (interface_list_cache[datadump['autogen_item']])
    671670  # lo0 configuration:
     
    684683  masterip_used = False
    685684  for iface_key in get_interface_keys(datadump):
    686     if datadump[iface_key].has_key('ip') and datadump[iface_key]['ip'].startswith(datadump['masterip']):
     685    if 'ip' in datadump[iface_key] and datadump[iface_key]['ip'].startswith(datadump['masterip']):
    687686      masterip_used = True
    688687      break
     
    710709
    711710    # Flag dhclient is possible
    712     if not dhclient_if.has_key(ifname) or dhclient_if[ifname] == False:
     711    if not ifname in dhclient_if or dhclient_if[ifname] == False:
    713712      dhclient_if[ifname] = dhcp_type(ifacedump) == DHCP_CLIENT
    714713
    715714    # Ethernet address
    716     if ifacedump.has_key('ether'):
     715    if 'ether' in ifacedump:
    717716        flags_if[ifname]['ether'] = ifacedump['ether']
    718717
     
    721720
    722721    # Add interface IP to list
    723     if ifacedump.has_key('ip'):
     722    if 'ip' in ifacedump:
    724723      item = (ifacedump['ip'], ifacedump['comment'])
    725       if addrs_list.has_key(ifname):
     724      if ifname in addrs_list:
    726725        addrs_list[ifname].append(item)
    727726      else:
     
    740739        ifacedump['autogen_wlanmode'] = "ap"
    741740
    742       if not ifacedump.has_key('channel'):
     741      if not 'channel' in ifacedump:
    743742        if ifacedump['type'] == '11a':
    744743          ifacedump['channel'] = 36
     
    747746
    748747      # Allow special hacks at the back like wds and stuff
    749       if not ifacedump.has_key('extra'):
     748      if not 'extra' in ifacedump:
    750749        ifacedump['autogen_extra'] = 'regdomain ETSI country NL'
    751750      else:
    752751        ifacedump['autogen_extra'] = ifacedump['extra']
    753        
    754       ifacedump['autogen_ssid_hex'] = '0x' + ''.join(x.encode('hex') for x in ifacedump['ssid'])
     752
     753      ifacedump['autogen_ssid_hex'] = '0x' + codecs.encode(ifacedump['ssid'].encode('ascii'), 'hex').decode('ascii')
    755754
    756755      output += "wlans_%(autogen_ifbase)s='%(autogen_ifname)s'\n" % ifacedump
     
    797796  """ Generate configuration file '/etc/rc.conf.local' """
    798797  item = datadump['autogen_item']
    799   if rc_conf_local_cache.has_key(item):
     798  if item in rc_conf_local_cache:
    800799    return rc_conf_local_cache[item]
    801800
    802   if not datadump.has_key('ileiden'):
     801  if not 'ileiden' in datadump:
    803802    datadump['autogen_ileiden_enable'] = False
    804803  else:
     
    856855  #
    857856  list_ileiden_proxies="
    858   {% for serviceid,item in autogen_ileiden_proxies.iteritems() -%}
     857  {% for serviceid,item in autogen_ileiden_proxies.items() -%}
    859858    {{ "%-16s"|format(serviceid) }} # {{ item.nodename }}
    860859  {% endfor -%}
     
    997996  # Print IP address which needs to be assigned over here
    998997  output += "\n"
    999   for iface,addrs in sorted(addrs_list.iteritems()):
     998  for iface,addrs in sorted(addrs_list.items()):
    1000999    for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
    10011000      output += "# %s || %s || %s\n" % (iface, addr, comment)
     
    10131012    # Make sure the external address is always first as this is needed in the
    10141013    # firewall setup
    1015     addrs = sorted(
    1016         [x for x in addrs if not '0.0.0.0' in x[0]],
    1017         key=lambda x: x[0].split('.')[0],
    1018         cmp=lambda x,y: cmp(1 if x == '172' else 0, 1 if y == '172' else 0)
    1019       )
    1020 
     1014    addrs_tmp = []
     1015    for addr in addrs:
     1016        if addr[0].startswith('172'):
     1017            addrs_tmp.insert(0, addr)
     1018        else:
     1019            addrs_tmp.append(addr)
     1020    addrs = addrs_tmp
    10211021
    10221022    idx_offset = 0
    10231023    # Set MAC is required
    1024     if flags_if[iface].has_key('ether'):
     1024    if 'ether' in flags_if[iface]:
    10251025      output += prefix + "ifconfig_%s='link %s'\n" % (iface, flags_if[iface]['ether'])
    10261026      output += prefix + "ifconfig_%s_alias0='inet %s'\n" % (iface, addrs[0][0])
     
    10961096  (poel, errors) = make_relations()
    10971097  table = []
    1098   for iface,addrs in sorted(addrs_list.iteritems()):
     1098  for iface,addrs in sorted(addrs_list.items()):
    10991099    if iface in ['lo0']:
    11001100      continue
     
    11151115    ifacedump = datadump[iface_key]
    11161116
    1117     if not ifacedump.has_key('ns_ip'):
     1117    if not 'ns_ip' in ifacedump:
    11181118      continue
    11191119
     
    12301230  autogen_ips = []
    12311231  (addrs_list, _, _, dhclient_if, _, extra_ouput) = make_interface_list(datadump)
    1232   for iface,addrs in sorted(addrs_list.iteritems()):
     1232  for iface,addrs in sorted(addrs_list.items()):
    12331233    for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
    12341234      if addr.startswith('172'):
     
    12581258    forward-addr: 208.67.220.220 # OpenDNS DNS B
    12591259{% else -%}
    1260 {%- for serviceid,item in autogen_ileiden_proxies.iteritems() %}
     1260{%- for serviceid,item in autogen_ileiden_proxies.items() %}
    12611261  {%- if loop.index <= 5 %}
    12621262    forward-addr: {{ "%-16s"|format(serviceid) }} # {{ item.nodename }}
     
    13061306  (addrs_list, vlan_list, bridge_list, dhclient_if, flags_if, extra_ouput) = make_interface_list(datadump)
    13071307  table = []
    1308   for iface,addrs in sorted(addrs_list.iteritems()):
     1308  for iface,addrs in sorted(addrs_list.items()):
    13091309    if iface in ['lo0']:
    13101310      continue
     
    13911391      output += "%s:\n" % iface_key
    13921392      for key,required in key_order:
    1393         if datadump[iface_key].has_key(key):
     1393        if key in datadump[iface_key]:
    13941394          output += "  %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
    13951395      output += "\n\n"
     
    14061406  output = generate_header(datadump, "#") if header else ''
    14071407
    1408   for key in datadump.keys():
     1408  for key in list(datadump.keys()):
    14091409    if key.startswith('autogen_'):
    14101410      del datadump[key]
    14111411    # Interface autogen cleanups
    14121412    elif type(datadump[key]) == dict:
    1413       for key2 in datadump[key].keys():
     1413      for key2 in list(datadump[key].keys()):
    14141414        if key2.startswith('autogen_'):
    14151415          del datadump[key][key2]
     
    14701470    else:
    14711471      assert False, "Config not found!"
    1472   except IOError, e:
     1472  except IOError as e:
    14731473    output += "[ERROR] Config file not found"
    14741474  return output
     
    15021502
    15031503  # Update repository if requested
    1504   form = urlparse.parse_qs(environ['QUERY_STRING']) if environ.has_key('QUERY_STRING') else None
    1505   if form and form.has_key("action") and "update" in form["action"]:
     1504  form = urllib.parse.urlparse.parse_qs(environ['QUERY_STRING']) if QUERY_STRING in environ else None
     1505  if form and 'action' in form and 'update' in form['action']:
    15061506    refresh_rate = 5
    15071507    output = "[INFO] Updating subverion, please wait...\n"
     
    15101510    output += subprocess.Popen([SVN, 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
    15111511    new_version = subprocess.Popen([SVNVERSION, '-c', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
    1512     if old_version != new_version or (environ.has_key('QUERY_STRING') and 'force' in environ['QUERY_STRING']):
     1512    if old_version != new_version or ('QUERY_STRING' in environ and 'force' in environ['QUERY_STRING']):
    15131513        try:
    15141514            generate_static(CACHE_DIR, False)
     
    15751575    fqdn = datadump['nodename']
    15761576 
    1577     if datadump.has_key('rdr_host'):
     1577    if 'rdr_host' in datadump:
    15781578      remote_target = datadump['rdr_host']
    1579     elif datadump.has_key('remote_access') and datadump['remote_access']:
     1579    elif 'remote_access' in datadump and datadump['remote_access']:
    15801580      remote_target = datadump['remote_access'].split(':')[0]
    15811581    else:
     
    16101610        cidr = int(cidr)
    16111611        addr = addr & ~((1 << (32 - cidr)) - 1)
    1612         if pool.has_key(addr):
     1612        if addr in pool:
    16131613          pool[addr] += [(iface_name, fqdn, ip)]
    16141614        else:
     
    16241624
    16251625  # Automatic naming convention of interlinks namely 2 + remote.lower()
    1626   for (key,value) in pool.iteritems():
     1626  for (key,value) in pool.items():
    16271627    # Make sure they are sorted from low-ip to high-ip
    16281628    value = sorted(value, key=lambda x: parseaddr(x[2]))
     
    16741674    k, items = line.items()[0]
    16751675    if type(items) == dict:
    1676       if items.has_key('reverse'):
     1676      if 'reverse' in items:
    16771677        reverse = items['reverse']
    16781678        items = items['a']
     
    17241724  f.write(dns_header % details)
    17251725
    1726   for host,items in wleiden_zone.iteritems():
     1726  for host,items in wleiden_zone.items():
    17271727    for ip,reverse in items:
    17281728      if ip not in ['0.0.0.0']:
    17291729        f.write("%s.wleiden.net. IN A %s\n" % (host.lower(), ip))
    1730   for source,dest in wleiden_cname.iteritems():
     1730  for source,dest in wleiden_cname.items():
    17311731    dest = dest if dest.endswith('.') else dest + ".wleiden.net."
    17321732    f.write("%s.wleiden.net. IN CNAME %s\n" % (source.lower(), dest.lower()))
    1733   for source, dest in wleiden_raw.iteritems():
     1733  for source, dest in wleiden_raw.items():
    17341734    f.write("%s.wleiden.net. %s\n" % (source, dest))
    17351735  f.close()
     
    17431743    #XXX: Not effient, fix to proper data structure and do checks at other
    17441744    # stages
    1745     for host,items in wleiden_zone.iteritems():
     1745    for host,items in wleiden_zone.items():
    17461746      for ip,reverse in items:
    17471747        if not reverse:
     
    17561756
    17571757def usage():
    1758   print """Usage: %(prog)s <argument>
     1758  print("""Usage: %(prog)s <argument>
    17591759Argument:
    17601760\tcleanup                      =  Cleanup all YAML files to specified format
     
    17831783  VIEW differences and VERIFY all are OK:
    17841784  $ diff -urI 'Generated' -r /tmp/pre /tmp/post
    1785 """ % { 'prog' : sys.argv[0], 'files'  : '|'.join(files) }
     1785""" % { 'prog' : sys.argv[0], 'files'  : '|'.join(files) })
    17861786  exit(0)
    17871787
     
    18141814def fix_conflict(left, right, default='i'):
    18151815  while True:
    1816     print "## %-30s | %-30s" % (left, right)
     1816    print("## %-30s | %-30s" % (left, right))
    18171817    c = raw_input("## Solve Conflict (h for help) <l|r|e|i|> [%s]: " % default)
    18181818    if not c:
     
    18281828      return None
    18291829    else:
    1830       print "#ERROR: '%s' is invalid input (left, right, edit or ignore)!" % c
     1830      print("#ERROR: '%s' is invalid input (left, right, edit or ignore)!" % c)
    18311831
    18321832
     
    18341834def print_cgi_response(response_headers, output):
    18351835  for header in response_headers:
    1836      print "%s: %s" % header
    1837   print
    1838   print output
     1836     print("%s: %s" % header)
     1837  print("")
     1838  print(output)
    18391839
    18401840
     
    18431843  """Hard working sub"""
    18441844  # Allow easy hacking using the CLI
    1845   if not os.environ.has_key('REQUEST_URI'):
     1845  if not os.environ.get('REQUEST_URI', None):
    18461846    if len(sys.argv) < 2:
    18471847      usage()
     
    18831883        node = sys.argv[2]
    18841884      except IndexError:
    1885         print "Invalid argument"
     1885        print("Invalid argument")
    18861886        exit(1)
    18871887      except IOError as e:
    1888         print e
     1888        print(e)
    18891889        exit(1)
    18901890
     
    18981898      for config in gen_files:
    18991899         logger.info("## Generating %s %s", node, config)
    1900          print generate_config(node, config, datadump)
     1900         print(generate_config(node, config, datadump))
    19011901    elif sys.argv[1] == "test-cgi":
    19021902      os.environ['REQUEST_URI'] = "/".join(['config'] + sys.argv[2:])
     
    19271927        for iface_key in sorted([elem for elem in datadump.keys() if elem.startswith('iface_')]):
    19281928          ifacedump = datadump[iface_key]
    1929           if ifacedump.has_key('mode') and ifacedump['mode'] == 'ap-wds':
     1929          if 'mode' in ifacedump and ifacedump['mode'] == 'ap-wds':
    19301930            ifacedump['nodename'] = datadump['nodename']
    1931             if not ifacedump.has_key('channel') or not ifacedump['channel']:
     1931            if not 'channel' in ifacedump or not ifacedump['channel']:
    19321932              ifacedump['channel'] = 0
    19331933            sql = """INSERT INTO links (node_id, type, ssid, protocol, channel, status)
     
    19381938            datadump = get_yaml(host)
    19391939            if datadump.get('service_proxy_normal', False) or datadump.get('service_proxy_ileiden', False):
    1940                 print textwrap.dedent("""\
     1940                print(textwrap.dedent("""\
    19411941                    ++ wleiden-gw-%(nodename)s
    19421942                    menu = %(nodename)s.gw
    19431943                    title = Wireless Leiden gateway %(nodename)s.gw.wleiden.net.
    19441944                    host = %(nodename)s.gw.wleiden.net.
    1945                     """ % datadump)
     1945                    """ % datadump))
    19461946    elif sys.argv[1] == "nagios-export":
    19471947      try:
     
    19631963        ip2host[datadump['masterip']] = datadump['autogen_fqdn']
    19641964        for iface in get_interface_keys(datadump):
    1965           if datadump[iface].has_key('autogen_gateway'):
     1965          if 'autogen_gateway' in datadump[iface]:
    19661966            ip2host[datadump[iface]['autogen_gateway']] = datadump['autogen_fqdn']
    19671967
     
    19791979              parents[ip2host[ip]].append(ip2host[stack[-1]])
    19801980            except KeyError as e:
    1981               print >> stderr, "# Unable to find %s in configuration files" % e.args[0]
     1981              print("# Unable to find %s in configuration files" % e.args[0])
    19821982            stack.append(ip)
    19831983          elif prev_depth > depth:
     
    19871987              parents[ip2host[ip]].append(ip2host[stack[-1]])
    19881988            except KeyError as e:
    1989               print >> stderr, "# Unable to find %s in configuration files" % e.args[0]
     1989              print("# Unable to find %s in configuration files" % e.args[0])
    19901990
    19911991
     
    20032003      } 
    20042004
    2005       print '''\
     2005      print('''\
    20062006define host {
    20072007        name                  wleiden-node           ; Default Node Template
     
    20752075# TDB: Advanced local passive checks
    20762076# /usr/local/libexec/nagios/check_by_ssh
    2077 ''' % params
    2078 
    2079       print '''\
     2077''' % params)
     2078
     2079      print('''\
    20802080# Service Group, not displayed by default       
    20812081define hostgroup {
     
    21052105        check_command         check_dns_wl!"www.wirelessleiden.nl"
    21062106}
    2107 '''
     2107''')
    21082108
    21092109      if heavy_load:
    2110         print '''\
     2110        print('''\
    21112111define service {
    21122112        use                   wleiden-service
     
    21432143        check_command         check_snmp_disk
    21442144}
    2145 '''
     2145''')
    21462146      for node in get_hostlist():
    21472147        datadump = get_yaml(node)
    21482148        if not datadump['status'] == 'up':
    21492149          continue
    2150         if not hostgroup_details.has_key(datadump['monitoring_group']):
     2150        if not datadump['monitoring_group'] in hostgroup_details:
    21512151           hostgroup_details[datadump['monitoring_group']] = datadump['monitoring_group']
    2152         print '''\
     2152        print('''\
    21532153define host {
    21542154        use                   wleiden-node,host-pnp
     
    21572157        address               %(masterip)s
    21582158        hostgroups            srv_hybrid,%(monitoring_group)s\
    2159 ''' % datadump
     2159''' % datadump)
    21602160        if (len(parents[datadump['autogen_fqdn']]) > 0) and parents[datadump['autogen_fqdn']][0] != 'root':
    2161           print '''\
     2161          print('''\
    21622162        parents               %(parents)s\
    2163 ''' % { 'parents' : parents[datadump['autogen_fqdn']][0] }
    2164         print '''\
     2163''' % { 'parents' : parents[datadump['autogen_fqdn']][0] })
     2164        print('''\
    21652165}
    2166 '''
    2167 
    2168 
    2169       for name,alias in hostgroup_details.iteritems():
    2170         print '''\
     2166''')
     2167
     2168
     2169      for name,alias in hostgroup_details.items():
     2170        print('''\
    21712171define hostgroup {
    21722172       hostgroup_name         %s
    21732173       alias                  %s
    2174 } ''' % (name, alias)     
     2174} ''' % (name, alias))
    21752175
    21762176
     
    21802180        datadump = get_yaml(node)
    21812181        hosts[datadump['nodename']] = datadump
    2182       print yaml.dump(hosts)
     2182      print(yaml.dump(hosts))
    21832183
    21842184    elif sys.argv[1] == "dns":
     
    21962196
    21972197      (poel, errors) = make_relations()
    2198       print "\n".join(["# WARNING: %s" % x for x in errors])
    2199 
    2200       for host,datadump in datadumps.iteritems():
     2198      print("\n".join(["# WARNING: %s" % x for x in errors]))
     2199
     2200      for host,datadump in datadumps.items():
    22012201        try:
    22022202          # Convert all yes and no to boolean values
     
    22302230                datadump[iface_key]['mode'] = 'ap'
    22312231              # Wireless Leiden SSID have an consistent lowercase/uppercase
    2232               if datadump[iface_key].has_key('ssid'):
     2232              if 'ssid' in datadump[iface_key]:
    22332233                ssid = datadump[iface_key]['ssid']
    22342234                prefix = 'ap-WirelessLeiden-'
    22352235                if ssid.lower().startswith(prefix.lower()):
    22362236                  datadump[iface_key]['ssid'] = prefix + ssid[len(prefix)].upper() + ssid[len(prefix) + 1:]
    2237               if datadump[iface_key].has_key('ns_ip') and not datadump[iface_key].has_key('mode'):
     2237              if 'ns_ip' in datadump[iface_key] and not 'mode' in  datadump[iface_key]:
    22382238                datadump[iface_key]['mode'] = 'autogen-FIXME'
    2239               if not datadump[iface_key].has_key('comment'):
     2239              if not 'comment' in datadump[iface_key]:
    22402240                datadump[iface_key]['comment'] = 'autogen-FIXME'
    22412241
    2242               if datadump[iface_key].has_key('ns_mac'):
     2242              if 'ns_mac' in datadump[iface_key]:
    22432243                datadump[iface_key]['ns_mac'] = datadump[iface_key]['ns_mac'].lower()
    22442244
    2245               if datadump[iface_key]['comment'].startswith('autogen-') and datadump[iface_key].has_key('comment'):
     2245              if 'comment' in datadump[iface_key] and datadump[iface_key]['comment'].startswith('autogen-'):
    22462246                datadump[iface_key] = datadump[iface_key]['desc']
    22472247
     
    22612261              # See: https://en.wikipedia.org/wiki/List_of_WLAN_channels
    22622262              channels_at_2400Mhz = (1,6,11)
    2263               if datadump[iface_key]['type'] == '11g' and datadump[iface_key].has_key('channel'):
     2263              if datadump[iface_key]['type'] == '11g' and 'channel' in datadump[iface_key]:
    22642264                datadump[iface_key]['channel'] = int(datadump[iface_key]['channel'])
    22652265                if datadump[iface_key]['channel'] not in channels_at_2400Mhz:
     
    22672267
    22682268              # Mandatory interface keys
    2269               if not datadump[iface_key].has_key('status'):
     2269              if not 'status' in datadump[iface_key]:
    22702270                datadump[iface_key]['status'] = 'planned'
    22712271
     
    22792279
    22802280              # Making sure description works
    2281               if datadump[iface_key].has_key('desc'):
     2281              if 'desc' in datadump[iface_key]:
    22822282                if datadump[iface_key]['comment'].lower() == datadump[iface_key]['desc'].lower():
    22832283                  del datadump[iface_key]['desc']
    22842284                else:
    2285                   print "# ERROR: At %s - %s" % (datadump['nodename'], iface_key)
     2285                  print("# ERROR: At %s - %s" % (datadump['nodename'], iface_key))
    22862286                  response = fix_conflict(datadump[iface_key]['comment'], datadump[iface_key]['desc'])
    22872287                  if response:
     
    23342334       output = datadump['autogen_fqdn'] if use_fqdn else system
    23352335       if sys.argv[2] == "all":
    2336          print output
     2336         print(output)
    23372337       elif datadump['status'] == sys.argv[2]:
    2338          print output
     2338         print(output)
    23392339    elif sys.argv[1] == "create":
    23402340      if sys.argv[2] == "network.kml":
    2341         print make_network_kml.make_graph()
     2341        print(make_network_kml.make_graph())
    23422342      elif sys.argv[2] == "host-ips.txt":
    23432343        for system in get_hostlist():
     
    23462346          for ifkey in get_interface_keys(datadump):
    23472347            ips.append(datadump[ifkey]['ip'].split('/')[0])
    2348           print system, ' '.join(ips)
     2348          print(system, ' '.join(ips))
    23492349      elif sys.argv[2] == "host-pos.txt":
    23502350        for system in get_hostlist():
    23512351          datadump = get_yaml(system)
    2352           print system, datadump['rdnap_x'], datadump['rdnap_y']
     2352          print(system, datadump['rdnap_x'], datadump['rdnap_y'])
    23532353      elif sys.argv[2] == "nodeplanner.json":
    2354         print make_network_kml.make_nodeplanner_json()
     2354        print(make_network_kml.make_nodeplanner_json())
    23552355      elif sys.argv[2] == 'ssh_config':
    2356         print  '''
     2356        print('''
    23572357Host *.wleiden.net
    23582358  User root
     
    23602360Host 172.16.*.*
    23612361  User root
    2362 '''
     2362''')
    23632363        for system in get_hostlist():
    23642364          datadump = get_yaml(system)
    2365           print '''\
     2365          print('''\
    23662366Host %s
    23672367  User root
     
    23752375Host %s
    23762376  User root
    2377 ''' % (system, system.lower(), datadump['nodename'], datadump['nodename'].lower())
     2377''' % (system, system.lower(), datadump['nodename'], datadump['nodename'].lower()))
    23782378      else:
    23792379        usage()
     
    23882388      response_headers, output = process_cgi_request()
    23892389    except:
    2390       print ''
    2391       print ''
     2390      print('')
     2391      print('')
    23922392      raise
    23932393
  • tools/make_network_kml.py

    r14313 r14374  
    8181  try:
    8282    for host in gformat.get_hostlist():
    83       if debug: print "## Processing host", host
     83      if debug: print("## Processing host", host)
    8484      datadump = gformat.get_yaml(host)
    8585      nodename = datadump['nodename']
     
    100100        mask = int(mask)
    101101        addr = addr & ~((1 << (32 - mask)) - 1)
    102         if poel.has_key(addr):
     102        if addr in poel:
    103103          poel[addr] += [nodename]
    104104          if link_status[addr] == 'linkUp':
     
    112112          poel[addr] = [nodename]
    113113          # Assume all ns_ip to be 11a for a moment
    114           if datadump[iface_key].has_key('ns_ip'):
     114          if 'ns_ip' in datadump[iface_key]:
    115115            link_type[addr] = '11a'
    116116          else:
     
    130130          nodename = datadump['nodename']
    131131          INTERVAL = 60 * 10
    132           if store and store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
    133             if store and store['traffic'][nodename].has_key(iface):
     132          if store and nodename in store['uptime'] and nodename in store['snmp'] and nodename in store['traffic']:
     133            if store and iface in store['traffic'][nodename]:
    134134              (b_in, b_out) = store['traffic'][nodename][iface]
    135135              uptime = store['uptime'][nodename]
    136136              t_kb = float(b_in + b_out) / 1024
    137               if debug: print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
     137              if debug: print("# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL))
    138138              retval = ((t_kb) / uptime) * INTERVAL
    139139              link_data[addr] = retval
    140140
    141           if debug: print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
    142   except (KeyError, ValueError), e:
    143     if debug: print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
    144     if debug: print e
     141          if debug: print("### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr]))
     142  except (KeyError, ValueError) as e:
     143    if debug: print("[FOUT] in '%s' interface '%s'" % (host,iface_key))
     144    if debug: print(e)
    145145    sys.exit(1)
    146146  return (poel, link_type, link_data, link_status, hosts)
     
    169169  poel, link_type, link_data, link_status, hosts = get_graph_data(debug)
    170170  output = ''
    171   if debug: print "# Building KML file"
     171  if debug: print("# Building KML file")
    172172  output += HEADER
    173173  for nodename, datadump in hosts.iteritems():
     
    204204  f.write(kml_data)
    205205  f.close()
    206   print "# COMPLETED find your output in %s\n" % OUTFILE
    207 
     206  print("# COMPLETED find your output in %s\n" % OUTFILE)
     207
  • tools/syntax-checker.py

    r14025 r14374  
    1 #!/usr/bin/env python
     1#!/usr/bin/env python3
    22# vim:ts=2:et:sw=2:ai
    33#
     
    1212__version__ = '$Id$'
    1313
    14 allowed_multi_use = map(lambda x: ipaddress.ip_network(x, strict=True), [
    15     u'192.168.0.0/22',
    16     u'192.168.0.0/16',
    17     u'192.168.0.0/24',
    18     u'192.168.1.0/24',
    19     u'192.168.178.0/24',
    20     ])
    21 
    22 
    23 
     14allowed_multi_use = list(map(lambda x: ipaddress.ip_network(x, strict=True), [
     15    '192.168.0.0/22',
     16    '192.168.0.0/16',
     17    '192.168.0.0/24',
     18    '192.168.1.0/24',
     19    '192.168.178.0/24',
     20    ]))
    2421
    2522
     
    2825  try:
    2926    for host in gformat.get_hostlist():
    30       print "## Processing host %-25s: " % host,
     27      print("## Processing host %-25s: " % host, end='')
    3128      datadump = gformat.get_yaml(host,add_version_info=False)
    32       masterip_addr = ipaddress.IPv4Interface(unicode(datadump['masterip']))
     29      masterip_addr = ipaddress.IPv4Interface(datadump['masterip'])
    3330      masterip_is_used = False
    3431
     
    4643          for entry in ['ip', 'ns_ip']:
    4744            if entry in datadump[iface_key]:
    48               addr = ipaddress.IPv4Interface(unicode(datadump[iface_key][entry]))
     45              addr = ipaddress.IPv4Interface(datadump[iface_key][entry])
    4946              if masterip_addr in addr.network:
    5047                masterip_is_used = True
     
    5653            pool[masterip_addr.network].append((host, 'masterip', '', masterip_addr))
    5754
    58         print "OK"
    59       except (KeyError, ValueError), e:
    60         print "[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e)
     55        print("OK")
     56      except (KeyError, ValueError) as e:
     57        print("[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e))
    6158        raise
    6259  except Exception as e:
     
    7269        if not network2 in allowed_multi_use and network2.overlaps(network):
    7370          errors += 1
    74           print "[ERROR#%i] network %s overlaps with %s:" % (errors, network, network2)
     71          print("[ERROR#%i] network %s overlaps with %s:" % (errors, network, network2))
    7572          for (host, key, entry, addr) in sorted(pool[network] + pool[network2]):
    76             print "  - %-20s - %-20s - %-5s - %s" % (host, key, entry, addr)
     73            print("  - %-20s - %-20s - %-5s - %s" % (host, key, entry, addr))
    7774
    7875      leden = sorted(pool[network])
     
    8178          if lid[3] == lid2[3]:
    8279            errors += 1
    83             print "[ERROR#%i] Multiple usages of IP %s:" % (errors, lid[3])
    84             print "  - %-20s - %-20s - %-5s" % (lid[0], lid[1], lid[2])
    85             print "  - %-20s - %-20s - %-5s" % (lid2[0], lid2[1], lid2[2])
     80            print("[ERROR#%i] Multiple usages of IP %s:" % (errors, lid[3]))
     81            print("  - %-20s - %-20s - %-5s" % (lid[0], lid[1], lid[2]))
     82            print("  - %-20s - %-20s - %-5s" % (lid2[0], lid2[1], lid2[2]))
    8683           
    8784  if errors > 0:
    88     print "# %i Errors found" % errors
     85    print("# %i Errors found" % errors)
    8986    return 1
    9087  else:
    91     print "# No multiple usages of IPs found"
     88    print("# No multiple usages of IPs found")
    9289    return 0
    9390
Note: See TracChangeset for help on using the changeset viewer.