1cc347d9c57b6d462b30329858d5c26332bbfac9
[plcapi.git] / PLC / Methods / BootNotifyOwners.py
1 from PLC.Debug import log
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Auth import Auth, BootAuth
6 from PLC.Messages import Message, Messages
7 from PLC.Persons import Person, Persons
8 from PLC.Sites import Site, Sites
9
10 class BootNotifyOwners(Method):
11     """
12     Notify the owners of the node, and/or support about an event that
13     happened on the machine.
14
15     Returns 1 if successful.
16     """
17
18     roles = ['node']
19
20     accepts = [
21         BootAuth(),
22         Message.fields['message_id'],
23         Parameter(int, "Notify PIs"),
24         Parameter(int, "Notify technical contacts"),
25         Parameter(int, "Notify support")
26         ]
27
28     returns = Parameter(int, '1 if successful')
29
30     def call(self, auth, message_id, include_pis, include_techs, include_support):
31         messages = Messages(self.api, [message_id], enabled = True)
32         if not messages:
33             print >> log, "No such message template"
34             return 1
35         message = messages[0]
36         
37         if not self.api.config.PLC_MAIL_ENABLED:
38             return 1
39
40         from_addr = {}
41         from_addr[self.api.config.PLC_MAIL_SUPPORT_ADDRESS] = \
42         "%s %s" % ('Planetlab', 'Support')
43         recipients = {}
44
45         if self.api.config.PLC_MAIL_BOOT_ADDRESS:
46             recipients[self.api.config.PLC_MAIL_BOOT_ADDRESS] = "Boot Messages"
47
48         if include_support and self.api.config.PLC_MAIL_SUPPORT_ADDRESS:
49             recipients[self.api.config.PLC_MAIL_SUPPORT_ADDRESS] = self.api.config.PLC_NAME + " Support"
50
51         if include_pis or include_techs:
52             sites = Sites(self.api, [self.caller['site_id']])
53             if not sites:
54                 raise PLCAPIError, "No site associated with node"
55             site = sites[0]
56
57             persons = Persons(self.api, site['person_ids'])
58             for person in persons:
59                 if include_pis and 'pi' in person['roles'] or \
60                    include_techs and 'tech' in person['roles']:
61                     recipients[person['email']] = person['first_name'] + " " + person['last_name']
62
63         subject = message['subject']
64         template = message['template']
65         
66         # Send email
67         self.api.mailer.mail(recipients, None, from_addr, subject, template)
68
69         # Logging variables
70         self.message = "Node sent message %s to contacts"
71
72         return 1