zoukankan      html  css  js  c++  java
  • 生成centos7 安装脚本

    [root@us-1-217 install]# cat gen7.py 
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import os, crypt
    
    base_path = '/opt/opmgmt/install' 
    pxe_path = os.path.join(base_path, 'pxelinux.cfg')
    kickstart_path = os.path.join(base_path, 'kickstart')
    
    sitename = 'us.install.suntv.tv'
    installhost = '10.150.1.217'
    yumhost = 'us.yum.suntv.tv'
    hosts = 'hosts.txt'
    os = 'centos7'
    
    password = 'password'
    rootpw = crypt.crypt(password, '$6$MySalt')
    
    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_
    '''
    
        # ssh
        ssh = '''sed -i 's/#Port 22/Port 29922/g' /etc/ssh/sshd_config
    sed -i 's/GSSAPIAuthentication yes/#GSSAPIAuthentication yes/g' /etc/ssh/sshd_config
    sed -i 's/#GSSAPIAuthentication no/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
    '''
    
        # tcp/ip
        tcp_ip = '''cat >> /etc/sysctl.conf << _EOF_
    
    fs.file-max = 100000
    #net.ipv4.tcp_syncookies = 1
    #net.ipv4.tcp_tw_reuse = 1
    #net.ipv4.tcp_tw_recycle = 1
    #net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_keepalive_time = 1200
    net.ipv4.ip_local_port_range = 1024 65000
    #net.ipv4.tcp_max_syn_backlog = 8192
    #net.ipv4.tcp_max_tw_buckets = 5000
    _EOF_
    '''
    
        # limit
        limit ='''cat >> /etc/security/limits.conf << _EOF_
    *    soft    nofile  100000
    *    hard    nofile  100000
    _EOF_
    '''
    
        # hosts
        hosts ='''cat >> /etc/hosts << _EOF_
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    %s %s
    _EOF_
    ''' % (installhost, yumhost)
    
        # yum
        yum ='''
    rm -rf /etc/yum.repos.d/*
    curl http://%s/centos6.repo -o /etc/yum.repos.d/centos.repo
    curl http://%s/epel.repo -o /etc/yum.repos.d/epel.repo
    ''' % (yumhost, yumhost)
    
        kickstart = '''text
    keyboard us
    timezone Asia/Shanghai
    lang en_US.UTF-8
    skipx
    auth --enableshadow --passalgo=sha512
    rootpw --iscrypted %s
    
    #zerombr
    bootloader --boot-drive=sda --location=mbr
    ignoredisk --only-use=sda
    clearpart --drives=sda --all
    #part swap --fstype='swap' --ondisk=sda --size=8000
    part biosboot --fstype='biosboot' --size=1
    part / --fstype='xfs' --ondisk=sda --size=50000
    part /opt --fstype='xfs' --ondisk=sda --size=1 --grow
    
    network --bootproto=dhcp --device=%s --activate
    
    install
    url --url='http://%s/%s'
    logging level=info
    
    firewall --disabled
    selinux --disabled
    firstboot --disabled
    services --enabled=network,rc-local --disabled=NetworkManager,postfix
    reboot
    
    %%packages
    @core
    %%end
    
    %%pre
    /usr/sbin/parted -s /dev/sda mklabel gpt
    %%end
    
    %%post
    %s
    %s
    %s
    %s
    %s
    %s
    %s
    %s
    %s
    %%end
    ''' % (rootpw, mac, sitename, os, private_interface, public_interface, network, dns, ssh, tcp_ip, limit, hosts, yum)
    
        filename = kickstart_path + '/' + mac
        with open(filename, 'w') as f:
            f.write(kickstart)
            print 'generate kickstart file: %s ' % mac 
    
    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.lower(), sitename, pxe_path)
            generate_kickstart_file(os, sitename, rootpw, kickstart_path, dev, prefix, mac.lower(), private_ip, private_mask, public_ip, public_mask, default_gw)
    
    
  • 相关阅读:
    Struts2
    Struts2
    学习python的第九天
    学习python的第八天--作业
    学习python的第七天--作业
    学习python第六天 --作业
    学习python的第六天---1(理论)
    学习python第五天
    学习python第四天
    学习python第三天
  • 原文地址:https://www.cnblogs.com/liujitao79/p/8441061.html
Copyright © 2011-2022 走看看