Exemplo n.º 1
0
 def remove_reviewer(cls, username):
     if username:
         try:
             user = User.objects(username=username).first()
             if user:
                 try:
                     found = Committee.objects(reviewers__contains=user)
                     if found:
                         Committee.objects().update_one(
                             pull__reviewers=user)
                         User.objects(username=username).update_one(
                             pull__roles="reviewer")
                         Console.info(
                             "User `{0}` removed as Reviewer.".format(
                                 username))
                     else:
                         Console.error("Please specify a valid user name.")
                 except:
                     Console.info(
                         "Reviewer {0} removal failed.".format(username))
             else:
                 Console.error("Please specify a valid user")
         except:
             Console.error(
                 "Oops! Something went wrong while trying to remove reviewer"
             )
     else:
         Console.error("Please specify a reviewer name to be removed.")
     pass
    def do_uebercool(self, args, arguments):
        """
        ::

          Usage:
              uebercool NAME

          tests via ping if the host ith the give NAME is reachable

          Arguments:

            NAME      Name of the machine to test

          Options:

             -v       verbose mode

        """
        # pprint(arguments)

        if arguments["NAME"] is None:
            Console.error("Please specify a host name")
        else:
            host = arguments["NAME"]
            Console.info("trying to reach {0}".format(host))
            status = command_uebercool.status(host)
            if status:
                Console.info("machine " + host + " has been found. ok.")
            else:
                Console.error("machine " + host + " not reachable. error.")
        pass
Exemplo n.º 3
0
    def run(self):
        banner("Setup the cloudmesh management yaml files ")

        yamlfiles = ['cloudmesh_user.yaml', 'cloudmesh_project.yaml']
        dir_path = path_expand("~/.cloudmesh/{0}".format("accounts"))

        if not os.path.exists(dir_path):
            Shell.mkdir(dir_path)

        for yamlfile in yamlfiles:

            filename = path_expand("~/.cloudmesh/{0}/{1}".format(
                "accounts", yamlfile))

            if os.path.isfile(filename):
                Console.error(
                    "File {0} already exists. If you like to reinstall it, please remove the file"
                    .format(yamlfile))
            else:
                Console.info("Copying file:  {0} -> {1} ".format(
                    path_expand("etc/{0}/{1}".format("accounts", yamlfile)),
                    filename))
                shutil.copy(
                    "etc/{0}/{1}".format("accounts", yamlfile),
                    path_expand("~/.cloudmesh/{0}/{1}".format(
                        "accounts", yamlfile)))
    def do_supercool(self, args, arguments):
     """
     Usage:
          supercool NAME

          Checks if the named file exists in root folder else giver error

          Arguments:

            NAME      Name of the file to create

          Options:

             -v       verbose mode

     """
     if arguments["NAME"] is None:
     	Console.error("Please specify a file name")
     else:
	file = arguments["NAME"]
        Console.info("Trying to create {0}".format(file))
        status = command_supercool.status(file)
       	if status:
		Console.error("A file with same name already exists in root!")
        else:
       	        Console.info(file + " can be created in the root folder.")
Exemplo n.º 5
0
    def create_user_from_file(cls, file_path):
        try:
            filename = path_expand(file_path)
            file_config = ConfigDict(filename=filename)
        except:
            Console.error("Could not load file, please check filename and its path")
            return

        try:
            user_config = file_config.get("cloudmesh", "user")
            user_name = user_config['username']
            user = User()
            update_document(user, user_config)
        except:
            Console.error("Could not get user information from yaml file, "
                          "please check you yaml file, users information must be "
                          "under 'cloudmesh' -> 'users' -> user1...")
            return

        try:
            if cls.check_exists(user_name) is False:
                cls.add(user)
                Console.info("User created in the database.")
            else:
                Console.error("User with user name " + user_name + " already exists.")
                return
        except:
            Console.error("User creation in database failed, " + str(sys.exc_info()))
            return
Exemplo n.º 6
0
    def create_project_from_file(cls, file_path):
        # implement()
        # return
        try:
            filename = path_expand(file_path)
            file_config = ConfigDict(filename=filename)
        except:
            Console.error("Could not load file, please check filename and its path")
            return

        try:
            project_config = file_config.get("cloudmesh", "project")
            project = Project()
            project_id = uuid.uuid4()
            project_config.update({'project_id': project_id})
            update_document(project, project_config)
        except:
            Console.error("Could not get project information from yaml file, "
                          "please check you yaml file, users information must be "
                          "under 'cloudmesh' -> 'project' -> project1..." + str(sys.exc_info()[0]))
            return

        try:
            cls.add(project)
            Console.info("Project created in the database.")
        except:
            Console.error("Project creation in database failed, " + str(sys.exc_info()))
        return
Exemplo n.º 7
0
 def set_password(cls, user_name, passwd):
     pass_hash = sha256_crypt.encrypt(passwd)
     try:
         User.objects(username=user_name).update_one(set__password=pass_hash)
         Console.info("User password updated.")
     except:
         Console.error("Oops! Something went wrong while trying to set user password")
    def do_spark(self, args, arguments):
        """
        ::

          Usage:
              spark NAME
              spark deploy HOST

          tests via ping if the host ith the give NAME is reachable

          Arguments:

            NAME      Name of the machine to test

          Options:

             -v       verbose mode

        """
        pprint(arguments)
        if arguments["deploy"]:
            Console.ok("I want to deploy")
            host = arguments["HOST"]
            print(host)
        elif arguments["NAME"] is None:
            Console.error("Please specify a host name")
        else:
            host = arguments["NAME"]
            Console.info("trying to reach {0}".format(host))
            status = command_spark.status(host)
            if status:
                Console.info("machine " + host + " has been found. ok.")
            else:
                Console.error("machine " + host + " not reachable. error.")
        pass
Exemplo n.º 9
0
 def add_reviewer(cls, username):
     if username:
         try:
             user = User.objects(username=username).first()
             if user:
                 try:
                     found = Committee.objects(reviewers__contains=user)
                     if not found:
                         Committee.objects().update_one(
                             push__reviewers=user)
                         User.objects(username=username).update_one(
                             push__roles="reviewer")
                         Console.info(
                             "User {0} added as Reviewer.".format(username))
                     else:
                         Console.error(
                             "User {0} already exists as a reviewer.".
                             format(username))
                 except:
                     print sys.exc_info()
                     Console.info(
                         "Reviewer {0} addition failed.".format(username))
             else:
                 Console.error("Please specify a valid user")
         except:
             print sys.exc_info()
             Console.error(
                 "Oops! Something went wrong while trying to add project reviewer"
             )
     else:
         Console.error("Please specify a reviewer name to be added.")
     pass
