示例#1
0
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 test_bad_regex(self):
     """ Test egrep with invalid regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     try:
         test_file.egrep('*rd')
     except Exception as error:
         self.assertEqual('nothing to repeat', str(error))
 def test_replace_regex(self):
     """ Test substitute lines using a valid regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     old = test_file.egrep('^[ ]+#.*')
     new = '###'
     self.assertTrue(test_file.replace(old, new))
     self.assertFalse(test_file.egrep('^[ ]+#.*'))
 def test_egrep_word_list(self):
     """ Test egrep with valid choice regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     result = test_file.egrep('(host|bird)')
     self.assertTrue(result)
     self.assertIsInstance(result, list)
 def test_egrep_string_start(self):
     """ Test egrep with valid regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     result = test_file.egrep('^10.*')
     self.assertTrue(len(result) == 5)
     self.assertIsInstance(result, list)
 def test_egrep_word(self):
     """ Test egrep with a word. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     result = test_file.egrep('bird')
     self.assertTrue(result)
     self.assertIsInstance(result, list)
 def test_good_regex(self):
     """ Test egrep with valid wildcard regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     result = test_file.egrep('.*rd')
     self.assertTrue(result)
     self.assertIsInstance(result, list)
 def test_egrep_no_matches(self):
     """ Test egrep with valid regex but pattern not in file. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     result = test_file.egrep('^this is not present in file.*')
     self.assertTrue(result is False)
     self.assertIsInstance(result, bool)
 def test_egrep_char_list(self):
     """ Test egrep with valid character selector regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     subject = 'h[o0]stname'
     result = test_file.egrep(subject)
     self.assertTrue(result)
     self.assertIsInstance(result, list)
 def test_grep_matches(self):
     """ Test grep substring present in file, multiple match. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     subject = 'www01'
     result = test_file.egrep(subject)
     self.assertTrue(result)
     self.assertTrue(result == ['10.2.5.2    www01   www01.example.tld', '#172.8.8.8    www01   www01.example.tld'])
 def test_grep_match(self):
     """ Test grep substring present in file, 1 match. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     subject = 'localhost'
     result = test_file.egrep(subject)
     self.assertTrue(result)
     self.assertTrue(result == ['127.0.0.1 localhost.localdomain localhost'])
示例#12
0
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
示例#13
0
 def test_replace_regex(self):
     test_file = FileAsObj(TestFile, verbose=True)
     old = test_file.egrep('^[ ]+#.*')
     new = '###'
     self.assertTrue(test_file.replace(old, new))
     self.assertFalse(test_file.egrep('^[ ]+#.*'))
示例#14
0
 def test_egrep_string_end(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('tld$'))
示例#15
0
 def test_egrep_string_start(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('^10.*'))
示例#16
0
 def test_egrep_char_range(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('[a-z]ird'))
示例#17
0
 def test_egrep_word_list(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('(host|bird)'))
示例#18
0
 def test_bad_regex(self):
     test_file = FileAsObj(TestFile, verbose=True)
     try:
         test_file.egrep('*rd')
     except Exception as error:
         self.assertEqual('nothing to repeat', str(error))
示例#19
0
 def test_good_regex(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('.*rd'))
示例#20
0
except Exception as msg:
    print(msg)
    sys.exit(10)

#
# If your file does not yet exist...
new_file = FileAsObj()
new_file.filename = './new_file.txt'
new_file.add('new data')
new_file.save()  # This will create the file on disk and put your data into it.
#


#
# Find mail servers in a hosts file that have IPs starting with 172
my_file.egrep('^172.*mail[0-9]')


#
# Check for a line in file.
#    if 'This entire line' in my_file:
#        return True
#
# or simply:
#    return my_file.check('This entire line')
#
#

#
# Three methods to append a given line to the file, all work the same
my_file.add('foo')
示例#21
0
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)


x = '#comment'
print('subtract {0}'.format(x))
print(test_file - x)
print('---')


x = 'w.*rd'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

x = 'bird'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

try:
    # Just using * is invalid,
    x = '*rd'
    print('Find {0}'.format(x))
    print(test_file.egrep(x))
except Exception as msg:
    print(type(msg))
    print(msg)
示例#22
0
def example_search_file_with_regex():
    """ Find mail servers in a hosts file that have IPs starting with 172. """
    my_file = FileAsObj('/etc/hosts')
    result = my_file.egrep('^172.*mail[0-9]')
    print(result)
示例#23
0
test_file + 'using __add__ three times, force unique'
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)

x = '#comment'
print('subtract {0}'.format(x))
print(test_file - x)
print('---')

x = 'w.*rd'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

x = 'bird'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

try:
    # Just using * is invalid,
    x = '*rd'
    print('Find {0}'.format(x))
    print(test_file.egrep(x))
except Exception as msg:
    print(type(msg))
    print(msg)
 def test_grep_no_matches(self):
     """ Test grep substring not present in file. """
     test_file = FileAsObj()
     test_file.contents = TESTCONTENTS.split('\n')
     result = test_file.egrep('substring_not_found')
     self.assertFalse(result)
示例#25
0
                        verbose=True)
except Exception as msg:
    print(msg)
    sys.exit(10)

#
# If your file does not yet exist...
new_file = FileAsObj()
new_file.filename = './new_file.txt'
new_file.add('new data')
new_file.save()  # This will create the file on disk and put your data into it.
#

#
# Find mail servers in a hosts file that have IPs starting with 172
my_file.egrep('^172.*mail[0-9]')

#
# Check for a line in file.
#    if 'This entire line' in my_file:
#        return True
#
# or simply:
#    return my_file.check('This entire line')
#
#

#
# Three methods to append a given line to the file, all work the same
my_file.add('foo')
my_file.append('foo')
示例#26
0
 def test_egrep_char_list(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('h[o0]stname'))
 def test_egrep_char_range(self):
     """ Test egrep with valid range regex. """
     test_file = FileAsObj()
     test_file.add(TESTCONTENTS)
     self.assertTrue(test_file.egrep('[a-z]ird'))
示例#28
0
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
示例#29
0
 def test_egrep_word(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.egrep('bird'))
示例#30
0
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()