forked from ClockworkNet/OpScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_notify_email.py
executable file
·52 lines (41 loc) · 1.41 KB
/
example_notify_email.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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Standard library
from __future__ import absolute_import, division, print_function
import logging
import sys
# Local/library specific
from opscripts.config import v5 as ops_config
from opscripts.logging import v2 as ops_logging
from opscripts.notify.email import v2 as ops_notify_email
from opscripts.utils import v4 as ops_utils
LOG = logging.getLogger(__name__)
def setup():
"""Instantiate and configure configargparse and logging.
Return configargsparse namespace.
"""
add_args = {"config": True, "EMAIL": True, "dryrun": True,
"verbosity": True}
cap = ops_config.OpsConfigArgParse(description=__doc__, add_args=add_args)
logger = ops_logging.OpScriptsLogging(cap.prog)
args = ops_config.parse_args(cap)
logger.set_log_level(args.verbosity)
logger.dryrun(args.dryrun)
return args
def main():
args = setup()
subject = "Test - disregard"
body = "This is a test message"
message = ops_notify_email.Message(args.program_name, subject, body)
message.send(args.email_from, args.email_to, dryrun=args.dryrun)
if __name__ == "__main__":
try:
main()
except SystemExit as e:
sys.exit(e.code)
except KeyboardInterrupt:
ops_utils.log_ctrlc_and_exit()
except ops_utils.Fatal:
ops_utils.log_fatal_and_exit()
except:
ops_utils.log_exception_and_exit()