Exemplo n.º 10
0
    def connect(self):

        client = MongoClient('localhost', self.port)
        self.database = client["jobsdb"]
        self.jobs = self.database["jobs"]
        self.id = self.database["id"]  # manages the counter for the job

        Console.info("Connecting to the Mongo Database")
Exemplo n.º 11
0
 def set_role(cls, user_name, role):
     try:
         if role in ROLE:
             User.objects(username=user_name).update_one(push__roles=role)
             Console.info("Role {0} added to user {1}".format(role, user_name))
         else:
             Console.error("Please specify a valid role. Role {0} is invalid.".format(role))
     except:
         Console.error("Oops! Something went wrong while trying to set user role.")
Exemplo n.º 12
0
 def list_committee(cls, project_id=None):
     if project_id is None:
         req_fields = ["reviewers"]
         committee_json = Committee.objects.only(*req_fields).to_json()
         committee_dict = json.loads(committee_json)
         if committee_dict:
             cls.display(committee_dict)
         else:
             Console.info("No committees found in the database.")
     pass
Exemplo n.º 13
0
 def list_committee(cls, project_id=None):
     if project_id is None:
         req_fields = ["reviewers"]
         committee_json = Committee.objects.only(*req_fields).to_json()
         committee_dict = json.loads(committee_json)
         if committee_dict:
             cls.display(committee_dict)
         else:
             Console.info("No committees found in the database.")
     pass
Exemplo n.º 14
0
 def clear(cls):
     """
     Removes all elements form the mongo db that are users
     """
     try:
         for user in User.objects:
             user.delete()
         Console.info("Users cleared from the database.")
     except:
         Console.error("Oops! Something went wrong while trying to clear the users from database")
    def do_wikicount(self, args, arguments):
        """
        ::
          Usage:
              wikicount build_cluster NAME [--count=N] 
                                           [--ln=S] 
                                           [--cloud=S]
                                           [--flavor=S]
                                           [--image=S]
              wikicount decomission_cluster NAME
              wikicount install
              wikicount install_mongodb

          Arguments:
            NAME      Name of the wikicount cluster group
          Options:
             --count=N  number of nodes to create
             --ln=S     login name
             --cloud=S  cloud to use
             --flavor=S flavor to use
             --image=S  image to use
        """
        pprint(arguments)
        if arguments['build_cluster']:
            Console.ok("I want to build a cluster")
            name = arguments['NAME']
            count = arguments['--count'] or 3
            ln = arguments['--ln']
            cloud = arguments['--cloud']
            flavor = arguments['--flavor']
            image = arguments['--image']	
            command_wikicount.build_cluster(name, count)
        elif arguments['decomission_cluster']:
            Console.ok("I want to decomission a cluster")
            name = arguments['NAME']
            command_wikicount.decomission_cluster(name)
        elif arguments['install']:
            Console.ok("Initializing environment")
            command_wikicount.install()
        elif arguments['install_mongodb']: 
            Console.ok("Installing mongodb")
            command_wikicount.install_mongodb()
        elif arguments["NAME"] is None:
            Console.error("Please specify a name for the cluster")
        else:
            name = arguments["NAME"]
            Console.info("trying to reach {0}".format(name))
            status = command_wikicount.status(name)
            if status:
                Console.info("machine " + name + " has been found. ok.")
            else:
                Console.error("machine " + name + " not reachable. error.")
        pass
Exemplo n.º 16
0
 def list_projects(cls, user_name=None):
     required_fields = ["username", "firstname", "lastname", "projects"]
     try:
         if user_name:
             user_json = User.objects.only(*required_fields).to_json()
             user_dict = json.loads(user_json)
             if user_dict:
                 cls.display(user_dict, user_name)
             else:
                 Console.info("No user details available in the database.")
     except:
         Console.error("Please provide a username.")
Exemplo n.º 17
0
 def delete_project(cls, project_id=None):
     if project_id:
         try:
             project = Project.objects(project_id=project_id)
             if project:
                 project.delete()
                 Console.info("Project with id `{0}` removed from the database.".format(project_id))
             else:
                 Console.error("Project with id `{0}` does not exist.".format(project_id))
         except:
             Console.error("Oops! Something went wrong while trying to remove a project")
     else:
         Console.error("Please specify the project to be removed")
Exemplo n.º 18
0
 def remove_project_reviewer(cls, project_id, username):
     if project_id:
         try:
             found = User.objects(username=username)
             if found.count() > 0:
                 Project.objects(project_id=project_id).update_one(pull__reviewers=username)
                 Console.info("User `{0}` removed as Reviewer.".format(username))
             else:
                 Console.error("Please specify a valid user")
         except:
             Console.error("Oops! Something went wrong while trying to remove project reviewer")
     else:
         Console.error("Please specify the project to be amended")
Exemplo n.º 19
0
def generate_users(n):
    """
    Generates n random users in an array containing dicts for users

    :param n: number of users
    :type n: integer
    :rtype: array of dicts
    """
    users.clear()
    for i in range(0, n):
        data = random_user()
        users.add(data)
    Console.info(str(n) + " users generated.")
Exemplo n.º 20
0
def generate_users(n):
    """
    Generates n random users in an array containing dicts for users

    :param n: number of users
    :type n: integer
    :rtype: array of dicts
    """
    users.clear()
    for i in range(0, n):
        data = random_user()
        users.add(data)
    Console.info(str(n)+" users generated.")
Exemplo n.º 21
0
    def do_ssh(self, args, arguments):
        """
        ::

          Usage:
              ssh list [--format=json|yaml]
              ssh register NAME COMMANDS
              ssh NAME


          conducts a ssh login into a machine while using a set of
          registered commands under the name of the machine.

          Arguments:

            NAME      Name of the machine to log in
            list      Lists the machines that are registered and
                      the commands to login to them
            register  Register the commands to a name
            COMMANDS  The list of commands executed when issuing a name

          Options:

             -v       verbose mode

        """
        pprint(arguments)

        if arguments["list"]:
            if 'json' == arguments["--format"]:
                print (print_format_dict(self.ssh_machines, kind='json'))
            elif 'yaml' == arguments["--format"]:
                print (print_format_dict(self.ssh_machines, kind='yaml'))
            else:
                print (two_column_table(self.ssh_machines,  header=['Machines', 'Commands']))

        elif arguments["register"]:
            Console.error("NOT YET IMPLEMENTED")
        else:
            machine = arguments["NAME"]
            if machine in self.ssh_machines:
                commands = self.ssh_machines[machine]
                print (commands)
                Console.info ("login to " + machine)
                os.system(commands)
            else:
                Console.error("machine " + machine + " not found")
            
            
        # shell_command_open_ssh(arguments)
        pass
