抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

在使用 KVM 与 VManagerPlatform 进行快速开通与交付方面,有一项精巧的技巧可以让您的工作流更加高效。通过运用 virt-copy-in 命令,您能够将预先生成的网卡配置文件融合至镜像中。此过程倚赖 libguestfs-tools-c 工具包,可克服因虚拟机无法生成 IP 地址而产生的自动化难题。我们还借助 Python 的 Fabric 模块与 Bashe脚本,代替了繁琐的 Ansible 自动化角色,使得 KVM 虚拟机的开通及其关联环境的规范化部署实现了高达 90% 的自动化水平。这一切不仅大幅减少了人工投入的时间,还显著降低了因人为操作而引发误操作的风险。

[toc]

一、先来看看需求及对应的设计图

  • 什么是Fabric
    Fabric是一个Python的库,提供了丰富的同SSH交互的接口,可以用来在本地或远程机器上自动化、流水化地执行Shell命令。
    非常适合用来做应用的远程部署及系统维护。简单易用,只需懂得基本的Shell命令。

  • 版本区分
    Fabric:官方Fabric,兼容 Python 2 & Python 3,但不兼容Fabric 1.x的fabfile;
    fabric2: 与Fabric相同,仅作为平滑迁移(使用Fabric包安装1.x 版本,使用Fabric2包安装2.x版本,来实现1.x和2.x的共存);
    Fabric3:是一个基于Fabric 1.x 的fork,兼容Python2 & Python3,兼容 Fabric1.x 的 fabfile;

二、项目结构

kvm_create/
├── application
│   ├── appcreate.py
│   ├── __init__.py
│   ├── kvmcreate.py
├── config
│   ├── hosts.py
│   ├── __init__.py
│   ├── messages.py
│   └── test
│       ├── __init__.py
│       ├── kvm_config.py
├── files
│   ├── adminset_agent.tar.gz
│   ├── mysqlxxxx
│   │   ├── backup.sh
│   │   ├── init_backup.sh
│   │   ├── lepus_slowquery.sh
│   │   ├── mysql-x.x.xx-linux-glibcx.xx-x86_64.tar.gz
│   │   └── percona-toolkit_x.x.xx-x.tar.gz
│   ├── mysqlxxxx
│   │   ├── backup.sh
│   │   ├── init_backup.sh
│   │   ├── lepus_slowquery.sh
│   │   ├── mysql-x.xx.xx-linux-glibcx.xx-86_64.tar.gz
│   │   └── percona-toolkit_x.x.xx-x.tar.gz
│   ├── nginx
│   │   ├── nginx-1.xx.x.tar.gz
│   │   └── nginx-1.xx.x.tar.gz
│   ├── node_exporter
│   │   ├── LICENSE
│   │   ├── node_exporter
│   │   └── NOTICE
│   ├── php
│   │   ├── php-x.x.xx.tar.gz
│   │   ├── php-x.x.xx.tar.gz
│   │   ├── php-x.x.xx.tar.gz
│   │   ├── php-x.x.xx.tar.gz
│   │   ├── php-x.x.xx.tar.gz
│   │   └── php.ini
│   ├── redis
│   │   └── redis-x.x.x.tar.gz
│   └── zabbix
│       └── zabbix-x.x.x.tar.gz
├── kernel
│   ├── clone.py
│   ├── __init__.py
│   ├── last.py
│   ├── mysqlxxxx.py
│   ├── mysqlxx.py
│   ├── nginx.py
│   ├── php.py
│   ├── redis.py
│   ├── standard.py
│   ├── start.py
│   ├── yum.py
│   ├── zabbix_agent.py
├── kvm.py
├── local
│   ├── __init__.py
│   ├── localall.py
│   ├── mysqlxxxxreplace.py
│   ├── mysqlxxreplace.py
│   ├── nginxreplace.py
│   ├── phpreplace.py
│   ├── redisreplace.py
│   ├── replacetemp.py
│   ├── zabbixreplace.py
├── templates
│   ├── hosts
│   ├── ifcfg-eth0
│   ├── mysqlxx
│   │   ├── my.cnf
│   │   ├── mysql_install.sh
│   │   └── mysql.service
│   ├── mysqlxxxx
│   │   ├── my.cnf
│   │   ├── mysql_install.sh
│   │   └── mysql.service
│   ├── nginx
│   │   ├── nginx.conf
│   │   ├── nginx_install.sh
│   │   ├── nginx.service
│   │   └── vhost.conf
│   ├── php
│   │   ├── php-fpm.conf
│   │   ├── php-fpm.service
│   │   ├── php_install.sh
│   │   └── www.conf
│   ├── redis
│   │   ├── redis.conf
│   │   ├── redis_install.sh
│   │   └── redis.service
│   └── zabbix
│       ├── zabbix_agentd
│       ├── zabbix_agentd.conf
│       └── zabbixagent_install.sh
├── tmp_cache
└── vars
    ├── __init__.py
    ├── kvmvar.py
    ├── mysqlxxxx.py
    ├── mysqlxxxx.py
    ├── nginxvar.py
    ├── phpvar.py
    ├── redisvar.py
    └── zabbix_agentvar.py

三、kernel 目录下的功能模块

kvm服务器镜像clone 同步、kvm xml 配置模板的生成同步,网卡配置文件整合kvm新服务器;

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.contrib.files import *
from config import *
from vars.kvmvar import *
from local.replacetemp import *
class generate_kvmimg(object):
    def __init__(self):
        pass

    def clone_cevsxx(self):

        run(' [ -d' + ' ' + kvmtmpdir + ' ' +']' + ' ' + '||'  + ' ' + 'mkdir -pv' + ' ' + kvmtmpdir)
        run('virt-clone -o ' + kvmce7name + ' ' + '-n' + ' ' + KVM_IP_HOSTNAME + ' ' + '-f' + ' ' + kvmtmpdir + KVM_IP_HOSTNAME + '.img')
      #  run('mv' + ' ' + kvmdata + KVM_IP_HOSTNAME + '.img' + ' ' + kvmtmpdir)
        run('sed -i' + ' '  + '"s' + '#' +  kvmtmpdir + '#' + kvmdata + '#g"' + ' ' + kvmqemu + KVM_IP_HOSTNAME + '.xml' )
        run('cp' + ' ' + kvmqemu + KVM_IP_HOSTNAME + '.xml' + ' ' + kvmtmpdir)
        run('virsh undefine' + ' ' + KVM_IP_HOSTNAME)


    def site_imgip(self):
        rekvmeth = replace_tmp_file()
        rekvmeth.kvm_eth0()
        put(local_write_eth0 , kvmtmpdir)
        rekvmeth.kvm_eth0_local_remove()
        with cd(kvmtmpdir):
            run('virt-copy-in' + ' ' + 'ifcfg-eth0' + ' ' + '-a' + ' ' + KVM_IP_HOSTNAME + '.img' + ' ' + kvm_eth0_dir + '/')
            run('rm -f' + ' ' + kvmtmpdir  + 'ifcfg-eth0')


    def site_imgxml_cpu(self):
        with cd(kvmtmpdir):
            run('sed -i \"s\\current=\'2\'\\current=\'' + KVM_CPU +'\'\\g\"' + ' ' + KVM_IP_HOSTNAME + '.xml')


    def site_imgxml_mem(self):
        with cd(kvmtmpdir):
            run('sed -i \"s\\4194304\\' + KVM_MEM + '\\g\"' + ' ' + KVM_IP_HOSTNAME + '.xml')

    def scp_cloned_img(self):
            run('scp' + ' ' + kvmtmpdir + KVM_IP_HOSTNAME + '.img' + ' ' + 'root@' + TARGETS_HOST + ':' + kvmdata)
            #run('scp' + ' '  + '-l 25000' + ' ' + kvmtmpdir + KVM_IP_HOSTNAME + '.img' + ' ' + 'root@' + TARGETS_HOST + ':' + kvmdata)
            run('rm -f' + ' ' + kvmtmpdir + KVM_IP_HOSTNAME + '.img')

    def scp_cloned_xml(self):
            run('scp' + ' ' + kvmtmpdir + KVM_IP_HOSTNAME + '.xml' + ' ' + 'root@' + TARGETS_HOST + ':' + kvmqemu)
            run('rm -f' + ' ' + kvmtmpdir + KVM_IP_HOSTNAME + '.xml')

突破 virt-copy-in 命令可以将已生成的网卡配置文件焙进镜像,依赖工具包libguestfs-tools-c 这样虚拟服务器开机后就可以直接分配指定的ip

操作目标服务器,将已经同步xml配置定义为虚拟机,扩展相应磁盘、并开启虚拟服务器;

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.contrib.files import *
from config import *
from vars.kvmvar import *

