Esempio n. 1
0
def you_are_information_server(argv):
    '''
	dest = master only
	start up database service, create table
	'''
    conn = argv[0][0]
    s = argv[0][1]
    #get parameter
    #rootpassword=argv[1]
    #dbname=argv[2]

    dbController.setMasterConfig(
    )  #very important for make it can be connected via remote host
    dbController.stop()
    dbController.start(setting.DB_ROOT_PASSWORD, conn, s)
    dbController.createDB(setting.DB_ROOT_PASSWORD, setting.DB_NAME)

    print dbController.createInitialTables('root', setting.DB_ROOT_PASSWORD,
                                           setting.DB_NAME)

    #This line allow robot user to connect to database
    dbController.createUser(setting.DB_ROOT_PASSWORD, setting.DB_USERNAME,
                            setting.DB_PASSWORD)

    return 'OK'
Esempio n. 2
0
def wake_up_database(argv):
    '''
	check that this is real database or not (and start it if it is real)
	'''
    conn = argv[0][0]
    s = argv[0][1]

    myMAC = network.getMyMACAddr()
    dbMAC = cacheFile.getDatabaseMAC()
    if str(dbMAC) != str(myMAC):
        return str(dbMAC)  #recomendation

    dbController.start(setting.DB_ROOT_PASSWORD, conn, s)

    try:
        db = MySQLdb.connect("localhost", "root", setting.DB_ROOT_PASSWORD,
                             setting.DB_NAME)
        cursor = db.cursor()

        cursor.execute(
            "SELECT `MACAddress` FROM `hosts` WHERE `isInformationServer`=1")
        ans = cursor.fetchone()[0]
        if ans == str(myMAC):
            #yes this is real
            return "OK"
        else:
            return ans  #recomendation
    except:
        dbController.stop()
        return "Sorry, this is not a real database"
Esempio n. 3
0
def you_are_slave_db(argv):
    '''
	dest = slave only
	'''
    conn = argv[0][0]
    s = argv[0][1]

    masterHost = argv[1]
    fileData = argv[2]
    positionData = argv[3]

    dumpString = argv[4]
    dumpFile = open(setting.DB_DUMP_FILE, 'w')
    dumpFile.write(dumpString)
    dumpFile.close()

    dbController.setSlaveConfig(
        str(masterHost
            ))  #very important for make it can be connected via remote host
    dbController.stop()
    dbController.start(setting.DB_ROOT_PASSWORD, conn, s)
    dbController.createDB(setting.DB_ROOT_PASSWORD, setting.DB_NAME,
                          True)  #drop and create new database
    print "before runSQL"
    dumpResult = dbController.runSQL(setting.DB_DUMP_FILE, "root",
                                     setting.DB_ROOT_PASSWORD, setting.DB_NAME,
                                     "localhost")
    if dumpResult == False:
        return "runSQL error, please check .sql file"

    db = MySQLdb.connect("localhost", "root", setting.DB_ROOT_PASSWORD)
    cursor = db.cursor()
    cursor.execute("SLAVE STOP;")
    cursor.execute(
        "CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='******', MASTER_PASSWORD='******', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;"
        % (str(masterHost), setting.DB_USERNAME, setting.DB_PASSWORD, fileData,
           positionData))
    print "before slave start"
    cursor.execute("SLAVE START;")

    db.close()

    print "after slave start"

    os.remove(setting.DB_DUMP_FILE)

    return 'OK'
Esempio n. 4
0
def you_are_db_migration_destination(argv):
    '''
	migrate from source to dest of socketCall
	'''
    conn = argv[0][0]
    s = argv[0][1]

    #targetFile=setting.DB_NFS_DUMP_FILE

    targetFile = setting.DB_DUMP_TEMP_FILE
    dumpString = argv[1]
    dumpFile = open(targetFile, 'w')
    dumpFile.write(dumpString)
    dumpFile.close()

    myIP = network.getMyIPAddr()

    dbController.setMasterConfig(
    )  #very important for make it can be connected via remote host
    dbController.start(setting.DB_ROOT_PASSWORD, conn, s)
    dbController.createDB(setting.DB_ROOT_PASSWORD, setting.DB_NAME,
                          True)  #drop and create new database
    dbController.createUser(setting.DB_ROOT_PASSWORD, setting.DB_USERNAME,
                            setting.DB_PASSWORD)

    dumpResult = dbController.runSQL(targetFile, "root",
                                     setting.DB_ROOT_PASSWORD, setting.DB_NAME,
                                     "localhost")
    if dumpResult == False:
        return "runSQL error, please check .sql file"

    db = MySQLdb.connect("localhost", "root", setting.DB_ROOT_PASSWORD,
                         setting.DB_NAME)
    cursor = db.cursor()
    cursor.execute(
        "UPDATE `hosts` SET `isInformationServer`=1 WHERE `IPAddress`='%s'" %
        (str(myIP)))
    db.close()

    os.remove(targetFile)

    return 'OK'
Esempio n. 5
0
def turn_slave_to_master_db(argv):
    '''
	dest = slave only
	1. turn
	2. update db
	3. tell every host
	'''
    conn = argv[0][0]
    s = argv[0][1]

    myIP = network.getMyIPAddr()
    myMAC = network.getMyMACAddr()

    db = MySQLdb.connect("localhost", "root", setting.DB_ROOT_PASSWORD)
    cursor = db.cursor()
    cursor.execute("SLAVE STOP;")
    db.close()

    dbController.setMasterConfig()
    dbController.stop()
    dbController.start(setting.DB_ROOT_PASSWORD, conn, s)

    dbController.createUser(setting.DB_ROOT_PASSWORD, setting.DB_USERNAME,
                            setting.DB_PASSWORD)

    db = MySQLdb.connect("localhost", "root", setting.DB_ROOT_PASSWORD)
    cursor = db.cursor()
    cursor.execute(
        "UPDATE `hosts` SET `isInformationServer`=1 WHERE `IPAddress`='%s'" %
        (str(myIP)))
    db.close()

    dbController.broadcastNewMasterInformationServer(str(myIP), str(myMAC))

    #should create new slave if there is another active host

    return 'OK'