Exemplo n.º 22
0
 def delete_user(cls, user_name=None):
     if user_name:
         try:
             user = User.objects(username=user_name)
             if user:
                 user.delete()
                 if user_name != "super":
                     Console.info("User " + user_name + " removed from the database.")
             else:
                 Console.error("User with the name '{0}' does not exist.".format(user_name))
         except:
             Console.error("Oops! Something went wrong while trying to remove a user")
     else:
         Console.error("Please specify the user to be removed")
Exemplo n.º 23
0
 def setup_committee(cls):
     try:
         user = User.objects(username="******").first()
         count = Committee.objects.count()
         print count
         if count > 0:
             Console.error("Committee has already been setup.")
             return
         data = Committee(reviewers=[user], reviews=[])
         data.save()
         Console.info("Committee setup successful.")
     except:
         print sys.exc_info()
         Console.error("Committee setup failed.")
Exemplo n.º 24
0
    def connect(self):
        """
        Creates a connection to the database with the given configuration
        from the yaml file
        """

        client = MongoClient('localhost', self.port)
        self.database = client["jobsdb"]
        self.jobid = self.database["jobsid"]
        self.jobs = self.database["jobs"]
        self.jobscripts = self.database["jobscripts"]
        self.id = self.database["id"]  # manages the counter for the job

        if self.debug:
            Console.info("Connecting to the Mongo Database")
Exemplo n.º 25
0
 def setup_committee(cls):
     try:
         user = User.objects(username="******").first()
         count = Committee.objects.count()
         print count
         if count > 0:
             Console.error("Committee has already been setup.")
             return
         data = Committee(
             reviewers=[user],
             reviews=[]
         )
         data.save()
         Console.info("Committee setup successful.")
     except:
         print sys.exc_info()
         Console.error("Committee setup failed.")
Exemplo n.º 26
0
    def run(self):
        banner("Setup the cloudmesh management yaml files ")

        yamlfiles = ['cloudmesh_user.yaml', 'cloudmesh_project.yaml']
        dir_path = path_expand("~/.cloudmesh/{0}".format("accounts"))

        if not os.path.exists(dir_path):
            Shell.mkdir(dir_path)

        for yamlfile in yamlfiles:

            filename = path_expand("~/.cloudmesh/{0}/{1}".format("accounts", yamlfile))

            if os.path.isfile(filename):
                Console.error("File {0} already exists. If you like to reinstall it, please remove the file".format(yamlfile))
            else:
                Console.info("Copying file:  {0} -> {1} ".format(path_expand("etc/{0}/{1}".format("accounts", yamlfile)), filename))
                shutil.copy("etc/{0}/{1}".format("accounts", yamlfile), path_expand("~/.cloudmesh/{0}/{1}".format("accounts", yamlfile)))
Exemplo n.º 27
0
    def do_color(self, args, arguments):
        """
        ::
        
          Usage:
              color on
              color off
              color

              Turns the shell color printing on or off

          Description:

              color on   switched the color on

              color off  switches the color off

              color      without parameters prints a test to display
                         the various colored mesages. It is intended
                         as a test to see if your terminal supports
                         colors.

        """
        if arguments['on']:
            key = "cloudmesh.shell.color"
            value = True
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = True
            print("color on.")
        elif arguments['off']:
            key = "cloudmesh.shell.color"
            value = False
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = False
            print("color off.")
        else:
            print("Color:", Console.color)
            Console.warning("Warning")
            Console.error("Error")
            Console.info("Info")
            Console.msg("Msg")
            Console.ok("Success")
Exemplo n.º 28
0
    def do_color(self, args, arguments):
        """
        ::
        
          Usage:
              color on
              color off
              color

              Turns the shell color printing on or off

          Description:

              color on   switched the color on

              color off  switches the color off

              color      without parameters prints a test to display
                         the various colored mesages. It is intended
                         as a test to see if your terminal supports
                         colors.

        """
        if arguments['on']:
            key = "cloudmesh.shell.color"
            value = True
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = True
            print ("color on.")
        elif arguments['off']:
            key = "cloudmesh.shell.color"
            value = False
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = False
            print ("color off.")
        else:
            print("Color:", Console.color)
            Console.warning("Warning")
            Console.error("Error")
            Console.info("Info")
            Console.msg("Msg")
            Console.ok("Success")
Exemplo n.º 29
0
    def run(self):
        banner("Reset the cloudmesh management yaml files ")

        yaml_files = ['cloudmesh_user.yaml', 'cloudmesh_project.yaml']
        dir_path = path_expand("~/.cloudmesh/{0}".format("accounts"))

        if not os.path.exists(dir_path):
            Shell.mkdir(dir_path)

        for yaml_file in yaml_files:
            filename = path_expand("~/.cloudmesh/{0}/{1}".format(
                "accounts", yaml_file))
            if os.path.isfile(filename):
                Console.info("Removing file:  {0}".format(filename))
                Shell.rm(filename)
                Console.info("Copying file:  {0} -> {1} ".format(
                    path_expand("etc/{0}/{1}".format("accounts", yaml_file)),
                    filename))
                shutil.copy(
                    "etc/{0}/{1}".format("accounts", yaml_file),
                    path_expand("~/.cloudmesh/{0}/{1}".format(
                        "accounts", yaml_file)))
            else:
                Console.info("Copying file:  {0} -> {1} ".format(
                    path_expand("etc/{0}/{1}".format("accounts", yaml_file)),
                    filename))
                shutil.copy(
                    "etc/{0}/{1}".format("accounts", yaml_file),
                    path_expand("~/.cloudmesh/{0}/{1}".format(
                        "accounts", yaml_file)))
Exemplo n.º 30
0
 def list_users(cls, display_fmt=None, username=None, status=None):
     # req_fields = ["username", "title", "firstname", "lastname",
     # "email", "phone", "url", "citizenship",
     #               "institution", "institutionrole", "department",
     #               "advisor", "address", "status", "projects"]
     req_fields = ["username", "firstname", "lastname",
                   "email", "phone", "institution", "institutionrole",
                   "advisor", "address", "status", "projects", "roles"]
     try:
         if username is None:
             if status:
                 if status not in STATUS:
                     Console.info("Invalid status requested.. Displaying all users..")
                     user_json = User.objects.only(*req_fields).to_json()
                 else:
                     user_json = User.objects(status=status).only(*req_fields).to_json()
             else:
                 user_json = User.objects.only(*req_fields).to_json()
             user_dict = json.loads(user_json)
             if user_dict:
                 if display_fmt != 'json':
                     cls.display(user_dict, username)
                 else:
                     cls.display_json(user_dict, username)
             else:
                 Console.info("No users in the database.")
         else:
             user_json = User.objects(username=username).to_json()
             users_list = json.loads(user_json)
             for item in users_list:
                 users_dict = item
                 if users_dict:
                     if display_fmt != 'json':
                         cls.display_two_columns(users_dict)
                     else:
                         cls.display_json(users_dict, username)
                 else:
                     Console.error("User not in the database.")
     except:
         Console.error("Oops.. Something went wrong in the list users method " + sys.exc_info()[0])
