コード例 #1
0
def deactivate_user_request(cursor, args, args_dict):
    # first, get user's active project memberships
    cursor.execute(thomas_queries.projectinfo(), args_dict)
    debug_cursor(cursor, args)
    results = cursor.fetchall()
    if (args.debug):
        print("Active projects found:")
        thomas_utils.tableprint_dict(results)
    if not args.force:
        # if they have any active, prompt (may have access via another inst)
        for row in results:
            answer = thomas_utils.are_you_sure("User has active membership in project " + row['project'] + " - deactivate it?", False)
            # they said no, exit
            if not answer:
                print("Active project membership being kept: user will not be deactivated.")
                exit(0)
    # deactivate all user's active memberships
    cursor.execute(thomas_queries.deactivatemembership(), args_dict)
    debug_cursor(cursor, args)

    # status in requests is pending until the full deactivation is done by us
    args_dict['status'] = "pending"
    
    # set user status to deactivated (can't run jobs but doesn't affect login)
    cursor.execute(thomas_queries.deactivateuser(), args_dict)
    debug_cursor(cursor, args)
    # get the deletion requestor and add to dictionary
    #cursor.execute(run_poc_email(), args_dict)
    #poc_email = cursor.fetchall()[0][0]
    #args_dict['poc_email'] = poc_email
    # add the account deactivation request to the database
    cursor.execute(run_deactivaterequest(), args_dict)
    debug_cursor(cursor, args)
コード例 #2
0
def check_dups(key_string, cursor, args, args_dict):
    cursor.execute(thomas_queries.findduplicate(key_string), args_dict)
    results = cursor.fetchall()
    rows_count = cursor.rowcount
    if rows_count > 0:
        # We have duplicate(s). Show results and ask them to pick one or none
        print(
            str(rows_count) + " user(s) with this " + key_string +
            " already exist:\n")
        data = []
        # put the results into a list of dictionaries, keys being db column names.
        for i in range(rows_count):
            data.append(dict(list(zip(cursor.column_names, results[i]))))
            # while we do this, print out the results, numbered.
            print(
                str(i + 1) + ") " + data[i]['username'] + ", " +
                data[i]['givenname'] + " " + data[i]['surname'] + ", " +
                data[i]['email'] + ", created " +
                str(data[i]['creation_date']))

        # make a string list of options, counting from 1 and ask the user to pick one
        options_list = [str(x) for x in range(1, rows_count + 1)]
        response = thomas_utils.select_from_list(
            "\nDo you want to add a new project to one of the existing accounts instead? \n(You should do this if it is the same individual). \n Please respond with a number in the list or n for none.",
            options_list)

        # said no to using existing user
        if response == "n":
            # can create a duplicate if it is *not* a username duplicate
            if key_string != "username":
                if thomas_utils.are_you_sure(
                        "Do you want to create a second account with that " +
                        key_string + "?"):
                    # create new duplicate user
                    create_new_user(cursor, args, args_dict)
                    return True
                # said no to everything
                else:
                    print(
                        "No second account requested, doing nothing and exiting."
                    )
                    exit(0)
            # Was a username duplicate
            else:
                print("Username in use, doing nothing and exiting.")
                exit(0)
        # picked an existing user
        else:
            # go back to zero-index, get chosen username
            args.username = data[int(response) - 1]['username']
            print("Using existing user " + args.username)
            create_user_request(cursor, args, args_dict)
            return True

    # there were no duplicates and we did nothing
    return False
コード例 #3
0
def check_dups(key_string, cursor, args, args_dict):
    cursor.execute(thomas_queries.findduplicate(key_string), args_dict)
    results = cursor.fetchall()
    rows_count = cursor.rowcount
    if rows_count > 0:
        # We have duplicate(s). Show results and ask them to pick one or none
        print(str(rows_count) + " user(s) with this " +key_string+ " already exist:\n")
        data = []
        # put the results into a list of dictionaries, keys being db column names.
        for i in range(rows_count):
            data.append(dict(list(zip(cursor.column_names, results[i]))))
            # while we do this, print out the results, numbered.
            print(str(i+1) + ") "+ data[i]['username'] +", "+ data[i]['givenname'] +" "+ data[i]['surname'] +", "+ data[i]['email'] + ", created " + str(data[i]['creation_date']))

        # make a string list of options, counting from 1 and ask the user to pick one
        options_list = [str(x) for x in range(1, rows_count+1)]
        response = thomas_utils.select_from_list("\nDo you want to add a new project to one of the existing accounts instead? \n(You should do this if it is the same individual). \n Please respond with a number in the list or n for none.", options_list)

        # said no to using existing user
        if response == "n":
            # can create a duplicate if it is *not* a username duplicate
            if key_string != "username":
                if thomas_utils.are_you_sure("Do you want to create a second account with that "+key_string+"?"):
                    # create new duplicate user
                    create_new_user(cursor, args, args_dict)
                    return True
                # said no to everything
                else: 
                    print("No second account requested, doing nothing and exiting.")
                    exit(0)
            # Was a username duplicate
            else:
                print("Username in use, doing nothing and exiting.")
                exit(0) 
        # picked an existing user
        else:
            # go back to zero-index, get chosen username
            args.username = data[int(response)-1]['username']
            print("Using existing user " + args.username)
            create_user_request(cursor, args, args_dict) 
            return True

    # there were no duplicates and we did nothing
    return False