class start_kvm(object):
    def __init__(self):
        pass

    def define_kvm(self):
        run('virsh define' + ' ' + kvmqemu + KVM_IP_HOSTNAME + '.xml')
        run(' [ -d' + ' ' + kvmtmpdir + ' ' + ']' + ' ' + '||' + ' ' + 'mkdir -pv' + ' ' + kvmtmpdir)

    def start_kvm(self):
        run('virsh start' + ' ' + KVM_IP_HOSTNAME)

    def extend_disk(self):
        run('qemu-img create -f qcow2' + ' ' + kvmdata + KVM_IP_HOSTNAME + '-data1.img' + ' ' + KVM_DISK)
        run('virsh attach-disk' + ' ' + KVM_IP_HOSTNAME + ' ' + kvmdata + KVM_IP_HOSTNAME + '-data1.img' + ' ' + 'vdc --subdriver=qcow2')
        run('virsh dumpxml' + ' ' + KVM_IP_HOSTNAME + ' ' + '>' + ' ' + kvmqemu + KVM_IP_HOSTNAME + '.xml')
        run('virsh destroy' + ' ' + KVM_IP_HOSTNAME )
        run('virsh define' + ' ' + kvmqemu  + KVM_IP_HOSTNAME + '.xml')
        run('virsh start' + ' ' + KVM_IP_HOSTNAME)

虚拟服务器开机后完成相应的补充操作

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.contrib.files import *
from vars.kvmvar import localsrc,kvmsrc,kvm_install_local
from config import LOGING_USER
from local.replacetemp import *
from local.localall import *
class standard_kvm(object):
    def __init__(self):
        pass

    def site_kvm_hosts(self):
        kvm_host_init  = replace_tmp_file()
        kvm_host_init.kvm_hosts()
        put(local_write_hosts,'/etc')
        local('rm -f' + ' ' + local_write_hosts)
        run('hostnamectl set-hostname' + ' ' + KVM_IP_HOSTNAME)
        run('mkfs.xfs /dev/vdb')
        run('ls -l /data &>/dev/null || mkdir /data')
        fstab_shell_list = '''
            cat /etc/fstab | grep data > /dev/null || \\
            for i in `ls -l  /dev/disk/by-uuid/  | grep vdb  | awk '{print $9}'` ; \\
            do echo "UUID=$i  /data                   xfs     defaults        0 0" >> /etc/fstab; \\
            done

            '''
        run(fstab_shell_list)
        run('mount -a')
    def install_kvm_adminsetd(self):
        put(localsrc + '/' + 'adminset_agent.tar.gz',kvmsrc)
        run('yum clean all')
        yum_clean_wait = command(25)
        yum_clean_wait.wait_sleep()
        with cd(kvmsrc):
            run('tar -xvf' + ' ' + 'adminset_agent.tar.gz')
        with cd(kvmsrc +'/' + 'adminset_agent'):
            run('sh install.sh')

    def install_node_exporter(self):
        put(localsrc + '/' + 'node_exporter', kvm_install_local) 
        run('chmod +x /usr/local/node_exporter/node_exporter')
        run('echo \" nohup /usr/local/node_exporter/node_exporter &> /dev/null & \" >> /etc/rc.local')
        run('nohup /usr/local/node_exporter/node_exporter &> /dev/null &')
        run('hwclock --systohc')
    def set_kvm_passwd(self):
        run('echo' + ' ' + '\"' + new_pass  + '\"' + ' ' + '| passwd --stdin' + ' ' + LOGING_USER)

mysqlxx 补充模块, 实现功能与ansible-playbook roles 类似,现整合在kvm自动化中

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from vars.mysqlxx import  *
from local.mysqlxxreplace import replace_mysqlxx_tmp
from fabric.contrib.files import *
from vars.kvmvar import  tmp_cache, systemd
replace_mysqlxx = replace_mysqlxx_tmp()
class mysqlxx_kvm(object):

    def __init__(self):
        pass

    def delete_old_conf(self):
        run('rm -f /etc/my.cnf')

    def delete_old_conf_dir(self):
        run('rm -rf /etc/my.cnf')

    def cp_source(self):
        put(mysqlfile + 'mysql-' + mysqlversion + '.tar.gz', datasrc)
        with cd(datasrc):
            run('tar -xvf' + ' ' 'mysql-' + mysqlversion + '.tar.gz' + ' ' + '-C /usr/local')

    def cp_mysql_conf(self):
        put(mysqltemp + 'my.cnf', '/etc/')

    def copy_mysql_install_sh(self):
        replace_mysqlxx.mysql_install_sh()
        put(tmp_cache + 'mysql_install.sh', datasrc)

    def add_mysql_user_and_group(self):
        run('id -g' + ' ' + mysqlgroup + ' ' + '||' + ' ' + 'groupadd' + ' ' + mysqlgroup)
        run('id -u' + ' ' + mysqluser + ' ' + '||' + ' ' + 'useradd -r' + ' ' + mysqluser + ' ' + '-g' + ' ' + mysqluser + ' ' + '-s' + ' ' + '/sbin/nologin')

    def create_related_directories(self):
        run('mkdir -pv' + ' ' + mysqldatapath )
        run('mkdir -pv' + ' ' + mysqlbinlogpath)
        run('mkdir -pv' + ' ' + mysqlslowlogpath)
        run('mkdir -pv' + ' ' + mysqltmppath)
        run('mkdir -pv' + ' ' + mysqlerrlogpath)
        run('chmod 755' + ' ' + mysqldatapath)
        run('chmod 755' + ' ' + mysqlbinlogpath)
        run('chmod 755' + ' ' + mysqlslowlogpath)
        run('chmod 755' + ' ' + mysqltmppath)
        run('chmod 755' + ' ' + mysqlerrlogpath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqldatapath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlbinlogpath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlslowlogpath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqltmppath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlerrlogpath)

    def create_slowlog_file(self):
        run('touch' + ' ' + mysqlslowlogpath + 'slowquery.log')
        run('chmod 644' + ' ' + mysqlslowlogpath + 'slowquery.log')
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlslowlogpath + 'slowquery.log')
    def install_mysql(self):
        with cd(datasrc):
            run('/bin/bash' + ' ' + 'mysql_install.sh' + ' ' + '&& touch /tmp/mysql_installed')
    def add_mysql_to_PATH(self):
        run('echo \'export PATH=$PATH:' + mysqlinstallpath + 'bin\' >> /etc/profile && touch /tmp/mysql_addtopath')

    def copy_mysql_service_file(self):
        replace_mysqlxx.mysql_service()
        put(tmp_cache + 'mysql.service', systemd)

    def clean_local_tmp_cache_file(self):
        replace_mysqlxx.clean_mysqlxx_tmp_cache()

    def start_mysql_and_enable_it_onboot(self):
        run('systemctl daemon-reload')
        run('systemctl start mysql')
        run('systemctl enable mysql')
        run('pip install MySQL-python')
        run('ln -s /data/mysqltmp/mysql.sock /tmp/mysql.sock')
        run('ln -s /usr/local/mysql-x.x.xx-linux-glibcx.xx-x86_64/lib/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20')
        run('ln -s /usr/local/mysql-x.x.xx-linux-glibcx.xx-x86_64/ /usr/local/mysql')
        run('[ -d /data/scripts/ ] || mkdir /data/scripts/')
        put(mysqlfile + 'backup.sh', '/data/scripts')
        put(mysqlfile + 'lepus_slowquery.sh', '/data/scripts')
        put(mysqlfile + 'percona-toolkit_x.x.xx-x.tar.gz', '/data/scripts')
        put(mysqlfile + 'init_backup.sh', '/data/scripts')

