from crontab import CronTab import subprocess, os, makelog mylogger = makelog.get_logger('cron') # execute cron jobs using crontab def executeCron(timeslices, command): mylogger.info('Register new cron job: %s %s' % (timeslices, command)) os.system("crontab -l > /tmp/curcronfile.txt") cmd = "echo " + "\'" + timeslices + " " + command + "\'" + " >> /tmp/curcronfile.txt\n" os.system(cmd) os.system("crontab < /tmp/curcronfile.txt") os.system("rm /tmp/curcronfile.txt") # judge whether the cron of test case already exists def testCron(timeslices, filepath): test_path = os.getcwd() command = "/usr/bin/python " + test_path + "/dashboard/testsrc/blackbox.py " + filepath pipe = subprocess.Popen(('crontab', '-l'), stdout=subprocess.PIPE) result = pipe.communicate()[0] lines = result.splitlines(True) line = timeslices + " " + command + "\n" if not lines: # empty executeCron(timeslices, command) else: # not empty if line not in lines: executeCron(timeslices, command) # remove cron job (uncompleted)
# -*- coding: utf-8 -*- import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders from email import Utils from email.header import Header from email.utils import formataddr import os, makelog mylogger = makelog.get_logger('notification') def send_email(error, info): test_path = os.getcwd() mylogger.info('Preparing sending email...') smtp_server = "smtp.gmail.com" port = 587 userid = "*****@*****.**" passwd = "tkfkdgo09" to_user = info[1] from_user = "******" cur_path = os.getcwd() html_path = cur_path + "/dashboard/testsrc/templates/mail.html" author = formataddr((str(Header('MONITT')), from_user)) msg = MIMEMultipart("alternative") msg["From"] = author msg["To"] = to_user msg["Subject"] = Header(s="[MONITT] %s: %s" % (info[9], info[2]))
# -*- coding: utf-8 -*- import sys, yaml, requests, json, re, os, base64, datetime import MySQLdb, random, string import logging import notification, cron, makelog db = MySQLdb.connect(host="localhost", user="******", passwd="qwer1234", db="blackbox") cur = db.cursor() fail_list = [] test_info = [] mylogger = makelog.get_logger('blackbox') # read a test case file from command line argument def readFile(filepath): mylogger.info('Reading test case...') try: f = open(filepath).read() except: mylogger.error('Cannot find file %s', filepath) sys.exit(-1) mylogger.info('Success to read %s', filepath) f = f.replace('\t', '') js = yaml.load(f) fileExtension = os.path.splitext(filepath)[1] if fileExtension != '.json': mylogger.error('Unproper file format: %s', filepath) sys.exit(-1)