basic.py 2.9 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
class BuildDockerCluser:

    def __init__(self, hostName, user, password, configDir, numOfNodes, clusterVersion):
        self.hostName = hostName
        self.user = user
        self.password = password
        self.configDir = configDir
        self.numOfNodes = numOfNodes
        self.clusterVersion = clusterVersion        

    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")
44 45

    def run(self):
46 47 48 49 50 51 52
        if self.numOfNodes < 2 or self.numOfNodes > 5:
            print("the number of nodes must be between 2 and 5")
            exit(0)        
        os.system("./buildClusterEnv.sh -n %d -v %s" % (self.numOfNodes, self.clusterVersion))
        self.getConnection()
        self.createDondes()
        self.startArbitrator() 
53

54 55 56
parser = argparse.ArgumentParser()
parser.add_argument(
    '-H',
57
    '--host',
58
    action='store',
59
    default='tdnode1',
60
    type=str,
61
    help='host name to be connected (default: tdnode1)')
62
parser.add_argument(
63 64
    '-u',
    '--user',
65
    action='store',
66
    default='root',
67
    type=str,
68
    help='user (default: root)')
69 70
parser.add_argument(
    '-p',
71
    '--password',
72
    action='store',
73
    default='taosdata',
74
    type=str,
75
    help='password (default: taosdata)')
76
parser.add_argument(
77 78
    '-c',
    '--config-dir',
79
    action='store',
80
    default='/etc/taos',
81
    type=str,
82
    help='configuration directory (default: /etc/taos)')
83 84
parser.add_argument(
    '-n',
85
    '--num-of-nodes',
86 87 88
    action='store',
    default=2,
    type=int,
89
    help='number of nodes in the cluster (default: 2, min: 2, max: 5)')
90
parser.add_argument(
91 92
    '-v',
    '--version',
93
    action='store',
94 95 96
    default='2.0.14.1',
    type=str,
    help='the version of the cluster to be build, Default is 2.0.14.1')
97 98

args = parser.parse_args()
99 100
cluster = BuildDockerCluser(args.host, args.user, args.password, args.config_dir, args.num_of_nodes, args.version)
cluster.run()