Example #1
0
    def loop(self):
        logging.basicConfig(level = logging.DEBUG)

        base_url = appier.conf("BASE_URL", BASE_URL)
        secret_key = appier.conf("SECRET_KEY", None)
        node_id = appier.conf("NODE_ID", "node")
        node_name = appier.conf("NODE_NAME", "node")
        node_location = appier.conf("NODE_LOCATION", "undefined")

        headers = dict()
        if secret_key: headers["X-Secret-Key"] = secret_key

        while True:
            try:
                logging.info("Submitting node information")
                appier.post(
                    base_url + "nodes/%s" % node_id,
                    data_j = dict(
                        name = node_name,
                        location = node_location
                    ),
                    headers = headers
                )
                logging.info("Retrieving jobs for node '%s'" % node_id)
                jobs = appier.get(
                    base_url + "nodes/%s/jobs" % node_id,
                    headers = headers,
                    timeout = 600
                )
                logging.info("Retrieved %d jobs for node '%s'" % (len(jobs), node_id))
                for job in jobs: self.print_job(job)
            except BaseException as exception:
                logging.info("Exception while looping '%s'" % str(exception))
                logging.info("Sleeping for %.2f seconds" % self.sleep_time)
                time.sleep(self.sleep_time)
Example #2
0
    def enter(self, info = None, app = None, *args, **kwargs):
        info = info or dict()
        payload = kwargs.get("payload", {})
        entity = payload.get("entity", None)
        key = payload.get("key", None)

        # in case either no entity or no key is provided there's nothing
        # to be done for this enter operation
        if not entity or not key: return

        # ensures that the entity being used is a legitimate one, meaning
        # that the key that is provided is the expected one
        crossline.Entity.verify_g(entity, key)

        # retrieves the entity instance from the database and tries to
        # gather both the external service code and secret values in case
        # they are not available returns the control flow immediately
        entity_e = crossline.Entity.get_by_id(entity)
        code = entity_e.meta.get("pica:codigo")
        secret = entity_e.meta.get("pica:senha")
        if not code or not secret: return

        # retrieves the global configuration values that are going to be used
        # in the HTTP POST operation to the back-end service
        base_url = appier.conf("PICA_BASE_URL", "https://picaponto.pt/")
        company = appier.conf("PICA_COMPANY", cast = int)

        # resolves the kind of movement in context and then sets it under the
        # info dictionary to be stored latter, notice that the saving of the
        # entity related with the enter action is validated so that no extra
        # storage occurs for the "duplicated" enter operations
        movement = self._res_movement(entity)
        if not movement in ("Duplicado",): info["pica:movimento"] = movement
        info["save"] = info.get("save", True) and not movement in ("Duplicado",)

        # in case the movement is part of the "blacklisted" ones the control flow
        # should return immediately to avoid remote communication
        if movement in ("Duplicado",): return

        # runs the concrete HTTP operation, effectively persisting the
        # information on the external service
        appier.post(
            base_url + "ponto/ajax_add",
            params = dict(
                empresa_id = company,
                codigo = code,
                senha = secret,
                tipo_movimento = movement
            )
        )
Example #3
0
 def notify_http(self, arguments = {}):
     cls = self.__class__
     url = arguments.get("url", None)
     retries = arguments.get("retries", 3)
     logger = appier.get_logger()
     logger.debug("Running HTTP notification for '%s' ..." % url)
     return cls._retry(lambda: appier.post(url, data_j = arguments), count = retries)
Example #4
0
 def notify_http(self, arguments={}):
     cls = self.__class__
     url = arguments.get("url", None)
     retries = arguments.get("retries", 3)
     logger = appier.get_logger()
     logger.debug("Running HTTP notification for '%s' ..." % url)
     return cls._retry(lambda: appier.post(url, data_j=arguments),
                       count=retries)
Example #5
0
    def test_file(self):
        data, response = appier.post(
            "https://%s/post" % self.httpbin,
            data=appier.legacy.BytesIO(b"hello world"),
            handle=True,
            reuse=False)

        code = response.getcode()
        self.assertNotEqual(code, 302)
        self.assertEqual(code, 200)
        self.assertEqual(data["data"], "hello world")
Example #6
0
def recaptcha_ensure(self, token, action="default", force=False):
    if not _recaptcha_available() and not force: return
    secret = appier.conf("RECAPTCHA_SECRET", None)
    min_score = appier.conf("RECAPTCHA_MIN", 0.5)
    result = appier.post("https://www.google.com/recaptcha/api/siteverify",
                         params=dict(secret=secret, response=token))
    if result.get("score", 0) >= min_score and\
        result.get("action", None) == action:
        return token
    raise appier.AppierException(message="Invalid reCAPTCHA score or action",
                                 code=403)
