PLC support scripts as part of myplc package.
[myplc.git] / support-scripts / renew_reminder.py
1 #!/usr/bin/python
2 #
3 # Notify users of slices that are about to expire
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
7 #
8 # $Id: renew_reminder.py 7225 2007-11-29 18:21:31Z faiyaza $
9 #
10
11 import sys
12 import time
13 from optparse import OptionParser
14
15 # Load shell with default configuration
16 sys.path.append('/usr/share/plc_api')
17 from PLC.Shell import Shell
18 plc = Shell(globals())
19
20 PLC_WWW_HOST = plc.config.PLC_WWW_HOST
21 PLC_NAME = plc.config.PLC_NAME
22
23 # Debug
24 verbose = False;
25
26 # E-mail parameteres
27 slice_url = """https://%(PLC_WWW_HOST)s/db/slices/index.php?id=""" % locals()
28
29 parser = OptionParser()
30 parser.add_option("-s", "--slice", action = "append", dest = "slices", default = None,
31                   help = "Slice(s) to check (default: all)")
32 parser.add_option("-x", "--expires", type = "int", default = 5,
33                   help = "Warn if slice expires this many days from now (default: %default)")
34 parser.add_option("-n", "--dryrun", action = "store_true", default = False,
35                   help = "Dry run, do not actually e-mail users (default: %default)")
36 parser.add_option("-f", "--force", action = "store_true", default = False,
37                   help = "Force, send e-mail even if slice is not close to expiring (default: %default)")
38 parser.add_option("-v", "--verbose", action = "store_true", default = False,
39                   help = "Be verbose (default: %default)")
40 (options, args) = parser.parse_args()
41
42 now = int(time.time())
43 expires = now + (options.expires * 24 * 60 * 60)
44
45 if options.verbose:
46     print "Checking for slices that expire before " + time.ctime(expires)
47
48 slice_filter = {'peer_id': None}
49 if options.slices:
50     slice_filter['name'] = options.slices
51
52 for slice in GetSlices(slice_filter, ['slice_id', 'name', 'expires', 'description', 'url', 'person_ids']):
53     # See if slice expires before the specified warning date
54     if not options.force and slice['expires'] > expires:
55         continue
56
57     # Calculate number of whole days left
58     delta = slice['expires'] - now
59     days = delta / 24 / 60 / 60
60     if days == 0:
61         days = "less than a day"
62     else:
63         if days > 1:
64             suffix = "s"
65         else:
66             suffix = ""
67         days = "%d day%s" % (days, suffix)
68
69     name = slice['name']
70     slice_id = slice['slice_id']
71
72     message = """
73 The %(PLC_NAME)s slice %(name)s will expire in %(days)s.
74 """
75
76     # Explain that slices must have descriptions and URLs
77     if not slice['description'] or not slice['description'].strip() or \
78        not slice['url'] or not slice['url'].strip():
79         message += """
80 Before you may renew this slice, you must provide a short description
81 of the slice and a link to a project website.
82 """
83
84     # Provide links to renew or delete the slice
85     message += """
86 To update, renew, or delete this slice, visit the URL:
87
88         %(slice_url)s%(slice_id)d
89 """
90
91     # Send it
92     if slice['person_ids']:
93         if options.dryrun:
94             print message % locals()
95         else:
96             NotifyPersons(slice['person_ids'],
97                           "%(PLC_NAME)s slice %(name)s expires in %(days)s" % locals(),
98                           message % locals())
99     elif options.verbose:
100         print slice['name'], "has no users, skipping"