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