def test_subtract(self): """ Test __sub__ method. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertFalse(test_file.changed) self.assertTrue(test_file - '#comment') self.assertTrue(test_file.changed)
def test_subtract_fail(self): """ Test __sub__ method fails. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertFalse(test_file.changed) self.assertFalse(test_file - 'this string does not exist in file!') self.assertFalse(test_file.changed)
def test_save_with_changes(self): """ Instance an empty test file, overwrite its contents and save to disk. """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.contents = TESTCONTENTS.split('\n') self.assertEqual(TESTCONTENTS, str(test_file)) self.assertTrue(test_file.save())
def vlan_create(form): """ Add a VLAN to Kickstart 1. Validate IP information 2. Set gateway to lowest host 3. Set kickstart server IP to highest host minus 2. 4. Add 'include vlan_XX.conf' line to dhcpd.conf file 5. Create vlan_XX.conf file On error raise an exception, let the view handle it. :param: form - A cleaned form object from view.form_valid() :return: form """ netinfo = ipcalc.Network('{0}/{1}'.format(form.instance.network, form.instance.cidr), version=4) # # Set model data to actual network, it's often entered incorrectly. form.instance.network = netinfo.network() # # Gateway and ServerIP are auto-selected during VLAN.Create, but specified during VLAN.Update. if 'gateway' not in form.cleaned_data: form.instance.gateway = netinfo.host_first() else: # # If user entered no gateway during VLAN.Update then set to lowest host. if not form.cleaned_data['gateway']: form.instance.gateway = netinfo.host_first() elif form.cleaned_data['gateway'] not in netinfo: raise ValueError('Gateway is outside this VLAN!') # if 'server_ip' not in form.cleaned_data or not form.cleaned_data['server_ip']: # # No server IP given, default to form.instance.server_ip = ipcalc.IP(int(netinfo.host_last()) - 2, version=4) else: if form.cleaned_data['server_ip'] not in netinfo: raise ValueError('Kickstart Server IP is outside this VLAN!') # name = form.cleaned_data['name'] dhcpd_conf = FileAsObj(os.path.join(KSROOT, 'dhcpd.conf')) # /opt/kickstart/etc/dhcpd.conf dhcpd_conf.add('include "{0}/vlan_{1}.conf";'.format(KSROOT, name)) # vlan_conf = FileAsObj() vlan_conf.filename = os.path.join(KSROOT, 'vlan_{0}.conf'.format(name)) # /opt/kickstart/etc/vlan_{name}.conf vlan_conf.contents = base_vlan.format( NETWORK=form.instance.network, CIDR=form.instance.cidr, GATEWAY=form.instance.gateway, SERVER_IP=form.instance.server_ip, ).split('\n') # # All is OK, save changes and return form. dhcpd_conf.write() vlan_conf.write() return form
def test_unique_failure_during_read(self): """ Test wrong attribute type of self.unique during read() """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.contents = BLANKFILE.split('\n') self.assertTrue(test_file.save()) test_file = FileAsObj() test_file.unique = 'this is invalid attr type' with self.assertRaises(AttributeError): test_file.read(TESTFILE)
def test_blank_file_without_unique(self): """ Testing not-unique on empty lines during .read() """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.contents = BLANKFILE.split('\n') self.assertTrue(test_file.save()) test_file = FileAsObj() test_file.unique = False test_file.read(TESTFILE) self.assertTrue(test_file.contents == ['', '', '', ''])
def update_kickstart_file(form): """ Update The kickstart config file for a client: """ filename = os.path.join(KS_CONF_DIR, '{0}.ks'.format(form.instance.name)) content = form.cleaned_data['kickstart_cfg'].replace('\r', '') # file = FileAsObj(filename, verbose=True) # /opt/kickstart/etc/ks.d/hostname.ks file.contents = [content] file.write() return form
def test_replace_list(self): """ Test substitute lines using a list of strings. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertFalse(test_file.changed) old = ['#', '# ', '#1'] new = '###' self.assertTrue(test_file.replace(old, new)) self.assertTrue(test_file.changed) for this in old: self.assertFalse(test_file.check(this)) self.assertEqual(test_file.check(new), new)
def activate(self): """ Update KSROOT/eth1.sh with this VLAN and set self.active to True. """ file = FileAsObj(os.path.join(KSROOT, 'eth1.sh')) file.contents = [] file.add('/sbin/ifconfig eth1 inet {IP} netmask {MASK} up'.format( IP=self.server_ip, MASK=self.cidr, )) file.add('/sbin/service dhcpd restart') file.write() self.active = True self.save() return
def test_004_save_with_changes(self): test_file = FileAsObj() test_file.filename = TestFile # Create object even if file does not exist test_file.contents = TestContains.split('\n') # Override file contents. self.assertEqual(TestContains, str(test_file)) # Confirm strings match self.assertTrue(test_file.save())
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)
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 test_check_line_not_present(self): """ Test check() method with line that is not present in file. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertFalse(test_file.check('This line does not exist in the file.'))
def test_check_blank_line(self): """ Test check() method with empty line that is present in file. """ test_file = FileAsObj() test_file.contents = BLANKFILE.split('\n') self.assertEqual(test_file.check(''), '')
def test_check_present(self): """ Test check() method with line that is present in file. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertTrue(test_file.check('# This is a test hosts file'))
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