mysqlxxxx 同上,实现多版本并存

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from vars.mysqlxxxx import  *
from local.mysqlxxxxreplace import replace_mysqlxx13_tmp
from fabric.contrib.files import *
from vars.kvmvar import  tmp_cache, systemd
replace_mysqlxxxx = replace_mysqlxxxx_tmp()
class mysqlxxxx_kvm(object):

    def __init__(self):
        pass

    def delete_old_conf(self):
        run('rm -f /etc/my.cnf')

    def delete_old_conf_dir(self):
        run('rm -rf /etc/my.cnf')

    def cp_source(self):
        put(mysqlfile + 'mysql-' + mysqlversion + '.tar.gz', datasrc)
        with cd(datasrc):
            run('tar -xvf' + ' ' 'mysql-' + mysqlversion + '.tar.gz' + ' ' + '-C /usr/local')

    def cp_mysql_conf(self):
        put(mysqltemp + 'my.cnf', '/etc/')

    def copy_mysql_install_sh(self):
        replace_mysqlxxxx.mysql_install_sh()
        put(tmp_cache + 'mysql_install.sh', datasrc)

    def add_mysql_user_and_group(self):
        run('id -g' + ' ' + mysqlgroup + ' ' + '||' + ' ' + 'groupadd' + ' ' + mysqlgroup)
        run('id -u' + ' ' + mysqluser + ' ' + '||' + ' ' + 'useradd -r' + ' ' + mysqluser + ' ' + '-g' + ' ' + mysqluser + ' ' + '-s' + ' ' + '/sbin/nologin')

    def create_related_directories(self):
        run('mkdir -pv' + ' ' + mysqldatapath )
        run('mkdir -pv' + ' ' + mysqlbinlogpath)
        run('mkdir -pv' + ' ' + mysqlslowlogpath)
        run('mkdir -pv' + ' ' + mysqltmppath)
        run('mkdir -pv' + ' ' + mysqlerrlogpath)
        run('chmod 755' + ' ' + mysqldatapath)
        run('chmod 755' + ' ' + mysqlbinlogpath)
        run('chmod 755' + ' ' + mysqlslowlogpath)
        run('chmod 755' + ' ' + mysqltmppath)
        run('chmod 755' + ' ' + mysqlerrlogpath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqldatapath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlbinlogpath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlslowlogpath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqltmppath)
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlerrlogpath)

    def create_slowlog_file(self):
        run('touch' + ' ' + mysqlslowlogpath + 'slowquery.log')
        run('chmod 644' + ' ' + mysqlslowlogpath + 'slowquery.log')
        run('chown' + ' ' + mysqluser + ':' + mysqlgroup + ' ' + mysqlslowlogpath + 'slowquery.log')
    def install_mysql(self):
        with cd(datasrc):
            run('/bin/bash' + ' ' + 'mysql_install.sh' + ' ' + '&& touch /tmp/mysql_installed')
    def add_mysql_to_PATH(self):
        run('echo \'export PATH=$PATH:' + mysqlinstallpath + 'bin\' >> /etc/profile && touch /tmp/mysql_addtopath')

    def copy_mysql_service_file(self):
        replace_mysqlxxxx.mysql_service()
        put(tmp_cache + 'mysql.service', systemd)

    def clean_local_tmp_cache_file(self):
        replace_mysqlxxxx.clean_mysqlxxxx_tmp_cache()

    def start_mysql_and_enable_it_onboot(self):
        run('systemctl daemon-reload')
        run('systemctl start mysql')
        run('systemctl enable mysql')
        run('pip install MySQL-python')
        run('ln -s /data/mysqltmp/mysql.sock /tmp/mysql.sock')
        run('ln -s /usr/local/mysql-x.x.xx-linux-glibc2.5-x86_64/lib/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20')
        run('ln -s /usr/local/mysql-x.x.xx-linux-glibc2.5-x86_64/ /usr/local/mysql')
        run('[ -d /data/scripts/ ] || mkdir /data/scripts/')
        put(mysqlfile + 'backup.sh', '/data/scripts')
        put(mysqlfile + 'lepus_slowquery.sh', '/data/scripts')
        put(mysqlfile + 'percona-toolkit_x.x.xx-1.tar.gz', '/data/scripts')
        put(mysqlfile + 'init_backup.sh', '/data/scripts')

kvm 整合 redis安装自动化

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from local.redisreplace import  replace_redis_tmp
from vars.redisvar import *
from fabric.contrib.files import *
from vars.kvmvar import tmp_cache, systemd
replace_redis = replace_redis_tmp()
class redis_kvm(object):
    def __init__(self):
        pass
    def cp_source(self):
        put(redisfile + 'redis-' + redisversion + '.tar.gz', datasrc)
        with cd(datasrc):
            run('tar -xvf' + ' ' + 'redis-' + redisversion + '.tar.gz')
    def cp_redis_install_shell(self):
        replace_redis.redis_install_sh()
        put(tmp_cache + 'redis_install.sh', datasrc)
    def create_redis_group_and_user(self):
        run('id -g' + ' ' + redisgroup + ' ' + '||' + ' ' + 'groupadd' + ' ' + redisgroup)
        run('id -u' + ' ' + redisgroup + ' ' + '||' + ' ' + 'useradd -r' + ' ' + redisuser + ' ' + '-g' + ' ' + redisgroup + ' ' + '-s' + ' ' + '/sbin/nologin')
    def install_redis(self):
        run('/bin/bash' + ' ' + datasrc + 'redis_install.sh' + ' ' + '&& touch /tmp/redis_installed')
    def create_redis_log_file(self):
        run('touch' + ' ' + redislogpath)
        run('chmod 644' + ' ' + redislogpath)
        run('chown' + ' ' + redisuser + ':' + redisgroup + ' ' + redislogpath)
    def crate_redis_data_dir(self):
        run('mkdir -pv' + ' ' + redisdatapath)
        run('chmod 755' + ' ' + redisdatapath)
        run('chown' + ' ' + redisuser + ':' + redisgroup + ' ' + redisdatapath)
    def copy_redis_config(self):
        replace_redis.redis_conf()
        put(tmp_cache + 'redis.conf', redisdatapath + 'redis' + redisport + '.conf')
        run('chmod 644' + ' ' + redisdatapath + 'redis' + redisport + '.conf')
        run('chown' + ' ' + redisuser + ':' + redisgroup + ' ' + redisdatapath + 'redis' + redisport + '.conf')

    def copy_redis_service_config(self):
        replace_redis.redis_service()
        put(tmp_cache + 'redis.service', systemd + 'redis' + redisport + '.service')
        run('systemctl daemon-reload')
        run('systemctl start' + ' ' + 'redis' + redisport)
    def clean_local_tmp_cache_file(self):
        replace_redis.clean_redis_tmp_cache()
        run('echo \'export PATH=$PATH:/usr/local/src/redis-' + redisversion + '/src/' + '\' >> /etc/profile')

kvm整合nginx 安装自动化

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from local.nginxreplace import replace_nginx_tmp
from fabric.contrib.files import *
from vars.nginxvar import *
from vars.kvmvar import kvmsrc,tmp_cache,systemd
replace_nginx = replace_nginx_tmp()
class nginx_kvm(object):
    def __init__(self):
        pass
    def copy_source(self):
        put(nginxfile + 'nginx-' + nginxversion + '.tar.gz' , kvmsrc)
        with cd(kvmsrc):
            run('tar -xvf' + ' ' + 'nginx-' + nginxversion + '.tar.gz')
    def copy_nginx_install_shell(self):
        replace_nginx.nginx_install_sh()
        put(tmp_cache + 'nginx_install.sh', kvmsrc)
    def create_nginx_log_dir(self):
        run('mkdir -pv' + ' ' + nginxlogpath)
        run('chmod 755' + ' ' + nginxlogpath)
    def create_nginx_group_and_user(self):
        run('id -g' + ' ' + nginxgroup + ' ' + '||' + ' ' + 'groupadd' + ' ' + nginxgroup)
        run('id -u' + ' ' + nginxuser + ' ' + '||' + ' ' + 'useradd -r' + ' ' + nginxuser + ' ' + '-g' + ' ' + nginxgroup + ' ' + '-s' + ' ' + '/sbin/nologin')
    def install_nginx(self):
        with cd(kvmsrc):
            run('/bin/bash' + ' ' + 'nginx_install.sh')
    def create_nginx_vhosts_dir(self):
        run('mkdir -pv' + ' ' + nginxinstallpath + 'conf/vhost')
        run('chmod 755' + ' ' + nginxinstallpath + 'conf/vhost')
    def copy_main_template_config_file(self):
        replace_nginx.nginx_conf()
      #  run('mv' + ' ' + nginxinstallpath + 'conf/nginx.conf{,.bak}')
        put(tmp_cache + 'nginx.conf', nginxinstallpath + 'conf/nginx.conf')
    def copy_vhost_template_config_file(self):
        replace_nginx.nginx_vhost()
        put(tmp_cache + 'vhost.conf', nginxinstallpath + 'conf/vhost')
    def clean_local_tmp_cache_file(self):
        replace_nginx.clean_nginx_tmp_cache()
    def copy_service_config_file(self):
        replace_nginx.nginx_service()
        put(tmp_cache + 'nginx.service',systemd)
    def start_nginx(self):
        run('systemctl daemon-reload')
        run('systemctl start nginx')

kvm 整合php 自动化

