52a9fd93275dcca5ba2c6af41fc3c58462b1fbd1
[plcapi.git] / PLC / Boot.py
1 #
2 # Boot Manager support
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2007 The Trustees of Princeton University
6 #
7
8 from PLC.Faults import *
9 from PLC.Debug import log
10 from PLC.Messages import Message, Messages
11 from PLC.Persons import Person, Persons
12 from PLC.Sites import Site, Sites
13 from PLC.sendmail import sendmail
14
15 def notify_owners(method, node, message_id,
16                   include_pis = False, include_techs = False, include_support = False,
17                   fault = None):
18     messages = Messages(method.api, [message_id], enabled = True)
19     if not messages:
20         print >> log, "No such message template '%s'" % message_id
21         return 1
22     message = messages[0]
23
24     To = []
25
26     if method.api.config.PLC_MAIL_BOOT_ADDRESS:
27         To.append(("Boot Messages", method.api.config.PLC_MAIL_BOOT_ADDRESS))
28
29     if include_support and method.api.config.PLC_MAIL_SUPPORT_ADDRESS:
30         To.append(("%s Support" % method.api.config.PLC_NAME,
31                    method.api.config.PLC_MAIL_SUPPORT_ADDRESS))
32
33     if include_pis or include_techs:
34         sites = Sites(method.api, [node['site_id']])
35         if not sites:
36             raise PLCAPIError, "No site associated with node"
37         site = sites[0]
38
39         persons = Persons(method.api, site['person_ids'])
40         for person in persons:
41             if (include_pis and 'pi' in person['roles'] and person['enabled']) or \
42                (include_techs and 'tech' in person['roles'] and person['enabled']) :
43                 To.append(("%s %s" % (person['first_name'], person['last_name']), person['email']))
44
45     # Send email
46     params = {'node_id': node['node_id'],
47               'hostname': node['hostname'],
48               'PLC_WWW_HOST': method.api.config.PLC_WWW_HOST,
49               'PLC_WWW_SSL_PORT': method.api.config.PLC_WWW_SSL_PORT,
50               'fault': fault}
51
52     sendmail(method.api, To = To,
53              Subject = message['subject'] % params,
54              Body = message['template'] % params)
55
56     # Logging variables
57     method.object_type = "Node"
58     method.object_ids = [node['node_id']]
59     method.message = "Sent message %s" % message_id