em tm 00:00:00:00:00:12 10.180.1.12 255.255.255.0 173.45.34.25 255.255.255.225 173.45.34.1
em tm 00:00:00:00:00:21 10.180.1.21 255.255.255.0 10.180.110.21 255.255.255.0 10.180.1.11
eth tm 00:00:00:00:00:22 10.180.1.22 255.255.255.0 10.180.110.22 255.255.255.0 10.180.1.11
eth tm 00:00:00:00:00:23 10.180.1.23 255.255.255.0 10.180.110.23 255.255.255.0 10.180.1.11
eth tm 00:00:00:00:00:24 10.180.1.24 255.255.255.0 10.180.110.24 255.255.255.0 10.180.1.11
eth tm 00:00:00:00:00:25 10.180.1.25 255.255.255.0 10.180.110.25 255.255.255.0 10.180.1.11
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, crypt
def generate_pxe_file(os, mac, sitename, pxe_path):
pxe = '''default menu.c32
prompt 0
timeout 100
LABEL %s
MENU DEFAULT
MENU LABEL %s
KERNEL %s/vmlinuz
APPEND initrd=%s/initrd.img ks=http://%s/kickstart/%s ksdevice=link ramdisk_size=102400 console=ttyS1,115200
''' % (os, mac, os, os, sitename, mac)
filename = pxe_path + '/01-' + '-'.join(mac.split(':'))
with open(filename, 'w') as f:
f.write(pxe)
print 'generate pxe file: %s' % ('01-' + '-'.join(mac.split(':')))
def generate_kickstart_file(os, sitename, rootpw, kickstart_path, dev, prefix, mac, private_ip, private_mask, public_ip, public_mask, default_gw):
# interface
if dev == 'em':
private_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s1 << _EOF_
DEVICE=%s1
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, private_ip, private_mask)
if public_ip != '0' and public_mask !='0':
public_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s2 << _EOF_
DEVICE=%s2
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, public_ip, public_mask)
else:
public_interface = ''
if dev == 'eth':
private_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s0 << _EOF_
DEVICE=%s0
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, private_ip, private_mask)
if public_ip != '0' and public_mask !='0':
public_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s1 << _EOF_
DEVICE=%s1
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, public_ip, public_mask)
else:
public_interface = ''
# network
network = '''cat > /etc/sysconfig/network << _EOF_
NETWORKING=yes
HOSTNAME=%s-%s-%s
GATEWAY=%s
_EOF_
''' % (prefix, private_ip.split('.')[2], private_ip.split('.')[3], default_gw)
# dns
dns = '''cat > /etc/resolv.conf << _EOF_
nameserver 8.8.8.8
nameserver 8.8.4.4
_EOF_
'''
kickstart = '''text
keyboard us
timezone Asia/Shanghai
lang en_US.UTF-8
skipx
auth --enableshadow --passalgo=sha512
rootpw --iscrypted %s
zerombr
bootloader --location=mbr --driveorder=sda
part swap --fstype='swap' --ondisk=sda --size=8000
part / --fstype='ext4' --ondisk=sda --size=50000
part /opt --fstype='ext4' --ondisk=sda --size=1 --grow
network --bootproto=dhcp --device=%s --activate
install
url --url='http://%s/%s'
logging level=info
firewall --disabled
selinux --disabled
services --disabled=NetworkManager,ip6tables,iptables,postfix,cpuspeed
services --enabled=network
firstboot --disabled
reboot
%%packages
@base
@core
%%end
%%pre
clearpart --drives=sda --all
/usr/sbin/parted -s /dev/sda mklabel gpt
%%end
%%post
%s
%s
%s
%s
%%end
''' % (rootpw, mac, sitename, os, private_interface, public_interface, network, dns)
filename = kickstart_path + '/' + mac
with open(filename, 'w') as f:
f.write(kickstart)
print 'generate kickstart file: %s ' % mac
# main
base_path = '/opt/opmgmt/install'
pxe_path = os.path.join(base_path, 'pxelinux.cfg')
kickstart_path = os.path.join(base_path, 'kickstart')
sitename = 'install.localhost'
hosts = 'hosts.txt'
os = 'centos6'
password = 'password123'
rootpw = crypt.crypt(password, '$6$MySalt')
with open(hosts, 'r') as f:
for host in f:
dev, prefix, mac, private_ip, private_mask, public_ip, public_mask, default_gw = host.strip('
').split(' ')
generate_pxe_file(os, mac, sitename, pxe_path)
generate_kickstart_file(os, sitename, rootpw, kickstart_path, dev, prefix, mac, private_ip, private_mask, public_ip, public_mask, default_gw)