Example #7
0
    def test_file(self):
        data, response = appier.post(
            "https://%s/post" % self.httpbin,
            data = appier.legacy.BytesIO(b"hello world"),
            handle = True,
            reuse = False
        )

        code = response.getcode()
        self.assertNotEqual(code, 302)
        self.assertEqual(code, 200)
        self.assertEqual(data["data"], "hello world")
Example #8
0
    def test_generator(self):
        def text_g(message=[b"hello", b" ", b"world"]):
            yield sum(len(value) for value in message)
            for value in message:
                yield value

        data, response = appier.post("https://%s/post" % self.httpbin,
                                     data=text_g(),
                                     handle=True,
                                     reuse=False)

        code = response.getcode()
        self.assertNotEqual(code, 302)
        self.assertEqual(code, 200)
        self.assertEqual(data["data"], "hello world")
Example #9
0
    def test_generator(self):
        def text_g(message = [b"hello", b" ", b"world"]):
            yield sum(len(value) for value in message)
            for value in message:
                yield value

        data, response = appier.post(
            "https://%s/post" % self.httpbin,
            data = text_g(),
            handle = True,
            reuse = False
        )

        code = response.getcode()
        self.assertNotEqual(code, 302)
        self.assertEqual(code, 200)
        self.assertEqual(data["data"], "hello world")
Example #10
0
 def notify_http(self, arguments = {}):
     url = arguments.get("url", None)
     logger = appier.get_logger()
     logger.debug("Running HTTP notification for '%s' ..." % url)
     return appier.post(url, data_j = arguments)
Example #11
0
def _upload(path, repo="colony", generate=True, delete=True):
    import json
    import appier

    # tries to runs the expansion of the glob for the provided path and
    # in case it expands to multiple values the same upload operation is
    # run on each of the items that compromise the expansion
    expansion = glob.glob(path)
    is_multiple = len(expansion) > 1
    if is_multiple:
        for item in expansion:
            _upload(item, repo=repo, generate=generate, delete=delete)
        return
    if not expansion: raise RuntimeError("No path found for '%s'" % path)
    path = expansion[0]

    # in case the generate flag is active the package file is generated
    # for the path and then the descriptor information dictionary is read
    # so that it's possible to properly upload the file to the repository
    if generate: path = _generate(path)
    descriptor = _read(path)

    # opens the path of the package file that is going to be uploaded
    # in order to be able to read the full contents of it, these are
    # going to be the contents to be uploaded to the repository
    file = open(path, "rb")
    try:
        contents = file.read()
    finally:
        file.close()

    # tries to retrieve the currently targeted repository info taking
    # into account both the environment and the static values
    repo_url = colony.conf("REPO_URL", REPO_URL)
    repo_username = colony.conf("REPO_USERNAME", "root")
    repo_password = colony.conf("REPO_PASSWORD", "root")

    # enforces the repository url to be defined as a sequence for better
    # handling of the logic and then retrieves the requested target
    # repository base url value, raising an exception in case it's not found
    if not type(repo_url) in (list, tuple): repo_url = (("colony", repo_url), )
    repo_url = dict(repo_url)
    repo_url = repo_url.get(repo)
    repo_url = colony.conf("REPO_URL_" + repo.upper(), repo_url)
    if not repo_url:
        raise RuntimeError("URL for repository '%s' not found" % repo)

    # prints a message about the upload operation that is going to occur
    # so that the end user knows where the upload is going
    output("Uploading %s into %s repo (%s)" %
           (descriptor["short_name"], repo, repo_url))

    # creates the url format, taking into account the defined url and the
    # current descriptor and then runs the upload, using a post operation
    url = repo_url + "packages"
    login_url = repo_url + "api/admin/login"
    auth = appier.post(login_url,
                       params=dict(username=repo_username,
                                   password=repo_password))
    appier.post(url,
                data_m=dict(sid=auth["sid"],
                            identifier=descriptor["id"],
                            name=descriptor["short_name"],
                            version=descriptor["version"],
                            type=descriptor["type"],
                            info=json.dumps(descriptor),
                            contents=("contents", contents)))

    # in case the delete flag is active the package file is delete after
    # a proper upload operation is performed (house keeping)
    if delete: os.remove(path)
Example #12
0
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import sys

import appier

UPLOAD_URL = "https://httpbin.org/post"

path = sys.argv[1]
url = sys.argv[2] if len(sys.argv) > 2 else UPLOAD_URL

contents, _response = appier.post(url,
                                  data=appier.file_g(path),
                                  handle=True,
                                  silent=True,
                                  redirect=True,
                                  retry=0)

print(contents)