Ejemplo n.º 1
0
def newuser(cursor, config, args, ticketid):
    # get the ticket (the ID is unique so there is only one)
    cursor.execute(thomas_queries.getsafeticket(), {'id':ticketid})
    result = cursor.fetchall()
    # this dict will be needed when we create the user
    user_dict = {'username': result[0]['account_name'], 
                 'given_name': result[0]['firstname'],
                 'surname': result[0]['lastname'],
                 'email': result[0]['email'],
                 'ssh_key': result[0]['publickey'],
                 'status': "active"}
    # check that we don't already have a username for them
    if "to_be_allocated_" in user_dict['username']:
        # check if they are a UCL user: UCL email
        if "ucl.ac.uk" in user_dict['email']:
            # UCL: get username from AD
            user_dict['username'] = thomas_utils.AD_username_from_email(config, user_dict['email'])
            print("UCL username found from AD: " + user_dict['username'])
        else:
            # not UCL, get next mmm username
            user_dict['username'] = thomas_utils.getunusedmmm(cursor)
            print("Not UCL email, username is " + user_dict['username'])
    # we have a non-placeholder username
    else:
        print("Using ticket-provided username: "******"SAFE ticket was for " + result[0]['machine'].casefold() + "and you are on " + cluster + ", exiting.")
        exit(1)    
    
    # update SAFE and close the ticket
    updateticket(config, args, updatenewuser(ticketid, user_dict['username']))
    # update ticket status in our DB
    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticketid, 'status':'Completed'})
Ejemplo n.º 2
0
def movegold(cursor, config, args, ticketid):
    # get the ticket (the ID is unique so there is only one)
    cursor.execute(thomas_queries.getsafeticket(), {'id':ticketid})
    result = cursor.fetchall()

    # this dict will be needed when we move the Gold.
    #gold_dict = {"SourceAccountID": result[0]['source_account_id'],
    #             "SourceAllocation": result[0]['source_allocation'],
    #             "Amount": result[0]['gold_amount'], 
    #             "Project": result[0]['project']}
    description = "transfer_received_from_SAFE"

    thomas_utils.transfergold(result[0]['source_account_id'], result[0]['source_allocation'], result[0]['project'], description, result[0]['gold_amount'], args)
    # update SAFE and close the ticket
    updateticket(config, args, updategeneric(ticketid))
    # update ticket status in our DB
    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticketid, 'status':'Completed'})
Ejemplo n.º 3
0
def updateaccount(cursor, config, args, ticketid):
    # get the ticket (the ID is unique so there is only one)
    cursor.execute(thomas_queries.getsafeticket(), {'id':ticketid})
    result = cursor.fetchall()

    textinfo = result[0]['extratext']

    # update ssh key
    if "public key added" in textinfo:
        thomas_utils.addsshkey(result[0]['account_name'], result[0]['publickey'], args)
    # we don't know the other possible texts for this ticket yet
    else:
        print("Update account ticket with this text cannot currently be handled: "+textinfo)
        exit(1)
    # update SAFE and close the ticket
    updateticket(config, args, updategeneric(ticketid))
    # update ticket status in our DB
    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticketid, 'status':'Completed'})
Ejemplo n.º 4
0
def newbudget(cursor, config, args, ticketid):
    # get the ticket (the ID is unique so there is only one)
    cursor.execute(thomas_queries.getsafeticket(), {'id':ticketid})
    result = cursor.fetchall()

    # this dict will be needed when we create the budget
    budget_dict = {'project_ID': result[0]['project'],
                   'inst_ID': ''}

    # need to work out what institute it is for
    # use the first part of the project_ID up to any underscore as institute
    budget_dict['inst_ID'] = budget_dict['project_ID'].partition("_")[0] 

    # add new project to database
    thomas_utils.addproject(args, budget_dict, cursor)

    # update SAFE and close the ticket
    updateticket(config, args, updatebudget(ticketid, projectname))
    # update ticket status in our DB
    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticketid, 'status':'Completed'})
Ejemplo n.º 5
0
def addtobudget(cursor, config, args, ticketid):
    # get the ticket (the ID is unique so there is only one)
    cursor.execute(thomas_queries.getsafeticket(), {'id':ticketid})
    result = cursor.fetchall()

    # this dict will be needed when we create the projectuser
    projectuser_dict = {'username': result[0]['account_name'],
                        'project_ID': result[0]['project'],
                        'poc_id': '',
                        'poc_firstname': result[0]['poc_firstname'],
                        'poc_lastname': result[0]['poc_lastname'], 
                        'poc_email': result[0]['poc_email'],
                        'status': 'active'}
    # budget exists: get the point of contact
    projectuser_dict['poc_id'] = thomas_utils.findpocID(cursor, projectuser_dict)

    thomas_utils.addprojectuser(args, projectuser_dict, cursor)

    # update SAFE and close the ticket
    updateticket(config, args, updategeneric(ticketid))
    # update ticket status in our DB
    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticketid, 'status':'Completed'})