Exemplo n.º 31
0
 def list_projects(cls, display_fmt=None, project_id=None):
     req_fields = ["title", "status", "lead", "managers", "members", "project_id"]
     try:
         if project_id is None:
             projects_json = Project.objects.only(*req_fields).to_json()
             projects_dict = json.loads(projects_json)
             if projects_dict:
                 if display_fmt != 'json':
                     cls.display(projects_dict, project_id)
                 else:
                     cls.display_json(projects_dict, project_id)
             else:
                 Console.info("No projects in the database.")
         else:
             projects_json = Project.objects(project_id=project_id).to_json()
             projects_list = json.loads(projects_json)
             for item in projects_list:
                 projects_dict = item
             cls.display_two_column(projects_dict)
     except:
         Console.error("Oops.. Something went wrong in the list projects method " + sys.exc_info())
     pass
Exemplo n.º 32
0
 def remove_reviewer(cls, username):
     if username:
         try:
             user = User.objects(username=username).first()
             if user:
                 try:
                     found = Committee.objects(reviewers__contains=user)
                     if found:
                         Committee.objects().update_one(pull__reviewers=user)
                         User.objects(username=username).update_one(pull__roles="reviewer")
                         Console.info("User `{0}` removed as Reviewer.".format(username))
                     else:
                         Console.error("Please specify a valid user name.")
                 except:
                     Console.info("Reviewer {0} removal failed.".format(username))
             else:
                 Console.error("Please specify a valid user")
         except:
             Console.error("Oops! Something went wrong while trying to remove reviewer")
     else:
         Console.error("Please specify a reviewer name to be removed.")
     pass
Exemplo n.º 33
0
    def do_qstat(self, args, arguments):
        """
        ::

          Usage:
            qstat HOST [-a]  [(--view VIEW | ATTRIBUTES...)] [--output=(dict|table)]

          tests via ping if the host ith the give NAME is reacahble

          Arguments:

            VIEW  the name of the view [default: default].
            HOST      Name of the machine to test

          Options:

             -v       verbose mode

        """

        if arguments["HOST"]:
            host = arguments["HOST"]

            Console.info("trying to reach {0}".format(host))

            if len(arguments['ATTRIBUTES']) == 0 and not arguments['--view']:
                arguments['--view'] = True
                arguments['VIEW'] = 'default'
                # pprint(arguments)

            r = {}
            try:
                pbs = OpenPBS(deploy=True)
                if arguments["-a"]:
                    r = pbs.qstat(host, user=False)
                else:
                    r = pbs.qstat(host)
                Console.info("machine " + host + " has been found. ok.")

                if len(arguments['ATTRIBUTES']
                       ) != 0 and not arguments['--view']:
                    r = OpenPBS.list(r, arguments['ATTRIBUTES'])
                elif arguments['--view']:
                    view = arguments['VIEW']
                    attributes = pbs.data.get(
                        "cloudmesh.pbsview.{0}".format(view))
                    r = OpenPBS.list(r, attributes)

            except Exception, e:
                Console.error("machine " + host + " not reachable. error.")
                print(e)
            if len(r.keys()) == 0:
                Console.info("No jobs found")
            else:
                if arguments['--output'] == 'dict' or None:
                    pprint(r)
                else:
                    print(dict_printer(r))
Exemplo n.º 34
0
    def write_to_file(self, snippet, filename):
        """
        Method is used to write the "snippet" into the "filename" being passed
        :param snippet:
                Contains the content of the file to be written
        :param filename:
                File name to which the contents would be written to.
        :return:
                True is write was successful
                False, for any issues
        """
        target = None
        self.etc_path = os.getcwd() + os.sep + "cloudmesh_management" + os.sep
        # self.etc_path = os.sep.join(os.getcwd().split(os.sep)[:-1])+"/etc/"
        if not os.path.exists(self.etc_path):
            os.makedirs(self.etc_path)
        self.filename = self.etc_path + filename + ".py"
        Console.info("File: {0} regenerated.".format(self.filename))
        try:
            target = open(self.filename, 'w+')
        except IOError:
            Console.error("Error in opening the file.")
            return False
        else:
            if target:
                try:
                    target.write(string.join(self.headers, ""))
                    target.write("\n")
                    target.write(string.join(self.options, ""))
                    target.write(snippet)
                except IOError:
                    Console.error("Error in writing to file")
                    return False
            target.close()

            return True
Exemplo n.º 35
0
 def add_reviewer(cls, username):
     if username:
         try:
             user = User.objects(username=username).first()
             if user:
                 try:
                     found = Committee.objects(reviewers__contains=user)
                     if not found:
                         Committee.objects().update_one(push__reviewers=user)
                         User.objects(username=username).update_one(push__roles="reviewer")
                         Console.info("User {0} added as Reviewer.".format(username))
                     else:
                         Console.error("User {0} already exists as a reviewer.".format(username))
                 except:
                     print sys.exc_info()
                     Console.info("Reviewer {0} addition failed.".format(username))
             else:
                 Console.error("Please specify a valid user")
         except:
             print sys.exc_info()
             Console.error("Oops! Something went wrong while trying to add project reviewer")
     else:
         Console.error("Please specify a reviewer name to be added.")
     pass
Exemplo n.º 36
0
    def do_qstat(self, args, arguments):
        """
        ::

          Usage:
            qstat HOST [-a]  [(--view VIEW | ATTRIBUTES...)] [--output=(dict|table)]

          tests via ping if the host ith the give NAME is reacahble

          Arguments:

            VIEW  the name of the view [default: default].
            HOST      Name of the machine to test

          Options:

             -v       verbose mode

        """

        if arguments["HOST"]:
            host = arguments["HOST"]

            Console.info("trying to reach {0}".format(host))

            if len(arguments['ATTRIBUTES']) == 0 and not arguments['--view']:
                arguments['--view'] = True
                arguments['VIEW'] = 'default'
                # pprint(arguments)

            r = {}
            try:
                pbs = OpenPBS(deploy=True)
                if arguments["-a"]:
                    r = pbs.qstat(host, user=False)
                else:
                    r = pbs.qstat(host)
                Console.info("machine " + host + " has been found. ok.")

                if len(arguments['ATTRIBUTES']) != 0 and not arguments['--view']:
                    r = OpenPBS.list(r, arguments['ATTRIBUTES'])
                elif arguments['--view']:
                    view = arguments['VIEW']
                    attributes = pbs.data.get("cloudmesh.pbsview.{0}".format(view))
                    r = OpenPBS.list(r, attributes)

            except Exception, e:
                Console.error("machine " + host + " not reachable. error.")
                print(e)
            if len(r.keys()) == 0:
                Console.info("No jobs found")
            else:
                if arguments['--output'] == 'dict' or None:
                    pprint(r)
                else:
                    print(dict_printer(r))
