• python安装mongodb


    提前把mongodb-linux-x86_64-rhel70-3.2.4.tgz放到和脚本相同目录下,然后把下复制到脚本里面,开始执行
    
    #!/usr/bin/python
    #-*- codinig: UTF-8 -*-
    from __future__ import print_function
    import os
    import shutil
    import tarfile
    import subprocess
    
    os.chdir('/root')
    
    def execute_cmd(cmd):
        p=subprocess.Popen(cmd,
                            shell=True,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
        stdout,stdeer=p.communicate()
        if p.returncode!=0:
            return p.returncode,stdeer
        return p.returncode,stdout
    
    def unpackage_monge(package,package_dir):
        unpackage_dir=os.path.splitext(package)[0]
        if os.path.exists(unpackage_dir):
            shutil.rmtree(unpackage_dir)
        if os.path.exists(package_dir):
            shutil.rmtree(package_dir)
        t=tarfile.open(package,'r:gz')
        t.extractall('.')
        shutil.move(unpackage_dir,package_dir)
    
    def create_datadir(data_dir):
        if os.path.exists(data_dir):
            shutil.rmtree(data_dir)
        os.mkdir(data_dir)
    
    def format_mongod_mommand(package_dir,data_dir,logfile):
        mongod=os.path.join(package_dir,'bin','mongod')
        mongod_format="""{0} --fork --dbpath {1} --logpath {2}"""
        return mongod_format.format(mongod,data_dir,logfile)
    def start_mongod(cmd):
        returncode, out = execute_cmd(cmd)
        if returncode !=0:
            raise SystemExit('excete {0} error :{1}'.format(cmd,out))
        else:
            print('execute command {0} seccessful'.format(cmd))
    def main():
        package='mongodb-linux-x86_64-rhel70-3.2.4.tgz'
        cur_dir=os.path.abspath('.')
        package_dir=os.path.join(cur_dir,'mongo')
        data_dir=os.path.join(cur_dir,'mongodata')
        logfile=os.path.join(data_dir,'mongod.log')
        if not os.path.exists(package):
            raise SystemExit('{0} not found'.format(package))
        unpackage_monge(package,package_dir)
        create_datadir(data_dir)
        start_mongod(format_mongod_mommand(package_dir,data_dir,logfile))
    if __name__ == '__main__':
        main()
  • 相关阅读:
    [笔记]一道C语言面试题:IPv4字符串转为UInt整数
    linux内核代码注释 赵炯 第三章引导启动程序
    bcd码
    2章 perl标量变量
    1章 perl入门
    perl第三章 列表和数组
    浮动 float
    文字与图像
    3.深入理解盒子模型
    4.盒子的浮动和定位
  • 原文地址:https://www.cnblogs.com/effortsing/p/10281318.html
Copyright © 2020-2023  润新知