!/usr/bin/env python2
# -*- coding: utf-8 -*-
from local.phpreplace import replace_php_tmp
from vars.phpvar import *
from fabric.contrib.files import *
from vars.kvmvar import kvmsrc, tmp_cache, systemd
replace_php = replace_php_tmp()
class php_kvm(object):
    def __init__(self):
        pass
    def cp_source(self):
        put(phpfile + 'php-' + phpversion + '.tar.gz', kvmsrc)
        with cd(kvmsrc):
            run('tar -xvf' + 'php-' + phpversion + '.tar.gz')
    def cp_php_install_shell(self):
        replace_php.php_install_sh()
        put(tmp_cache + 'php_install.sh', kvmsrc)
    def create_php_group_and_user(self):
        run('id -g' + ' ' + phpgroup + ' ' + '||' + ' ' + 'groupadd' + ' ' + phpgroup)
        run('id -u' + ' ' + phpuser + ' ' + '||' + ' ' + 'useradd -r' + ' ' + phpuser + ' ' + '-g' + ' ' + phpgroup + ' ' + '-s' + ' ' + '/sbin/nologin')
    def create_php_log_dir(self):
        run('mkdir -pv' + ' ' + phplogpath)
        run('chmod 755' + ' ' + phplogpath)
        run('chown' + ' ' + phpuser + ':' + phpgroup + ' ' + phplogpath)
    def create_php_error_log_file(self):
        run('touch' + ' ' + phplogpath + 'php_errors.log')
        run('chmod 644' + ' ' + phplogpath + 'php_errors.log')
        run('chown' + ' ' + phpuser + ':' + phpgroup + ' ' + phplogpath + 'php_errors.log')
    def install_php(self):
        with cd(kvmsrc):
            run('/bin/bash' + ' ' + 'php_install.sh')
    def cp_main_phpini_config(self):
        put(phpfile + 'php.ini', phpinstallpath + 'etc/')
    def cp_php_www_conf_config(self):
        replace_php.php_www()
        put(tmp_cache + 'www.conf', phpinstallpath + 'etc/php-fpm.d/')
    def cp_php_php_fpm_config(self):
        replace_php.php_conf()
        put(tmp_cache + 'php-fpm.conf', phpinstallpath + 'etc/')
    def cp_php_php_fpm_service(self):
        replace_php.php_service()
        put(tmp_cache + 'php-fpm.service', systemd)
    def start_php(self):
        run('systemctl daemon-reload')
        run('systemctl start php-fpm || ss -tnl | grep 9000')
    def clean_local_tmp_cache_files(self):
        replace_php.clean_php_tmp_cache()

kvm 整合zabbix_agent自动化

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from vars.zabbix_agentvar import  *
from local.zabbixreplace import  replace_zabbix_tmp
from fabric.contrib.files import *
from vars.kvmvar import kvmsrc, tmp_cache, systemd
replace_zabbix_agent = replace_zabbix_tmp()

class zabbix_agent_kvm(object):

    def __init__(self):
        pass

    def cp_source(self):
        put(zabbixfile + 'zabbix-' + zabbixagentversion + '.tar.gz' , datasrc)
        with cd(datasrc):
            run('tar -xvf' + ' ' + 'zabbix-' + zabbixagentversion + '.tar.gz')

    def cp_zabbix_agent_install_shell(self):
        replace_zabbix_agent.zabbix_agent_install_sh()
        put(tmp_cache + 'zabbixagent_install.sh', datasrc)

    def create_zabbix_group_and_user(self):
        run('id -g' + ' ' + zabbixagentgroup + ' ' + '||' + ' ' + 'groupadd' + ' ' + zabbixagentgroup)
        run('id -u' + ' ' + zabbixagentuser + ' ' + '||' + ' ' + 'useradd -r' + ' ' + zabbixagentuser + ' ' + '-g' + ' ' + zabbixagentgroup + ' ' + '-s' + ' ' + '/sbin/nologin')

    def create_zabbix_agent_log_dir(self):
        run('mkdir -pv' + ' ' + zabbixagentlogpath)
        run('chmod 755' + ' ' + zabbixagentlogpath)
        run('chown' + ' ' + zabbixagentuser + ':' + zabbixagentgroup + ' ' + zabbixagentlogpath)

    def install_zabbix_agent(self):
        with cd(datasrc):
            run('/bin/bash' + ' ' + 'zabbixagent_install.sh' + ' ' + 'touch /tmp/zabbixagent_installed')

    def copy_zabbix_agent_config_file(self):
        replace_zabbix_agent.zabbix_agent_conf()
        put(tmp_cache + 'zabbix_agentd.conf', zabbixagentinstallpath + '/etc/')

    def copy_zabbix_agent_init_file(self):
        replace_zabbix_agent.zabbix_agent_service()
        put(tmp_cache + 'zabbix_agentd', '/etc/init.d/')
        run('chmod 755' + ' ' + '/etc/init.d/zabbix_agentd')

    def clean_local_tmp_cache_file(self):
        replace_zabbix_agent.clean_zabbix_tmp_cache()

    def add_zabbix_agent_init(self):
        run('chkconfig --add zabbix_agentd')
        run('chkconfig zabbix_agentd on')

    def start_zabbix_agentd(self):
        run('service zabbix_agentd start')

yum 安装相关依赖包

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.contrib.files import *
from vars.kvmvar import yum_install_require_packages,yum_install_jdk_packages
def yum_install_packages():
    run('yum -y install' + '\\' + yum_install_require_packages)

def yum_install_jdk():
    run('yum -y install' + '\\' + yum_install_jdk_packages)

输入输出信息打印

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.colors import red
from config import TARGETS_HOST, KVM_IP_HOSTNAME, KVM_IP_HOST, KVM_IP_GATEWAY, KVM_IP_NETMASK, ENV, APP, KVM_CPU, KVM_MEM ,KVM_DISK,KVM_IP_PASSWD
from config.messages import MESSAGE_INFO
from local.localall import new_pass, common_first
class last(object):
    def __init__(self):
        pass


    def print_start_kvm_message(self):
        app_list = str(APP)
        app_list = app_list.replace("'","")
        app_list = app_list.replace("[","")
        app_list = app_list.replace("]","")
        app_list = app_list.replace(",","")
        first_print = common_first(MESSAGE_INFO['env'] + ENV, MESSAGE_INFO['name'] + KVM_IP_HOSTNAME,
                                   MESSAGE_INFO['ip'] + KVM_IP_HOST, MESSAGE_INFO['target'] +TARGETS_HOST,
                                   MESSAGE_INFO['gateway'] + KVM_IP_GATEWAY, MESSAGE_INFO['netmask'] + KVM_IP_NETMASK,
                                   MESSAGE_INFO['app'] + app_list, MESSAGE_INFO['cpu'] + KVM_CPU,
                                   MESSAGE_INFO['memory'] + KVM_MEM, MESSAGE_INFO['disk'] + KVM_DISK,
                                   MESSAGE_INFO['passwd'] + KVM_IP_PASSWD)
        first_print.message_all()
        user_choice = raw_input(red('If the information is incorrect, please input no: '))
        if user_choice == "N" or user_choice == "n" or user_choice == "no" or user_choice == "No" or user_choice == "NO":
            print
            print(red('User check configuration is incorrect. Please rewrite configuration!'))
            print
            exit(1000)


    def print_kvm_message(self):
        app_list = str(APP)
        app_list = app_list.replace("'","")
        app_list = app_list.replace("[","")
        app_list = app_list.replace("]","")
        app_list = app_list.replace(",","")
        last_print = common_first(MESSAGE_INFO['env'] + ENV, MESSAGE_INFO['name'] + KVM_IP_HOSTNAME,
                                   MESSAGE_INFO['ip'] + KVM_IP_HOST, MESSAGE_INFO['target'] + TARGETS_HOST,
                                   MESSAGE_INFO['gateway'] + KVM_IP_GATEWAY, MESSAGE_INFO['netmask'] + KVM_IP_NETMASK,
                                   MESSAGE_INFO['app'] + app_list, MESSAGE_INFO['cpu'] + KVM_CPU,
                                   MESSAGE_INFO['memory'] + KVM_MEM, MESSAGE_INFO['disk'] + KVM_DISK,
                                   MESSAGE_INFO['passwd'] + new_pass)
        last_print.last_message_all()

local 实现配置文件修改与替换

例ifcfg-eth0

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.api import *
from vars.kvmvar import local_read_eth0,local_write_eth0,local_read_hosts,local_write_hosts,tmp_cache
from config import KVM_IP_HOST,KVM_IP_NETMASK,KVM_IP_GATEWAY,KVM_IP_STATUE,KVM_IP_HOSTNAME
import re


class replace_tmp_file():
    def __init__(self):
        pass
    def kvm_eth0(self):
        network_file_read = open(local_read_eth0,"r")
        network_file_write = open(local_write_eth0,"w")

        with network_file_read as f:
            data = f.read()
            data = data.replace('$NETWORK_STATUS$',KVM_IP_STATUE)
            data = data.replace('$NETWORK_IPADDR$',KVM_IP_HOST)
            data = data.replace('$NETWORK_NETMASK$',KVM_IP_NETMASK)
            data = data.replace('$NETWORK_GATEWAY$',KVM_IP_GATEWAY)
        with network_file_write as f:
            f.write(data)
    def kvm_eth0_local_remove(self):
        local('rm -f' + ' ' + local_write_eth0)

    def kvm_hosts(self):
        hosts_file_read = open(local_read_hosts,"r")
        hosts_file_write = open(local_write_hosts,"w")

        with hosts_file_read as f:
            hosts = f.read()
            hosts = hosts.replace('$HOSTSIP$', KVM_IP_HOST)
            hosts = hosts.replace('$HOSTNAME$', KVM_IP_HOSTNAME)
        with hosts_file_write as f:
            f.write(hosts)

    def kvm_hosts_remove(self):
        local('rm -f' + ' ' + local_write_eth0)

