def add_task_to_history(loanid, msg): session = current_app.db.session('relengapi') l = session.query(Loans).get(loanid) history = History(for_loan=l, timestamp=tz.utcnow(), msg=msg) session.add(history) session.commit() logger.debug("Log_line: %s" % msg)
def test_UTCDateTime_no_convert_utc(app): session = app.db.session('test_db') now = tz.utcnow() session.add(DevTable(date=now)) session.commit() instances = session.query(DevTable).all() eq_(1, len(instances)) ok_(isinstance(instances[0].date, datetime.datetime)) eq_(instances[0].date.replace(tzinfo=None), now.replace(tzinfo=None))
def update_loan_action(action_id, body): "Update a specific manual actions for a loan" session = g.db.session('relengapi') action = ManualActions.query.get(action_id) if body.complete and action.timestamp_complete is None: action.timestamp_complete = tz.utcnow() action.complete_by = current_user.authenticated_email elif not body.complete: raise BadRequest("Once actions are completed, cannot undo.") else: logger.debug("Attempted to complete this action twice") return action.to_wsme() session.add(action) history = History(loan_id=action.loan_id, timestamp=tz.utcnow(), msg="Admin marked action (id: %s) as complete via web" % (action.id)) session.add(history) session.commit() return action.to_wsme()
def complete_loan(loanid): "Get the details of a loan, by id" # XXX: Use permissions to ensure admin | loanee session = g.db.session('relengapi') l = session.query(Loans).get(loanid) l.status = "COMPLETE" hist_line = "%s marked loan as complete" % \ (current_user.authenticated_email) history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(history) session.commit() return l.to_wsme()
def register_action_needed(self, loanid, action_name): if not action_name: raise ValueError("must supply an action name") try: session = current_app.db.session('relengapi') l = session.query(Loans).get(loanid) if action_name == "add_to_vpn": action_message = ( "Add user (%s) and machine (%s) to the VPN. " "Following https://wiki.mozilla.org/ReleaseEngineering/How_To/Update_VPN_ACL" % (l.human.ldap, l.machine.fqdn) ) elif action_name == "create_aws_system": action_message = ( "Create an aws machine for %s of the type requested (see loan history)." " Following " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave#AWS_machines" % (l.human.ldap,) ) elif action_name == "clean_secrets": action_message = ( "Clean secrets from the machine. See instructions at " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave#Cleaning" ) elif action_name == "notify_complete": action_message = ( "Notify the loanee in e-mail and the loan bug (Bug %s) that the loan is ready. " "See template text for both in " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave#Notifying" % l.bug_id ) elif action_name == "gpo_switch": action_message = ( "Need to switch host (%s) to be in the Loaner GPO group. Follow " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave" "#t-xp32-ix.2C_t-w732-ix.2C_t-w864-ix.2C_w64-ix-slave " "for more information" % (l.machine.fqdn) ) else: raise ValueError("Invalid action name") action = ManualActions(for_loan=l, timestamp_start=tz.utcnow(), msg=action_message) session.add(action) session.commit() return action.id except ValueError: raise # Don't indefinitely retry in this case except Exception as exc: self.retry(exc=exc)
def test_complete_loan_history(app, client): "DELETEing a loan adds a history line" initial_time = tz.utcnow() with app.app_context(): q = History.query q = q.filter(History.loan_id == 1) q = q.order_by(History.timestamp) eq_(0, len(q.all())) resp = client.open('/slaveloan/loans/1', method='DELETE') eq_(resp.status_code, 200) histories = q.all() eq_(1, len(q.all())) ok_(histories[0].timestamp > initial_time)
def new_loan_request(body): "User Loan Requesting, returns the id of the loan" if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not p.slaveloan.admin.can(): if not body.ldap_email == current_user.authenticated_email: raise BadRequest("You can't request loans on behalf of others.") if not body.requested_slavetype: raise BadRequest("Missing slavetype") slavetype = slave_to_slavetype(body.requested_slavetype) if not slavetype: raise BadRequest("Unsupported slavetype") if not body.bugzilla_email: # Set bugzilla e-mail to ldap e-mail by default body.bugzilla_email = body.ldap_email session = g.db.session('relengapi') try: h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") if body.loan_bug_id: l = Loans(status="PENDING", human=h, bug_id=body.loan_bug_id) else: l = Loans(status="PENDING", human=h) hist_line = "%s issued a loan request for slavetype %s (original: '%s')" % \ (current_user.authenticated_email, slavetype, body.requested_slavetype) if body.ldap_email != current_user.authenticated_email: hist_line += "on behalf of %s" % body.ldap_email history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(l) session.add(history) session.commit() logger.info(hist_line) chain_of_stuff = task_groups.generate_loan(loanid=l.id, slavetype=slavetype) chain_of_stuff.delay() return l.to_wsme()
def new_loan_from_admin(body): "Creates a new loan entry" if not body.status: raise BadRequest("Missing Status Field") if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not body.bugzilla_email: raise BadRequest("Missing Bugzilla E-Mail") if body.status != 'ACTIVE': raise BadRequest("Only ACTIVE loans supported at this time") if body.status != 'PENDING': if not body.fqdn: raise BadRequest("Missing Machine FQDN") if not body.ipaddress: raise BadRequest("Missing Machine IP Address") session = g.db.session('relengapi') try: if body.status != 'PENDING': m = Machines.as_unique(session, fqdn=body.fqdn, ipaddress=body.ipaddress) h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") loan_data = dict(status=body.status, human=h) if body.status != 'PENDING': loan_data.update(dict(machine=m)) if body.loan_bug_id: loan_data.update(dict(bug_id=body.loan_bug_id)) l = Loans(**loan_data) history = History(for_loan=l, timestamp=tz.utcnow(), msg="%s added this entry to slave loan tool via admin interface" % current_user.authenticated_email) session.add(l) session.add(history) session.commit() logger.info("%s manually added slave loan entry ID %s via admin interface" % (current_user.authenticated_email, l.id)) return l.to_wsme()
def register_action_needed(self, loanid, action_name): if not action_name: raise ValueError("must supply an action name") try: session = current_app.db.session('relengapi') l = session.query(Loans).get(loanid) if action_name == "add_to_vpn": action_message = ( "Add user (%s) and machine (%s) to the VPN. " "Following https://wiki.mozilla.org/ReleaseEngineering/How_To/Update_VPN_ACL" % (l.human.ldap, l.machine.fqdn)) elif action_name == "create_aws_system": action_message = ( "Create an aws machine for %s of the type requested (see loan history)." " Following " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave#AWS_machines" % (l.human.ldap, )) elif action_name == "clean_secrets": action_message = ( "Clean secrets from the machine. See instructions at " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave#Cleaning" ) elif action_name == "notify_complete": action_message = ( "Notify the loanee in e-mail and the loan bug (Bug %s) that the loan is ready. " "See template text for both in " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave#Notifying" % l.bug_id) elif action_name == "gpo_switch": action_message = ( "Need to switch host (%s) to be in the Loaner GPO group. Follow " "https://wiki.mozilla.org/ReleaseEngineering/How_To/Loan_a_Slave" "#t-xp32-ix.2C_t-w732-ix.2C_t-w864-ix.2C_w64-ix-slave " "for more information" % (l.machine.fqdn)) else: raise ValueError("Invalid action name") action = ManualActions(for_loan=l, timestamp_start=tz.utcnow(), msg=action_message) session.add(action) session.commit() return action.id except ValueError: raise # Don't indefinitely retry in this case except Exception as exc: self.retry(exc=exc)
def test_utcnow(datetime_mock): datetime_mock.utcnow.return_value = NOW util_dt = tz.utcnow() eq_(util_dt.tzinfo, pytz.UTC) eq_(util_dt.replace(tzinfo=None), NOW)
def new_loan_request(body): "User Loan Requesting, returns the id of the loan" if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not p.slaveloan.admin.can(): if not body.ldap_email == current_user.authenticated_email: raise BadRequest("You can't request loans on behalf of others.") if body.status: if not p.slaveloan.admin.can(): raise Forbidden("Permission denied to set loan status manually") if body.status not in ["PENDING", "COMPLETE", "ACTIVE"]: raise BadRequest("Loan status (%s) is unsupported at this time" % body.status) if body.status and body.status != 'PENDING': if not body.fqdn: raise BadRequest("Must set Machine FQDN") if not body.ipaddress: raise BadRequest("Must set Machine IP Address") else: if body.fqdn or body.ipaddress: if p.slaveloan.admin.can(): msg = ("Unable to explicitly set fqdn or ipaddress when not" "also explicitly setting status") if body.status == "PENDING": msg += " (to something other than PENDING)" raise BadRequest(msg) else: raise Forbidden( "Permission denied to set fqdn and ipaddress manually") if not body.requested_slavetype: if not body.fqdn and not body.ipaddress: raise BadRequest("Missing slavetype") else: if body.fqdn or body.ipaddress: raise BadRequest( "Unable to request a host if you're passing in the specifics") slavetype = slave_to_slavetype(body.requested_slavetype) if not slavetype: raise BadRequest("Unsupported slavetype") if not body.bugzilla_email: # Set bugzilla e-mail to ldap e-mail by default body.bugzilla_email = body.ldap_email session = g.db.session('relengapi') try: h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError( "Integrity Error from Database, please retry.") m = None if body.fqdn and body.ipaddress: try: m = Machines.as_unique(session, fqdn=body.fqdn, ipaddress=body.ipaddress) except sa.exc.IntegrityError: raise InternalServerError( "Integrity Error from Database, please retry.") loan_data = dict(human=h) if body.loan_bug_id: loan_data.update(dict(bug_id=body.loan_bug_id)) if m: loan_data.update(dict(machine=m)) if body.status: loan_data.update(dict(status=body.status)) else: loan_data.update(dict(status="PENDING")) l = Loans(**loan_data) if m: hist_line = "%s logged a %s loan on host: %s (ip: %s)" % \ (current_user.authenticated_email, body.status, body.fqdn, body.ipaddress) else: hist_line = "%s issued a loan request for slavetype %s (original: '%s')" % \ (current_user.authenticated_email, slavetype, body.requested_slavetype) if body.ldap_email != current_user.authenticated_email: hist_line += " on behalf of %s" % body.ldap_email history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(l) session.add(history) session.commit() logger.info(hist_line) if not m: chain_of_stuff = task_groups.generate_loan(loanid=l.id, slavetype=slavetype) chain_of_stuff.delay() return l.to_wsme()
def new_loan_request(body): "User Loan Requesting, returns the id of the loan" if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not p.slaveloan.admin.can(): if not body.ldap_email == current_user.authenticated_email: raise BadRequest("You can't request loans on behalf of others.") if body.status: if not p.slaveloan.admin.can(): raise Forbidden("Permission denied to set loan status manually") if body.status not in ["PENDING", "COMPLETE", "ACTIVE"]: raise BadRequest("Loan status (%s) is unsupported at this time" % body.status) if body.status and body.status != 'PENDING': if not body.fqdn: raise BadRequest("Must set Machine FQDN") if not body.ipaddress: raise BadRequest("Must set Machine IP Address") else: if body.fqdn or body.ipaddress: if p.slaveloan.admin.can(): msg = ("Unable to explicitly set fqdn or ipaddress when not" "also explicitly setting status") if body.status == "PENDING": msg += " (to something other than PENDING)" raise BadRequest(msg) else: raise Forbidden("Permission denied to set fqdn and ipaddress manually") if not body.requested_slavetype: if not body.fqdn and not body.ipaddress: raise BadRequest("Missing slavetype") else: if body.fqdn or body.ipaddress: raise BadRequest("Unable to request a host if you're passing in the specifics") slavetype = slave_to_slavetype(body.requested_slavetype) if not slavetype: raise BadRequest("Unsupported slavetype") if not body.bugzilla_email: # Set bugzilla e-mail to ldap e-mail by default body.bugzilla_email = body.ldap_email session = g.db.session('relengapi') try: h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") m = None if body.fqdn and body.ipaddress: try: m = Machines.as_unique(session, fqdn=body.fqdn, ipaddress=body.ipaddress) except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") loan_data = dict(human=h) if body.loan_bug_id: loan_data.update(dict(bug_id=body.loan_bug_id)) if m: loan_data.update(dict(machine=m)) if body.status: loan_data.update(dict(status=body.status)) else: loan_data.update(dict(status="PENDING")) l = Loans(**loan_data) if m: hist_line = "%s logged a %s loan on host: %s (ip: %s)" % \ (current_user.authenticated_email, body.status, body.fqdn, body.ipaddress) else: hist_line = "%s issued a loan request for slavetype %s (original: '%s')" % \ (current_user.authenticated_email, slavetype, body.requested_slavetype) if body.ldap_email != current_user.authenticated_email: hist_line += " on behalf of %s" % body.ldap_email history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(l) session.add(history) session.commit() logger.info(hist_line) if not m: chain_of_stuff = task_groups.generate_loan(loanid=l.id, slavetype=slavetype) chain_of_stuff.delay() return l.to_wsme()