basic.py 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import os
import taos

class BuildDockerCluser:

    def init(self, numOfNodes, dockerDir):
        self.numOfNodes = numOfNodes
        self.dockerDir = dockerDir

        self.hostName = "tdnode1"
        self.user = "root"
        self.password = "taosdata"
        self.configDir = "/etc/taos"
        self.dirs = ["data", "cfg", "log", "core"]
        self.cfgDict = {
            "numOfLogLines":"100000000",
            "mnodeEqualVnodeNum":"0",
            "walLevel":"1",                                 
            "numOfThreadsPerCore":"2.0",
            "monitor":"0",
            "vnodeBak":"1",
            "dDebugFlag":"135",
            "mDebugFlag":"135",
            "sdbDebugFlag":"135",
            "rpcDebugFlag":"135",
            "tmrDebugFlag":"131",
            "cDebugFlag":"135",
            "httpDebugFlag":"135",
            "monitorDebugFlag":"135",
            "udebugFlag":"135",
            "jnidebugFlag":"135",
            "qdebugFlag":"135",
            "maxSQLLength":"1048576"
        }
        cmd = "mkdir -p %s" % self.dockerDir
        self.execCmd(cmd)

        cmd = "cp *.yml %s" % self.dockerDir
        self.execCmd(cmd)

        cmd = "cp Dockerfile %s" % self.dockerDir
        self.execCmd(cmd)


    # execute command, and return the output 
    # ref: https://blog.csdn.net/wowocpp/article/details/80775650  
    def execCmdAndGetOutput(self, cmd):  
        r = os.popen(cmd)  
        text = r.read()  
        r.close()  
        return text
    
    def execCmd(self, cmd):
        if os.system(cmd) != 0:
            quit()

    def getTaosdVersion(self):
        cmd = "taosd -V |grep version|awk '{print $3}'"
        taosdVersion = self.execCmdAndGetOutput(cmd)
        cmd = "find %s -name '*server*.tar.gz' | awk -F- '{print $(NF-2)}'|sort|awk 'END {print}'" % self.dockerDir
        packageVersion = self.execCmdAndGetOutput(cmd)

        if (taosdVersion is None or taosdVersion.isspace()) and (packageVersion is None or packageVersion.isspace()):
            print("Please install taosd or have a install package ready")
            quit()
        else:
            self.version = taosdVersion  if taosdVersion >= packageVersion else packageVersion
            return self.version.strip()

    def getConnection(self):
        self.conn = taos.connect(
            host = self.hostName,
            user = self.user,
            password = self.password,
            config = self.configDir)
    
    def removeFile(self, rootDir, index, dir):
        cmd = "rm -rf %s/node%d/%s/*" % (rootDir, index, dir)        
        self.execCmd(cmd)
    
    def clearEnv(self):
        cmd = "cd %s && docker-compose down --remove-orphans" % self.dockerDir
        self.execCmd(cmd)
        for i in range(1, self.numOfNodes + 1):            
            self.removeFile(self.dockerDir, i, self.dirs[0])
            self.removeFile(self.dockerDir, i, self.dirs[1])
            self.removeFile(self.dockerDir, i, self.dirs[2])

    def createDir(self, rootDir, index, dir):
        cmd = "mkdir -p %s/node%d/%s" % (rootDir, index, dir)
        self.execCmd(cmd)

    def createDirs(self):
        for i in range(1, self.numOfNodes + 1):
            for j in range(len(self.dirs)):
                self.createDir(self.dockerDir, i, self.dirs[j])

    def addExtraCfg(self, option, value):
        self.cfgDict.update({option: value})

    def cfg(self, option, value, nodeIndex):
        cfgPath = "%s/node%d/cfg/taos.cfg" % (self.dockerDir, nodeIndex)
        cmd = "echo '%s %s' >> %s" % (option, value, cfgPath)
        self.execCmd(cmd)
    
    def updateLocalhosts(self):
        cmd = "grep '172.27.0.7 *tdnode1' /etc/hosts | sed 's: ::g'"
        result = self.execCmdAndGetOutput(cmd)
        print(result)
        if result is None or result.isspace():
            print("==========")
            cmd = "echo '172.27.0.7 tdnode1' >> /etc/hosts"
            display = "echo %s" % cmd
            self.execCmd(display)
            self.execCmd(cmd)
    
    def deploy(self):
        self.clearEnv()
        self.createDirs()
        for i in range(1, self.numOfNodes + 1):
            self.cfg("firstEp", "tdnode1:6030", i)

            for key, value in self.cfgDict.items():
                self.cfg(key, value, i)

    def createDondes(self):
        self.cursor = self.conn.cursor()        
        for i in range(2, self.numOfNodes + 1):            
            self.cursor.execute("create dnode tdnode%d" % i)
    
    def startArbitrator(self):
        for i in range(1, self.numOfNodes + 1):
            self.cfg("arbitrator", "tdnode1:6042", i)
        cmd = "docker exec -d $(docker ps|grep tdnode1|awk '{print $1}') tarbitrator"
        self.execCmd(cmd)

    def prepardBuild(self):
        if self.numOfNodes < 2 or self.numOfNodes > 10:
            print("the number of nodes must be between 2 and 10")
            exit(0)
        self.updateLocalhosts()
        self.deploy()

    def run(self):           
        cmd = "./buildClusterEnv.sh -n %d -v %s -d %s" % (self.numOfNodes, self.getTaosdVersion(), self.dockerDir)
        display = "echo %s" % cmd
        self.execCmd(display)
        self.execCmd(cmd)
        self.getConnection()
        self.createDondes()

cluster = BuildDockerCluser()