Exemple #1
0
 def connectToMongoDB(self):
     """
     Connect To Mongo DataBase
     :return:connection
     """
     try:
         db_log.debug("server_ip ={} port ={}".format(self.server_ip,self.port))
         conn=pymongo.MongoClient('mongodb://{}:{}'.format(self.server_ip,self.port))
     except pymongo.errors.ConnectionFailure, e:
         print "Could not connect to MongoDB: %s" % e
         db_log.error( "Could not connect to MongoDB: %s" % e)
 def add_student(self,doc):
     '''
     add one document to student collection
     :param doc: -json object to insert to student collection
     '''
     for retry in range (4):
         try:
             students=student_manager.student_collection("college","localhost")
             students.insert_students(doc)
             db_log.debug( "Inserted Document: ")
             time.sleep(.1)
             break
         except pymongo.errors.AutoReconnect as e:
             db_log.error("Exception ",type(e), e)
             db_log.debug( "Retrying..")
             time.sleep(5)
         except pymongo.errors.DuplicateKeyError as e:
             db_log.debug( "duplicate..but it worked")
             break
Exemple #3
0
    def mongodb_action(self,deployment):
        '''
        handle all database action insert data delete backup and restore
        :param deployment: standalone replicaset or sharding
        :return:OK -1
        '''
        my_student =student_main.student()
        while True:
            os.system("clear")
            print " a - Add  new student "
            print " d - Delete all students "
            print " b - Backup db "
            print " r - Restore db "
            print " i - Insert all student"
            print " q - Return to main menu \n"
            user_choice = raw_input("Enter choice: ")
            if user_choice == 'q':
                break
            elif user_choice == 'a':
                new_student = { }
                new_student['student_id']= randint(10000,99999)
                new_student['first_name'] = raw_input("Enter first name: ")
                new_student['last_name'] = raw_input("Enter last name: ")
                new_student['course'] = {}
                my_student.add_student(new_student)
            elif user_choice == 'b':
                if deployment =="standalone":
                    db_log.debug("lock Database")
                    subprocess.call(shlex.split('{} {} {}'.format("mongo", "--eval", "db.fsyncLock()")))
                    print "*********************lock Database*********************\n"
                    print "******************************************************"
                    time.sleep(5)
                    self.mongo_dump_and_restore(self.STANDALONE_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.STANDALONE_PORT_27017,
                                                self.STANDALONE_DUMP_FILE_PATH)
                    db_log.debug("Unlock Database")
                    print "*********************Unlock Database*********************\n"
                    print "******************************************************"
                    subprocess.call(shlex.split('{} {} {}'.format("mongo", "--eval", "db.fsyncUnlock()")))
                    time.sleep(5)
                elif deployment =="replicaset":
                    self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.REPLICASET_SEONDARY_PORT_27018,
                                                self.REPLICASET_DUMP_FILE_PATH)
                elif deployment =="sharding":
                    #stop balancer
                    self.stop_blancer()
                    #for each shard backup his secondary
                    db_log.debug( "dump shard 1 port {}".format(self.SHARD_1_SEONDARY_PORT_37018))
                    self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.SHARD_1_SEONDARY_PORT_37018,
                                                self.SHARD_1_DUMP_PATH)
                    time.sleep(2)
                    db_log.debug("dump shard 2 port {}".format(self.SHARD_2_SEONDARY_PORT_47018))
                    self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.SHARD_2_SEONDARY_PORT_47018,
                                                self.SHARD_2_DUMP_PATH)
                    time.sleep(2)
                    db_log.debug( "dump shard 3 port {}".format(self.SHARD_3_SEONDARY_PORT_57018))
                    self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.SHARD_3_SEONDARY_PORT_57018,
                                                self.SHARD_3_DUMP_PATH)
                    time.sleep(2)
                    #backup config server
                    db_log.debug( "dump config server  port {}".format(self.SHARD_CONFIG_SERVER_PORT_57040))
                    self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.SHARD_CONFIG_SERVER_PORT_57040,
                                                self.CONFIG_SERVER_DUMP_PATH)
                    self.start_blancer()
                else:
                    db_log.debug( "not valid deployment")
                    exit(1)
            elif user_choice == 'r':
                if deployment =="standalone":
                   self.mongo_dump_and_restore(self.STANDALONE_RESTORE_PATH,
                                               self.LOCALHOST,
                                               self.STANDALONE_PORT_27017,
                                               self.STANDALONE_DUMP_FILE_PATH)
                elif deployment =="replicaset":
                    db_log.debug( "replica set restore parameters {} {} {} {}".format(self.REPLICASET_RESTORE_PATH,
                                                                              self.LOCALHOST,
                                                                              self.REPLICASET_PRIMARY_PORT_27017,
                                                                              self.REPLICASET_DUMP_FILE_PATH))
                    self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH,
                                           self.LOCALHOST,
                                           self.REPLICASET_PRIMARY_PORT_27017,
                                           self.REPLICASET_DUMP_FILE_PATH)
                elif deployment =="sharding":
                    #for each shard restore primary
                    print
                    self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH,
                                           self.LOCALHOST,
                                           self.SHARD_1_PRIMARY_PORT_37017,
                                           self.SHARD_1_DUMP_PATH)
                    self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH,
                                           self.LOCALHOST,
                                           self.SHARD_2_PRIMARY_PORT_47017,
                                           self.SHARD_2_DUMP_PATH)
                    self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH,
                                           self.LOCALHOST,
                                           self.SHARD_3_PRIMARY_PORT_57017,
                                           self.SHARD_3_DUMP_PATH)
                    self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH,
                                           self.LOCALHOST,
                                           self.SHARD_CONFIG_SERVER_PORT_57040,
                                           self.CONFIG_SERVER_DUMP_PATH)
                else:
                    db_log.error(" Error not valid deployment")
                    exit(1)
            elif user_choice == 'd':
                my_student.delete_student_collection()
            elif user_choice == 'i':
                my_student.insert_data_to_student_collection()
            else:
                db_log.debug( "you did not choose from the above -{} error {} \n".format(user_choice,student_main.ERR_WRONG))
                time.sleep(5)

        return 1
