Esempio n. 1
0
    def run(self):
        LOG.info("Sending email message "
                 "[from=%s, to=%s, subject=%s, using smtp=%s, body=%s...]" %
                 (self.sender, self.to, self.subject, self.smtp_server,
                  self.body[:128]))

        # TODO(dzimine): handle utf-8, http://stackoverflow.com/a/14506784
        message = text.MIMEText(self.body)
        message['Subject'] = self.subject
        message['From'] = self.sender
        message['To'] = self.to

        try:
            s = smtplib.SMTP(self.smtp_server)

            if self.password is not None:
                # Sequence to request TLS connection and log in (RFC-2487).
                s.ehlo()
                s.starttls()
                s.ehlo()
                s.login(self.sender, self.password)

            s.sendmail(from_addr=self.sender,
                       to_addrs=self.to,
                       msg=message.as_string())
        except (smtplib.SMTPException, IOError) as e:
            raise exc.ActionException("Failed to send an email message: %s" %
                                      e)
Esempio n. 2
0
    def run(self):
        LOG.info("Running HTTP action "
                 "[url=%s, method=%s, params=%s, body=%s, headers=%s,"
                 " cookies=%s, auth=%s, timeout=%s, allow_redirects=%s,"
                 " proxies=%s, verify=%s]" %
                 (self.url, self.method, self.params, self.body, self.headers,
                  self.cookies, self.auth, self.timeout, self.allow_redirects,
                  self.proxies, self.verify))

        try:
            resp = requests.request(self.method,
                                    self.url,
                                    params=self.params,
                                    data=self.body,
                                    headers=self.headers,
                                    cookies=self.cookies,
                                    auth=self.auth,
                                    timeout=self.timeout,
                                    allow_redirects=self.allow_redirects,
                                    proxies=self.proxies,
                                    verify=self.verify)
        except Exception as e:
            raise exc.ActionException("Failed to send HTTP request: %s" % e)

        LOG.info("HTTP action response:\n%s\n%s" %
                 (resp.status_code, resp.content))

        # TODO(everyone): Not sure we need to have this check here in base HTTP
        #                 action.
        if resp.status_code not in range(200, 307):
            raise exc.ActionException("Received error HTTP code: %s" %
                                      resp.status_code)

        # Construct all important resp data in readable structure.
        headers = dict(resp.headers.items())
        status = resp.status_code

        try:
            content = resp.json()
        except Exception as e:
            LOG.debug("HTTP action response is not json.")
            content = resp.content

        return {'content': content, 'headers': headers, 'status': status}
Esempio n. 3
0
 def run(self):
     try:
         script = """function f() {
             %s
         }
         f()
         """ % self.script
         return javascript.evaluate(script, self.context)
     except Exception as e:
         raise exc.ActionException("JavaScriptAction failed: %s" % str(e))
Esempio n. 4
0
 def run(self):
     try:
         method = self._get_client_method(self._get_client())
         result = method(**self._kwargs_for_run)
         if inspect.isgenerator(result):
             return [v for v in result]
         return result
     except Exception as e:
         raise exc.ActionException("%s failed: %s" %
                                   (self.__class__.__name__, e))
Esempio n. 5
0
    def run(self):
        try:
            method = self._get_client_method(self._get_client())

            return method(**self._kwargs_for_run)
        except Exception as e:
            e_str = '%s: %s' % (type(e), e.message)

            raise exc.ActionException(
                "%s.%s failed: %s" %
                (self.__class__.__name__, self.client_method_name, e_str))
Esempio n. 6
0
 def test(self):
     raise exc.ActionException('Fail action expected exception.')
Esempio n. 7
0
    def run(self):
        LOG.info('Running fail action.')

        raise exc.ActionException('Fail action expected exception.')
Esempio n. 8
0
 def raise_exc(parent_exc=None):
     message = ("Failed to execute ssh cmd "
                "'%s' on %s" % (self.cmd, self.host))
     if parent_exc:
         message += "\nException: %s" % str(parent_exc)
     raise exc.ActionException(message)