其它功相关调用

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import random
import string
from fabric.colors import *
from time import sleep
class command(object):
    def __init__(self,info):
        self.info = info
    def wait_sleep(self):
        sleep(self.info)
    def create_pass(self,chars=string.ascii_uppercase+string.ascii_lowercase+string.digits):
#string.punctuation
        return ''.join([random.choice(chars) for i in range(self.info)])
from fabric.colors import *
class common(object):
    def __init__(self,info):
        self.info = info
    def wait(self):
        for waits in range(1,2):
            sleep(waits)
    def message_green(self):
        print green('**' * 50)
        print green('{:<10}{:^80}{:>10}'.format('*' * 10, self.info , '*' * 10))
        print green('**' * 50)
        print

    def message_blue(self):
        print blue('**' * 50)
        print blue('{:<10}{:^80}{:>10}'.format('*' * 10, self.info , '*' * 10))
        print blue('**' * 50)
        print

    def message_orange(self):
        print yellow('**' * 50)
        print yellow('{:<10}{:^80}{:>10}'.format('*' * 10, self.info , '*' * 10))
        print yellow('**' * 50)
        print

class common_first(object):
    def __init__(self, env, name, ip, target, gateway, netmask, app, cpu, memory, disk ,passwd):
        self.env = env
        self.name = name
        self.ip = ip
        self.target = target
        self.gateway = gateway
        self.netmask = netmask
        self.app = app
        self.cpu = cpu
        self.memory = memory
        self.disk = disk
        self.passwd = passwd

    def message_all(self):
        print magenta('**' * 50)
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.env, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.name, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.ip, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.target, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.gateway, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.netmask, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.app, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.cpu, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.memory, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.disk, '*' * 10))
        print magenta('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.passwd, '*' * 10))
        print magenta('**' * 50)
        print
    def last_message_all(self):
        print cyan('**' * 50)
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.env, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.name, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.ip, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.target, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.gateway, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.netmask, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.app, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.cpu, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.memory, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.disk, '*' * 10))
        print cyan('{:<10}{:<80}{:>10}'.format('*' * 10, '  ' + self.passwd, '*' * 10))
        print cyan('**' * 50)
        print
newpasswd = command(12)
new_pass = newpasswd.create_pass()

config 目录存放配置相关的信息

message.py

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from config import ENV
MESSAGE_INFO = {
    'start_kvm_site': 'Now Start KVM Create',
    'app_clone': 'App Clone Genreate CentOS74',
    'site_image_ip': 'Source Hosts Site Image IP',
    'site_imgxml_cpu': 'Sources Hosts Site Image XML CPU',
    'site_imgxml_mem': 'Sources Hosts Site Image XML Memory',
    'scp_cloned_img': 'Sources Hosts Scp Cloned Image To Targets hosts',
    'scp_cloned_xml': 'Sources Hosts Scp Cloned XML To Targets hosts',
    'define_kvm': 'Targets Hosts Define Kvm Hosts',
    'start_kvm': 'Targets Hosts Start Kvm Hosts',
    'extend_disk': 'Targets Hosts Extend Kvm Disk',
    'wait_sleep': 'Sleep 60s For Wait Start Kvm Hosts',
    'site_kvm_hosts': 'KVM Site KVM Hosts',
    'install_kvm_adminsetd': 'KVM Install  Adminsetd And Node_exporter',
    'start_yum_tools_install': 'Now Start Yum Tools Install',
    'start_nginx_install': 'Now Start Nginx Install',
    'nginx_copy': 'Nginx Copy Source File',
    'copy_nginx_install_shell': 'Nginx Copy Install Shell',
    'create_nginx_log_dir': 'Nginx Create Log Dir',
    'create_nginx_group_and_user': 'Nginx Create Group And User',
    'install_nginx': 'Nginx Install',
    'create_nginx_vhosts_dir': 'Nginx Create Vhosts Dir',
    'copy_main_template_config_file': 'Nginx Main Template Config File',
    'copy_vhost_template_config_file': 'Nginx Copy Vhost Template Config File',
    'copy_service_config_file': 'Nginx Copy Service Config File',
    'start_nginx': 'Nginx Start',
    'nginx_clean_local_tmp_cache_file': 'Nginx Clean local Tmp Cache File',
    'start_php_install': 'Now Start PHP Install',
    'php_copy': 'PHP Copy Source File',
    'cp_php_install_shell': 'PHP Copy Install Shell',
    'create_php_group_and_user': 'PHP Create Group And User',
    'create_php_log_dir': 'PHP Create Log Dir',
    'create_php_error_log_file': 'PHP Create Error Log File',
    'install_php': 'PHP Install',
    'cp_main_phpini_config': 'PHP Copy Phpini Config',
    'cp_php_www_conf_config': 'PHP Copy PHP WWW Config',
    'cp_php_php_fpm_config': 'PHP Copy php-fpm Config',
    'cp_php_php_fpm_service': 'PHP Copy php-fpm Service',
    'start_php': 'PHP Start',
    'php_clean_local_tmp_cache_files': 'PHP Clean local Tmp Cache File',
    'start_redis_install': 'Now Start Reids Install',
    'redis_copy': 'Redis Copy Source File',
    'cp_redis_install_shell': 'Redis Copy Install Shell',
    'create_redis_group_and_user': 'Redis Create Grup And User',
    'install_redis': 'Redis Install',
    'create_redis_log_file': 'Redis Create Redis Log File',
    'crate_redis_data_dir': 'Redis Create Data Dir',
    'copy_redis_config': 'Redis Copy Config',
    'copy_redis_service_config': 'Redis Copy Service Config',
    'redis_clean_local_tmp_cache_file': 'Redis Clean Local Tmp Cache File',
    'start_jdk_install': 'Now Start Install JDK',
    'yum_install_jdk': 'JDK Yum Install',
    'start_zabbix_agentd_install': 'Now Start Zabbix Agent Install',
    'zabbix_copy': 'Zabbix Copy Source File',
    'cp_zabbix_agent_install_shell': 'Zabbix Copy Agent Install Shell',
    'create_zabbix_group_and_user': 'Zabbix Create Zabbix Group And User',
    'create_zabbix_agent_log_dir': 'Zabbix Create Agent Log Dir',
    'install_zabbix_agent': 'Zabbix Install Agent',
    'copy_zabbix_agent_config_file': 'Zabbix Copy Zabbix Agent Config',
    'copy_zabbix_agent_init_file': 'Zabbix Copy Zabbix Agent init',
    'add_zabbix_agent_init': 'Zabbix Agent init',
    'start_zabbix_agentd': 'Zabbix Agent start',
    'zabbix_clean_local_tmp_cache_file': 'Zabbix Clean Local Tmp Cache File',
    'start_mysqlxx_install': 'Now Start Mysqlxx Install',
    'deletexx_old_conf': 'MySQLxx Delete Old Conf',
    'deletexx_old_conf_dir': 'MySQLxx Delete Old Conf Dir',
    'mysqlxx_source': 'MySQLxx Copy Source File',
    'cp_mysqlxx_conf': 'MySQLxx Copy Conf',
    'copy_mysqlxx_install_sh': 'MySQLxx Copy Install Shell',
    'add_mysqlxx_user_and_group': 'MySQLxx Add User And Group',
    'createxx_related_directories': 'MySQlxx Create Related Directories',
    'createxx_slowlog_file': 'MySQLxx Create Slowlog File',
    'install_mysqlxx': 'MySQLxx Install',
    'add_mysqlxx_to_PATH': 'MySQLxx Add To PATH',
    'cleanxx_local_tmp_cache_file': "MySQLxx Clean Local Tmp Cache File",
    'copy_mysqlxx_service_file': "MySQLxx Copy Service File",
    'start_mysqlxx_and_enable_it_onboot': 'MySQLxx start And Enable Onboot',
    'start_mysqlxx13_install': 'Now Start Mysqlxx13 Install',
    'deletexx13_old_conf': 'MySQLxx13 Delete Old Conf',
    'deletexx13_old_conf_dir': 'MySQLxx13 Delete Old Conf Dir',
    'mysqlxx13_source': 'MySQLxx13 Copy Source File',
    'cp_mysqlxx13_conf': 'MySQLxx13 Copy Conf',
    'copy_mysqlxx13_install_sh': 'MySQLxx13 Copy Install Shell',
    'add_mysqlxx13_user_and_group': 'MySQLxx13 Add User And Group',
    'createxx13_related_directories': 'MySQlxx13 Create Related Directories',
    'createxx13_slowlog_file': 'MySQLxx13 Create Slowlog File',
    'install_mysqlxx13': 'MySQLxx13 Install',
    'add_mysqlxx13_to_PATH': 'MySQLxx13 Add To PATH',
    'cleanxx13_local_tmp_cache_file': "MySQLxx13 Clean Local Tmp Cache File",
    'copy_mysqlxx13_service_file': "MySQLxx13 Copy Service File",
    'start_mysqlxx13_and_enable_it_onboot': 'MySQLxx13 Start And Enable Onboot',
    'set_kvm_passwd': 'KVM Set root passwd',
    'env': 'KVM ENV Is: ',
    'name': 'KVM Host Name Is: ',
    'ip': 'KVM IP Is: ',
    'target': 'TARGET IP Is: ',
    'gateway': 'KVM Gateway Is: ',
    'netmask': 'KVM Netmask Is: ',
    'app': 'KVM APP Is: ',
    'cpu': 'KVM CPU Is: ',
    'memory': 'KVM Memory Is: ',
    'disk': 'KVM Disk Is: ',
    'passwd': 'KVM Passwd IS: '
}