コード例 #4
0
def create_new_user(cursor, args, args_dict):
    if (args.livedebug):
        print("-- start thomas_add.create_new_user")
    # if no username was specified, get the next available mmm username
    if (args.username == None or args_dict['username'] == ''):
        args.username = nextmmm()
        args_dict['username'] = args.username
    # users status is pending until the request is approved
    args_dict['status'] = "pending"
    # confirm that info is ok unless --noconfirm is set
    if not args.noconfirm:
        if not thomas_utils.are_you_sure("\nDo you want to create the user account with this information? \n    Username: "******"\n    Email: "+args.email+ "\n    SSH key: "+args.ssh_key+"\n"):
            print("Entry rejected: doing nothing and exiting.")
            exit(0)
  
    print("")
    # insert new user into users table      
    cursor.execute(thomas_queries.adduser(args.surname), args_dict)
    debug_cursor(cursor, args)
    # create the account creation request and get the request id (as a list)
    create_user_request(cursor, args, args_dict)
コード例 #5
0
def check_dups(key_string, cursor, args, args_dict):
    if (args.livedebug):
        print("-- start thomas_create.check_dups")
    cursor.execute(thomas_queries.findduplicate(key_string), args_dict)
    results = cursor.fetchall()
    rows_count = cursor.rowcount
    if rows_count > 0:
        # We have duplicate(s). Show results and ask them to pick one or none
        print(
            str(rows_count) + " user(s) with this " + key_string +
            " already exist:\n")
        print(results)
        # put the results into a list of dictionaries, keys being db column names.
        #for i in range(rows_count):
        #data.append(dict(list(zip(cursor.column_names, results[i]))))
        # while we do this, print out the results, numbered.
        #print(str(i+1) + ") "+ data[i]['username'] +", "+ data[i]['givenname'] +" "+ data[i]['surname'] +", "+ data[i]['email'] + ", created " + str(data[i]['creation_date']))

        # can create a duplicate if it is *not* a username duplicate
        if key_string != "username":
            if thomas_utils.are_you_sure(
                    "Do you want to create a second account with that " +
                    key_string + "?"):
                return True
            # said no to everything
            else:
                print(
                    "No second account requested, doing nothing and exiting.")
                exit(0)
        # Was a username duplicate
        else:
            print("\n Username in use, doing nothing and exiting.")
            exit(0)

    # there were no duplicates and we did nothing
    return False
コード例 #6
0
def main(argv):

    # get the name of this cluster
    nodename = thomas_utils.getnodename()