Exemplo n.º 37
0
    def write_to_file(self, snippet, filename):
        """
        Method is used to write the "snippet" into the "filename" being passed
        :param snippet:
                Contains the content of the file to be written
        :param filename:
                File name to which the contents would be written to.
        :return:
                True is write was successful
                False, for any issues
        """
        target = None
        self.etc_path = os.getcwd()+os.sep+"cloudmesh_management"+os.sep
        # self.etc_path = os.sep.join(os.getcwd().split(os.sep)[:-1])+"/etc/"
        if not os.path.exists(self.etc_path):
            os.makedirs(self.etc_path)
        self.filename = self.etc_path+filename+".py"
        Console.info("File: {0} regenerated.".format(self.filename))
        try:
            target = open(self.filename, 'w+')
        except IOError:
            Console.error("Error in opening the file.")
            return False
        else:
            if target:
                try:
                    target.write(string.join(self.headers, ""))
                    target.write("\n")
                    target.write(string.join(self.options, ""))
                    target.write(snippet)
                except IOError:
                    Console.error("Error in writing to file")
                    return False
            target.close()

            return True
Exemplo n.º 38
0
 def remove_user(cls, user_name, project_id, role):
     if role not in ROLES_LIST:
         Console.error("Invalid role `{0}`".format(role))
         return
     user = User.objects(username=user_name).first()
     if user and role != "alumni":
         if role == "member":
             Project.objects(project_id=project_id).update_one(pull__members=user)
             Console.info("User `{0}` removed as Project member.".format(user_name))
         elif role == "lead":
             Project.objects(project_id=project_id).update_one(pull__lead=user)
             Console.info("User `{0}` removed as Project lead.".format(user_name))
     elif role == "alumni":
         Project.objects(project_id=project_id).update_one(pull__alumnis=user)
         Console.info("User `{0}` removed as alumni.".format(user_name))
     else:
         Console.error("The user `{0}` has not registered with Future Systems".format(user_name))
Exemplo n.º 39
0
    def run(self):
        banner("Reset the cloudmesh management yaml files ")

        yaml_files = ['cloudmesh_user.yaml', 'cloudmesh_project.yaml']
        dir_path = path_expand("~/.cloudmesh/{0}".format("accounts"))

        if not os.path.exists(dir_path):
            Shell.mkdir(dir_path)

        for yaml_file in yaml_files:
            filename = path_expand("~/.cloudmesh/{0}/{1}".format("accounts", yaml_file))
            if os.path.isfile(filename):
                Console.info("Removing file:  {0}".format(filename))
                Shell.rm(filename)
                Console.info("Copying file:  {0} -> {1} ".format(path_expand("etc/{0}/{1}".format("accounts", yaml_file)), filename))
                shutil.copy("etc/{0}/{1}".format("accounts", yaml_file), path_expand("~/.cloudmesh/{0}/{1}".format("accounts", yaml_file)))
            else:
                Console.info("Copying file:  {0} -> {1} ".format(path_expand("etc/{0}/{1}".format("accounts", yaml_file)), filename))
                shutil.copy("etc/{0}/{1}".format("accounts", yaml_file), path_expand("~/.cloudmesh/{0}/{1}".format("accounts", yaml_file)))
Exemplo n.º 40
0
    def add_user(cls, user_name, project_id, role):
        """
        Adds a member to the project.

        :param role: the role of the user
        :type role: String
        :param user_name: the username
        :type user_name: String
        :param project_id: the project id
        """

        if role not in ROLES_LIST:
            Console.error("Invalid role `{0}`".format(role))
            return

        """adds members to a particular project"""
        user = User.objects(username=user_name).first()
        project = Project.objects(project_id=project_id).first()
        if project:
            if user and role != 'alumni':
                if role == "member":
                    Project.objects(project_id=project_id).update_one(push__members=user)
                    User.objects(username=user_name).update_one(push__projects=project)
                    Console.info("User `{0}` added as Project member.".format(user_name))
                elif role == "lead":
                    Project.objects(project_id=project_id).update_one(push__lead=user)
                    Console.info("User `{0}` set as Lead.".format(user_name))
                else:
                    Console.error("Role `{0}` cannot be amended".format(role))
            elif role == "alumni":
                Project.objects(project_id=project_id).update_one(push__alumnis=user_name)
                Console.info("User `{0}` added as Alumni.".format(user_name))
            else:
                Console.error("The user `{0}` has not registered with Future Systems".format(user_name))
        else:
            Console.error("The project `{0}` is not registered with Future Systems".format(project_id))
