Skip to content

Commit

Permalink
Chore: Fix various linting issues
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Watkins <[email protected]>
  • Loading branch information
ModeSevenIndustrialSolutions committed May 14, 2024
1 parent a6b2e15 commit 6a20076
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ repos:
hooks:
- id: mypy
verbose: true
args: [--show-error-codes]
args: ["--show-error-codes", "--install-types", "--non-interactive"]
additional_dependencies: ["pytest", "types-requests"]

- repo: https://github.com/astral-sh/ruff-pre-commit
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
40 changes: 23 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,52 @@ Python must be available, preferably in a virtual environment (venv).

Some environment variables are used by various source code and scripts.
Setup your environment as follows (note that "source" is used)
~~~~

```console
source ./bin/environment.sh
~~~~
```

It is recommended that a Python virtual environment be created.
We have provided several convenience scripts to create and activate
a virtual environment. To create a new virtual environment using
these convenience scripts, execute the following (this will
create a directory called "venv" in your current working directory):
~~~~

```console
$PROJECT_DIR/bin/venv.sh
~~~~
```

Once your virtual enviornment has been created, it can be activated
as follows (note: you *must* activate the virtual environment
as follows (note: you _must_ activate the virtual environment
for it to be used, and the command requires "source" to ensure
environment variables to support venv are established correctly):
~~~~

```console
source $PROJECT_DIR/bin/vactivate.sh
~~~~
```

Install the required libraries as follows:
~~~~

```console
pip install -r requirements.txt
~~~~
```

Note that if you wish to run test cases then you will need
to also install "pytest" (it is not installed by default as
it is a development rather than product dependency).
~~~~

```console
pip install pytest
~~~~
```

## Creating a Docker Image

A Dockefile is provided for this service. A docker image for this
A Dockefile is provided for this service. A docker image for this
service can be creating using the following script:
~~~~

```console
$PROJECT_DIR/bin/dockerize.sh
~~~~
```

## Starting the Service

Expand All @@ -73,7 +79,7 @@ can be found [here](https://github.com/brodagroupsoftware/osc-dm-mesh-srv)

A standalone server can be started for testing purposes
using the following command:
~~~~
$PROJECT_DIR/app/start.sh
~~~~

```console
$PROJECT_DIR/app/start.sh
```
2 changes: 1 addition & 1 deletion app/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
HOST="0.0.0.0" ;
PORT=8000 ;
CONFIGURATION="./config/config.yaml" ;
python ./src/server.py --host $HOST --port $PORT --configuration $CONFIGURATION
python ./src/server.py --host $HOST --port $PORT --configuration $CONFIGURATION
10 changes: 5 additions & 5 deletions app/startd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if [ -z ${ROOT_DIR+x} ] ; then
fi

# Show the environment
$PROJECT_DIR/bin/show.sh
"$PROJECT_DIR"/bin/show.sh

usage() {
echo " "
Expand All @@ -39,15 +39,15 @@ echo "HOSTNAME: $HOSTNAME <--- This is the hostname running in docker"
echo " "

NETWORK_NAME="localnet"
docker network create $NETWORK_NAME
docker network create "$NETWORK_NAME"

compose() {
docker-compose -f $PROJECT_DIR/docker/docker-compose.yml up
docker-compose -f "$PROJECT_DIR"/docker/docker-compose.yml up
}

decompose() {
docker-compose -f $PROJECT_DIR/docker/docker-compose.yml down
docker-compose -f "$PROJECT_DIR"/docker/docker-compose.yml down
}

compose;
decompose;
decompose;
106 changes: 53 additions & 53 deletions bin/dockerize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ POSITIONAL_ARGS=()
PUBLISH="false" # default value

while [[ $# -gt 0 ]]; do
POSITIONAL_ARGS+=("$1")
case $1 in
POSITIONAL_ARGS+=("$1")
case $1 in
-p|--publish)
PUBLISH="true"
shift #move past arg
;;
PUBLISH="true"
shift #move past arg
;;
*) # ignore other args
shift
;;
esac
shift
;;
esac
done

function showHelp {
Expand Down Expand Up @@ -75,9 +75,9 @@ echo " "

prepare() {
echo "Preparing..."
cp $PROJECT_DIR/requirements.txt $WORKING_DIR
cp $PROJECT_DIR/docker/Dockerfile $WORKING_DIR
cp $PROJECT_DIR/src/*.py $WORKING_DIR
cp "$PROJECT_DIR/requirements.txt" "$WORKING_DIR"
cp "$PROJECT_DIR/docker/Dockerfile" "$WORKING_DIR"
cp "$PROJECT_DIR/src/*.py" "$WORKING_DIR"
}

cleanup() {
Expand All @@ -93,69 +93,69 @@ build() {
echo "Building Docker image $IMAGE_NAME:$VERSION"
echo "Images built using version $VERSION and latest"

docker build --build-arg GITHUB_TOKEN=$GITHUB_TOKEN \
docker build --build-arg GITHUB_TOKEN="$GITHUB_TOKEN" \
-t "$IMAGE_NAME:$VERSION" \
-t "$IMAGE_NAME:latest" \
-f Dockerfile . \
--no-cache=true
}

dockerhub_login() {
echo "logging in to docker hub with username $DOCKERHUB_USERNAME"
docker login -u "$DOCKERHUB_USERNAME" -p "$DOCKERHUB_TOKEN"
LOGIN_RETURN=$?

if [[ $LOGIN_RETURN -eq 0 ]]; then
echo "Successfully logged into dockerhub as $DOCKERHUB_USERNAME"
return 0
else
echo "Could not log into dockerhub as $DOCKERHUB_USERNAME"
return 1
fi
echo "logging in to docker hub with username $DOCKERHUB_USERNAME"
docker login -u "$DOCKERHUB_USERNAME" -p "$DOCKERHUB_TOKEN"
LOGIN_RETURN=$?

if [[ "$LOGIN_RETURN" -eq 0 ]]; then
echo "Successfully logged into dockerhub as $DOCKERHUB_USERNAME"
return 0
else
echo "Could not log into dockerhub as $DOCKERHUB_USERNAME"
return 1
fi
}

dockerhub_logout() {
echo "logging out from dockerhub"
docker logout
LOGOUT_RETURN=$?
if [[ $LOGOUT_RETURN -eq 0 ]]; then
echo "Successfully logged out of dockerhub"
else
echo "problem occurred logging out of dockerhub!"
fi
echo "logging out from dockerhub"
docker logout
LOGOUT_RETURN=$?
if [[ "$LOGOUT_RETURN" -eq 0 ]]; then
echo "Successfully logged out of dockerhub"
else
echo "problem occurred logging out of dockerhub!"
fi
}

publish() {
echo "Pushing image to DockerHub"
docker push "$IMAGE_NAME"
DOCKER_RETURN=$?

if [[ $DOCKER_RETURN -eq 0 ]]; then
echo "Image successfully published!"
else
echo "Attempt to publish docker image failed!"
fi
echo "Pushing image to DockerHub"
docker push "$IMAGE_NAME"
DOCKER_RETURN=$?

if [[ "$DOCKER_RETURN" -eq 0 ]]; then
echo "Image successfully published!"
else
echo "Attempt to publish docker image failed!"
fi
}

echo "Changing directory to sandbox directory ($WORKING_DIR)"
mkdir -p "$WORKING_DIR"
cd "$WORKING_DIR"
cd "$WORKING_DIR" || exit

prepare;
cleanup;
build;

if [[ "$PUBLISH" == "true" ]]; then
dockerhub_login;
PUBLISH_LOGIN_SUCCESS=$?
if [[ $PUBLISH_LOGIN_SUCCESS -eq 0 ]]; then
publish;
else
echo "Not publishing due to login failure"
dockerhub_logout;
exit 1
fi
dockerhub_login;
PUBLISH_LOGIN_SUCCESS=$?
if [[ "$PUBLISH_LOGIN_SUCCESS" -eq 0 ]]; then
publish;
else
echo "Not publishing due to login failure"
dockerhub_logout;
exit 1
fi

dockerhub_logout;
exit 0
fi
dockerhub_logout;
exit 0
fi
2 changes: 1 addition & 1 deletion bin/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export ROOT_DIR="$HOME_DIR"
export PROJECT="osc-dm-monitor-srv"
export PROJECT_DIR="$ROOT_DIR/$PROJECT"

$PROJECT_DIR/bin/show.sh
"$PROJECT_DIR"/bin/show.sh
7 changes: 4 additions & 3 deletions bin/vactivate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ fi
VENV_NAME="venv"
if [[ "$OSTYPE" == "msys" ]]; then
# For Ms/Windows
source $PROJECT_DIR/$VENV_NAME/Scripts/activate
# shellcheck disable=SC1090
source "$PROJECT_DIR/$VENV_NAME/Scripts/activate"
else
# For Linux/MacOS
source $PROJECT_DIR/$VENV_NAME/bin/activate
# shellcheck disable=SC1090
source "$PROJECT_DIR/$VENV_NAME/bin/activate"
fi
echo "Activated virtual environment: $VENV_NAME"

2 changes: 1 addition & 1 deletion bin/venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ function showHelp {
}

# Create the virtual environment in a directory called
cd $PROJECT_DIR
cd "$PROJECT_DIR" || exit
NAME="venv"
python3 -m venv $NAME
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ services:

networks:
localnet:
external: true
external: true
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "osc-dm-monitor-srv"
version = "v0.1.1"
dynamic = ["version"]
description = "Ecosystem Platform Monitor."
authors = [
{ name = "Eric Broda", email = "[email protected]" },
Expand Down Expand Up @@ -71,7 +71,7 @@ dependencies = [

[tool.setuptools.package-data]
itr = [
"py.typed"
"py.typed", "types-PyYAML"
]

[project.urls]
Expand All @@ -83,7 +83,7 @@ Documentation = "https://github.com/brodagroupsoftware/osc-dm-mesh-doc"
"Source Code" = "https://github.com/os-climate/osc-dm-monitor-srv"

[build-system]
requires = ["pdm-backend"]
requires = ["pdm-backend", "types-PyYAML"]
build-backend = "pdm.backend"

[tool.pdm.scripts]
Expand Down
2 changes: 1 addition & 1 deletion src/bgsexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class BgsNotFoundException(BgsException):
"""
def __init__(self, message, original_exception=None):
super().__init__(message)
self.original_exception = original_exception
self.original_exception = original_exception
10 changes: 6 additions & 4 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
#
# Created: 2024-04-15 by [email protected]

import asyncio

# Library imports
import logging

import uvicorn as uvicorn
from fastapi import FastAPI, HTTPException
import yaml
import asyncio
from fastapi import FastAPI, HTTPException

import state

# Project imports
import utilities
import state

# Set up logging
LOGGING_FORMAT = \
Expand Down Expand Up @@ -173,4 +175,4 @@ async def _repeat_every(interval_sec, func, *args):
except Exception as e:
logger.info(f"STOPPING service, exception:{e}")
finally:
logger.info(f"TERMINATING service on host:{args.host} port:{args.port}")
logger.info(f"TERMINATING service on host:{args.host} port:{args.port}")
Loading

0 comments on commit 6a20076

Please sign in to comment.