Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-components
Browse files Browse the repository at this point in the history
  • Loading branch information
cgerull committed Aug 3, 2023
2 parents d597a7e + d466181 commit 7e687d9
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ __pycache__/
.pytest_cache
*.py[cod]
/venv
/.venv
/.venv*
/instance/
*.log
*.tar
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.17
FROM alpine:3.18
# Use alpine linux as base image
ARG DOCKER_TAG

Expand Down
17 changes: 11 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ PY_FILES := test_server/*.py
TEMPLATES := test_server/templates/*

# default target, when make executed without arguments
help: ## Show this help.
help: ## Show this help.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

install_modules:
install_modules: ## Install development requirements
pip install --upgrade pip &&\
pip install -r requirements-dev.txt

lint:
lint: ## Run pylint
pylint --disable=R,C,E0237,E1101,W0511 test_server

test:
test:lint ## Execute python unit tests
pytest -vv --junitxml=test-results.xml --cov-report term-missing --cov=test_server tests/test_*.py

run:test ## Run application local in python
export ENV=development && \
export TOOLS_ENABLED=False && \
./run_gunicorn.sh

format:
black *.py

Expand All @@ -30,7 +35,7 @@ testserver.tar: Dockerfile $(PY_FILES) $(TEMPLATES) ## Build docker image and sa

scan: testserver.tar ## Scan docker image
@docker load -i testserver-latest.tar
docker scan testserver:latest
docker scout cves testserver:latest

push: scan ## Push to registry, parameters are REGISTRY, IMAGE and TAG
@docker load -i testserver-latest.tar
Expand All @@ -50,7 +55,7 @@ cross-build: ## Build for configure architectures and pushes to docker hub.
@docker buildx build --platform ${BUILDX_PLATFORMS} -t ${PROD_IMAGE} --push ./app

up: ## Run docker-compose and start the container
@docker-compose up -d
@docker-compose up -d

down: ## Run docker-compose and stop the container
@docker-compose down
Expand Down
6 changes: 3 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ behave>=1.2.6
black==23.7.0
Flask==2.3.2
Flask-Bootstrap==3.3.7.1
gunicorn==20.1.0
gunicorn==21.2.0
jinja2>=3.1.2
pdoc==12.1.0
pdoc==14.0.0
prometheus-client==0.17.1
prometheus-flask-exporter==0.22.4
PyHamcrest>=2.0.4
Expand All @@ -15,4 +15,4 @@ pytest-testinfra==8.1.0
PyYaml==6.0.1
redis==4.6.0
Werkzeug>=2.2.3
wrapt==1.15.0p
wrapt>=1.15.0
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==2.3.2
Flask==2.3.2.
Flask-Bootstrap==3.3.7.1
gunicorn==21.2.0
jinja2>=3.1.2
Expand Down
1 change: 0 additions & 1 deletion test_server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,3 @@ def wrapped_view(**kwargs):
return view(**kwargs)

return wrapped_view

10 changes: 0 additions & 10 deletions test_server/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
URL: /echo
"""
# from datetime import datetime
# import socket
# import platform

from flask import (
Blueprint, current_app, render_template, request, flash
Expand All @@ -27,11 +24,7 @@

from test_server.persistent_counter import get_redis_connection
from test_server.persistent_counter import increment_redis_counter
# from werkzeug.security import check_password_hash, generate_password_hash

from test_server.db import get_db

# from test_server.local_data import LocalData
from test_server.echo_data import EchoData

bp = Blueprint('echo', __name__, url_prefix='/')
Expand All @@ -44,10 +37,7 @@ def index():
@bp.route('/echo', methods=['GET'])
def echo():
"""Build HTML response data and send page to requester."""
# local_data = LocalData()
remote_data = EchoData(request)
# response_data = build_response_data()
# response_data = echo_data.get_local_data()

page_views = 0
my_db= get_db()
Expand Down
22 changes: 14 additions & 8 deletions test_server/local_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datetime import datetime
import socket
import platform
# import os
import re
import subprocess

Expand All @@ -23,8 +22,7 @@ class LocalData():

def __init__(self):
self.local_data = self.set_local_data()
# self.server_info = self.set_server_info()
# self.server_state = self.set_server_state()


@staticmethod
def set_server_info():
Expand All @@ -44,6 +42,7 @@ def set_server_info():
'Hostname': hostname,
}


@staticmethod
def set_server_state():
"""
Expand All @@ -52,8 +51,17 @@ def set_server_state():
"""
hostname = socket.gethostname()
sys_metrics = subprocess.check_output("uptime").decode("utf-8")
uptime = re.search(r' up (.+?), \d* users', sys_metrics).group(1)
load = re.search(r' averages: ((\d*\.\d+ ?)+)', sys_metrics).group(1)
# uptime = re.search(r' up (.+?), \d* users', sys_metrics).group(1)
uptime = ""
uptime_re = re.search(r' up (.+?), \d* users', sys_metrics)
if uptime_re:
uptime = uptime_re.group(1)

load = ""
# load = re.search(r' averages: ((\d*\.\d+ ?)+)', sys_metrics).group(1)
load_re = re.search(r' averages: ((\d*\.\d+ ?)+)', sys_metrics)
if load_re:
load = load_re.group(1)

return {
'Server time': datetime.now().isoformat(sep=' '),
Expand All @@ -76,6 +84,7 @@ def get_server_state(self):
"""
return self.set_server_state()


@staticmethod
def get_secret_key():
"""
Expand Down Expand Up @@ -115,7 +124,4 @@ def set_local_data(self):
'local_ip': socket.gethostbyname(hostname),
'container_name': hostname,
'secret': self.get_secret_key(),
# 'remote_ip': get_remote_ip(),
# 'version': current_app.config['VERSION'],
# 'environment': current_app.config['ENV']
}
1 change: 0 additions & 1 deletion test_server/persistent_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def get_redis_connection(host, port=6379, password=None):
if host:
try:
print(f"Connect to Redis service on {host}.")
# redis_connection = redis.from_url(redis_url,)
redis_connection = redis.Redis(
host = host,
port = port,
Expand Down
76 changes: 6 additions & 70 deletions test_server/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@
URL: /status
"""
# from datetime import datetime
# import os
# import platform
# import socket

from flask import (
Blueprint, current_app, render_template, flash
Blueprint, current_app, render_template, flash, session, redirect, url_for
)
from test_server.local_data import LocalData
# from flask.helpers import url_for
# from werkzeug.utils import redirect

# from test_server.persistent_counter import get_redis_connection
# from test_server.persistent_counter import increment_redis_counter
# from werkzeug.security import check_password_hash, generate_password_hash

# from test_server.db import get_db

bp = Blueprint('status', __name__, url_prefix='/')

Expand All @@ -34,71 +22,19 @@
# @login_required
def status():
"""Build response data and send page to requester."""
# response_data = build_response_data()
local_data = LocalData()
error = None


# return render_template('status/status.html',resp=response_data)
if error:
flash(error)
if 'user_id' not in session.keys():
flash("Not logged on, redirecting to login page.")
return redirect(url_for("auth.login"))
else:
print(f"Statuspage permitted for user {session['user_id']}")

return render_template('status/status.html',
server_info=local_data.get_server_info(),
server_state=local_data.get_server_state(),
# state=local_data.get_server_state(),
env={
'version': current_app.config['VERSION'],
'environment': current_app.config['ENV']
},
)
#
# Utiliy functions
# ############################################################################
# def build_response_data():
# """
# Build a dictionary with timestamp, server ip,
# server name, secret and requester ip.
# """
# hostname = socket.gethostname()

# return {
# 'now': datetime.now().isoformat(sep=' '),
# 'platform': platform.platform(),
# 'os_version': platform.version(),
# 'load_average': os.system("uptime | cut -f 4 -d ,"),
# 'hostname' : hostname
# }

# def get_remote_ip():
# """
# Get client ip address, trying to resolve any
# proxies that modify the request.
# """
# client_ip = ''
# if 'HTTP_X_REAL_IP' in request.environ :
# client_ip = request.environ['HTTP_X_REAL_IP']
# elif 'X_REAL_IP' in request.environ :
# client_ip = request.environ['X_REAL_IP']
# elif 'HTTP_X_FORWARDED_FOR' in request.environ :
# client_ip = request.environ['HTTP_X_FORWARDED_FOR']
# else:
# client_ip = request.remote_addr

# return client_ip

# def get_secret_key():
# """
# Return secret key from:
# Docker secret file or
# Environment variable SECRET_KEY or
# a default value
# """
# secret = ''
# try:
# with open(current_app.config['SECRET_FILE'], mode = 'r', encoding = 'utf_8') as file:
# secret = file.read()
# except IOError:
# # no file, return configured secret
# secret = current_app.config['SECRET_KEY']
# return secret
Loading

0 comments on commit 7e687d9

Please sign in to comment.