简化 Openstack Havana 版 compute 节点部署程序

#!/usr/bin/env python
#-*-coding:UTF-8-*-
"""
@Item   :  Install Openstack Compute
@Author :  william
@Group  :  Network System Group
@Date   :  2014-03-20
@Funtion:
    Function: Depoly Openstack Compute  ...
"""
import os,sys,time
import traceback,shutil
import commands
import re
def LOG(info):
    logfile = '/tmp/openstack_compute_install.log'
    files = open(logfile,'a')
    try:
        files.write('%s : %s'%(time.ctime(),info))
    except IOError:
        files.close()
    files.close()
class DepolyCompute(object):
    def __init__(self):
        print "Please write in the script master IP public IP and local management ... \n"
        self.masterIPaddr = '192.168.220.1'
        self.localIPaddr  = '192.168.220.1'
        self.URL = 'http://%s/source/'%self.masterIPaddr
        self.nova_path  = '/etc/nova/'
    def install_packstack(self):
        yum_path = '/etc/yum.repos.d/'
        # epel 源目前不在更新OpenStack Havana版本,在下载源的时候默认已经移植了目录结构 ,需要重新安装RDO,在修改源路径,才能正常安装OpenStack 等
        comds = "https://repos.fedorapeople.org/repos/openstack/EOL/openstack-havana/rdo-release-havana-9.noarch.rpm"
        commands.getstatusoutput(cmd)
        try:
            cmd = "https://repos.fedorapeople.org/repos/openstack/EOL/openstack-havana/rdooo-release-havana-9.noarch.rpm"
            x,y = commands.getstatusoutput(cmd)
            if x != 0:
                fp = open('%s/rdo-release.repo'%yum_path,'w')
                fp.write("""[openstack-havana] \n""")
                fp.write("""name=OpenStack Havana Repository \n""")
                fp.write("""baseurl=https://repos.fedorapeople.org/repos/openstack/EOL/openstack-havana/epel-6/ \n""")
                fp.write("""enabled=1 \n""")
                fp.write("""skip_if_unavailable=0 \n""")
                fp.write("""gpgcheck=1 \n""")
                fp.write("""gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-RDO-Havana \n""")
                fp.write("""priority=98 \n""")
                fp.close()
            cmd = "rpm -Uvh http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm"
            x,y = commands.getstatusoutput(cmd)
            if x != 0:
                fp = open('%s/epel.repo'%yum_path,'w')
                fp.write("""[epel] \n""")
                fp.write("""name=Extra Packages for Enterprise Linux 6 - $basearch \n""")
                fp.write("""#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch \n""")
                fp.write("""mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch \n""")
                fp.write("""failovermethod=priority \n""")
                fp.write("""enabled=1 \n""")
                fp.write("""gpgcheck=1 \n""")
                fp.write("""gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 \n""")
                fp.write(""" \n""")
                fp.write("""[epel-debuginfo] \n""")
                fp.write("""name=Extra Packages for Enterprise Linux 6 - $basearch - Debug \n""")
                fp.write("""#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug \n""")
                fp.write("""mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch \n""")
                fp.write("""failovermethod=priority \n""")
                fp.write("""enabled=0 \n""")
                fp.write("""gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 \n""")
                fp.write("""gpgcheck=1 \n""")
                fp.write(""" \n""")
                fp.write("""[epel-source] \n""")
                fp.write("""name=Extra Packages for Enterprise Linux 6 - $basearch - Source \n""")
                fp.write("""#baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS \n""")
                fp.write("""mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch \n""")
                fp.write("""failovermethod=priority \n""")
                fp.write("""enabled=0 \n""")
                fp.write("""gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 \n""")
                fp.write("""gpgcheck=1 \n""")
                fp.close()
        except:
            LOG(traceback.format_exc())
    def install_kvm_compute(self):
        try:
            os.system(""" yum -y install kvm libvirt qemu virt-*""")
            os.system(""" yum -y install lrzsz gcc sysstat net-snmp-* """)
            os.system(""" yum -y install openstack-nova-compute openstack-nova-network libguestfs-winsupport""")
            #ip = self.localIPaddr.split('.')
            #hostname = "athCompute.%s.%s.autohome.com.cn"%(ip[2],ip[3])
            #os.system("hostname %s"% hostname)
            #os.system(""" sed -i 's/^HOSTNAME=.*/HOSTNAME=%s/' /etc/sysconfig/network """%hostname)
            os.system("easy_install psutil")
            os.system("easy_install ipdb")
        except:
            LOG(traceback.format_exc())

    def install_compute_source(self):
        try:
            cmd = " wget %s/athCloudOpenStack.tgz  -O /opt/athCloudOpenStack.tgz"%self.URL
            x,y = commands.getstatusoutput(cmd)
            if x == 0:
                os.chdir('/opt/')
                os.system(""" tar -xzvf athCloudOpenStack.tgz -C /""")
                os.chdir(self.nova_path)
                if os.path.exists('nova.conf'):
                    shutil.move('nova.conf','nova.conf.default')
                if os.path.exists('nova.conf.example'):
                    shutil.move('nova.conf.example','nova.conf')
                    self._nova_conf()
                self._start_nova_compute()
            else:
                LOG("wget athCloudOpenStack.tgz false")
                sys.exit()
        except:
            LOG(traceback.format_exc())
    def _start_nova_compute(self):
        try:
            servers = ['openstack-nova-compute','openstack-nova-network','openstack-nova-extend','libvirtd']
            for i in servers:
                os.system(""" /sbin/service %s restart """ %i)
                os.system(""" /sbin/chkconfig %s on """ %i)
        except:
            LOG(traceback.format_exc())
    def _nova_config(self):
        # Compute node nova.conf init
        try:
            fp = open('%s/nova.conf'%self.nova_path)
            f = fp.readlines()
            fp.close()
            for n, s in enumerate(f):
                if re.match('#?glance_api_servers.*',s):
                    f[n] = 'glance_api_servers=%s:9292\n' %self.masterIPaddr
                if re.match('#?metadata_host.*',s):
                    f[n] = 'metadata_host=%s  \n' %self.masterIPaddr
                if re.match('#?qpid_hostname.*',s):
                    f[n] = 'qpid_hostname=%s\n' %self.masterIPaddr
                if re.match('#?novncproxy_base_url.*',s):
                    f[n] = 'novncproxy_base_url=http://%s:6080/vnc_auto.html \n' %self.masterIPaddr
                if re.match('#?sql_connection.*',s):
                    str = s.split('@')[0] + '@%s/nova'%self.masterIPaddr
                    f[n] = '%s \n' %str
                if re.match('#?my_ip.*',s):
                    f[n] = 'my_ip=%s\n' %self.localIPaddr
                if re.match('#?vncserver_listen.*',s):
                    f[n] = 'vncserver_listen=%s\n' %self.localIPaddr
                if re.match('#?vncserver_proxyclient_address.*',s):
                    f[n] = 'vncserver_proxyclient_address=%s\n' %self.localIPaddr
            fp = open('%s/nova.conf'%self.nova_path,'w')
            fp.writelines(f)
            fp.close()
        except Exception,e:
            LOG(traceback.format_exc())
if __name__ == "__main__":
    sc = DepolyCompute()
    sc.install_packstack()
    sc.install_kvm_compute()
    sc.install_compute_source()


你可能感兴趣的:(python,OS,openstack,SYS,NetWork,compute)