def db_setup(app): session = app.db.session('relengapi') machines = [] for m in (("127.0.0.1", "host1.mozilla.org"), ("127.0.0.2", "host2.mozilla.org"), ("127.0.0.3", "host3.mozilla.org"), ("127.0.0.4", "host4.mozilla.org"), ("127.0.0.5", "host5.mozilla.org")): machines.append(Machines(ipaddress=m[0], fqdn=m[1])) session.add_all(machines) humans = [] for u in (("*****@*****.**", "*****@*****.**"), ("*****@*****.**", "*****@*****.**"), ("*****@*****.**", "*****@*****.**"), ("*****@*****.**", "*****@*****.**")): humans.append(Humans(ldap=u[0], bugzilla=u[1])) session.add_all(humans) loans = [] for l in ( # status, bug, machine, human ("ACTIVE", 1234001, machines[0], humans[0]), ("COMPLETE", 1234002, machines[1], humans[1]), ("PENDING", 1234003, machines[2], humans[2]), ("ACTIVE", 1234004, machines[3], humans[0]), ("ACTIVE", 1234005, machines[4], humans[1]), ("PENDING", 1234006, None, humans[0])): loans.append(Loans(status=l[0], bug_id=l[1], machine=l[2], human=l[3])) session.add_all(loans) # XXX History Tests # XXX ManualAction Tests # _ = (History, ManualActions) # silence pyflakes session.commit()
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 fixup_machine(self, machine, loanid): try: fqdn = socket.getfqdn("%s.build.mozilla.org" % machine) ipaddress = socket.gethostbyname("%s.build.mozilla.org" % machine) session = current_app.db.session('relengapi') m = Machines.as_unique(session, fqdn=fqdn, ipaddress=ipaddress) # Re-check validity of fqdn and ip if m.fqdn != fqdn: m.fqdn = fqdn if m.ipaddress != ipaddress: m.ipaddress = ipaddress l = session.query(Loans).get(loanid) l.machine = m session.commit() except Exception as exc: # pylint: disable=W0703 logger.exception(exc) self.retry(exc=exc)
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()