host.py 开通服务器所要操控的三台服务器

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from config import *
SOURCES_HOSTS = [
    LOGING_USER + '@' + SOURCES_HOST
]

TARGETS_HOSTS = [
    LOGING_USER + '@' + TARGETS_HOST
]

KVM_HOSTS = [
    LOGING_USER + '@' + KVM_IP_HOST
]

init.py 生产与生产环境配置的开关

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#from config.prod.kvm_config import *
from config.test.kvm_config import *

kvm_create.py 每次开通服务器所需要的配置信息

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from fabric.api import env
from local.localall import newpasswd

########################################### 以下为通常不变的信息 ########################################

## 登录用户信息,默认为root
ENV = 'prod'
LOGING_USER = 'root'
## 生产环境网关信息,生产网关默认不变
KVM_IP_GATEWAY = 'xxx.xx.xx.xx'
## kvm服务器密码信息kvm服务器中的模板,一般不变
KVM_IP_PASSWD = 'xxxxxxxxXXXX'
## 源服务器信息,一般从物理服务器(xxx.xx.xx.xx)进行克隆
SOURCES_HOST = 'xxx.xx.xx.xx'
SOURCES_PASS = 'xxxxxxxxxxxxxxxxxxxxxxx'

## kvm虚拟机启动后网卡的开启状态,一般不变
KVM_IP_STATUE = 'yes'

########################################## 以下为通常改变的信息 #########################################
#
### 目标服务器信息(切记改变配置信息)
#TARGETS_HOST = 'xxx.xx.xxx.xx'
#TARGETS_PASS = 'xxxxxxxxxxxxxxxxxxxxxxxx'
### kvm服务器信息(切记改变配置信息)
#KVM_IP_HOSTNAME = 'xx-xxxx-xx-xxx-xxxx-xxxx'
#KVM_IP_HOST = '172.30.70.'
#KVM_IP_NETMASK = '255.255.192.0'
#KVM_CPU = '2'
#KVM_DISK = '100G'
##KVM_MEM = '10xx144'
##KVM_MEM = '2114288'
##KVM_MEM = '2097152'
##KVM_MEM = '4194304'
##KVM_MEM = '6291456'
#KVM_MEM = '8388608'
##KVM_MEM = '1048xx60'
##KVM_MEM = '12582912'
##KVM_MEM = '14680064'
##KVM_MEM = '16777216'
##KVM_MEM = '20971520'
##KVM_MEM = '25165824'
#APP = ['zabbix_agent', 'jdk']
##APP = ['zabbix_agent', 'nginx', 'php', 'redis', 'jdk', 'mysql_xx', 'mysql_xx13']
############################################# 以下为代替的执行登录信息 ####################################
env.passwords[LOGING_USER + '@' + SOURCES_HOST + ':22'] = SOURCES_PASS
env.passwords[LOGING_USER + '@' + TARGETS_HOST + ':22'] = TARGETS_PASS
env.passwords[LOGING_USER + '@' + KVM_IP_HOST + ':22'] = KVM_IP_PASSWD

application 定义自动化执行流程

kvmcreate.py 定义kvm 自动部署流程

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from config import KVM_CPU,KVM_MEM,KVM_DISK
from config.messages import MESSAGE_INFO
from kernel.clone import *
from kernel.start import *
from kernel.standard import *
from kernel.last import *
from local.localall import common
app_clone_generate = generate_kvmimg()
app_targets_hosts_start = start_kvm()
app_kvm_hosts_start_site = standard_kvm()
app_kvm_hosts_start_print = last()
def kvm_create():
    print_first_message = last()
    print_first_message.print_start_kvm_message()
    print_kvm_message = common(MESSAGE_INFO['start_kvm_site'])
    print_kvm_message.message_blue()
    print_kvm_message = common(MESSAGE_INFO['app_clone'])
    print_kvm_message.message_green()
    app_clone_generate.clone_cevs74()
    print_kvm_message = common(MESSAGE_INFO['site_image_ip'])
    print_kvm_message.message_green()
    app_clone_generate.site_imgip()
    if KVM_CPU != '2' and KVM_MEM != '4194304':
        print_kvm_message = common(MESSAGE_INFO['site_imgxml_cpu'])
        print_kvm_message.message_green()
        app_clone_generate.site_imgxml_cpu()
        print_kvm_message = common(MESSAGE_INFO['site_imgxml_mem'])
        print_kvm_message.message_green()
        app_clone_generate.site_imgxml_mem()
    if KVM_CPU != '2' and KVM_MEM == '4194304':
        print_kvm_message = common(MESSAGE_INFO['site_imgxml_cpu'])
        print_kvm_message.message_green()
        app_clone_generate.site_imgxml_cpu()
    if KVM_MEM != '4194304' and KVM_CPU == '2':
        print_kvm_message = common(MESSAGE_INFO['site_imgxml_mem'])
        print_kvm_message.message_green()
        app_clone_generate.site_imgxml_mem()
    print_kvm_message = common(MESSAGE_INFO['scp_cloned_img'])
    print_kvm_message.message_green()
    app_clone_generate.scp_cloned_img()
    print_kvm_message = common(MESSAGE_INFO['scp_cloned_xml'])
    print_kvm_message.message_green()
    app_clone_generate.scp_cloned_xml()
def targets_hosts_start_kvm():
    print_kvm_message = common(MESSAGE_INFO['define_kvm'])
    print_kvm_message.message_green()
    app_targets_hosts_start.define_kvm()
    print_kvm_message = common(MESSAGE_INFO['start_kvm'])
    print_kvm_message.message_green()
    app_targets_hosts_start.start_kvm()
    if KVM_DISK is not '':
        print_kvm_message = common(MESSAGE_INFO['extend_disk'])
        print_kvm_message.message_green()
        app_targets_hosts_start.extend_disk()
    print_kvm_message = common(MESSAGE_INFO['wait_sleep'])
    print_kvm_message.message_green()
    time_sleep_wait_start = command(60)
    time_sleep_wait_start.wait_sleep()
def kvm_virture_hosts_site():
    print_kvm_message = common(MESSAGE_INFO['site_kvm_hosts'])
    print_kvm_message.message_green()
    app_kvm_hosts_start_site.site_kvm_hosts()
    print_kvm_message = common(MESSAGE_INFO['install_kvm_adminsetd'])
    print_kvm_message.message_green()
    app_kvm_hosts_start_site.install_kvm_adminsetd()
    app_kvm_hosts_start_site.install_node_exporter()
def kvm_tirture_hosts_site_passwd():
    print_kvm_message = common(MESSAGE_INFO['set_kvm_passwd'])
    print_kvm_message.message_green()
    app_kvm_hosts_start_site.set_kvm_passwd()
def kvm_virture_hosts_print():
    app_kvm_hosts_start_print.print_kvm_message()

appcreate.py 用于定义应用程序的部署流程

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from config.messages import MESSAGE_INFO
from local.localall import common
from kernel.yum import yum_install_packages, yum_install_jdk
from kernel.nginx import nginx_kvm
from kernel.php import php_kvm
from kernel.redis import redis_kvm
from kernel.zabbix_agent import zabbix_agent_kvm
from kernel.mysqlxx import  mysqlxx_kvm
from kernel.mysqlxxxx import mysqlxxxx_kvm
from config import APP

nginx_install = nginx_kvm()
php_install = php_kvm()
redis_install = redis_kvm()
zabbix_agent_install = zabbix_agent_kvm()
mysqlxx_install = mysqlxx_kvm()
mysqlxxxx_install = mysqlxxxx_kvm()
def run_yum_install():
    yum_install_packages()