Exemple #4
0
    def mongodb_action(self, deployment):
        '''
        handle all database action insert data delete backup and restore
        :param deployment: standalone replicaset or sharding
        :return:OK -1
        '''
        my_student = student_main.student()
        while True:
            os.system("clear")
            print " a - Add  new student "
            print " d - Delete all students "
            print " b - Backup db "
            print " r - Restore db "
            print " i - Insert all student"
            print " q - Return to main menu \n"
            user_choice = raw_input("Enter choice: ")
            if user_choice == 'q':
                break
            elif user_choice == 'a':
                new_student = {}
                new_student['student_id'] = randint(10000, 99999)
                new_student['first_name'] = raw_input("Enter first name: ")
                new_student['last_name'] = raw_input("Enter last name: ")
                new_student['course'] = {}
                my_student.add_student(new_student)
            elif user_choice == 'b':
                if deployment == "standalone":
                    db_log.debug("lock Database")
                    subprocess.call(
                        shlex.split('{} {} {}'.format("mongo", "--eval",
                                                      "db.fsyncLock()")))
                    print "*********************lock Database*********************\n"
                    print "******************************************************"
                    time.sleep(5)
                    self.mongo_dump_and_restore(self.STANDALONE_DUMP_PATH,
                                                self.LOCALHOST,
                                                self.STANDALONE_PORT_27017,
                                                self.STANDALONE_DUMP_FILE_PATH)
                    db_log.debug("Unlock Database")
                    print "*********************Unlock Database*********************\n"
                    print "******************************************************"
                    subprocess.call(
                        shlex.split('{} {} {}'.format("mongo", "--eval",
                                                      "db.fsyncUnlock()")))
                    time.sleep(5)
                elif deployment == "replicaset":
                    self.mongo_dump_and_restore(
                        self.REPLICASET_DUMP_PATH, self.LOCALHOST,
                        self.REPLICASET_SEONDARY_PORT_27018,
                        self.REPLICASET_DUMP_FILE_PATH)
                elif deployment == "sharding":
                    #stop balancer
                    self.stop_blancer()
                    #for each shard backup his secondary
                    db_log.debug("dump shard 1 port {}".format(
                        self.SHARD_1_SEONDARY_PORT_37018))
                    self.mongo_dump_and_restore(
                        self.REPLICASET_DUMP_PATH, self.LOCALHOST,
                        self.SHARD_1_SEONDARY_PORT_37018,
                        self.SHARD_1_DUMP_PATH)
                    time.sleep(2)
                    db_log.debug("dump shard 2 port {}".format(
                        self.SHARD_2_SEONDARY_PORT_47018))
                    self.mongo_dump_and_restore(
                        self.REPLICASET_DUMP_PATH, self.LOCALHOST,
                        self.SHARD_2_SEONDARY_PORT_47018,
                        self.SHARD_2_DUMP_PATH)
                    time.sleep(2)
                    db_log.debug("dump shard 3 port {}".format(
                        self.SHARD_3_SEONDARY_PORT_57018))
                    self.mongo_dump_and_restore(
                        self.REPLICASET_DUMP_PATH, self.LOCALHOST,
                        self.SHARD_3_SEONDARY_PORT_57018,
                        self.SHARD_3_DUMP_PATH)
                    time.sleep(2)
                    #backup config server
                    db_log.debug("dump config server  port {}".format(
                        self.SHARD_CONFIG_SERVER_PORT_57040))
                    self.mongo_dump_and_restore(
                        self.REPLICASET_DUMP_PATH, self.LOCALHOST,
                        self.SHARD_CONFIG_SERVER_PORT_57040,
                        self.CONFIG_SERVER_DUMP_PATH)
                    self.start_blancer()
                else:
                    db_log.debug("not valid deployment")
                    exit(1)
            elif user_choice == 'r':
                if deployment == "standalone":
                    self.mongo_dump_and_restore(self.STANDALONE_RESTORE_PATH,
                                                self.LOCALHOST,
                                                self.STANDALONE_PORT_27017,
                                                self.STANDALONE_DUMP_FILE_PATH)
                elif deployment == "replicaset":
                    db_log.debug(
                        "replica set restore parameters {} {} {} {}".format(
                            self.REPLICASET_RESTORE_PATH, self.LOCALHOST,
                            self.REPLICASET_PRIMARY_PORT_27017,
                            self.REPLICASET_DUMP_FILE_PATH))
                    self.mongo_dump_and_restore(
                        self.REPLICASET_RESTORE_PATH, self.LOCALHOST,
                        self.REPLICASET_PRIMARY_PORT_27017,
                        self.REPLICASET_DUMP_FILE_PATH)
                elif deployment == "sharding":
                    #for each shard restore primary
                    print
                    self.mongo_dump_and_restore(
                        self.REPLICASET_RESTORE_PATH, self.LOCALHOST,
                        self.SHARD_1_PRIMARY_PORT_37017,
                        self.SHARD_1_DUMP_PATH)
                    self.mongo_dump_and_restore(
                        self.REPLICASET_RESTORE_PATH, self.LOCALHOST,
                        self.SHARD_2_PRIMARY_PORT_47017,
                        self.SHARD_2_DUMP_PATH)
                    self.mongo_dump_and_restore(
                        self.REPLICASET_RESTORE_PATH, self.LOCALHOST,
                        self.SHARD_3_PRIMARY_PORT_57017,
                        self.SHARD_3_DUMP_PATH)
                    self.mongo_dump_and_restore(
                        self.REPLICASET_RESTORE_PATH, self.LOCALHOST,
                        self.SHARD_CONFIG_SERVER_PORT_57040,
                        self.CONFIG_SERVER_DUMP_PATH)
                else:
                    db_log.error(" Error not valid deployment")
                    exit(1)
            elif user_choice == 'd':
                my_student.delete_student_collection()
            elif user_choice == 'i':
                my_student.insert_data_to_student_collection()
            else:
                db_log.debug(
                    "you did not choose from the above -{} error {} \n".format(
                        user_choice, student_main.ERR_WRONG))
                time.sleep(5)

        return 1