Beispiel #1
0
def send_manager(output):
    # recipient's email
    toEmail1 = "*****@*****.**"
    toEmail2 = "*****@*****.**"

    msg1 = Message(subject="Health Recording System: Manager Report",
                   sender='*****@*****.**',
                   recipients=[toEmail1])
    msg1.body = "Good Morning, Qixuan \n" + output

    with webapp.app_context():
        mail.send(msg1)

    print("SendToQixuan")

    msg2 = Message(subject="Health Recording System: Manager Report",
                   sender='*****@*****.**',
                   recipients=[toEmail2])
    msg2.body = "Good Morning, Shixiong \n" + output

    with webapp.app_context():
        mail.send(msg2)

    print("SendToShixiong!!!")

    return True
Beispiel #2
0
 def welcomeMailer(user):
     name = Utils.getUserName(user)
     with webapp.app_context():
         email = Message('Welcome to Ostrich!', recipients=[user.email])
         email.html = transform(
             render_template('mailers/welcome.html', name=name))
         mail.send(email)
     return True
Beispiel #3
0
def delete_outdated(delete):
    with webapp.app_context():
        query = "DELETE FROM requests WHERE id = %s"

        cnx = get_db()
        cursor = cnx.cursor()
        for id in delete:
            # print("deleting HTTP request rate id: "+str(id))
            cursor.execute(query, (id, ))
        cnx.commit()
Beispiel #4
0
 def sendUpsellEmail(data):
     name = Utils.getUserName(data['user'])
     with webapp.app_context():
         consumer_mail = render_template(
             'mailers/extend_order.html',
             name=name,
             book_name=data['book_name'],
             order_id=data['order_id'],
             items=data['items'],
             curated_items=data['curated_items'],
             quote=data['quote'],
             quote_author=data['quote_author'])
         pre = Premailer(consumer_mail,
                         remove_classes=False,
                         strip_important=False)
         consumer_mail = pre.transform()
         email = Message('Enjoying the book?',
                         recipients=[data['user'].email])
         email.html = consumer_mail
         mail.send(email)
     return True
Beispiel #5
0
def cpuUtilHelper(id):
    '''
    request boto3 cloudwatch service and get the average CPU usage of the specific instance during the past 2 minuets
    :param id:
    :return: average CPU rate
    '''
    with webapp.app_context():
        client = boto3.client('cloudwatch')
        metric_name = 'CPUUtilization'
        namespace = 'AWS/EC2'
        statistic = 'Average'  # could be Sum,Maximum,Minimum,SampleCount,Average
        cpu = client.get_metric_statistics(
            Period=1 * 60,
            StartTime=datetime.datetime.utcnow() - timedelta(seconds=2 * 60),
            EndTime=datetime.datetime.utcnow() - timedelta(seconds=0 * 60),
            MetricName=metric_name,
            Namespace=namespace,  # Unit='Percent',
            Statistics=[statistic],
            Dimensions=[{
                'Name': 'InstanceId',
                'Value': id
            }])
        return cpu
Beispiel #6
0
def start_auto_scalling():
    '''
    Perform a continues loop and keeps checking if the average CPU meet the criteria of increasing/shrinking the instance
    :return:
    '''
    with webapp.app_context():
        write_log("Start Auto Scaling:")
        while True:
            if isAutoScaling:
                delete_log()
                write_log("=== === ===" + str(datetime.datetime.now()) +
                          "=== === ===")
                cnx = get_database()
                cnx.connect()
                cursor = cnx.cursor()
                query = "SELECT * FROM autoscaler_config"
                cursor.execute(query)
                results = cursor.fetchall()
                cpu_threshold_grow = results[0][1]
                cpu_threshold_shrinking = results[0][2]
                ratio_grow = results[0][3]
                ratio_shrink = results[0][4]
                write_log("---> CPU Threshold grow: " +
                          str(cpu_threshold_grow))
                write_log("---> CPU Threshold shrinking: " +
                          str(cpu_threshold_shrinking))
                write_log("---> CPU ratio grow: " + str(ratio_grow))
                write_log("---> CPU ratio shrink: " + str(ratio_shrink))
                try:
                    auto_scaling(cpu_threshold_grow, cpu_threshold_shrinking,
                                 ratio_grow, ratio_shrink)
                except Exception as e:
                    write_log(e)
                write_log("=== === === === === === === === ===")
                sleep(time_interval)
                cnx.close()
Beispiel #7
0
def increaseHelper():
    with webapp.app_context():
        ec2 = boto3.resource('ec2')
        instances = ec2.instances.all()
        workingWorkerCounter = 0
        cmd = '''
                    #!/bin/bash
                    kill -9 $(lsof -t -i:5000)
                    source /var/lib/jenkins/workspace/flaskvenv/bin/activate
                    gunicorn -b 0.0.0.0:5000 --chdir /var/lib/jenkins/workspace/ece1779-image-processing/ app:webapp
                    '''
        for instance in instances:
            if instance.id != "i-0a4596b36ad81d462" and \
                    (instance.state['Name'] == "running" or instance.state['Name'] == "pending"):
                workingWorkerCounter += 1

        # if still have avaliable slots for new workers
        if workingWorkerCounter < 10:
            newInstance = ec2.create_instances(
                ImageId=config.ami_id,
                MinCount=1,
                MaxCount=1,
                InstanceType='t2.small',
                KeyName="ECE1779_NEW",
            )
            newInstance[0].wait_until_running(
                Filters=[{
                    'Name': 'instance-id',
                    'Values': [str(newInstance[0].id)]
                }])
            registerInstanceToLB(newInstance[0].id)
            result = boto3.client('ec2').monitor_instances(
                InstanceIds=[str(newInstance[0].id)])
            return True
        else:
            return False