def run_install_nginx():
    print_application_message = common(MESSAGE_INFO['nginx_copy'])
    print_application_message.message_green()
    nginx_install.copy_source()

    print_application_message = common(MESSAGE_INFO['copy_nginx_install_shell'])
    print_application_message.message_green()
    nginx_install.copy_nginx_install_shell()

    print_application_message = common(MESSAGE_INFO['create_nginx_log_dir'])
    print_application_message.message_green()
    nginx_install.create_nginx_log_dir()

    print_application_message = common(MESSAGE_INFO['create_nginx_group_and_user'])
    print_application_message.message_green()
    nginx_install.create_nginx_group_and_user()

    print_application_message = common(MESSAGE_INFO['install_nginx'])
    print_application_message.message_green()
    nginx_install.install_nginx()

    print_application_message = common(MESSAGE_INFO['create_nginx_vhosts_dir'])
    print_application_message.message_green()
    nginx_install.create_nginx_vhosts_dir()

    print_application_message = common(MESSAGE_INFO['copy_main_template_config_file'])
    print_application_message.message_green()
    nginx_install.copy_main_template_config_file()

    print_application_message = common(MESSAGE_INFO['copy_vhost_template_config_file'])
    print_application_message.message_green()
    nginx_install.copy_vhost_template_config_file()

    print_application_message = common(MESSAGE_INFO['copy_service_config_file'])
    print_application_message.message_green()
    nginx_install.copy_service_config_file()

    print_application_message = common(MESSAGE_INFO['start_nginx'])
    print_application_message.message_green()
    nginx_install.start_nginx()

    print_application_message = common(MESSAGE_INFO['nginx_clean_local_tmp_cache_file'])
    print_application_message.message_green()
    nginx_install.clean_local_tmp_cache_file()

def run_install_php():
    print_application_message = common(MESSAGE_INFO['php_copy'])
    print_application_message.message_green()
    php_install.cp_source()

    print_application_message = common(MESSAGE_INFO['cp_php_install_shell'])
    print_application_message.message_green()
    php_install.cp_php_install_shell()

    print_application_message = common(MESSAGE_INFO['create_php_group_and_user'])
    print_application_message.message_green()
    php_install.create_php_group_and_user()

    print_application_message = common(MESSAGE_INFO['create_php_log_dir'])
    print_application_message.message_green()
    php_install.create_php_log_dir()


    print_application_message = common(MESSAGE_INFO['create_php_error_log_file'])
    print_application_message.message_green()
    php_install.create_php_error_log_file()

    print_application_message = common(MESSAGE_INFO['install_php'])
    print_application_message.message_green()
    php_install.install_php()

    print_application_message = common(MESSAGE_INFO['cp_main_phpini_config'])
    print_application_message.message_green()
    php_install.cp_main_phpini_config()

    print_application_message = common(MESSAGE_INFO['cp_php_www_conf_config'])
    print_application_message.message_green()
    php_install.cp_php_www_conf_config()

    print_application_message = common(MESSAGE_INFO['cp_php_php_fpm_config'])
    print_application_message.message_green()
    php_install.cp_php_php_fpm_config()

    print_application_message = common(MESSAGE_INFO['cp_php_php_fpm_service'])
    print_application_message.message_green()
    php_install.cp_php_php_fpm_service()

    print_application_message = common(MESSAGE_INFO['start_php'])
    print_application_message.message_green()
    php_install.start_php()

    print_application_message = common(MESSAGE_INFO['php_clean_local_tmp_cache_files'])
    print_application_message.message_green()
    php_install.clean_local_tmp_cache_files()

def run_install_redis():
    print_application_message = common(MESSAGE_INFO['redis_copy'])
    print_application_message.message_green()
    redis_install.cp_source()

    print_application_message = common(MESSAGE_INFO['cp_redis_install_shell'])
    print_application_message.message_green()
    redis_install.cp_redis_install_shell()

    print_application_message = common(MESSAGE_INFO['create_redis_group_and_user'])
    print_application_message.message_green()
    redis_install.create_redis_group_and_user()

    print_application_message = common(MESSAGE_INFO['install_redis'])
    print_application_message.message_green()
    redis_install.install_redis()

    print_application_message = common(MESSAGE_INFO['create_redis_log_file'])
    print_application_message.message_green()
    redis_install.create_redis_log_file()

    print_application_message = common(MESSAGE_INFO['crate_redis_data_dir'])
    print_application_message.message_green()
    redis_install.crate_redis_data_dir()

    print_application_message = common(MESSAGE_INFO['copy_redis_config'])
    print_application_message.message_green()
    redis_install.copy_redis_config()

    print_application_message = common(MESSAGE_INFO['copy_redis_service_config'])
    print_application_message.message_green()
    redis_install.copy_redis_service_config()

    print_application_message = common(MESSAGE_INFO['redis_clean_local_tmp_cache_file'])
    print_application_message.message_green()
    redis_install.clean_local_tmp_cache_file()

def run_install_jdk():
    print_application_message = common(MESSAGE_INFO['yum_install_jdk'])
    print_application_message.message_green()
    yum_install_jdk()

def run_install_zabbix_agent():
    print_application_message = common(MESSAGE_INFO['zabbix_copy'])
    print_application_message.message_green()
    zabbix_agent_install.cp_source()

    print_application_message = common(MESSAGE_INFO['cp_zabbix_agent_install_shell'])
    print_application_message.message_green()
    zabbix_agent_install.cp_zabbix_agent_install_shell()

    print_application_message = common(MESSAGE_INFO['create_zabbix_group_and_user'])
    print_application_message.message_green()
    zabbix_agent_install.create_zabbix_group_and_user()

    print_application_message = common(MESSAGE_INFO['create_zabbix_agent_log_dir'])
    print_application_message.message_green()
    zabbix_agent_install.create_zabbix_agent_log_dir()

    print_application_message = common(MESSAGE_INFO['install_zabbix_agent'])
    print_application_message.message_green()
    zabbix_agent_install.install_zabbix_agent()

    print_application_message = common(MESSAGE_INFO['copy_zabbix_agent_config_file'])
    print_application_message.message_green()
    zabbix_agent_install.copy_zabbix_agent_config_file()

    print_application_message = common(MESSAGE_INFO['copy_zabbix_agent_init_file'])
    print_application_message.message_green()
    zabbix_agent_install.copy_zabbix_agent_init_file()

    print_application_message = common(MESSAGE_INFO['zabbix_clean_local_tmp_cache_file'])
    print_application_message.message_green()
    zabbix_agent_install.clean_local_tmp_cache_file()

    print_application_message = common(MESSAGE_INFO['add_zabbix_agent_init'])
    print_application_message.message_green()
    zabbix_agent_install.add_zabbix_agent_init()

    print_application_message = common(MESSAGE_INFO['start_zabbix_agentd'])
    print_application_message.message_green()
    zabbix_agent_install.start_zabbix_agentd()



def run_install_mysqlxx():
    print_application_message = common(MESSAGE_INFO['deletexx_old_conf'])
    print_application_message.message_green()
    mysqlxx_install.delete_old_conf()

    print_application_message = common(MESSAGE_INFO['deletexx_old_conf_dir'])
    print_application_message.message_green()
    mysqlxx_install.delete_old_conf_dir()

    print_application_message = common(MESSAGE_INFO['mysqlxx_source'])
    print_application_message.message_green()
    mysqlxx_install.cp_source()

    print_application_message = common(MESSAGE_INFO['cp_mysqlxx_conf'])
    print_application_message.message_green()
    mysqlxx_install.cp_mysql_conf()

    print_application_message = common(MESSAGE_INFO['copy_mysqlxx_install_sh'])
    print_application_message.message_green()
    mysqlxx_install.copy_mysql_install_sh()

    print_application_message = common(MESSAGE_INFO['add_mysqlxx_user_and_group'])
    print_application_message.message_green()
    mysqlxx_install.add_mysql_user_and_group()

    print_application_message = common(MESSAGE_INFO['createxx_related_directories'])
    print_application_message.message_green()
    mysqlxx_install.create_related_directories()

    print_application_message = common(MESSAGE_INFO['createxx_slowlog_file'])
    print_application_message.message_green()
    mysqlxx_install.create_slowlog_file()

    print_application_message = common(MESSAGE_INFO['install_mysqlxx'])
    print_application_message.message_green()
    mysqlxx_install.install_mysql()

    print_application_message = common(MESSAGE_INFO['add_mysqlxx_to_PATH'])
    print_application_message.message_green()
    mysqlxx_install.add_mysql_to_PATH()


    print_application_message = common(MESSAGE_INFO['copy_mysqlxx_service_file'])
    print_application_message.message_green()
    mysqlxx_install.copy_mysql_service_file()

    print_application_message = common(MESSAGE_INFO['cleanxx_local_tmp_cache_file'])
    print_application_message.message_green()
    mysqlxx_install.clean_local_tmp_cache_file()

    print_application_message = common(MESSAGE_INFO['start_mysqlxx_and_enable_it_onboot'])
    print_application_message.message_green()
    mysqlxx_install.start_mysql_and_enable_it_onboot()

