VirtualBox

Changeset 76325 in vbox for trunk/src/VBox/ValidationKit


Ignore:
Timestamp:
Dec 20, 2018 7:08:46 PM (6 years ago)
Author:
vboxsync
Message:

Reset hanged testboxes and sending e-mail alert. Disable testboxes failing with module loading/unloading errors.

Location:
trunk/src/VBox/ValidationKit/testmanager
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/testmanager/batch/virtual_test_sheriff.py

    r73145 r76325  
    4343import os;
    4444import hashlib;
     45import subprocess;
     46import smtplib
    4547if sys.version_info[0] >= 3:
    4648    from io       import StringIO as StringIO;      # pylint: disable=import-error,no-name-in-module
     
    4951from optparse import OptionParser;                  # pylint: disable=deprecated-module
    5052from PIL import Image;                              # pylint: disable=import-error
     53
     54from email.mime.multipart import MIMEMultipart
     55from email.mime.text import MIMEText
     56from email.utils import COMMASPACE
    5157
    5258# Add Test Manager's modules path
     
    6571from testmanager.core.testresultfailures    import TestResultFailureLogic, TestResultFailureData;
    6672from testmanager.core.useraccount           import UserAccountLogic;
     73from testmanager.config                     import g_ksSmtpHost, g_kcSmtpPort, g_ksAlertSubject, g_asAlertList;
    6774
    6875# Python 3 hacks:
     
    358365                rcExit = self.eprint(u'Cannot find my user account "%s"!' % (VirtualTestSheriff.ksLoginName,));
    359366        return rcExit;
     367
     368    def emailAlert(self, uidAuthor, sBodyText):
     369        asEmailList = [];
     370
     371        # Get author email
     372        self.oDb.execute('SELECT sEmail FROM Users WHERE uid=%s', (uidAuthor,));
     373        sFrom = self.oDb.fetchOne();
     374        if sFrom is not None:
     375            sFrom = sFrom[0];
     376        else:
     377            sFrom = '[email protected]';
     378
     379        for sUser in g_asAlertList:
     380            self.oDb.execute('SELECT sEmail FROM Users WHERE sUsername=%s', (sUser,));
     381            sEmail = self.oDb.fetchOne();
     382            if sEmail is None:
     383                # No address to send an alert.
     384                return;
     385            asEmailList.append(sEmail[0]);
     386
     387        oMsg = MIMEMultipart();
     388        oMsg['From'] = sFrom;
     389        oMsg['To'] = COMMASPACE.join(asEmailList);
     390        oMsg['Subject'] = g_ksAlertSubject;
     391        oMsg.attach(MIMEText(sBodyText, 'plain'))
     392
     393        try:
     394            oSMTP = smtplib.SMTP(g_ksSmtpHost, g_kcSmtpPort);
     395            oSMTP.sendmail(sFrom, asEmailList, oMsg.as_string())
     396            oSMTP.quit()
     397        except smtplib.SMTPException as oXcpt:
     398            rcExit = self.eprint('Failed to send mail: %s' % (oXcpt,));
    360399
    361400
     
    454493                                                                 % (iFirstOkay, cBad, cOkay),);
    455494                        except Exception as oXcpt:
    456                             rcExit = self.eprint(u'Error rebooting testbox #%u (%u): %s\n' % (idTestBox, oTestBox.sName, oXcpt,));
     495                            rcExit = self.eprint(u'Error rebooting testbox #%u (%s): %s\n' % (idTestBox, oTestBox.sName, oXcpt,));
    457496            else:
    458497                self.dprint(u'badTestBoxManagement: #%u (%s) looks ok:  iFirstOkay=%u cBad=%u cOkay=%u'
    459498                            % ( idTestBox, oTestBox.sName, iFirstOkay, cBad, cOkay));
     499        #
     500        # Reset hanged testboxes
     501        #
     502        cStatusTimeoutMins = 10;
     503
     504        self.oDb.execute('SELECT idTestBox FROM TestBoxStatuses WHERE tsUpdated < (CURRENT_TIMESTAMP - interval \'%s minutes\')', (cStatusTimeoutMins,));
     505        for idTestBox in self.oDb.fetchAll():
     506            idTestBox = idTestBox[0];
     507            try:
     508                oTestBox = TestBoxData().initFromDbWithId(self.oDb, idTestBox);
     509            except Exception as oXcpt:
     510                rcExit = self.eprint('Failed to get data for test box #%u in badTestBoxManagement: %s' % (idTestBox, oXcpt,));
     511                continue;
     512            # Skip if the testbox is already disabled, already reset or there's no iLOM
     513            if not oTestBox.fEnabled or oTestBox.ipLom is None or \
     514                oTestBox.sComment is not None and oTestBox.sComment.find('Automatically reset') >= 0:
     515                    self.dprint(u'badTestBoxManagement: Skipping test box #%u (%s) as it has been disabled already.'
     516                                % ( idTestBox, oTestBox.sName, ));
     517                    continue;
     518            ## @todo get iLOM credentials from a table?
     519            sCmd = 'sshpass -pchangeme ssh -oStrictHostKeyChecking=no root@%s show /SP && reset /SYS' % (oTestBox.ipLom,);
     520            try:
     521                oPs = subprocess.Popen(sCmd, stdout=subprocess.PIPE, shell=True);
     522                sStdout = oPs.communicate()[0];
     523                iRC = oPs.wait();
     524
     525                oTestBox.sComment = 'Automatically reset (iRC=%u sStdout=%s)' % (iRC, sStdout,);
     526                oTestBoxLogic.editEntry(oTestBox, self.uidSelf, fCommit = True);
     527
     528                sComment = u'Reset testbox #%u (%s) - iRC=%u sStduot=%s' % ( idTestBox, oTestBox.sName, iRC, sStdout);
     529                self.vprint(sComment);
     530
     531                self.emailAlert(self.uidSelf, sComment);
     532
     533            except Exception as oXcpt:
     534                rcExit = self.eprint(u'Error reseting testbox #%u (%s): %s\n' % (idTestBox, oTestBox.sName, oXcpt,));
     535
    460536        return rcExit;
    461537
  • trunk/src/VBox/ValidationKit/testmanager/config.py

    r69111 r76325  
    186186## @}
    187187
     188## @name Virtual Sheriff email alerts
     189## @{
     190
     191## SMTP server host name.
     192g_ksSmtpHost            = 'internal-mail-router.oracle.com';
     193## SMTP server port number.
     194g_kcSmtpPort            = 25;
     195## Subject for email alert.
     196g_ksAlertSubject        = 'Virtual Sheriff alert';
     197## List of users to send alerts.
     198g_asAlertList           = ['lelik', 'werner'];
     199
     200## @}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette