From d1f9da788faa0e32f63d21d629107f2e55e2e715 Mon Sep 17 00:00:00 2001 From: parmentelat Date: Thu, 13 Dec 2018 12:49:52 +0100 Subject: [PATCH 1/1] db-config scripts also need to go python3 - plus autopep8 in the mix --- db-config.d/000-functions | 49 +++--- db-config.d/001-admin_user | 22 +-- db-config.d/002-system_site | 39 ++--- db-config.d/010-slice_tags | 276 ++++++++++++++++----------------- db-config.d/030-interface_tags | 32 ++-- 5 files changed, 210 insertions(+), 208 deletions(-) diff --git a/db-config.d/000-functions b/db-config.d/000-functions index 5779b0e..bc0e8e7 100644 --- a/db-config.d/000-functions +++ b/db-config.d/000-functions @@ -1,5 +1,5 @@ # -*-python-*- -#################### +#################### import sys, os g_url = "" @@ -37,7 +37,7 @@ def SetTagType(tag_type): roles=['admin'] # just in case if 'min_role_id' in tag_type: - print "WARNING: ignoring deprecated field min_role_id for tagtype %s"%tagname + print("WARNING: ignoring deprecated field min_role_id for tagtype %s"%tagname) del tag_type['min_role_id'] # Create/update default slice tag types if tagname not in g_known_tag_types: @@ -53,26 +53,25 @@ def SetTagType(tag_type): for plus_role in set(roles).difference(set(old_roles)): AddRoleToTagType(plus_role,tagname) except: - # something went wrong for that tagname, + # something went wrong for that tagname, # but don't want to break the whole startup sequence - print "Could not enforce tagtype %s --- beg"%tagname + print("Could not enforce tagtype %s --- beg"%tagname) import traceback traceback.print_exc() - print "Could not enforce tagtype %s --- end"%tagname + print("Could not enforce tagtype %s --- end"%tagname) # Get list of existing (enabled, global) files g_conf_files = GetConfFiles() -g_conf_files = filter(lambda conf_file: conf_file['enabled'] and \ +g_conf_files = [conf_file for conf_file in g_conf_files if conf_file['enabled'] and \ not conf_file['node_ids'] and \ - not conf_file['nodegroup_ids'], - g_conf_files) + not conf_file['nodegroup_ids']] g_dests = [conf_file['dest'] for conf_file in g_conf_files] -g_conf_files = dict(zip(g_dests, g_conf_files)) +g_conf_files = dict(list(zip(g_dests, g_conf_files))) # Get list of existing initscripts g_oldinitscripts = GetInitScripts() g_oldinitscript_names = [script['name'] for script in g_oldinitscripts] -g_oldinitscripts = dict(zip(g_oldinitscript_names, g_oldinitscripts)) +g_oldinitscripts = dict(list(zip(g_oldinitscript_names, g_oldinitscripts))) def SetInitScript(initscript): global g_oldinitscripts, g_oldinitscript_names @@ -85,7 +84,7 @@ def SetInitScript(initscript): orig_initscript = g_oldinitscripts[initscript['name']] initscript_id = orig_initscript['initscript_id'] UpdateInitScript(initscript_id, initscript) - + def SetConfFile(conf_file): global g_conf_files, g_dests if conf_file['dest'] not in g_dests: @@ -102,50 +101,51 @@ def SetSlice(slice, tags): slices = GetSlices([slice_name]) if len(slices)==1: slice_id = slices[0]['slice_id'] - if slice.has_key('name'): + if 'name' in slice: del slice['name'] UpdateSlice(slice_id, slice) slice['name']=slice_name else: expires = None - if slice.has_key('expires'): + if 'expires' in slice: expires = slice['expires'] del slice['expires'] slice_id = AddSlice(slice) - if expires <> None: + if expires is not None: UpdateSlice(slice_id, {'expires':expires}) - + # Get slice structure with all fields slice = GetSlices([slice_name])[0] - + # Create/delete all tags - # NOTE: update is not needed, since unspecified tags are deleted, + # NOTE: update is not needed, since unspecified tags are deleted, # and new tags are added slice_tags = [] if slice['slice_tag_ids']: # Delete unknown attributes for slice_tag in GetSliceTags(slice['slice_tag_ids']): # ignore sliver tags, as those are custom/run-time values - if slice_tag['node_id'] <> None: continue + if slice_tag['node_id'] is not None: + continue if (slice_tag['tagname'], slice_tag['value']) not in tags: DeleteSliceTag(slice_tag['slice_tag_id']) else: slice_tags.append((slice_tag['tagname'],slice_tag['value'])) - + # only add slice tags that are new for (name, value) in tags: if (name,value) not in slice_tags: - AddSliceTag(slice_name, name, value) + AddSliceTag(slice_name, name, value) else: - # NOTE: this confirms that the user-specified tag is + # NOTE: this confirms that the user-specified tag is # returned by GetSliceTags pass except: - # something went wrong for that tagname, - print "Could not create init slice %s --- beg"%slice['name'] + # something went wrong for that tagname, + print("Could not create init slice %s --- beg"%slice['name']) import traceback traceback.print_exc() - print "Could not create init slice %s --- end"%slice['name'] + print("Could not create init slice %s --- end"%slice['name']) def SetMessage(message): messages = GetMessages([message['message_id']]) @@ -174,4 +174,3 @@ def SetPCUType(pcu_type): # for each protocol, also add this. for ptype in protocol_types: AddPCUProtocolType(id, ptype) - diff --git a/db-config.d/001-admin_user b/db-config.d/001-admin_user index 74a9723..b01039f 100644 --- a/db-config.d/001-admin_user +++ b/db-config.d/001-admin_user @@ -1,23 +1,25 @@ # -*-python-*- -#################### +#################### # Create/update the default administrator account (should be person_id 2). -the_admin_id=2 -admin = { 'first_name': "Default", - 'last_name': "Administrator", - 'email': plc['root_user'], - 'password': plc['root_password'] } +the_admin_id = 2 + +admin = {'first_name': "Default", + 'last_name': "Administrator", + 'email': plc['root_user'], + 'password': plc['root_password']} + persons = GetPersons(the_admin_id) + if not persons: # AddPerson won't let you pass a person_id person_id = AddPerson(admin) if person_id != the_admin_id: # Huh? Someone deleted the account manually from the database. DeletePerson(person_id) - raise Exception, "Someone deleted the \"%s %s\" account from the database!" % \ - (admin['first_name'], admin['last_name']) - UpdatePerson(person_id, { 'enabled': True }) + raise Exception("Someone deleted the \"%s %s\" account from the database!" % + (admin['first_name'], admin['last_name'])) + UpdatePerson(person_id, {'enabled': True}) else: person_id = persons[0]['person_id'] UpdatePerson(person_id, admin) - diff --git a/db-config.d/002-system_site b/db-config.d/002-system_site index 1564694..698ea59 100644 --- a/db-config.d/002-system_site +++ b/db-config.d/002-system_site @@ -2,7 +2,7 @@ #################### # Create/update and populate the default site (should be site_id 1) -### plc_www holds the contents of the PLC_WWW configuration category +# plc_www holds the contents of the PLC_WWW configuration category if plc_www['port'] == '80': url = "http://" + plc_www['host'] + "/" elif plc_www['port'] == '443': @@ -12,21 +12,22 @@ else: SetMyPLCURL(url) -site = { 'site_id': 1, - 'name': plc['name'] + " Central", - 'abbreviated_name': plc['name'], - 'login_base': plc['slice_prefix'], - 'is_public': False, - 'url': url, - 'max_slices': 100 } +site = {'site_id': 1, + 'name': plc['name'] + " Central", + 'abbreviated_name': plc['name'], + 'login_base': plc['slice_prefix'], + 'is_public': False, + 'url': url, + 'max_slices': 100} sites = GetSites([site['site_id']]) if not sites: - site_id = AddSite(site['name'], site['abbreviated_name'], site['login_base'], site) + site_id = AddSite( + site['name'], site['abbreviated_name'], site['login_base'], site) if site_id != site['site_id']: DeleteSite(site_id) - raise Exception, "Someone deleted the \"%s\" site from the database!" % \ - site['name'] + raise Exception("Someone deleted the \"%s\" site from the database!" % + site['name']) sites = [site] # Must call UpdateSite() even after AddSite() to update max_slices @@ -43,24 +44,24 @@ AddRoleToPerson(10, the_admin_id) AddRoleToPerson(20, the_admin_id) # Associate root ssh key with the default administrator -keyfile=plc['root_ssh_key_pub'] +keyfile = plc['root_ssh_key_pub'] person = GetPersons(the_admin_id)[0] keys = GetKeys(person['key_ids']) if os.path.exists(keyfile): with open(keyfile) as feed: sshkey = feed.read() - found=False + found = False for key in keys: - if key['key_type']=='ssh': + if key['key_type'] == 'ssh': if key['key'] == sshkey: - found=True + found = True else: # should we delete other keys? pass if not found: - key_id = AddPersonKey(the_admin_id,{'key_type':'ssh','key':sshkey}) + key_id = AddPersonKey(the_admin_id, {'key_type': 'ssh', 'key': sshkey}) else: - if len(keys)==0: - print "WARNING: default administrator does not have an ssh key" - print "and the default ssh root pub key (%s) file does not exist." % keyfile + if not keys: + print("WARNING: default administrator does not have an ssh key") + print("and the default ssh root pub key (%s) file does not exist." % keyfile) diff --git a/db-config.d/010-slice_tags b/db-config.d/010-slice_tags index 9a4184f..9237252 100644 --- a/db-config.d/010-slice_tags +++ b/db-config.d/010-slice_tags @@ -1,156 +1,156 @@ # -*-python-*- -#################### slice tag types +# slice tag types # xxx this should move to PLC/Accessors # vref is now defined in an accessor # initscript is now defined in an accessor - + # Setup default slice tag types slicetag_types = \ -[ - -### this applies on Node, not on Slice -### # Slice type (only vserver is supported) -### {'tagname': "type", -### 'description': "Type of slice (e.g. vserver)", -### 'category' : 'slice/general', -### 'roles': ['admin','pi']}, - - # System slice - {'tagname': "system", - 'description': "Is a default system slice (1) or not (0 or unset)", - 'category' : 'slice/general'}, - - # Slice enabled (1) or suspended (0) - {'tagname': "enabled", - 'description': "Slice enabled (1 or unset) or suspended (0)", - 'category' : 'slice/general'}, - - # IP Addresses for a Slice - {'tagname': "ip_addresses", - 'description': "Add an ip address to a slice/sliver.", - 'category' : 'slice/rspec'}, - {'tagname': "isolate_loopback", - 'description': "Create an isolated loopback interface within the vserver rather than sharing with all vservers.", - 'category' : 'slice/rspec'}, - - # CPU share - {'tagname': "cpu_pct", - 'description': "Reserved CPU percent", - 'category' : 'slice/rspec'}, - {'tagname': "cpu_share", - 'description': "Number of CPU shares", - 'category' : 'slice/rspec'}, - {'tagname': "cpu_cores", - 'description': "Number of CPU cores", - 'category': 'slice/rspec'}, - {'tagname': "cpu_freezable", - 'description': "Slice processes should be frozen if cpu_cores is 0", - 'category': 'slice/rspec'}, - - # Bandwidth limits - {'tagname': "net_min_rate", - 'description': "Minimum bandwidth (kbps)", - 'category' : 'slice/rspec'}, - {'tagname': "net_max_rate", - 'description': "Maximum bandwidth (kbps)", - 'category' : 'slice/rspec'}, - {'tagname': "net_i2_min_rate", - 'description': "Minimum bandwidth over I2 routes (kbps)", - 'category' : 'slice/rspec'}, - {'tagname': "net_i2_max_rate", - 'description': "Maximum bandwidth over I2 routes (kbps)", - 'category' : 'slice/rspec'}, - {'tagname': "net_max_kbyte", - 'description': "Maximum daily network Tx KByte limit.", - 'category' : 'slice/rspec'}, - {'tagname': "net_thresh_kbyte", - 'description': "KByte limit before warning and throttling.", - 'category' : 'slice/rspec'}, - {'tagname': "net_i2_max_kbyte", - 'description': "Maximum daily network Tx KByte limit to I2 hosts.", - 'category' : 'slice/rspec'}, - {'tagname': "net_i2_thresh_kbyte", - 'description': "KByte limit to I2 hosts before warning and throttling.", - 'category' : 'slice/rspec'}, - {'tagname': "net_share", - 'description': "Number of bandwidth shares", - 'category' : 'slice/rspec'}, - {'tagname': "net_i2_share", - 'description': "Number of bandwidth shares over I2 routes", - 'category' : 'slice/rspec'}, - - # Disk quota - {'tagname': "disk_max", - 'description': "Disk quota (1k disk blocks)", - 'category' : 'slice/rspec'}, - - # deprecated in nov. 2010 - # Proper operations - #{'tagname': "proper_op", - # 'description': "Proper operation (e.g. bind_socket)", - # 'category' : 'slice/rspec'}, - - # VServer capabilities - {'tagname': "capabilities", - 'description': "VServer bcapabilities (separate by commas)", - 'category' : 'slice/rspec'}, - - # Vsys - # need to allow this one so that slice can have that set from PLC_VSYS_DEFAULT - {'tagname': "vsys", - 'description': "Bind vsys script fd's to a slice's /vsys directory.", - 'category' : 'slice/rspec', - 'roles': AllPersonRoles()}, - {'tagname': "vsys_vnet", - 'description': """Specify the IP range that can be used in a given slice + [ + + # this applies on Node, not on Slice + # Slice type (only vserver is supported) + # {'tagname': "type", + # 'description': "Type of slice (e.g. vserver)", + # 'category' : 'slice/general', + # 'roles': ['admin','pi']}, + + # System slice + {'tagname': "system", + 'description': "Is a default system slice (1) or not (0 or unset)", + 'category': 'slice/general'}, + + # Slice enabled (1) or suspended (0) + {'tagname': "enabled", + 'description': "Slice enabled (1 or unset) or suspended (0)", + 'category': 'slice/general'}, + + # IP Addresses for a Slice + {'tagname': "ip_addresses", + 'description': "Add an ip address to a slice/sliver.", + 'category': 'slice/rspec'}, + {'tagname': "isolate_loopback", + 'description': "Create an isolated loopback interface within the vserver rather than sharing with all vservers.", + 'category': 'slice/rspec'}, + + # CPU share + {'tagname': "cpu_pct", + 'description': "Reserved CPU percent", + 'category': 'slice/rspec'}, + {'tagname': "cpu_share", + 'description': "Number of CPU shares", + 'category': 'slice/rspec'}, + {'tagname': "cpu_cores", + 'description': "Number of CPU cores", + 'category': 'slice/rspec'}, + {'tagname': "cpu_freezable", + 'description': "Slice processes should be frozen if cpu_cores is 0", + 'category': 'slice/rspec'}, + + # Bandwidth limits + {'tagname': "net_min_rate", + 'description': "Minimum bandwidth (kbps)", + 'category': 'slice/rspec'}, + {'tagname': "net_max_rate", + 'description': "Maximum bandwidth (kbps)", + 'category': 'slice/rspec'}, + {'tagname': "net_i2_min_rate", + 'description': "Minimum bandwidth over I2 routes (kbps)", + 'category': 'slice/rspec'}, + {'tagname': "net_i2_max_rate", + 'description': "Maximum bandwidth over I2 routes (kbps)", + 'category': 'slice/rspec'}, + {'tagname': "net_max_kbyte", + 'description': "Maximum daily network Tx KByte limit.", + 'category': 'slice/rspec'}, + {'tagname': "net_thresh_kbyte", + 'description': "KByte limit before warning and throttling.", + 'category': 'slice/rspec'}, + {'tagname': "net_i2_max_kbyte", + 'description': "Maximum daily network Tx KByte limit to I2 hosts.", + 'category': 'slice/rspec'}, + {'tagname': "net_i2_thresh_kbyte", + 'description': "KByte limit to I2 hosts before warning and throttling.", + 'category': 'slice/rspec'}, + {'tagname': "net_share", + 'description': "Number of bandwidth shares", + 'category': 'slice/rspec'}, + {'tagname': "net_i2_share", + 'description': "Number of bandwidth shares over I2 routes", + 'category': 'slice/rspec'}, + + # Disk quota + {'tagname': "disk_max", + 'description': "Disk quota (1k disk blocks)", + 'category': 'slice/rspec'}, + + # deprecated in nov. 2010 + # Proper operations + # {'tagname': "proper_op", + # 'description': "Proper operation (e.g. bind_socket)", + # 'category' : 'slice/rspec'}, + + # VServer capabilities + {'tagname': "capabilities", + 'description': "VServer bcapabilities (separate by commas)", + 'category': 'slice/rspec'}, + + # Vsys + # need to allow this one so that slice can have that set from PLC_VSYS_DEFAULT + {'tagname': "vsys", + 'description': "Bind vsys script fd's to a slice's /vsys directory.", + 'category': 'slice/rspec', + 'roles': AllPersonRoles()}, + {'tagname': "vsys_vnet", + 'description': """Specify the IP range that can be used in a given slice for virtual devices involved in topologies, e.g. 192.168.100.0/24""", - 'category': 'slice/rspec'}, - - # CoDemux - {'tagname': "codemux", - 'description': "Demux HTTP between slices using localhost ports. Value in the form 'host, localhost port'.", - 'category' : 'slice/rspec'}, - - # Delegation - {'tagname': "delegations", - 'description': "Coma seperated list of slices to give delegation authority to.", - 'category' : 'slice/rspec', - 'roles' : ['admin','pi','user']}, - - # Capability to give a sliver access to unused raw disk - {'tagname': "rawdisk", - 'description': "map unused raw disk devices into the slice", - 'category' : 'slice/access', # we should get rid of this category thing - 'roles': ['admin','pi']}, - - { 'tagname' : 'exempt_slice_until', - 'description' : 'Exclude this slice from MyOps until given date (YYYYMMDD)', - 'category' : 'slice/myops'}, - - # DistributedRateLimiting slice - {'tagname': "drl", - 'description': "Is a default Distributed Rate Limiting slice (1) or not (0 or unset)", - 'category' : 'slice/general'}, - - {'tagname' : 'interface', - 'description' : 'The interface tag holds network configuration information until VirtualInterface objects are in PLCAPI', - 'category' : 'slice/network'}, - -] + 'category': 'slice/rspec'}, + + # CoDemux + {'tagname': "codemux", + 'description': "Demux HTTP between slices using localhost ports. Value in the form 'host, localhost port'.", + 'category': 'slice/rspec'}, + + # Delegation + {'tagname': "delegations", + 'description': "Coma seperated list of slices to give delegation authority to.", + 'category': 'slice/rspec', + 'roles': ['admin', 'pi', 'user']}, + + # Capability to give a sliver access to unused raw disk + {'tagname': "rawdisk", + 'description': "map unused raw disk devices into the slice", + 'category': 'slice/access', # we should get rid of this category thing + 'roles': ['admin', 'pi']}, + + {'tagname': 'exempt_slice_until', + 'description': 'Exclude this slice from MyOps until given date (YYYYMMDD)', + 'category': 'slice/myops'}, + + # DistributedRateLimiting slice + {'tagname': "drl", + 'description': "Is a default Distributed Rate Limiting slice (1) or not (0 or unset)", + 'category': 'slice/general'}, + + {'tagname': 'interface', + 'description': 'The interface tag holds network configuration information until VirtualInterface objects are in PLCAPI', + 'category': 'slice/network'}, + + ] import resource # add in the platform supported rlimits to the default_attribute_types -for entry in resource.__dict__.keys() + ["VLIMIT_OPENFD"]: - if entry.find("LIMIT_")==1: +for entry in list(resource.__dict__.keys()) + ["VLIMIT_OPENFD"]: + if entry.find("LIMIT_") == 1: rlim = entry[len("RLIMIT_"):] rlim = rlim.lower() - for ty in ("min","soft","hard"): + for ty in ("min", "soft", "hard"): attribute = { - 'tagname': "%s_%s"%(rlim,ty), - 'description': "Per sliver RLIMIT %s_%s."%(rlim,ty), + 'tagname': "%s_%s" % (rlim, ty), + 'description': "Per sliver RLIMIT %s_%s." % (rlim, ty), 'category': 'slice/limit', - } + } slicetag_types.append(attribute) for slicetag_type in slicetag_types: diff --git a/db-config.d/030-interface_tags b/db-config.d/030-interface_tags index 6d3d46b..76328c8 100644 --- a/db-config.d/030-interface_tags +++ b/db-config.d/030-interface_tags @@ -1,24 +1,24 @@ # -*-python-*- -#################### interface tag types +# interface tag types # xxx this should move to PLC/Accessors interfacetag_types = \ -[ - {'category': u'interface/ovs', - 'description': u'Name of Open vSwitch bridge', - 'tagname': u'ovs_bridge'}, + [ + {'category': 'interface/ovs', + 'description': 'Name of Open vSwitch bridge', + 'tagname': 'ovs_bridge'}, - # Used by M-lab for IPv6 addresses - {'category': u'interface/config', - 'description': u'IPv6 gateway', - 'tagname': u'ipv6_defaultgw'}, - {'category': u'interface/config', - 'description': u'IPv6 address for the interface', - 'tagname': u'ipv6addr'}, - {'category': u'interface/config', - 'description': u'IPv6 slice addresses', - 'tagname': u'ipv6addr_secondaries'}, -] + # Used by M-lab for IPv6 addresses + {'category': 'interface/config', + 'description': 'IPv6 gateway', + 'tagname': 'ipv6_defaultgw'}, + {'category': 'interface/config', + 'description': 'IPv6 address for the interface', + 'tagname': 'ipv6addr'}, + {'category': 'interface/config', + 'description': 'IPv6 slice addresses', + 'tagname': 'ipv6addr_secondaries'}, + ] for interfacetag_type in interfacetag_types: SetTagType(interfacetag_type) -- 2.43.0