Exemplo n.º 41
0
    def do_management(self, args, arguments):
        """management: Command line for Administrators to manage users and projects

        Usage:
            management version
            management admin user generate [--count=N]
            management admin user list [USERNAME] [--format=FORMAT] [--status=STATUS]
            management admin user add [YAMLFILE]
            management admin user delete [USERNAME]
            management admin user clear
            management admin user status USERNAME
            management admin user approve [USERNAME]
            management admin user activate [USERNAME]
            management admin user suspend [USERNAME]
            management admin user block [USERNAME]
            management admin user deny [USERNAME]
            management admin user assign [USERNAME] [ROLE]
            management admin user password USERNAME PASSWORD
            management admin user projects USERNAME
            management admin project generate [--count=N]
            management admin project list [PROJECTID] [--format=FORMAT]
            management admin project add [YAMLFILE]
            management admin project delete [PROJECTID]
            management admin project clear
            management admin project status [PROJECTID]
            management admin project activate [PROJECTID]
            management admin project deactivate [PROJECTID]
            management admin project close [PROJECTID]
            management admin project add [USERNAME] [PROJECTID] [ROLE]
            management admin project remove [USERNAME] [PROJECTID] [ROLE]
            management admin export [DATABASE] [COLLECTION] [--user=USERNAME] [--password=PASSWORD]
            management admin import [--file=FILENAME] [--dir=DIRNAME] [--db=DBNAME] [--collection=NAME]  [--user=USERNAME] [--password=PASSWORD]
            management committee setup
            management committee reviewer add [PROJECTID] [USERNAME]
            management committee reviewer remove [PROJECTID] [USERNAME]
            management committee list
            management committee project list [PROJECTID] [--format=FORMAT] [--status=STATUS]
            management committee project status
            management committee project approve [PROJECTID]
            management committee project deny [PROJECTID]
            management committee project block [PROJECTID]
            management committee project activate [PROJECTID]
            management committee project close [PROJECTID]
            management committee project open [PROJECTID]
            management user apply [--user=USERFILE|--project=PROJECTFILE]
            management user configure [USERNAME]
            management user password
            management user status
            management user add-sshkey [FILENAME]

        Options:
            --format=json   Show the user details in json format
        """

        # arguments = docopt(management_command.__doc__, args[1:])

        try:
            if arguments['version']:
                Console.info("Version: " + get_version())
            elif arguments['admin'] and arguments['user'] and arguments[
                    'generate']:
                if arguments['--count']:
                    count = int(arguments['--count'])
                    generate_users(count)
                else:
                    generate_users(10)
            elif arguments['admin'] and arguments['user'] and arguments['list']:
                user = Users()
                display_fmt = None
                user_name = None
                status = None
                if arguments['--format']:
                    display_fmt = arguments['--format']
                if arguments['USERNAME']:
                    user_name = arguments['USERNAME']
                if arguments['--status']:
                    status = arguments['--status']
                user.list_users(display_fmt, user_name, status)
            elif arguments['admin'] and arguments['user'] and arguments['add']:
                user = Users()
                user.create_user_from_file(arguments['YAMLFILE'])
            elif arguments['admin'] and arguments['user'] and arguments[
                    'delete']:
                if arguments['USERNAME']:
                    user = Users()
                    user.delete_user(arguments['USERNAME'])
                else:
                    Console.error("Please specify a user to be removed")
            elif arguments['admin'] and arguments['user'] and arguments[
                    'clear']:
                user = Users()
                user.clear()
            elif arguments['admin'] and arguments['user'] and arguments[
                    'status']:
                user = Users()
                user_status = user.get_user_status(arguments['USERNAME'])
                if user_status:
                    Console.info("Status of user " + arguments['USERNAME'] +
                                 " - " +
                                 user.get_user_status(arguments['USERNAME']))
                else:
                    Console.info(
                        "User {0} not available in the database.".format(
                            arguments['USERNAME']))
                return
            elif arguments['admin'] and arguments['user'] and arguments[
                    'approve']:
                if arguments['USERNAME']:
                    user = Users()
                    user.amend_user_status(arguments['USERNAME'],
                                           new_status='approved')
                    Console.info("User " + arguments['USERNAME'] +
                                 " approved.")
                else:
                    Console.error("Please specify a user to be amended")
            elif arguments['admin'] and arguments['user'] and arguments[
                    'activate']:
                if arguments['USERNAME']:
                    user = Users()
                    user.amend_user_status(arguments['USERNAME'],
                                           new_status='active')
                    Console.info("User " + arguments['USERNAME'] +
                                 " activated.")
                else:
                    Console.error("Please specify a user to be amended")
            elif arguments['admin'] and arguments['user'] and arguments[
                    'suspend']:
                if arguments['USERNAME']:
                    user = Users()
                    user.amend_user_status(arguments['USERNAME'],
                                           new_status='suspended')
                    Console.info("User " + arguments['USERNAME'] +
                                 " suspended.")
                else:
                    Console.error("Please specify a user to be amended")
            elif arguments['admin'] and arguments['user'] and arguments[
                    'block']:
                if arguments['USERNAME']:
                    user = Users()
                    user.amend_user_status(arguments['USERNAME'],
                                           new_status='blocked')
                    Console.info("User " + arguments['USERNAME'] + " blocked.")
                else:
                    Console.error("Please specify a user to be amended")
            elif arguments['admin'] and arguments['user'] and arguments['deny']:
                if arguments['USERNAME']:
                    user = Users()
                    user.amend_user_status(arguments['USERNAME'],
                                           new_status='denied')
                    Console.info("User " + arguments['USERNAME'] + " denied.")
                else:
                    Console.error("Please specify a user to be amended")
            elif arguments['admin'] and arguments['user'] and arguments[
                    'assign']:
                if arguments['USERNAME'] and arguments['ROLE']:
                    user = Users()
                    user.set_role(arguments['USERNAME'], arguments['ROLE'])
            elif arguments['admin'] and arguments['user'] and arguments[
                    'password']:
                user = Users()
                user.set_password(arguments['USERNAME'], arguments['PASSWORD'])
            elif arguments['admin'] and arguments['user'] and arguments[
                    'projects']:
                user = Users()
                user.list_projects(arguments['USERNAME'])
            #
            # Project part
            #
            elif arguments['admin'] and arguments['project'] and arguments[
                    'generate']:
                if arguments['--count']:
                    count = int(arguments['--count'])
                    generate_projects(count)
                    Console.info(str(count) + " projects generated.")
                else:
                    generate_projects(10)
                    Console.info("10 projects generated.")
            elif arguments['admin'] and arguments['project'] and arguments[
                    'list']:
                project = Projects()
                display_fmt = None
                project_id = None
                if arguments['--format']:
                    display_fmt = arguments['--format']
                if arguments['PROJECTID']:
                    project_id = arguments['PROJECTID']
                project.list_projects(display_fmt, project_id)
            elif arguments['admin'] and arguments['project'] and arguments[
                    'add'] and arguments['YAMLFILE']:
                project = Projects()
                project.create_project_from_file(arguments['YAMLFILE'])
            elif arguments['admin'] and arguments['project'] and arguments[
                    'delete']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.delete_project(arguments['PROJECTID'])
                else:
                    Console.error("Please specify a project id to be removed")
            elif arguments['admin'] and arguments['project'] and arguments[
                    'clear']:
                project = Projects()
                project.clear()
                Console.info("Projects cleared from the database.")
            elif arguments['admin'] and arguments['project'] and arguments[
                    'status']:
                project = Projects()
                Console.info(
                    "Status of project is: " +
                    project.get_project_status(arguments['PROJECTID']))
            elif arguments['admin'] and arguments['project'] and arguments[
                    'activate']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.set_project_status(arguments['PROJECTID'],
                                               'active')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " activated.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['admin'] and arguments['project'] and arguments[
                    'deactivate']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.set_project_status(arguments['PROJECTID'],
                                               'blocked')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " de-activated.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['admin'] and arguments['project'] and arguments[
                    'close']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.set_project_status(arguments['PROJECTID'],
                                               'closed')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " closed.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['admin'] and arguments['project'] and arguments[
                    'add'] and arguments['USERNAME']:
                project = Projects()
                project.add_user(arguments['USERNAME'], arguments['PROJECTID'],
                                 arguments['ROLE'])
            elif arguments['admin'] and arguments['project'] and arguments[
                    'remove'] and arguments['USERNAME']:
                project = Projects()
                project.remove_user(arguments['USERNAME'],
                                    arguments['PROJECTID'], arguments['ROLE'])
            #
            # Database export/import part
            #
            elif arguments['admin'] and arguments['export']:
                database = None
                username = None
                password = None
                if arguments['DATABASE']:
                    database = arguments['DATABASE']
                else:
                    Console.info("Please specify the database..")
                #
                if arguments['COLLECTION']:
                    coll_name = arguments['COLLECTION']
                else:
                    coll_name = "*"
                #
                if arguments['--user']:
                    username = arguments['--user']

                if arguments['--password']:
                    password = arguments['--password']
                #
                DBUtil().serialize(db=database,
                                   collection=coll_name,
                                   user_name=username,
                                   pwd=password)
            elif arguments['admin'] and arguments['import']:
                database = None
                coll_name = None
                filename = None
                username = None
                password = None
                dir_name = None

                if arguments['--file']:
                    filename = arguments['--file']

                if arguments['--dir']:
                    dir_name = arguments['--dir']

                if arguments['--db']:
                    database = arguments['--db']

                if arguments['--collection']:
                    coll_name = arguments['--collection']

                if arguments['--user']:
                    username = arguments['--user']

                if arguments['--password']:
                    password = arguments['--password']
                #
                DBUtil().de_serialize(file=filename,
                                      dir=dir_name,
                                      db=database,
                                      collection=coll_name,
                                      user_name=username,
                                      pwd=password)
            #
            #
            # COMMITTEE SECTION
            #
            #
            elif arguments['committee'] and arguments['setup']:
                committee = Committees()
                committee.setup_committee()
            elif arguments['committee'] and arguments['remove']:
                if arguments['PROJECTID']:
                    project_id = arguments['PROJECTID']
                    committee = Committees()
                    committee.remove_committee(project_id)
                else:
                    Console.error("Please specify a valid project ID.")
            elif arguments['committee'] and arguments[
                    'reviewer'] and arguments['add']:
                if arguments['USERNAME'] and arguments['PROJECTID']:
                    username = arguments['USERNAME']
                    project_id = arguments['PROJECTID']
                    committee = Committees()
                    committee.add_reviewers(project_id, username)
                    Console.info(
                        "User {0} added as a reviewer for the project with ID {1}"
                        .format(username, project_id))
                else:
                    Console.error("Please specify a project ID and user name.")
            elif arguments['committee'] and arguments[
                    'reviewer'] and arguments['remove']:
                if arguments['USERNAME'] and arguments['PROJECTID']:
                    username = arguments['USERNAME']
                    project_id = arguments['PROJECTID']
                    committee = Committees()
                    committee.remove_reviewers(project_id, username)
                    Console.info(
                        "User {0} added as a reviewer for the project with ID {1}"
                        .format(username, project_id))
                else:
                    Console.error("Please specify a project ID and user name.")
            elif arguments['committee'] and arguments['list']:
                committee = Committees()
                committee.list_committee()
            elif arguments['committee'] and arguments['project'] and arguments[
                    'status']:
                project = Projects()
                Console.info(
                    "Status of project is: " +
                    project.get_project_status(arguments['PROJECTID']))
            elif arguments['committee'] and arguments['project'] and arguments[
                    'list']:
                project = Projects()
                display_fmt = None
                project_id = None
                if arguments['--format']:
                    display_fmt = arguments['--format']
                if arguments['PROJECTID']:
                    project_id = arguments['PROJECTID']
                project.list_projects(display_fmt, project_id)
            elif arguments['committee'] and arguments['project'] and arguments[
                    'approve']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.amend_project_status(arguments['PROJECTID'],
                                                 'approved')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " approved.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['committee'] and arguments['project'] and arguments[
                    'deny']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.amend_project_status(arguments['PROJECTID'],
                                                 'denied')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " denied.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['committee'] and arguments['project'] and arguments[
                    'block']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.amend_project_status(arguments['PROJECTID'],
                                                 'blocked')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " blocked.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['committee'] and arguments['project'] and arguments[
                    'activate']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.set_project_status(arguments['PROJECTID'],
                                               'active')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " activated.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['committee'] and arguments['project'] and arguments[
                    'close']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.amend_project_status(arguments['PROJECTID'],
                                                 'closed')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " closed.")
                else:
                    Console.error("Please specify a project to be amended")
            elif arguments['committee'] and arguments['project'] and arguments[
                    'open']:
                if arguments['PROJECTID']:
                    project = Projects()
                    project.amend_project_status(arguments['PROJECTID'],
                                                 'opened')
                    Console.info("Project " + arguments['PROJECTID'] +
                                 " opened.")
                else:
                    Console.error("Please specify a project to be amended")
            #
            #
            # USER SECTION
            #
            #
            elif arguments['user'] and arguments['apply']:
                if arguments['--user']:
                    user = Users()
                    user.create_user_from_file(arguments['--user'])
                elif arguments['--project']:
                    project = Projects()
                    project.create_project_from_file(arguments['--project'])
                else:
                    Console.info(
                        "Submit a yaml file in the following format if you are applying for a user account:"
                    )
                    with open('etc/cloudmesh_user_info.yaml', 'r') as f:
                        doc = yaml.load(f)
                    print(
                        "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
                    )
                    print(yaml.dump(doc, default_flow_style=False))
                    print(
                        "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
                    )
                    Console.info(
                        "Submit a yaml file in the following format if you want to setup a project:"
                    )
                    print(
                        "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
                    )
                    with open('etc/cloudmesh_project_info.yaml', 'r') as f:
                        doc = yaml.load(f)
                    print(yaml.dump(doc, default_flow_style=False))
                    print(
                        "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
                    )
            elif arguments['user'] and arguments['configure']:
                if arguments['USERNAME']:
                    user = Users()
                    user.create_config(arguments['USERNAME'])
                else:
                    Console.error("Please specify a username to configure.")
            elif arguments['user'] and arguments['password']:
                user = Users()
                current_user = get_current_user()
                if current_user:
                    if current_user == arguments['user']:
                        user.set_password(arguments['user'],
                                          arguments['password'])
                    else:
                        Console.error("Set the password for your username.")
                else:
                    Console.error("Local User configuration not found.")
                implement()
            elif arguments['user'] and arguments['status']:
                implement()
        except Exception, e:
            Console.error("Invalid arguments")
            print(e)