# Doesn't appear to be an MMM cluster, prompt whether you really want to do this
    if not ("thomas" in nodename or "michael" in nodename or "young" in nodename):
        answer = thomas_utils.are_you_sure("Current hostname does not appear to be on Thomas or Michael or Young ("+nodename+")\n Do you want to continue?", False)
        # they said no, exit
        if not answer:
            exit(1)

    # get all the parsed args
    try:
        args = getargs(argv)
        # add cluster name to args
        args.cluster = thomas_utils.getcluster(nodename)
        # make a dictionary from args to make string substitutions doable by key name
        args_dict = vars(args)
    except ValueError as err:
        print(err, file=sys.stderr)
        exit(1)

    # Check that the user running the add command is a member of ccsprcop or lgmmmpoc
    if not validate.user_has_privs():
        print("You need to be a member of the lgmmmpoc or ccsprcop groups to run the deactivate commands. Exiting.", file=sys.stderr)
        exit(1)

    # get the MMM db to connect to
    db = thomas_utils.getdb(nodename)

    # connect to MySQL database with write access.
    # (.thomas.cnf has readonly connection details as the default option group)

    try:
        #conn = mysql.connector.connect(option_files=os.path.expanduser('~/.thomas.cnf'), option_groups='thomas_update', database=db)
        #cursor = conn.cursor()
        # make sure we close the connection wherever we exit from
        with closing(mysql.connector.connect(option_files=os.path.expanduser('~/.thomas.cnf'), option_groups='thomas_update', database=db)) as conn, closing(conn.cursor()) as cursor:

            if (args.verbose or args.debug):
                print("")
                print(">>>> Queries being sent:")

            # cursor.execute takes a querystring and a dictionary or tuple
            if (args.subcommand == "user"):
                #deactivate_user_request(cursor, args, args_dict)
                #print(args.username + "'s membership of " + args.project + " has been deactivated.")
                print("User deactivation functionality is not available yet - please email [email protected]")
            elif (args.subcommand == "projectuser"):
                cursor.execute(thomas_queries.deactivateprojectuser(), args_dict)
                print(args.username + "'s membership of " + args.project + " is being deactivated.")
                debug_cursor(cursor, args)
            elif (args.subcommand == "project"):
                #cursor.execute(run_project(), args_dict)
                #debug_cursor(cursor, args)
                print("Whole project deactivation is not available yet. User membership in projects can be deactivated with the projectuser option.")
            elif (args.subcommand == "poc"):
                cursor.execute(run_poc(args.surname, args.username), args_dict)
                debug_cursor(cursor, args)
            elif (args.subcommand == "institute"):
                cursor.execute(run_institute(), args_dict)
                debug_cursor(cursor, args)

            # commit the change to the database unless we are debugging
            if (not args.debug):
                if (args.verbose):
                    print("")
                    print("Committing database change")
                    print("")
                conn.commit()

            # Databases are updated, now email rc-support unless nosupportemail is set
            #if (args.subcommand == "user" and args.nosupportemail == False):
                # get the last id added (which is from the requests table)
                # this has to be run after the commit
                #last_id = cursor.lastrowid
                #contact_rc_support(args, last_id)

    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Access denied: Something is wrong with your user name or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Database does not exist")
        else:
            print(err, file=sys.stderr)
    else:
        cursor.close()
        conn.close()
コード例 #7
0
            print("-- -- No automatable requests found.")


# end automaterequests

if __name__ == "__main__":

    # check we are on an MMM Hub cluster before continuing.
    # Later we also need to check if we are on the correct cluster for this project.
    nodename = thomas_utils.getnodename()
    # if fqdn does not contain a suitable hostname prompt whether you really want to do this
    # (in case you *are* somewhere on those clusters and fqdn is not useful)
    if not ("thomas" in nodename or "michael" in nodename
            or "young" in nodename):
        answer = thomas_utils.are_you_sure(
            "Current hostname does not appear to be on Thomas or Michael or Young ("
            + nodename + ")\n Do you want to continue?", False)
        # they said no, exit
        if not answer:
            exit(1)

    # get all the parsed args
    try:
        args = getargs()
        # make a dictionary from args to make string substitutions doable by key name
        args_dict = vars(args)
    except ValueError as err:
        print(err, file=sys.stderr)
        exit(1)

    # Check that the user running the create command is a member of ccsprcop or lgmmmpoc
コード例 #8
0
                else:
                    print("Request id " +str(row['id'])+ "was for "+args.cluster+" and this is "+nodename)
            else:
                print("Request id " + str(row['id']) + " was already approved by " + row['approver'])

# end approverequest    

if __name__ == "__main__":

    # check we are on Thomas or Michael before continuing.
    # Later we also need to check if we are on the correct cluster for this project.
    nodename = thomas_utils.getnodename()
    # if fqdn does not contain a suitable hostname prompt whether you really want to do this
    # (in case you *are* somewhere on those clusters and fqdn is not useful)
    if not ("thomas" in nodename or "michael" in nodename):
        answer = thomas_utils.are_you_sure("Current hostname does not appear to be on Thomas or Michael ("+nodename+")\n Do you want to continue?", False)    
        # they said no, exit
        if not answer:
            exit(1)

    # get all the parsed args
    try:
        args = getargs()
        # make a dictionary from args to make string substitutions doable by key name
        args_dict = vars(args)
    except ValueError as err:
        print(err)
        exit(1)

    # connect to MySQL database with write access.
    # (.thomas.cnf has readonly connection details as the default option group)