def run_install_mysqlxx13():
    print_application_message = common(MESSAGE_INFO['deletexx13_old_conf'])
    print_application_message.message_green()
    mysqlxx13_install.delete_old_conf()

    print_application_message = common(MESSAGE_INFO['deletexx13_old_conf_dir'])
    print_application_message.message_green()
    mysqlxx13_install.delete_old_conf_dir()

    print_application_message = common(MESSAGE_INFO['mysqlxx13_source'])
    print_application_message.message_green()
    mysqlxx13_install.cp_source()

    print_application_message = common(MESSAGE_INFO['cp_mysqlxx13_conf'])
    print_application_message.message_green()
    mysqlxx13_install.cp_mysql_conf()

    print_application_message = common(MESSAGE_INFO['copy_mysqlxx13_install_sh'])
    print_application_message.message_green()
    mysqlxx13_install.copy_mysql_install_sh()

    print_application_message = common(MESSAGE_INFO['add_mysqlxx13_user_and_group'])
    print_application_message.message_green()
    mysqlxx13_install.add_mysql_user_and_group()

    print_application_message = common(MESSAGE_INFO['createxx13_related_directories'])
    print_application_message.message_green()
    mysqlxx13_install.create_related_directories()

    print_application_message = common(MESSAGE_INFO['createxx13_slowlog_file'])
    print_application_message.message_green()
    mysqlxx13_install.create_slowlog_file()

    print_application_message = common(MESSAGE_INFO['install_mysqlxx13'])
    print_application_message.message_green()
    mysqlxx13_install.install_mysql()

    print_application_message = common(MESSAGE_INFO['add_mysqlxx13_to_PATH'])
    print_application_message.message_green()
    mysqlxx13_install.add_mysql_to_PATH()

    print_application_message = common(MESSAGE_INFO['copy_mysqlxx13_service_file'])
    print_application_message.message_green()
    mysqlxx13_install.copy_mysql_service_file()

    print_application_message = common(MESSAGE_INFO['cleanxx13_local_tmp_cache_file'])
    print_application_message.message_green()
    mysqlxx13_install.clean_local_tmp_cache_file()

    print_application_message = common(MESSAGE_INFO['start_mysqlxx13_and_enable_it_onboot'])
    print_application_message.message_green()
    mysqlxx13_install.start_mysql_and_enable_it_onboot()

def run_install_all_application_package():
    if len(APP) != (0):
        print_application_message = common(MESSAGE_INFO['start_yum_tools_install'])
        print_application_message.message_blue()
        run_yum_install()

    if 'nginx' in APP:
        print_application_message = common(MESSAGE_INFO['start_nginx_install'])
        print_application_message.message_blue()
        run_install_nginx()

    if 'php' in APP:
        print_application_message = common(MESSAGE_INFO['start_php_install'])
        print_application_message.message_blue()
        run_install_php()

    if 'redis' in APP:
        print_application_message = common(MESSAGE_INFO['start_redis_install'])
        print_application_message.message_blue()
        run_install_redis()

    if 'jdk' in APP:
        print_application_message = common(MESSAGE_INFO['start_jdk_install'])
        print_application_message.message_blue()
        run_install_jdk()

    if 'zabbix_agent' in APP:
        print_application_message = common(MESSAGE_INFO['start_zabbix_agentd_install'])
        print_application_message.message_blue()
        run_install_zabbix_agent()

    if 'mysql_xx' in APP:
        print_application_message = common(MESSAGE_INFO['start_mysqlxx_install'])
        print_application_message.message_blue()
        run_install_mysqlxx()

    if 'mysql_xx13' in APP:
        print_application_message = common(MESSAGE_INFO['start_mysqlxx13_install'])
        print_application_message.message_blue()
        run_install_mysqlxx13()

定义入口文件

kvm.py 是所有程序执行的入口文件

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from prettytable import PrettyTable
from config.hosts import SOURCES_HOSTS,TARGETS_HOSTS,KVM_HOSTS
from fabric.contrib.files import *
from application.kvmcreate import *
from application.appcreate import *
@hosts(SOURCES_HOSTS)
def kvm_sources_clone_scp():
    kvm_create()
@hosts(TARGETS_HOSTS)
def kvm_targets_site():
    targets_hosts_start_kvm()
@hosts(KVM_HOSTS)
def kvm_virture_host_start_site():
    kvm_virture_hosts_site()
    run_install_all_application_package()
    kvm_tirture_hosts_site_passwd()
    kvm_virture_hosts_print()
@task(name='install')
def run_all():
    execute(kvm_sources_clone_scp)
    execute(kvm_targets_site)
    execute(kvm_virture_host_start_site)
@task(name='help')
def deploy_help():
    print green("KVM产品及CentOS7应用部署")
    print yellow("    1.配置文件")
    helper = PrettyTable()
    helper.field_names = ["配置","说明"]
    helper.align = "l"
    helper.add_row(["config/__init__.py", "配置文件开关"])
    helper.add_row(["config/test/kvm_config.py", "测试环境部署配置"])
    helper.add_row(["config/prod/kvm_config.py", "生产环境部署配置"])
    print helper
    print red("    2.指令说明")
    helper = PrettyTable()
    helper.field_names = ["指令","说明"]
    helper.align = "l"
    helper.add_row(["fab -f kvm.py -l", "列出部署产品"])
    helper.add_row(["fab -f kvm.py install", "安装kvm,并整合配置的应用至kvm新虚拟服务器"])
    print helper
    print blue("    3.作者信息")
    helper = PrettyTable()
    helper.field_names = ["作者","信息"]
    helper.add_row(["姓名", "任锦"])
    helper.add_row(["手机号", "13161389224"])
    helper.add_row(["博客地址", "www.ssjinyao.com"])
    print helper

服务器部署完此代码后,定义别名调用kvm.py实现快速开通

# alias kc1='source  /root/renjin/kvm_pyenv/bin/activate && vim /root/renjin/kvm_create/config/__init__.py'
# alias kc2p='vim +33 /root/renjin/kvm_create/config/prod/kvm_config.py'
# alias kc2t='vim +33 /root/renjin/kvm_create/config/test/kvm_config.py'
# alias kc3='cd /root/renjin/kvm_create/ && fab -f kvm.py install'
# alias kcok1='vim /root/renjin/kvm_create/config/__init__.py'
# alias kcok2p='vim +33 /root/renjin/kvm_create/config/prod/kvm_config.py'
# alias kcok2t='vim +33 /root/renjin/kvm_create/config/test/kvm_config.py'

自动化快速开启开通kvm虚拟化服务器

****************************************************************************************************
**********  KVM ENV Is: test                                                              **********
**********  KVM Host Name Is: xx-xxxx-xx-xxx-xxxx-xxxx                                   **********
**********  KVM IP Is: xxx.xx.xx.xx                                                       **********
**********  TARGET IP Is: xxx.xx.xxx.xx                                                   **********
**********  KVM Gateway Is: xxx.xx.x.x                                                    **********
**********  KVM Netmask Is: xxx.xxx.xxx.x                                                 **********
**********  KVM APP Is: zabbix_agent nginx mysql_xx                                       **********
**********  KVM CPU Is: 2                                                                 **********
**********  KVM Memory Is: 4194304                                                        **********
**********  KVM Disk Is: 100G                                                             **********
**********  KVM Passwd IS: xxxxxxxXXXX                                                    **********
****************************************************************************************************

If the information is incorrect, please input no:

开通kvm虚似服务器已经完成,信息如下

****************************************************************************************************
**********  KVM ENV Is: test                                                              **********
**********  KVM Host Name Is: bj-xxxx-xx-xxx-xxxx-xxxxx                                   **********
**********  KVM IP Is: xxx.xx.xx.xx                                                       **********
**********  TARGET IP Is: xxx.xx.xxx.xx                                                   **********
**********  KVM Gateway Is: xxx.xx.xx.x                                                   **********
**********  KVM Netmask Is: xxx.xxx.xxx.0                                                 **********
**********  KVM APP Is:  zabbix_agent nginx mysql_xx                                      **********
**********  KVM CPU Is: 2                                                                 **********
**********  KVM Memory Is: 4194304                                                        **********
**********  KVM Disk Is: 100G                                                             **********
**********  KVM Passwd IS: XXXXXXXX                                                       **********
****************************************************************************************************


Done.
Disconnecting from xxx.xx.xx.xx... done.
Disconnecting from xxx.xx.xx.xx... done.
Disconnecting from xxx.xx.xx.xx... done.

补充基于api实现

补充基于python libguestfs api 方法实现

import libguestfs

# 定义虚拟机磁盘文件路径
disk_image = '/var/lib/libvirt/images/myvm.qcow2'

# 创建libguestfs对象
g = libguestfs.GuestFS(python_return_dict=True)

# 打开虚拟机磁盘
g.add_drive_opts(disk_image, format='qcow2')
g.launch()

# 挂载磁盘
g.mount('/dev/sda', '/')

# 将ifcfg-eth0配置文件写入磁盘
ifcfg_eth0 = "DEVICE=eth0\nBOOTPROTO=static\nIPADDR=192.168.1.100\nNETMASK=255.255.255.0\nONBOOT=yes\n"
g.write('/etc/sysconfig/network-scripts/ifcfg-eth0', ifcfg_eth0)

# 卸载磁盘并关闭
g.umount_all()
g.close()

评论