Exemplo n.º 1
0
            # - force a SIGKILL to shut the proxy down
            #
            running.kill()

        def configure(self, cluster):

            #
            # - grep our listeners (the CI hooks)
            # - render into our 'local' backend directive (which is a standalone file)
            #
            urls = cluster.grep('hook', 5000).split(',')
            env = Environment(loader=FileSystemLoader(join(dirname(__file__), 'templates')))
            logger.info('%d downstream urls ->\n - %s' % (len(urls), '\n - '.join(urls)))
            mappings = \
                {
                    'port': 9000,
                    'listeners': {'listener-%d' % index: endpoint for index, endpoint in enumerate(urls)}
                }

            template = env.get_template('haproxy.cfg')
            with open('%s/haproxy.cfg' % self.cwd, 'w') as f:
                f.write(template.render(mappings))

            #
            # - at this point we have both the global/frontend and our default backend
            # - start haproxy using both configuration files
            #
            return '/usr/sbin/haproxy -f haproxy.cfg', {}

    Pod().boot(Strategy, model=Model, tools=[Shell])
Exemplo n.º 2
0
    class Strategy(Piped):

        cwd = '/opt/handler'
        pid = None
        since = 0.0

        # This method is optional for Ochopod to work, but we will always implement it and
        # return the uptime of the sub-process at least.

        def sanity_check(self, pid):
            #
            # - simply use the provided process ID to start counting time
            # - this is a cheap way to measure the sub-process up-time
            #
            now = time.time()
            if pid != self.pid:
                self.pid = pid
                self.since = now
            lapse = (now - self.since) / 3600.0
            return {'uptime': '%.2f hours (pid %s)' % (lapse, pid)}

        def configure(self, cluster):
            #
            # - simply pause for a minute and stop
            # - specify what you need to run below
            #
            return 'python /opt/flaskChatbot/run.py', {}

    Pod().boot(Strategy)
Exemplo n.º 3
0
if __name__ == '__main__':

    class Echo(Tool):

        #: the tag is mandatory and used to identify the tool when you HTTP POST to the pod
        tag = 'echo'

        def body(self, args, _):

            #
            # - simply exec the echo
            # - please note we don't need any command line parsing
            # - the output will be returned back to the caller
            #
            return shell('echo "your command was %s"' % args)

    class Strategy(Piped):
        def configure(self, _):

            #
            # - just go to sleep, the point is not to run anything meaningful
            #
            return 'sleep 3600', {}

    #
    # - specify the tools your pod should support in the boot() call
    # - note you can also include default tools that are shipped with ochopod (e.g Shell for instance)
    #
    Pod().boot(Strategy, tools=[Echo, Shell], local=1)
Exemplo n.º 4
0
if __name__ == '__main__':

    class Strategy(Piped):

        #
        # - ochopod will detect cases where the sub-process fails on a non-zero exit code
        # - if we can't get the sub-process to either exit normally or just keep running the sanity check will fail
        # - here we want to tolerate up to 3 sanity check failures in a row with 5 seconds between each
        #
        checks = 3

        check_every = 5.0

        shell = True

        def configure(self, _):

            #
            # - attempt something that will fail on a non-zero exit code
            # - ochopod will attempt to re-run the command but the next sanity check will automatically trip
            # - after 3 such failures the pod will be turned off automatically
            #
            return 'echo kaboom && exit 1', {}

    #
    # - if you run this script locally you will notice the pod will turn off after around 15 seconds
    # - simply type CTRL-C to exit
    #
    Pod().boot(Strategy, local=1)
Exemplo n.º 5
0
        since = 0.0

        pipe_subprocess = True

        def sanity_check(self, pid):

            #
            # - simply use the provided process ID to start counting time
            # - this is a cheap way to measure the sub-process up-time
            #
            now = time.time()
            if pid != self.pid:
                self.pid = pid
                self.since = now

            lapse = (now - self.since) / 3600.0

            return \
                {
                    'uptime': '%.2f hours (pid %s)' % (lapse, pid)
                }

        def configure(self, cluster):

            #
            # - start the hosting web framework on TCP 9000
            #
            return 'gunicorn -b 0.0.0.0:9000 sink:endpoint', {}

    Pod().boot(Strategy, tools=[Shell])