def add_route_table(table_name): """ Adds a new routing table by name Add a new entry in /etc/iproute2/rt_tables Parameters ---------- table_name : str name of the new table Returns ------- bool True for success, False for failure """ # first , find a free number for the table tables_num = [] _all_new_lines = [] with open('/etc/iproute2/rt_tables') as f: for line in f.readlines(): _all_new_lines.append(line) if len(line.strip()) > 0 and not line.startswith('#'): # trust the format of that file tables_num.append(int(line.split()[0])) _new_table_num_to_use = -1 for n in xrange(255): if n not in tables_num: _new_table_num_to_use = n break _logger.debug('new table index : %d' % _new_table_num_to_use) _all_new_lines.append('%d\t%s\n' % (_new_table_num_to_use, table_name)) if sudo_utils.copy_file('/etc/iproute2/rt_tables', '/etc/iproute2/rt_tables.bck') != 0: _logger.debug( 'cannot backup file [%s] to %s' % ('/etc/iproute2/rt_tables', '/etc/iproute2/rt_tables.bck')) return False if sudo_utils.write_to_file('/etc/iproute2/rt_tables', ''.join(_all_new_lines)) != 0: _logger.debug('cannot write new content to file [%s]' '/etc/iproute2/rt_tables') sudo_utils.copy_file('/etc/iproute2/rt_tables.bck', '/etc/iproute2/rt_tables') return False else: sudo_utils.delete_file('/etc/iproute2/rt_tables.bck') return True
def delete_route_table(table_name): """ Deletes a routing table by name remove a entry in /etc/iproute2/rt_tables Parameters ---------- table_name : str name of the new table Returns ------- bool True for success, False for failure """ _all_new_lines = [] with open('/etc/iproute2/rt_tables') as f: _all_lines = f.readlines() for line in _all_lines: # format is '<index>\t<table name>' _s_l = line.split() if len(_s_l) > 1 and _s_l[1] == table_name: # foudn the table name , skip this line continue _all_new_lines.append(line) if sudo_utils.copy_file('/etc/iproute2/rt_tables', '/etc/iproute2/rt_tables.bck') != 0: _logger.debug( 'cannot backup file [%s] to %s' % ('/etc/iproute2/rt_tables', '/etc/iproute2/rt_tables.bck')) return False if sudo_utils.write_to_file('/etc/iproute2/rt_tables', ''.join(_all_new_lines)) != 0: _logger.debug('cannot write new content to file [%s]' '/etc/iproute2/rt_tables') sudo_utils.copy_file('/etc/iproute2/rt_tables.bck', '/etc/iproute2/rt_tables') return False else: sudo_utils.delete_file('/etc/iproute2/rt_tables.bck') return True
def remove(self): _logger.debug('removing file %s' % self.path) return sudo_utils.delete_file(self.path)
def remove(self): if sudo_utils.call([SYSTEMCTL_CMD, 'disable', '--now', self.name]): raise StandardError('stop of systend unit has failed') sudo_utils.delete_file('/etc/systemd/system/%s.service' % self.name)