Ejemplo n.º 6
0
def main(argv):

    try:
        args = getargs(argv)
        # 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)
    try:
        config = configparser.ConfigParser()
        config.read_file(open(os.path.expanduser('~/.thomas.cnf')))
    #except FileNotFoundError as err:
    except OSError as err:
        print(err)

    # Read tickets from a file
    if args.jsonfile is not None:
        parsejsonfile(args.jsonfile)

    # Show tickets live from SAFE
    if args.show:
        # get SAFE tickets
        ticketlist = gettickets(config)

        # print SAFE tickets
        for t in ticketlist:
            #print(str(t.Ticket))
            values = [t.Ticket.Id, t.Ticket.Type, t.Ticket.Status, t.Ticket.Account.Name, t.Ticket.Machine, t.Ticket.ProjectGroup.Code, t.Ticket.Account.Person.FirstName, t.Ticket.Account.Person.LastName, t.Ticket.Account.Person.Email, t.Ticket.Account.Person.NormalisedPublicKey, t.Ticket.Approver.FirstName, t.Ticket.Approver.LastName, t.Ticket.Approver.Email, t.Ticket.GoldTransfer.SourceAccountID, t.Ticket.GoldTransfer.SourceAllocation, t.Ticket.GoldTransfer.Amount, t.Ticket.ExtraText, t.Ticket.StartDate, t.Ticket.EndDate]
            print(values)
        print("Number of pending tickets: " + str(len(ticketlist)))

    # these options require a database connection
    if args.refresh or args.close is not None or args.reject is not None:
        try:
            conn = mysql.connector.connect(option_files=os.path.expanduser('~/.thomas.cnf'), option_groups='thomas_update', database='thomas')
            cursor = conn.cursor(dictionary=True)

            # Refresh the database tickets
            if args.refresh:
                # get SAFE tickets as list of dicts
                ticketdicts = ticketstodicts(gettickets(config))
                # refresh tickets in database
                for t in ticketdicts:
                    cursor.execute(thomas_queries.refreshsafetickets(), t)
                    thomas_utils.debugcursor(cursor, args.debug)
                # show database tickets (not inc ssh key)
                print("Refreshed tickets:")
                cursor.execute(thomas_queries.showpendingtickets())
                thomas_utils.tableprint_dict(cursor.fetchall())
    
            # Update and close SAFE tickets
            if args.close is not None:
                # for readability below
                ticket = args.close
                # get the type of ticket - ticket id is unique so there is only one
                # (Either make a temporary dict or pass in (ticket,) with the comma which is ugly).
                cursor.execute(thomas_queries.safetickettype(), {'id':ticket})
                result = cursor.fetchall()
                # make sure we got a result, or exit
                if cursor.rowcount < 1:
                    print("No tickets with id " + ticket + " found, exiting.")
                    exit(1)

                tickettype = result[0]['type']
                # store all the ticket info

                # new user
                if tickettype == "New User":
                    newuser(cursor, config, args, ticket)
                    # Each new user ticket should have a matching Add to budget ticket.
                    # Find it if it exists and complete it too.
                    match = matchbudgetticket(cursor, ticket)
                    if match is not None:
                        print("Matching 'Add to budget' ticket " + str(match['ticket_ID'])  +  " found for this new user, carrying out.")
                        addtobudget(cursor, config, args, match['ticket_ID'])

                # new budget
                elif tickettype == "New Budget":
                    newbudget(cursor, config, args, ticket)
                # add to budget
                elif tickettype == "Add to budget":
                    addtobudget(cursor, config, args, ticket)
                # update account info
                elif tickettype == "Update account":
                    updateaccount(cursor, config, args, ticket)
                # move Gold and refresh SAFE
                elif tickettype == "Move gold":
                    movegold(cursor, config, args, ticket)
                    thomas_utils.refreshSAFEgold(args)
                else:
                    print("Ticket " + ticket + " type unrecognised: " + tickettype)
                    exit(1)
                 
            # Reject SAFE tickets - there are two types of rejection so ask
            if args.reject is not None:
                ticket = args.reject
                answer = thomas_utils.select_from_list("Reason to reject ticket: would it cause an error, or is it being rejected for any other reason?", ("other", "error"), default_ans="other")
                if answer == "error":
                    updateticket(config, args, rejecterror(ticket))
                    # update ticket status in our DB
                    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticket, 'status':'Error'})

                else:
                    updateticket(config, args, rejectother(ticket))
                    # update ticket status in our DB
                    cursor.execute(thomas_queries.updatesafestatus(), {'id':ticket, 'status':'Refused'})

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

        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)
        else:
            cursor.close()
            conn.close()