Beispiel #8
0
def auto_scaling(cpu_threshold_grow, cpu_threshold_shrinking, ratio_grow,
                 ratio_shrink):
    '''
    check if the average CPU of the instances in the past 2 minuets meet the criteria of increasing/shrinking the instance, if so, the function will excute the corresponding operation
    :param cpu_threshold_grow: cpu_threshold_grow rate
    :param cpu_threshold_shrinking: cpu_threshold_shrinking rate
    :param ratio_grow: ratio_grow rate
    :param ratio_shrink: ratio_shrink rate
    :return:
    '''
    with webapp.app_context():
        # getting average cpu utils

        ec2 = boto3.resource('ec2')
        #    instances = ec2.instances.filter(
        #        Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
        instances = ec2.instances.all()
        instances = instance_filter(instances)
        instancesCumCpuUtilData = 0
        write_log(
            "---> Currently: " + str(len(instances)) +
            " instance(s) are registered under the load balancer and are running or pending"
        )
        if len(instances) == 0:
            write_log("---> Currently no instance available, add one!")
            increase()
        instances = ec2.instances.all()
        instances = instance_filter(instances)
        for instance in instances:
            cpuData = cpuUtilHelper(instance.id)
            if len(cpuData['Datapoints']) != 0:
                write_log("---> Average for instance: " +
                          str(cpuData['Datapoints'][0]['Average']))
                if instance.launch_time.replace(tzinfo=None) <= (
                        datetime.datetime.utcnow() -
                        timedelta(seconds=5 * 60)).replace(tzinfo=None):
                    instancesCumCpuUtilData += cpuData['Datapoints'][0][
                        'Average']
                else:
                    cnx = get_database()
                    cnx.connect()
                    cursor = cnx.cursor()
                    query = "SELECT * FROM autoscaler_config"
                    cursor.execute(query)
                    results = cursor.fetchall()
                    adjustment = results[0][2]
                    write_log(
                        "---> The instance: " + str(instance.id) +
                        " is just created. To avoid unusual high CPU util at the beginning of the boot, the current CPU util will be counted as "
                        + str(adjustment) +
                        " at this time. This instance's real CPU util will be counted and be used for auto-scaling after: "
                        +
                        str(instance.launch_time + timedelta(seconds=5 * 60)) +
                        "!")
                    instancesCumCpuUtilData += adjustment
            else:
                write_log("---> Instance: " + str(instance.id) +
                          "'s monitoring data is still under preparation.")
        average = instancesCumCpuUtilData / len(instances)
        write_log("---> Current average of all instances: " + str(average))
        # Enforcement
        # if average > 90 and len(instances) < 10:
        #     increase()
        # if average < 10 and len(instances) > 1:
        #     decrease()
        # User customizable cases:
        if average > cpu_threshold_grow:
            write_log("---> Start to increase instance: ")
            ctr = 0
            for i in range(len(instances),
                           math.ceil(len(instances) * (ratio_grow))):
                ctr = ctr + 1
                write_log("------> Start to increase instance: " + str(ctr))
                increase()
                write_log("------> Completed the increment")
        elif average < cpu_threshold_shrinking:
            write_log("---> Start to decrease instance: ")
            ctr = 0
            lenth = len(instances)
            for i in range(len(instances),
                           math.ceil(len(instances) * (ratio_shrink))):
                if lenth > 1:
                    ctr = ctr + 1
                    write_log("------> Start to decrease instance: " +
                              str(ctr))
                    decrease()
                    lenth = lenth - 1
                    write_log("------> Completed the decrement")
                elif lenth == 0:
                    write_log("Oops, all the instances deleted :( ")
                    increase()
                    write_log("Reopened a new one :) ")
                else:
                    write_log(
                        "------> Oops, failed to delete the instance as there is only one instance left :( "
                    )
        return average
Beispiel #9
0
import os

from app import webapp
from orm import DB, Session
import models

webapp.config.update({
    'SQLALCHEMY_DATABASE_URI': os.environ['UQFINAL_DB_URI'],
    'SQLALCHEMY_TRACK_MODIFICATIONS': False,
    'SQLALCHEMY_POOL_SIZE': 0,
    'SQLALCHEMY_POOL_RECYCLE': 500,
})
DB.init_app(webapp)


if __name__ == "__main__":
    import sys
    args = sys.argv
    if len(args) > 1 and args[1] == "generatedb":
        with webapp.app_context():
            s = Session()
            models.ORMBase.metadata.create_all(s.connection())
            s.commit()
    else:
        webapp.run(port=8080)
Beispiel #10
0
 def genericMailer(mail_obj, recipients=['*****@*****.**']):
     with webapp.app_context():
         email = Message(mail_obj['subject'], recipients=recipients)
         email.body = mail_obj['body']
         mail.send(email)
     return True
Beispiel #11
0
 def send_async_mail(webapp, email):
     with webapp.app_context():
         mail.send(email)