basic.py 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
###################################################################
#           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
15
import taos
16
import random
17
import argparse
18

19 20
class BuildDockerCluser:

P
Ping Xiao 已提交
21
    def __init__(self, hostName, user, password, configDir, numOfNodes, clusterVersion, dockerDir, removeFlag):
22 23 24 25 26
        self.hostName = hostName
        self.user = user
        self.password = password
        self.configDir = configDir
        self.numOfNodes = numOfNodes
P
Ping Xiao 已提交
27 28 29
        self.clusterVersion = clusterVersion
        self.dockerDir = dockerDir
        self.removeFlag = removeFlag
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    def getConnection(self):
        self.conn = taos.connect(
            host = self.hostName,
            user = self.user,
            password = self.password,
            config = self.configDir)

    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):
        print("start arbitrator")
        os.system("docker exec -d $(docker ps|grep tdnode1|awk '{print $1}') tarbitrator")
46

P
Ping Xiao 已提交
47
    def run(self):        
48 49
        if self.numOfNodes < 2 or self.numOfNodes > 10:
            print("the number of nodes must be between 2 and 10")
P
Ping Xiao 已提交
50 51 52 53 54
            exit(0)   
        print("remove Flag value %s" % self.removeFlag)     
        if self.removeFlag == False:
            os.system("./cleanClusterEnv.sh -d %s" % self.dockerDir)
        os.system("./buildClusterEnv.sh -n %d -v %s -d %s" % (self.numOfNodes, self.clusterVersion, self.dockerDir))
55 56
        self.getConnection()
        self.createDondes()
P
Ping Xiao 已提交
57
        self.startArbitrator()
58

59 60 61
parser = argparse.ArgumentParser()
parser.add_argument(
    '-H',
62
    '--host',
63
    action='store',
64
    default='tdnode1',
65
    type=str,
66
    help='host name to be connected (default: tdnode1)')
67
parser.add_argument(
68 69
    '-u',
    '--user',
70
    action='store',
71
    default='root',
72
    type=str,
73
    help='user (default: root)')
74 75
parser.add_argument(
    '-p',
76
    '--password',
77
    action='store',
78
    default='taosdata',
79
    type=str,
80
    help='password (default: taosdata)')
81
parser.add_argument(
82 83
    '-c',
    '--config-dir',
84
    action='store',
85
    default='/etc/taos',
86
    type=str,
87
    help='configuration directory (default: /etc/taos)')
88 89
parser.add_argument(
    '-n',
90
    '--num-of-nodes',
91 92 93
    action='store',
    default=2,
    type=int,
94
    help='number of nodes in the cluster (default: 2, min: 2, max: 5)')
95
parser.add_argument(
96 97
    '-v',
    '--version',
98
    action='store',
P
Ping Xiao 已提交
99
    default='2.0.17.1',
100
    type=str,
P
Ping Xiao 已提交
101 102 103 104 105 106 107 108 109 110 111 112
    help='the version of the cluster to be build, Default is 2.0.17.1')
parser.add_argument(
    '-d',
    '--docker-dir',
    action='store',
    default='/data',
    type=str,
    help='the data dir for docker, default is /data')
parser.add_argument(
    '--flag',
    action='store_true',        
    help='remove docker containers flag, default: True')
113 114

args = parser.parse_args()
P
Ping Xiao 已提交
115 116 117 118 119
cluster = BuildDockerCluser(args.host, args.user, args.password, args.config_dir, args.num_of_nodes, args.version, args.docker_dir, args.flag)
cluster.run()

# usage 1: python3 basic.py -n 2 --flag   (flag is True)
# usage 2: python3 basic.py -n 2 (flag should be False when it is not specified)