Exemplo n.º 42
0
def implement():
    Console.info("Yet to be implemented..")
Exemplo n.º 43
0
    def de_serialize(self, **kwargs):
        """
        The method is used to import the data in the provided json file into a collection within the database

        :param kwargs:
                    Can contain a collection name, database name, file name, directory name containing the json files,
                     credentials for authentication to the database.

                    If a collection name is not specified, uses the name of the file as the collection name or name of
                    the files in the specified directory as the collection name.
        :return:
                None
        """

        filename = None
        username = None
        password = None
        dir_name = None

        for key, value in kwargs.iteritems():
            if key == "collection":
                self._collection = value
            elif key == "db":
                self._database = value
            elif key == "file":
                filename = value
            elif key == "dir":
                dir_name = value
            elif key == "user_name":
                username = value
            elif key == "pwd":
                password = value
        #
        if self._database:
            if filename is None and dir_name is None:
                Console.error(
                    "Please specify a filename or a directory name that contains the data to be imported."
                )
                return

            if filename:
                if not self._collection:
                    sep1_idx = filename.rfind('/')
                    if sep1_idx == -1:
                        sep1_idx = 0
                    if filename.rfind('.') != -1:
                        self._collection = filename[sep1_idx +
                                                    1:filename.rfind('.')]
                    else:
                        self._collection = filename[sep1_idx:]

                self._db_conn = DBConnFactory.getconn(self._database)
                if self._collection not in self._db_conn.collection_names():
                    self._db_conn.create_collection(self._collection)
                #
                self._conn = self.connect(user_name=username, pwd=password)
                if self._conn:
                    f = None
                    try:
                        f = open(filename, "rb")
                        bson_data = f.read()
                        json_data = re.sub(r'ObjectId\s*\(\s*\"(\S+)\"\s*\)',
                                           r'{"$oid": "\1"}', bson_data)
                        json_data = re.sub(r'Date\s*\(\s*(\S+)\s*\)',
                                           r'{"$date": \1}', json_data)
                        data = json.loads(json_data,
                                          object_hook=json_util.object_hook)
                        _db = self._conn[self._database]
                        c = _db[self._collection]
                        for x in data:
                            c.insert(x)
                        count = c.count()
                        Console.info("{0} records imported into {1}.".format(
                            count, self._collection))
                    except IOError:
                        Console.error("File not found: {0}".format(filename))

                if self._conn:
                    try:
                        self._conn.close()
                    except:
                        Console.error("Error in closing connection..")

            if dir_name:
                if os.path.isdir(dir_name):
                    if os.listdir(dir_name):
                        for file_item in os.listdir(dir_name):
                            if file_item.endswith(".json"):
                                if file_item.rfind('.') != -1:
                                    self._collection = file_item[0:file_item.
                                                                 rfind('.')]
                                else:
                                    self._collection = filename[0:]

                            Console.info(self._collection)

                            self._db_conn = DBConnFactory.getconn(
                                self._database)
                            if self._collection not in self._db_conn.collection_names(
                            ):
                                self._db_conn.create_collection(
                                    self._collection)
                            #
                            self._conn = self.connect(user_name=username,
                                                      pwd=password)
                            if self._conn:
                                f = None
                                try:
                                    f = open(dir_name + "/" + file_item, "rb")
                                    bson_data = f.read()
                                    json_data = re.sub(
                                        r'ObjectId\s*\(\s*\"(\S+)\"\s*\)',
                                        r'{"$oid": "\1"}', bson_data)
                                    json_data = re.sub(
                                        r'Date\s*\(\s*(\S+)\s*\)',
                                        r'{"$date": \1}', json_data)
                                    data = json.loads(
                                        json_data,
                                        object_hook=json_util.object_hook)
                                    _db = self._conn[self._database]
                                    c = _db[self._collection]
                                    for x in data:
                                        c.insert(x)
                                    count = c.count()
                                    Console.info(
                                        "{0} records imported into {1}.".
                                        format(count, self._collection))
                                except IOError:
                                    Console.error(
                                        "Error in opening file: {0}".format(
                                            filename))

                            if self._conn:
                                try:
                                    self._conn.close()
                                except:
                                    Console.error(
                                        "Error in closing connection..")
                    else:
                        Console.error(
                            "Source Directory - {0} - is empty.".format(
                                dir_name))
                    return
                else:
                    Console.error(
                        "Invalid Source Directory - {0}.".format(dir_name))
        else:
            Console.error("Please specify a target database.")
        pass
