diff --git a/carpool/.dockerignore b/carpool/.dockerignore new file mode 100644 index 0000000..7af26dd --- /dev/null +++ b/carpool/.dockerignore @@ -0,0 +1,5 @@ +docker-compose.yml +*.sql +.dockerignore +*.json +Dockerfile diff --git a/carpool/Dockerfile b/carpool/Dockerfile new file mode 100644 index 0000000..dbfc688 --- /dev/null +++ b/carpool/Dockerfile @@ -0,0 +1,12 @@ +# syntax=docker/dockerfile:1 +FROM python:3.8-slim-buster +ARG DEBIAN_FRONTEND=noninteractive +ARG DEBCONF_NOWARNINGS="yes" +WORKDIR /app +COPY requirements.txt requirements.txt +RUN pip3 install --disable-pip-version-check -r requirements.txt +COPY . . +ENV FLASK_APP=python_rest +EXPOSE 5000 +STOPSIGNAL SIGINT +CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"] \ No newline at end of file diff --git a/carpool/car_1.json b/carpool/car_1.json new file mode 100644 index 0000000..3c68a8a --- /dev/null +++ b/carpool/car_1.json @@ -0,0 +1,6 @@ +{ + "license_plate": "BO-PR-72", + "car_type": "mini clubman SD", + "fuel": "diesel", + "number_of_seats": "5" +} diff --git a/carpool/car_2.json b/carpool/car_2.json new file mode 100644 index 0000000..6179693 --- /dev/null +++ b/carpool/car_2.json @@ -0,0 +1,6 @@ +{ + "license_plate": "BO-PR-46", + "car_type": "bmw", + "fuel": "benzin", + "number_of_seats": "5" +} diff --git a/carpool/docker-compose.yml b/carpool/docker-compose.yml new file mode 100644 index 0000000..4785bfd --- /dev/null +++ b/carpool/docker-compose.yml @@ -0,0 +1,44 @@ +version: "3.3" +services: + traefik: + image: "traefik:v2.8" + container_name: "traefik" + command: + #- "--log.level=DEBUG" + - "--api.insecure=true" + - "--providers.docker=true" + - "--providers.docker.exposedbydefault=false" + - "--entrypoints.web.address=:80" + ports: + - "80:80" + - "8080:8080" + volumes: + - "/var/run/docker.sock:/var/run/docker.sock:ro" + rest: + build: . + image: bee42/carpool:0.1.0 + labels: + - "traefik.enable=true" + - "traefik.http.routers.car.rule=Host(`car.localhost`)" + - "traefik.http.routers.car.entrypoints=web" + - "traefik.http.services.car.loadbalancer.server.port=5000" + environment: + FLASK_APP: python_rest + POSTGRES_DB: esentricar + POSTGRES_USER: cnbc + POSTGRES_PASSWORD: postgres + POSTGRES_HOST: postgres + postgres: + image: postgres + container_name: postgres + restart: always + environment: + POSTGRES_DB: postgres_db + POSTGRES_USER: cnbc + POSTGRES_PASSWORD: postgres + PGDATA: /var/lib/postgresql/data/pgdata + volumes: + - postgres:/var/lib/postgresql/data + - ./esentricar.sql:/docker-entrypoint-initdb.d/esentricar.sql +volumes: + postgres: \ No newline at end of file diff --git a/carpool/esentricar.sql b/carpool/esentricar.sql new file mode 100644 index 0000000..99fd2ae --- /dev/null +++ b/carpool/esentricar.sql @@ -0,0 +1,11 @@ +CREATE DATABASE esentricar; +\c esentricar; +CREATE TABLE cars ( + car_id SERIAL PRIMARY KEY, + license_plate varchar(30) NOT NULL, + car_type varchar(30) NOT NULL, + fuel varchar(30) NOT NULL, + number_of_seats integer NOT NULL +); +CREATE UNIQUE INDEX cars_license_plate +ON cars (license_plate); \ No newline at end of file diff --git a/carpool/index.env b/carpool/index.env new file mode 100644 index 0000000..3009d08 --- /dev/null +++ b/carpool/index.env @@ -0,0 +1 @@ +POSTGRES_PASSWORD=postgres \ No newline at end of file diff --git a/carpool/python_rest.py b/carpool/python_rest.py new file mode 100644 index 0000000..75415f4 --- /dev/null +++ b/carpool/python_rest.py @@ -0,0 +1,105 @@ +from flask import Flask, request, jsonify +from flask_sqlalchemy import SQLAlchemy +from flask_marshmallow import Marshmallow, fields +from flask_cors import CORS + +import os + +# Init app +app = Flask(__name__) + +user = os.environ.get('POSTGRES_USER') +db = os.environ.get('POSTGRES_DB') +passwd = os.environ.get('POSTGRES_PASSWORD') +host = os.environ.get('POSTGRES_HOST') + +#enable CORS +CORS(app) +# connect to already existing and running Database +app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' + user + ':' + passwd +'@' + host + ':5432/' + db + +# not important but otherwise we get a warning every time +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +#Init db +db = SQLAlchemy(app) +#Init ma +ma = Marshmallow(app) + +# Pool Car Class/Model +class Pool_Car(db.Model): + + #define table, in our case already existing + __tablename__ = 'cars' + car_id = db.Column(db.Integer, primary_key=True) + license_plate = db.Column(db.String(30), unique=True) + car_type = db.Column(db.String(20)) + fuel = db.Column(db.String(20)) + number_of_seats =db.Column(db.Integer) + + + # set class attributes + def __init__(self, license_plate, car_type, fuel, number_of_seats): + self.license_plate = license_plate + self.car_type = car_type + self.fuel = fuel + self.number_of_seats = number_of_seats + +class Pool_CarSchema(ma.Schema): + class Meta: + fields = ('car_id','license_plate','car_type','fuel','number_of_seats') + +# Init schema +pool_car_schema = Pool_CarSchema() +pool_cars_schema = Pool_CarSchema(many=True) + + +# Create a Pool_Car +@app.route('/car', methods=['POST']) +def add_pool_car(): + # Get request data as json + car_entity = request.get_json() + license_plate= car_entity.get('license_plate') + car_type = car_entity.get('car_type') + fuel = car_entity.get('fuel') + number_of_seats = car_entity.get('number_of_seats') + + new_pool_car = Pool_Car(license_plate, car_type, fuel, number_of_seats) + + db.session.add(new_pool_car) + db.session.commit() + + return pool_car_schema.jsonify(new_pool_car) + +# Get car_id,license_plate,car_type of all products of the table +@app.route('/car', methods=['GET']) +def get_pool_cars(): + all_pool_cars = Pool_Car.query.all() + result = pool_cars_schema.dump(all_pool_cars) + car_list= [] + for item in result: + car_details = { "car_id":None, "license_plate":None, "car_type":None} + car_details['car_id'] = item['car_id'] + car_details['license_plate'] = item['license_plate'] + car_details['car_type'] = item['car_type'] + car_list.append(car_details) + return jsonify(car_list) + +# Get Single Products +@app.route('/car/', methods=['GET']) +def get_pool_car(car_id): + pool_car = Pool_Car.query.get(car_id) + return pool_car_schema.jsonify(pool_car) + +# Delete Product +@app.route('/car/', methods=['DELETE']) +def delete_pool_car(car_id): + pool_car = Pool_Car.query.get(car_id) + db.session.delete(pool_car) + db.session.commit() + + return pool_car_schema.jsonify(pool_car) + + +if __name__ == '__main__': + app.run(debug=True) #port='5002' \ No newline at end of file diff --git a/carpool/requirements.txt b/carpool/requirements.txt new file mode 100644 index 0000000..a273034 --- /dev/null +++ b/carpool/requirements.txt @@ -0,0 +1,11 @@ +Flask==1.1.1 +Flask-Cors==3.0.8 +flask-marshmallow==0.10.1 +Flask-SQLAlchemy==2.4.1 +werkzeug==2.0.2 +marshmallow==3.2.2 +SQLAlchemy==1.3.11 +marshmallow-sqlalchemy==0.19.0 +psycopg2-binary==2.8.4 +itsdangerous==2.0.1 +Jinja2==3.0.3 \ No newline at end of file diff --git a/check_this b/check_this new file mode 100644 index 0000000..f5ee6df --- /dev/null +++ b/check_this @@ -0,0 +1,16 @@ +docker run -d --rm \ + --name my-distroless gcr.io/distroless/nodejs \ + -e 'setTimeout(() => console.log("Done"), 99999999)' + +CDEBUG_VERSION=0.0.3 + +curl -sL -o cdebug_linux_amd64.tar.gz \ + https://github.com/iximiuz/cdebug/releases/download/v${CDEBUG_VERSION}/cdebug_linux_amd64.tar.gz +mkdir cdebug +tar xzf cdebug_linux_amd64.tar.gz -C cdebug +sudo cp cdebug/cdebug /usr/local/bin +cdebug exec --privileged -it --image nixery.dev/shell/ps/vim/tshark my-distroless + + +https://github.com/iximiuz/cdebug + diff --git a/harbor_install b/harbor_install new file mode 100644 index 0000000..e883ed0 --- /dev/null +++ b/harbor_install @@ -0,0 +1,56 @@ +wget https://github.com/goharbor/harbor/releases/download/v2.6.2/harbor-online-installer-v2.6.2.tgz +tar xzf harbor-online-installer-v2.6.2.tgz +cd harbor +mkdir -p data/cert +cd data/cert +openssl genrsa -out ca.key 4096 +openssl req -x509 -new -nodes -sha512 -days 3650 \ + -subj "/C=CN/ST=NRW/L=Germany/O=linuxhotel/OU=Dev/CN=harbor.local" \ + -key ca.key \ + -out ca.crt +openssl genrsa -out harbor.local.key 4096 +openssl req -sha512 -new \ + -subj "/C=CN/ST=NRW/L=Germany/O= linuxhotel/OU=Dev/CN=harbor.local" \ + -key harbor.local.key \ + -out harbor.local.csr + +cat > v3.ext <