forked from IntegralDefense/o365_log_fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
o365_run.py
58 lines (46 loc) · 1.58 KB
/
o365_run.py
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
from uuid import uuid4
import asyncio
import argparse
import logging
import os
import time
from dotenv import load_dotenv
from o365_api.argparsing import add_arg_parser_args
from o365_api.handler import O365ManagementApi
from o365_api.logger import setup_logger
from o365_api.wrappers import ParserWrapper
if __name__ == "__main__":
load_dotenv(override=True)
run_id = uuid4()
try:
begin_total = time.time()
# Get configuration
config_file = os.environ.get("O365_MANAGEMENT_API_CONFIG")
log_level = os.environ.get("O365_LOG_LEVEL", "INFO")
setup_logger(log_level=log_level)
config_parser = ParserWrapper(config_file)
# Interact with arguments
arg_parser = argparse.ArgumentParser()
add_arg_parser_args(arg_parser)
args = arg_parser.parse_args()
if args.start_time and args.end_time:
start = args.start_time
end = args.end_time
else:
start = None
end = None
api = O365ManagementApi(config_parser, start, end, run_id)
loop = asyncio.get_event_loop()
loop.run_until_complete(api.run(loop))
loop.close()
api.save_last_log_time()
end_total = time.time()
except Exception as e:
# If an exception gets up here, it's serious. Log it and then
# let it bubble up like normal.
logging.exception("JobId={0} {1}".format(run_id, e))
raise
else:
runtime = str(int(end_total - begin_total))
logging.info("Total runtime: {} seconds.".format(runtime))
exit()