| 1 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | Enarksh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | Copyright 2013-2016 Set Based IT Consultancy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | Licence MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import smtplib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from email.mime.text import MIMEText | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from enarksh.C import C | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from enarksh.Config import Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | from enarksh.DataLayer import DataLayer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from enarksh.controller.node import ScheduleNode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | from enarksh.controller.node.SimpleNode import SimpleNode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class MailOperatorEventHandler: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     An event handler for event were an mail must be send to the operators. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     def __send_mail(to, subject, body): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         Sends an email to the operators. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         :param str|list[str] to: The email addresses of the operator(s). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         :param str subject: The subject op the email. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         :param str body: The email body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         config = Config.get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         from_email = config.get_controller_email() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         # Concat To mail addresses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         to_email = '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if isinstance(to, list): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             for email in to: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 if to_email: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     to_email += ', ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 to_email += email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             to_email = to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         msg = MIMEText(body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         msg['Subject'] = subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         msg['To'] = to_email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         msg['From'] = from_email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         # Send the message via our local SMTP server. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         s = smtplib.SMTP('localhost') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         s.send_message(msg) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         s.quit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     def __send_mail_simple_node_failed(rnd_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         """ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 60 |  | View Code Duplication |         Sends a mail to operators that a simple node has terminated with an error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         :param int rnd_id: The ID of the run node. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         node = DataLayer.enk_back_run_node_get_details(rnd_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         operators = DataLayer.enk_back_get_operators() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if operators: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             body = """Dear Enarksh operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | Job {} has run unsuccessfully. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | Greetings from Enarksh""".format(str(node['uri_uri'], 'utf-8')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             subject = "Job of schedule {} failed".format(str(node['sch_name'], 'utf-8')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             to = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             for operator in operators: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 to.append(operator['usr_email']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             MailOperatorEventHandler.__send_mail(to, subject, body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def __send_mail_schedule_node_failed(rnd_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         """ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 86 |  | View Code Duplication |         Sends a mail to operators that a schedule has terminated unsuccessfully. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         :param int rnd_id: The ID of the schedule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         node = DataLayer.enk_back_run_node_get_details(rnd_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         operators = DataLayer.enk_back_get_operators() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if operators: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             body = """Dear Enarksh operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | Schedule {} finished unsuccessfully. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | Greetings from Enarksh""".format(str(node['sch_name'], 'utf-8')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             subject = "Schedule {} finished unsuccessfully".format(str(node['sch_name'], 'utf-8')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             to = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             for operator in operators: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 to.append(operator['usr_email']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             MailOperatorEventHandler.__send_mail(to, subject, body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     def __send_mail_schedule_node_success(rnd_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         Sends a mail to operators that a schedule has terminated successfully. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         :param int rnd_id: The ID of the schedule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         node = DataLayer.enk_back_run_node_get_details(rnd_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         operators = DataLayer.enk_back_get_operators() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         if operators: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             body = """Dear Enarksh operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | Schedule {} finished successfully. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | Greetings from Enarksh""".format(str(node['sch_name'], 'utf-8')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             subject = "Schedule {} finished successfully".format(str(node['sch_name'], 'utf-8')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             to = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             for operator in operators: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 to.append(operator['usr_email']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             MailOperatorEventHandler.__send_mail(to, subject, body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     def __handle_simple_node_stop(_event, event_data, _listener_data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         Handles the termination of a simple node. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         :param * _event: Not used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         :param tuple[dict] event_data: The old and new node status. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         :param * _listener_data: Not used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         del _event, _listener_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         if event_data[1]['rst_id'] == C.ENK_RST_ID_ERROR: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             MailOperatorEventHandler.__send_mail_simple_node_failed(event_data[1]['rnd_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     def __handle_schedule_stop(_event, event_data, _listener_data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         Handles the termination of a schedule node. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         :param * _event: Not used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         :param tuple[dict] event_data: The old and new node status. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         :param * _listener_data: Not used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         del _event, _listener_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if event_data[0]['rst_id'] != event_data[1]['rst_id']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             # If status is error send mail. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             if event_data[1]['rst_id'] == C.ENK_RST_ID_ERROR: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 MailOperatorEventHandler.__send_mail_schedule_node_failed(event_data[1]['rnd_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             # If status is success send mail. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             if event_data[1]['rst_id'] == C.ENK_RST_ID_COMPLETED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 MailOperatorEventHandler.__send_mail_schedule_node_success(event_data[1]['rnd_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 170 |  |  |     # ------------------------------------------------------------------------------------------------------------------ | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |     def handle_node_creation(_event, node, _listener_data): | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |         Handles a node creation event. | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |         :param * _event : Not used. | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |         :param enarksh.controller.node.Node.Node node: The created node. | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |         :param * _listener_data: Not used. | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |         del _event, _listener_data | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |         if isinstance(node, SimpleNode): | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |             node.event_state_change.register_listener(MailOperatorEventHandler.__handle_simple_node_stop) | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |         if isinstance(node, ScheduleNode): | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |             node.event_state_change.register_listener(MailOperatorEventHandler.__handle_schedule_stop) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  | # ---------------------------------------------------------------------------------------------------------------------- | 
            
                                                        
            
                                    
            
            
                | 189 |  |  |  | 
            
                        
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.