From c1d80cabbd4898d770aa5ccc14a188731c63cb6e Mon Sep 17 00:00:00 2001 From: Andreas Balogh Date: Tue, 16 Feb 2021 15:44:39 +0100 Subject: [PATCH] simplest flask server --- .gitlab-ci.yml | 58 ------------- .settings/org.eclipse.core.resources.prefs | 2 - README.md | 4 +- src/webui/flaskr/redis_db.py | 26 ------ src/webui/flaskr/zcm.py | 94 ---------------------- src/webui/flaskr/zeo_db.py | 27 ------- src/zeo/Dockerfile | 12 --- src/zeo/run.sh | 9 --- src/zeo/zeo.conf | 9 --- {src/webui => webui}/Dockerfile | 0 webui/flaskr/hello.py | 8 ++ webui/requirements.txt | 1 + {src/webui => webui}/run.sh | 0 {src/webui => webui}/tester.sh | 0 14 files changed, 11 insertions(+), 239 deletions(-) delete mode 100644 .gitlab-ci.yml delete mode 100644 .settings/org.eclipse.core.resources.prefs delete mode 100644 src/webui/flaskr/redis_db.py delete mode 100644 src/webui/flaskr/zcm.py delete mode 100644 src/webui/flaskr/zeo_db.py delete mode 100644 src/zeo/Dockerfile delete mode 100644 src/zeo/run.sh delete mode 100644 src/zeo/zeo.conf rename {src/webui => webui}/Dockerfile (100%) create mode 100644 webui/flaskr/hello.py create mode 100644 webui/requirements.txt rename {src/webui => webui}/run.sh (100%) rename {src/webui => webui}/tester.sh (100%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index e2b2810..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,58 +0,0 @@ -build_zeo: - stage: build - tags: - - shell - - ubuntu - variables: - IMAGE_TAG: $CI_REGISTRY_IMAGE/zeo:$CI_COMMIT_REF_SLUG - RELEASE_TAG: $CI_REGISTRY_IMAGE/zeo:latest - script: - - cd src/zeo - - docker build -t $IMAGE_TAG . - - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - - docker push $IMAGE_TAG - -build_flask: - stage: build - tags: - - shell - - ubuntu - variables: - IMAGE_TAG: $CI_REGISTRY_IMAGE/webui:$CI_COMMIT_REF_SLUG - RELEASE_TAG: $CI_REGISTRY_IMAGE/webui:latest - script: - - cd src/webui - - docker build -t $IMAGE_TAG . - - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - - docker push $IMAGE_TAG - -build_tester: - stage: build - tags: - - shell - - ubuntu - script: - - echo "Build api image" - - export - -# https://gitlab.com/gitlab-org/gitlab-runner/-/issues/26785 -unit_test: - stage: test - services: - - name: $CI_REGISTRY_IMAGE/zeo:$CI_COMMIT_REF_SLUG - alias: zeo - - name: $CI_REGISTRY_IMAGE/webui:$CI_COMMIT_REF_SLUG - alias: webui - image: - name: $CI_REGISTRY_IMAGE/webui:$CI_COMMIT_REF_SLUG - entrypoint: [""] - tags: - - kubernetes - artifacts: - paths: - - var/ - script: - - '[ -d var ] || mkdir var' - - cd var - - wget webui:5000 -o wget.log - - echo "Done." diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 533a43b..0000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/webui/flaskr/zcm.py=utf8 diff --git a/README.md b/README.md index 73a6d09..51aea7a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# scraper +# Kubernetes AutoDevOps example -Ripper worming through blogs for videos, images, text. \ No newline at end of file +Example project to test gitlab CI, Docker, Kubernetes and AutoDevOps integration. \ No newline at end of file diff --git a/src/webui/flaskr/redis_db.py b/src/webui/flaskr/redis_db.py deleted file mode 100644 index 43bd109..0000000 --- a/src/webui/flaskr/redis_db.py +++ /dev/null @@ -1,26 +0,0 @@ -import time - -from flask import Flask -import redis - - -app = Flask(__name__) -cache = redis.Redis(host='redis', port=6379) - - -def get_hit_count(): - retries = 5 - while True: - try: - return cache.incr('hits') - except redis.exceptions.ConnectionError as exc: - if retries == 0: - raise exc - retries -= 1 - time.sleep(0.5) - - -@app.route('/') -def hello(): - count = get_hit_count() - return 'Hello World! I have been seen {} times.\n'.format(count) diff --git a/src/webui/flaskr/zcm.py b/src/webui/flaskr/zcm.py deleted file mode 100644 index 2e28135..0000000 --- a/src/webui/flaskr/zcm.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (c) 2016 Andreas -# See LICENSE for details. - -""" ZODB context manager """ - -# from BTrees.IOBTree import IOBTree -# from BTrees.OOBTree import OOBTree -# from persistent import Persistent -# from persistent.list import PersistentList as PList -# from persistent.mapping import PersistentMapping as PDict - -from ZEO.ClientStorage import ClientStorage -from ZODB import DB -from ZODB.FileStorage import FileStorage - - -class ZDatabase(): - """ Provides a ZODB database context manager """ - - def __init__(self, uri, **kwargs): - self.storage = create_storage(uri) - self.db = DB(self.storage, **kwargs) - - def __enter__(self): - return self.db - - def __exit__(self, exc_type, exc_value, traceback): - self.db.close() - return False - - -class ZConnection(): - """ Provides a ZODB connection with auto-abort (default). - Provides a tuple of connection and root object: - with ZConnection(db) as (cx, root): - root.one = "ok" - ZConnection implements a connection context manager. - Transaction context managers in contrast do auto-commit: - a) with db.transaction() as connection, or - b) with cx.transaction_manager as transaction, or - c) with transaction.manager as transaction (for the thread-local transaction manager) - See also http://www.zodb.org/en/latest/guide/transactions-and-threading.html - """ - def __init__(self, db, auto_commit=False, transaction_manager=None): - self.db = db - self.auto_commit = auto_commit - self.transaction_manager = transaction_manager - self.cx = None - - def __enter__(self): - if self.transaction_manager: - self.cx = self.db.open(self.transaction_manager) - else: - self.cx = self.db.open() - return self.cx, self.cx.root() - - def __exit__(self, exc_type, exc_value, traceback): - if self.auto_commit: - self.cx.transaction_manager.commit() - self.cx.close() - return False - - -def create_storage(uri): - """ supported URIs - file://e:/workspaces/zeo/bots.fs - zeo://localhost:8001 - e:/workspaces/zeo/bots.fs - @see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier - """ - if uri.startswith("file://"): - storage = FileStorage(uri[7:]) - elif uri.startswith("zeo://"): - addr, port = uri[6:].split(":") - # addr_ = addr.encode("ASCII") - storage = ClientStorage((addr, int(port))) - else: - storage = FileStorage(uri) - return storage - - -def database(uri): - """ convenience function for single thread, return one connection from the pool """ - storage = create_storage(uri) - db = DB(storage) - return db - - -def connection(db): - """ Convenience function for multi thread, returns - connection, transaction manager and root - """ - cx = db.open() - return cx, cx.root() diff --git a/src/webui/flaskr/zeo_db.py b/src/webui/flaskr/zeo_db.py deleted file mode 100644 index aa07f1d..0000000 --- a/src/webui/flaskr/zeo_db.py +++ /dev/null @@ -1,27 +0,0 @@ -from ZEO.ClientStorage import ClientStorage -from ZODB import DB -import socket -from flask import Flask - - -app = Flask(__name__) -ipv4 = socket.gethostbyname("zeo") -storage = ClientStorage((ipv4, 8100)) -db = DB(storage) -cx = db.open() - - -def get_hit_count(): - root = cx.root() - if "hits" not in root: - root['hits'] = 0 - hits = root['hits'] - root['hits'] = hits + 1 - cx.transaction_manager.commit() - return hits - - -@app.route('/') -def hello(): - count = get_hit_count() - return 'Hello World! I have been seen {} times.\n'.format(count) diff --git a/src/zeo/Dockerfile b/src/zeo/Dockerfile deleted file mode 100644 index 9b76660..0000000 --- a/src/zeo/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM python:3.9-slim -RUN apt-get update && apt-get install -y \ - gcc \ - && rm -rf /var/lib/apt/lists/* -RUN pip install ZODB==5.6.0 -RUN pip install ZEO==5.2.1 -COPY ./zeo.conf /etc/zeo.conf -COPY ./run.sh /run.sh -RUN chmod +x /run.sh -VOLUME ["/var/zeo/fs", "/var/zeo/blobs"] -EXPOSE 8100 -CMD /run.sh \ No newline at end of file diff --git a/src/zeo/run.sh b/src/zeo/run.sh deleted file mode 100644 index 3102c2d..0000000 --- a/src/zeo/run.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -# We need to add a empty Data.fs if it does not exist -# as zeo will not start without on. -if [ ! -f /var/zeo/fs/Data.fs ]; then - touch /var/zeo/fs/Data.fs -fi - -exec /usr/local/bin/runzeo -a 8100 -C /etc/zeo.conf \ No newline at end of file diff --git a/src/zeo/zeo.conf b/src/zeo/zeo.conf deleted file mode 100644 index 2d9b5c8..0000000 --- a/src/zeo/zeo.conf +++ /dev/null @@ -1,9 +0,0 @@ - - address 8100 - read-only false - - - - path /var/zeo/fs/Data.fs - blob-dir /var/zeo/blobs - \ No newline at end of file diff --git a/src/webui/Dockerfile b/webui/Dockerfile similarity index 100% rename from src/webui/Dockerfile rename to webui/Dockerfile diff --git a/webui/flaskr/hello.py b/webui/flaskr/hello.py new file mode 100644 index 0000000..3fbe2f6 --- /dev/null +++ b/webui/flaskr/hello.py @@ -0,0 +1,8 @@ +from flask import Flask + +app = Flask(__name__) + + +@app.route('/') +def hello(): + return 'Hello World!\n' diff --git a/webui/requirements.txt b/webui/requirements.txt new file mode 100644 index 0000000..e3e9a71 --- /dev/null +++ b/webui/requirements.txt @@ -0,0 +1 @@ +Flask diff --git a/src/webui/run.sh b/webui/run.sh similarity index 100% rename from src/webui/run.sh rename to webui/run.sh diff --git a/src/webui/tester.sh b/webui/tester.sh similarity index 100% rename from src/webui/tester.sh rename to webui/tester.sh