def test_rm_failure(self): """ Test wrong param type in rm() """ test_file = FileAsObj() with self.assertRaises(TypeError): test_file.rm(1) with self.assertRaises(TypeError): test_file.rm(True)
def example_all(): """ Use a bunch of methods on a file. """ my_file = FileAsObj() my_file.filename = '/tmp/example_file.txt' my_file.add('# First change!') my_file.save() my_file = FileAsObj('/tmp/example_file.txt') my_file.unique = True my_file.sorted = True my_file.add('1') my_file.add('1') my_file.add('2') my_file.add('20 foo') my_file.add('200 bar') my_file.add('# Comment') my_file.unique = False my_file.add('# Comment') my_file.add('# Comment') my_file.unique = True my_file.rm(my_file.egrep('^#.*')) my_file.rm(my_file.grep('foo')) my_file.replace(my_file.egrep('^2'), 'This line was replaced.') print(my_file) print(my_file.log)
def client_delete(client): """ Files to update: (these files must exist) pxeconf hostsfile allowfile Files to remove: (ok if already do not exist) tftp_pxe hostname_ks client_sh """ # client = obj.old hostname = client.name mac_addr = client.mac client_ip = client.ip dashmac = '-'.join(mac_addr.split(':')) dashmac = '01-{0}'.format(dashmac) # file = FileAsObj(etc_pxe_clients_conf, verbose=True) # /opt/kickstart/etc/pxe_clients.conf file.rm(file.grep(mac_addr)) pattern = ' {0}.{1} '.format(hostname, ks_domainname) file.rm(file.grep(pattern)) if not file.virgin: file.write() # file = FileAsObj(etc_hosts, verbose=True) # /etc/hosts pattern = '{0} # Kickstart Client '.format(hostname) file.rm(file.grep(pattern)) pattern = '^{0} '.format(client_ip) file.rm(file.egrep(pattern)) if not file.virgin: file.write() # file = FileAsObj(etc_hosts_allow, verbose=True) # /etc/hosts.allow pattern = 'ALL: ALL@{0} : ALLOW'.format(client_ip) file.rm(pattern) if not file.virgin: file.write() # for this in [os.path.join(TFTP, dashmac), os.path.join(KS_CONF_DIR, '{0}.ks'.format(hostname)), os.path.join(CLIENT_DIR, '{0}.sh'.format(hostname)), ]: if os.path.isfile(this): newname = '{0}_{1}'.format(os.path.basename(this), int(time.time())) target = os.path.join(BK_DIR, newname) shutil.move(this, target) # return
def vlan_delete(obj): """ Remove vlan from dhcpd.conf and delete vlan_XX.conf file OK if vlan not found. The only real errors are if we are unable to edit dhcpd.conf or if there a clients in the VLAN. No return values; just raise an exception if there's a problem. """ if obj.client.count() is not 0: raise ValueError('Unable to delete, there are clients in this vlan!') file_name = 'vlan_{0}.conf'.format(obj.name) # dhcpd_conf = FileAsObj(os.path.join(KSROOT, 'dhcpd.conf')) # /opt/kickstart/etc/dhcpd.conf # dhcpd_conf.rm(dhcpd_conf.grep(file_name)) if not dhcpd_conf.virgin: dhcpd_conf.write() # filename = os.path.join(KSROOT, file_name) # /opt/kickstart/etc/vlan_{name}.conf try: os.remove(filename) except: pass return
def test_remove_multi(self): test_file = FileAsObj(TestFile, verbose=True) self.assertTrue(test_file.rm(test_file.grep('#')))
print('Find {0}'.format(x)) print(test_file.egrep(x)) print('---') x = '.*mail[0-9]' print('Find {0}'.format(x)) print(test_file.egrep(x)) print('---') x = 'tld$' print('Find {0}'.format(x)) print(test_file.egrep(x)) print('---') x = '^10.*' print('Remove {0} from {1}, RESULT={2}'.format(x, test_file.egrep(x), test_file.rm(test_file.egrep(x)))) print('---') x = ' h0st.*' print('Remove {0} from {1}, RESULT={2}'.format( x, test_file.egrep(x), test_file.rm(test_file.egrep(x))) ) print('---') x = '#' print('Remove whole line "{0}" from {1}, RESULT={2}'.format( x, test_file.grep(x), test_file.rm(x))
print(my_file + 'foo') # # Add line even if it exists in the file already. my_file.add('foo', unique=False) # # Print number of lines in the file (as it exists in memory) print(len(my_file)) # # Remove all lines that are "foo bar" my_file.rm('foo bar') # or print(my_file - 'foo bar') # # Get all lines that contain a # result = my_file.grep('#') # # Remove all lines that contain the word "foo" # NOTE: this is dangerous and rarely needed. my_file.rm(my_file.grep('foo'))
print(test_file.egrep(x)) print('---') x = '.*mail[0-9]' print('Find {0}'.format(x)) print(test_file.egrep(x)) print('---') x = 'tld$' print('Find {0}'.format(x)) print(test_file.egrep(x)) print('---') x = '^10.*' print('Remove {0} from {1}, RESULT={2}'.format( x, test_file.egrep(x), test_file.rm(test_file.egrep(x)))) print('---') x = ' h0st.*' print('Remove {0} from {1}, RESULT={2}'.format( x, test_file.egrep(x), test_file.rm(test_file.egrep(x)))) print('---') x = '#' print('Remove whole line "{0}" from {1}, RESULT={2}'.format( x, test_file.grep(x), test_file.rm(x))) print(test_file.grep(x)) print('---') old = '172.19.18.17 freebird.example.com' new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh'
def test_remove_multi(self): """ Test deleting multiple lines. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertTrue(test_file.rm(test_file.grep('#')))
def client_create(form, old=False): """ 1. Get IP if Null 2. Figure out VLAN from IP 2a. make sure IP is valid for VLAN 3. unquie add to etc/hosts 4. unquie add to etc/hosts.allow 5. unquie add client in pxe_clients.conf 6. create ks.d/hostname.ks file (kickstart) 7. create clients.d/hostname.sh (shell variables) 8. create tftpboot/01-mac-address on first failure generate message and return false. """ hostname = form.cleaned_data['name'].lower() mac_addr = form.cleaned_data['mac'].lower() build_type = form.instance.get_build_type_display().lower() os_release = form.cleaned_data['os_release'] # # We use ext4 by default, if it's a EL5.x build we use ext3. fstype = 'ext4' if form.cleaned_data['os_release'] == 'el5': fstype = 'ext3' # # if not form.cleaned_data['ip']: # # No client_IP given, get from DNS (or fail) form.instance.ip = gethostbyname(hostname) if Client.objects.filter(ip=form.instance.ip).count() is not 0: raise ValueError('DNS returned {0} but that IP already in use.'.format(form.instance.ip)) # if not form.cleaned_data['vlan']: for thisv in VLAN.objects.all(): network_cidr = '{0}/{1}'.format(thisv.network, thisv.get_cidr_display()) getvlan = ipcalc.Network(network_cidr) if form.instance.ip in getvlan: form.instance.vlan = thisv if not form.instance.vlan: raise ValueError('IP {0} not valid for any known vlans!'.format(form.instance.ip)) # testing_net = ipcalc.Network('{0}/{1}'.format(form.instance.vlan.network, form.instance.vlan.get_cidr_display())) if form.instance.ip not in testing_net: raise ValueError('IP {0} not valid for VLAN {1}.'.format(form.instance.ip, form.instance.vlan)) # # Validate that client IP is not already a server IP, network or gateway. if VLAN.objects.filter(server_ip=form.instance.ip).count() is not 0 or \ VLAN.objects.filter(gateway=form.instance.ip).count() is not 0 or \ VLAN.objects.filter(network=form.instance.ip).count() is not 0: raise ValueError('Client IP "{0}" is in use by a VLAN object.'.format(form.instance.ip)) # # etc/hosts hostsfile = FileAsObj(etc_hosts, verbose=True) if hostsfile.egrep('^[0-9].*[ \\t]{0}'.format(hostname)): raise ValueError('Failed to update {0}, client "{1}" already exists!'.format(etc_hosts, hostname)) if hostsfile.egrep('^{0}[ \\t]'.format(form.instance.ip)): raise ValueError('Failed to update {0}, IP "{1}" already exists!'.format(etc_hosts, form.instance.ip)) toadd = '{CLIENT_IP} {HOSTNAME}.{DOMAINNAME} {HOSTNAME} # Kickstart Client added {NOW}'.format( CLIENT_IP=form.instance.ip, HOSTNAME=hostname, DOMAINNAME=ks_domainname, NOW=time.strftime('%Y.%m.%d', time.localtime()), ) hostsfile.add(toadd) # allowfile = FileAsObj(etc_hosts_allow, verbose=True) # /etc/hosts.allow pattern = 'ALL: ALL@{0} : ALLOW'.format(form.instance.ip) if pattern not in allowfile.contents: allowfile.add(pattern) # pxeconf = FileAsObj(etc_pxe_clients_conf) # /opt/kickstart/etc/pxe_clients.conf if pxeconf.grep(' {0}.{1} '.format(hostname, ks_domainname)): raise ValueError('{0} already present in {1}'.format(hostname, etc_pxe_clients_conf)) if pxeconf.grep(mac_addr): raise ValueError('Failed to update {0}, MAC "{1}" already present!'.format(etc_pxe_clients_conf, mac_addr)) toadd = 'host {HOSTNAME}.{DOMAINNAME} {{ hardware ethernet {MAC} ; fixed-address {CLIENT_IP} ;}}'.format( HOSTNAME=hostname, DOMAINNAME=ks_domainname, MAC=mac_addr, CLIENT_IP=form.instance.ip, ) pxeconf.add(toadd) pxeconf.add('}') # # tftpboot/pxe.default/01-mac-address.lower().replace(":","-") dashmac = '-'.join(mac_addr.split(':')) dashmac = '01-{0}'.format(dashmac) filename = os.path.join(TFTP, dashmac) if os.path.isfile(filename): raise ValueError('Failed to add client. The file "{0}" already exists!'.format(filename)) tftp_pxe = FileAsObj() tftp_pxe.filename = filename tftp_pxe.contents = base_tftp.format( KS_CONF_DIR=KS_CONF_DIR, OS_RELEASE=os_release, HOSTNAME=hostname, SERVER_IP=form.instance.vlan.server_ip, ).split('\n') # # {ksroot}/etc/ks.d/{hostname}.ks - kickstart config file filename = os.path.join(KS_CONF_DIR, '{0}.ks'.format(hostname)) if os.path.isfile(filename): raise ValueError('Failed to add client. The file "{0}" already exists!'.format(filename)) if old is not False: kscfg = old.kickstart_cfg kscfg = kscfg.replace(old.ip, form.instance.ip) kscfg = kscfg.replace(old.os_release, os_release) kscfg = kscfg.replace(old.vlan.cidr, form.instance.vlan.cidr) kscfg = kscfg.replace(old.vlan.gateway, form.instance.vlan.gateway) kscfg = kscfg.replace(old.name, hostname) if old.os_release == 'el5': kscfg = kscfg.replace('ext3', fstype) if old.os_release == 'el6': kscfg = kscfg.replace('ext4', fstype) kscfg = kscfg.replace(old.vlan.server_ip, form.instance.vlan.server_ip,) else: kscfg = base_ks.format( CLIENT_IP=form.instance.ip, OS_RELEASE=os_release, QUAD_MASK=form.instance.vlan.cidr, GATEWAY=form.instance.vlan.gateway, HOSTNAME=hostname, EXT34=fstype, SERVER_IP=form.instance.vlan.server_ip, ROOT_PW=ks_root_password, NAME_SERVERS=ks_nameservers, ) form.instance.kickstart_cfg = kscfg hostname_ks = FileAsObj() hostname_ks.filename = filename hostname_ks.contents = kscfg.split('\n') if form.cleaned_data['os_release'] == 'el5': # # EntLinux 5(.5) cannot understand multiple %end statements, remove all then re-add the last one. hostname_ks.rm('%end') hostname_ks.add('%end') # # {ksroot}/etc/clients.d/{hostname}.sh - shell variables for post build scripts to use filename = os.path.join(CLIENT_DIR, '{0}.sh'.format(hostname)) if os.path.isfile(filename): raise ValueError('Failed to add client. The file "{0}" already exists!'.format(filename)) client_sh = FileAsObj() client_sh.filename = filename client_sh.contents = base_sh.format( HOSTNAME=hostname, MAC=mac_addr, CLIENT_IP=form.instance.ip, QUAD_MASK=form.instance.vlan.cidr, GATEWAY=form.instance.vlan.gateway, BUILD_TYPE=build_type, OS_RELEASE=os_release, SERVER_IP=form.instance.vlan.server_ip, ).split('\n') # # If you made it this far everything went OK. Now write all the files! pxeconf.write() tftp_pxe.write() hostsfile.write() allowfile.write() hostname_ks.write() client_sh.write() return form
# Three methods to append a given line to the file, all work the same my_file.add('foo') my_file.append('foo') print(my_file + 'foo') # # Add line even if it exists in the file already. my_file.add('foo', unique=False) # # Print number of lines in the file (as it exists in memory) print(len(my_file)) # # Remove all lines that are "foo bar" my_file.rm('foo bar') # or print(my_file - 'foo bar') # # Get all lines that contain a # result = my_file.grep('#') # # Remove all lines that contain the word "foo" # NOTE: this is dangerous and rarely needed. my_file.rm(my_file.grep('foo')) # # Check for line in file, if exists remove it if 'foo bar' in my_file:
def example_remove_lines_matching_string(): """ Remove all lines that ARE '# T0DO: remove this line.' (This matches an entire line.) """ my_file = FileAsObj('/tmp/example_file.txt') my_file.rm('# T0DO: remove this line.') my_file.save()
def example_remove_lines_matching_substring(): """ Remove all lines that CONTAIN 'bad string' """ my_file = FileAsObj('/tmp/example_file.txt') my_file.rm(my_file.grep('bad string')) my_file.save()
def example_write_file_to_disk_if_changed(): """ Try to remove all comments from a file, and save it if changes were made. """ my_file = FileAsObj('/tmp/example_file.txt') my_file.rm(my_file.egrep('^#')) if my_file.changed: my_file.save()
def example_remove_lines_matching_string_with_print(): """ Remove all lines that ARE "# T0DO: remove this line." and print(True|False) if my_file was changed. """ my_file = FileAsObj('/tmp/example_file.txt') print(my_file.rm('# T0DO: remove this line.')) my_file.save()