Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[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 # $Id: Boot.py 5574 2007-10-25 20:33:17Z thierry $
8 #
9
10 from PLC.Faults import *
11 from PLC.Debug import log
12 from PLC.Messages import Message, Messages
13 from PLC.Persons import Person, Persons
14 from PLC.Sites import Site, Sites
15 from PLC.sendmail import sendmail
16
17 def notify_owners(method, node, message_id,
18                   include_pis = False, include_techs = False, include_support = False,
19                   fault = None):
20     messages = Messages(method.api, [message_id], enabled = True)
21     if not messages:
22         print >> log, "No such message template '%s'" % message_id
23         return 1
24     message = messages[0]
25
26     To = []
27
28     if method.api.config.PLC_MAIL_BOOT_ADDRESS:
29         To.append(("Boot Messages", method.api.config.PLC_MAIL_BOOT_ADDRESS))
30
31     if include_support and method.api.config.PLC_MAIL_SUPPORT_ADDRESS:
32         To.append(("%s Support" % method.api.config.PLC_NAME,
33                    method.api.config.PLC_MAIL_SUPPORT_ADDRESS))
34
35     if include_pis or include_techs:
36         sites = Sites(method.api, [node['site_id']])
37         if not sites:
38             raise PLCAPIError, "No site associated with node"
39         site = sites[0]
40
41         persons = Persons(method.api, site['person_ids'])
42         for person in persons:
43             if include_pis and 'pi' in person['roles'] or \
44                include_techs and 'tech' in person['roles']:
45                 To.append(("%s %s" % (person['first_name'], person['last_name']), person['email']))
46
47     # Send email
48     params = {'node_id': node['node_id'],
49               'hostname': node['hostname'],
50               'PLC_WWW_HOST': method.api.config.PLC_WWW_HOST,
51               'PLC_WWW_SSL_PORT': method.api.config.PLC_WWW_SSL_PORT,
52               'fault': fault}
53
54     sendmail(method.api, To = To,
55              Subject = message['subject'] % params,
56              Body = message['template'] % params)
57
58     # Logging variables
59     method.object_type = "Node"
60     method.object_ids = [node['node_id']]
61     method.message = "Sent message %s" % message_id