aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 9b0d88bae527229997fd7940ce3543f9df214b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3

from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import str
import os
import sys
import json
import pickle
import hashlib
import logging
import argparse
import binascii
import configparser

import requests

from twisted.internet import reactor, ssl
from twisted.web import server

from jobmanager import JobManager
from statustracker import StatusTracker
from servers import StatusSite, PingSite

# We only run one command on the hour and day marks, but we need to execute all the jobs in that instance,
# including our minute jobs and hour jobs
def convert_crontimes(crontime):
    ret = ["minute"]
    if crontime == "minute":
        pass
    elif crontime == "hour":
        ret.append("hour")
    elif crontime == "day":
        ret.append("hour")
        ret.append("day")
    elif crontime == "day_noon":
        ret.append("hour")
        ret.append("day_noon")
    return ret
        
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Check your stuff.")
    parser.add_argument('-m', '--mode', choices=['daemon', 'cron'], required=True, help='The mode the application will run it.')
    parser.add_argument('-c', '--crontime', choices=['minute', 'hour', 'day', 'day_noon'], help='When in cron mode, the increment of cron.')
    parser.add_argument('-v', action="store_true", help="Print verbose debugging information to the logfile")
    parser.add_argument('-d', action="store_true", help="Print verbose debugging information to stderr")
    parser.add_argument('--nomail', action="store_true", help="Do everything except sending email")

    args = parser.parse_args()

    config = configparser.ConfigParser()
    configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'settings.cfg')
    config.read(configfile)
    if args.nomail:
        config.set('email', 'nomail', "True")

    if not config.getboolean('email', 'nomail') and (\
        not config.get('email', 'user') or \
        not config.get('email', 'pass') or \
        not config.get('email', 'smtpserver') or \
        not config.get('email', 'smtpport') or \
        not config.get('email', 'imapserver')):
        print("Sending email address is not configured")
        sys.exit(1)
    if not config.get('general', 'servername') or \
        not config.get('general', 'alertcontact'):
        print("Default alert contact is not configured")
        sys.exit(1)



    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.CRITICAL)
    log_formatter = logging.Formatter(fmt="%(asctime)s:%(levelname)s:  %(message)s")
    log = logging.getLogger()
    if args.v:
        log.setLevel(logging.DEBUG)
        log_file_handler = logging.FileHandler(config.get('general', 'logfile'))
        log_file_handler.setFormatter(log_formatter)
        log.addHandler(log_file_handler)
    if args.d:
        log.setLevel(logging.DEBUG)
        log_stderr_handler = logging.StreamHandler()
        log_stderr_handler.setFormatter(log_formatter)
        log.addHandler(log_stderr_handler)

    if args.mode == 'daemon':
        log.info("Starting up daemon")
        statusTracker = StatusTracker(config)
        reactor.listenTCP(5000, server.Site(StatusSite(statusTracker)))
        reactor.listenTCP(5001, server.Site(PingSite(statusTracker)), interface='127.0.0.1')
        reactor.run()
    elif args.mode == 'cron':
        jobManager = JobManager(config)
        if not args.crontime:
            log.warn("Did not run cron, no crontime specified")
            parser.print_help()
            sys.exit(-1)
        else:
            log.info("Running cron at frequencies " + str(convert_crontimes(args.crontime)))
            try:
                if jobManager.execute_jobs(convert_crontimes(args.crontime)):
                    jobManager.mark_jobs_ran()
                else:
                    jobManager.mark_jobs_ran_with_error()
            except Exception as e:
                logging.critical("Caught an exception trying to execute jobs:" + str(e))
                jobManager.mark_jobs_ran_with_error()