Exemplo n.º 44
0
    def serialize(self, **kwargs):
        """
        The method is used to export the data from the requested collection into a json file

        :param kwargs:
                    Can contain a collection name, database name, credentials for authentication to
                    the database.

                    If a collection name is not specified, it tries to export all the collections within the
                    database.
        :return:
                Saves the data into a json file named after the collection. The file will be saved under
                dump/<database>/
        """
        username = None
        password = None
        for key, value in kwargs.iteritems():
            if key == "collection":
                self._collection = value
            elif key == "db":
                self._database = value
            elif key == "user_name":
                username = value
            elif key == "pwd":
                password = value
        #
        if self._database:
            self._conn = self.connect(user_name=username, pwd=password)
            if self._conn:
                self._db_conn = self._conn[self._database]
                if not self._collection == "*":
                    self._coll_conn = self._db_conn[self._collection]
                    #
                    dir_name = "dump"
                    if not os.path.exists(dir_name):
                        os.makedirs(dir_name)
                    sub_dir = dir_name + "/" + self._database
                    if not os.path.exists(sub_dir):
                        os.makedirs(sub_dir)
                    json_file = sub_dir + "/" + self._collection + ".json"
                    if self._db_conn:
                        docs = self._coll_conn.find()
                        if self._coll_conn.find().count() > 0:
                            with open(json_file, "w") as outfile:
                                dump = dumps([doc for doc in docs])
                                outfile.write(dump)
                            Console.info(
                                " Collection - {0} - written to file - {1}".
                                format(self._collection, json_file))
                        else:
                            Console.info("Looks like collection is empty.")
                    else:
                        Console.error(
                            "Error in getting connection to the collection..")
                else:
                    # implement()
                    for item in self._db_conn.collection_names(
                            include_system_collections=False):
                        self._coll_conn = self._db_conn[item]
                        #
                        dir_name = "dump"
                        if not os.path.exists(dir_name):
                            os.makedirs(dir_name)
                        sub_dir = dir_name + "/" + self._database
                        if not os.path.exists(sub_dir):
                            os.makedirs(sub_dir)
                        json_file = sub_dir + "/" + item + ".json"
                        if self._db_conn:
                            docs = self._coll_conn.find()
                            if self._coll_conn.find().count() > 0:
                                with open(json_file, "w") as outfile:
                                    dump = dumps([doc for doc in docs])
                                    outfile.write(dump)
                                Console.info(
                                    " Collection - {0} - written to file - {1}"
                                    .format(item, json_file))
                            else:
                                Console.info(
                                    "Looks like the collection \"{0}\" is empty."
                                    .format(item))
                        else:
                            Console.error(
                                "Error in getting connection to the collection.."
                            )
            if self._conn:
                try:
                    self._conn.close()
                except:
                    Console.error("Error in closing connection..")
        else:
            Console.error("Please specify a database name.")
        pass