Skip to content

Commit

Permalink
modified environment variables to use the "HERMES_" prefix (#170)
Browse files Browse the repository at this point in the history
* modified existing environment variables to use the "HERMES_" prefix

* modified HARDWARE_LOCKFILE_PATH env-var to use "HERMES_" prefix

* updated env-vars in server/scripts/test to use new naming scheme

* fixed docker deploy env-vars in server/scripts/develop script

* implemented .env file updater method
  • Loading branch information
larsfroelich authored Mar 20, 2024
1 parent 3714789 commit 6a0b65f
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 70 deletions.
2 changes: 1 addition & 1 deletion sensor/config/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# and are not configurable via the server but only via
# direct (terminal) access

HARDWARE_LOCKFILE_PATH=""
HERMES_HARDWARE_LOCKFILE_PATH=""

HERMES_MQTT_IDENTIFIER="..."
HERMES_MQTT_URL="..."
Expand Down
38 changes: 38 additions & 0 deletions sensor/run_automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,42 @@
ROOT_DIR = dirname(dirname(os.path.abspath(__file__)))
PROJECT_DIR = dirname(os.path.abspath(__file__))


# Modifies the dotenv file to add the prefix "HERMES_" to all keys that don't have it,
# while also preserving the original keys
def update_dotenv(dotenv_path: str) -> None:
# skip if dotenv doesn't exist
if not os.path.exists(dotenv_path):
return

with open(dotenv_path, "r") as file:
# read all lines
data = file.readlines()

hermes_vars = [line for line in data if line.strip().startswith("HERMES_")]

new_file = []
# add the prefix "HERMES_" to all keys that don't have it
for i, line in enumerate(data):
if line.strip() and not line.endswith("\n"):
line += "\n"

new_file.append(line)
if line.startswith("HERMES_"):
continue
# skip lines with whitespace or comments
if not line.strip() or line.strip().startswith("#"):
continue

if not ("HERMES_" + line) in hermes_vars:
new_file.append("HERMES_" + line)

with open(dotenv_path, "w") as file:
file.writelines(new_file)


if __name__ == "__main__":
update_dotenv(os.path.join(PROJECT_DIR, "config", ".env"))
dotenv.load_dotenv(os.path.join(PROJECT_DIR, "config", ".env"))
lock = filelock.FileLock(os.path.join(ROOT_DIR, "run_automation.lock"), timeout=2)

Expand All @@ -16,3 +51,6 @@
main.run()
except filelock.Timeout:
raise TimeoutError("automation is already running")



2 changes: 1 addition & 1 deletion sensor/src/hardware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
testing: bool = False,
) -> None:
global_hw_lock["lock"] = filelock.FileLock(
os.environ.get("HARDWARE_LOCKFILE_PATH") or "/home/pi/Documents/hermes/hermes-hardware.lock",
os.environ.get("HERMES_HARDWARE_LOCKFILE_PATH") or "/home/pi/Documents/hermes/hermes-hardware.lock",
timeout=5
)
self.config = config
Expand Down
20 changes: 10 additions & 10 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
ENVIRONMENT=production
HERMES_ENVIRONMENT=production

# PostgreSQL credentials
POSTGRESQL_URL=www.example.com
POSTGRESQL_PORT=5432
POSTGRESQL_IDENTIFIER=username
POSTGRESQL_PASSWORD=12345678
POSTGRESQL_DATABASE=database
HERMES_POSTGRESQL_URL=www.example.com
HERMES_POSTGRESQL_PORT=5432
HERMES_POSTGRESQL_IDENTIFIER=username
HERMES_POSTGRESQL_PASSWORD=12345678
HERMES_POSTGRESQL_DATABASE=database

# MQTT credentials
MQTT_URL=www.example.com
MQTT_PORT=8883
MQTT_IDENTIFIER=username
MQTT_PASSWORD=12345678
HERMES_MQTT_URL=www.example.com
HERMES_MQTT_PORT=8883
HERMES_MQTT_IDENTIFIER=username
HERMES_MQTT_PASSWORD=12345678
2 changes: 1 addition & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EXPOSE 8000
# Read commit hash and branch name as build arguments
ARG commit_sha branch_name
LABEL commit_sha=${commit_sha} branch_name=${branch_name}
ENV COMMIT_SHA=${commit_sha} BRANCH_NAME=${branch_name}
ENV HERMES_COMMIT_SHA=${commit_sha} HERMES_BRANCH_NAME=${branch_name}

COPY /app /app

Expand Down
24 changes: 12 additions & 12 deletions server/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@


# Environment: test, development, production
ENVIRONMENT = os.environ["ENVIRONMENT"]
ENVIRONMENT = os.environ["HERMES_ENVIRONMENT"]
# Git commit hash
COMMIT_SHA = os.environ["COMMIT_SHA"]
COMMIT_SHA = os.environ["HERMES_COMMIT_SHA"]
# Git branch name
BRANCH_NAME = os.environ["BRANCH_NAME"]
BRANCH_NAME = os.environ["HERMES_BRANCH_NAME"]
# Timestamp of server startup
START_TIMESTAMP = utils.timestamp()

# PostgreSQL connection details
POSTGRESQL_URL = os.environ["POSTGRESQL_URL"]
POSTGRESQL_PORT = int(os.environ["POSTGRESQL_PORT"])
POSTGRESQL_IDENTIFIER = os.environ["POSTGRESQL_IDENTIFIER"]
POSTGRESQL_PASSWORD = os.environ["POSTGRESQL_PASSWORD"]
POSTGRESQL_DATABASE = os.environ["POSTGRESQL_DATABASE"]
POSTGRESQL_URL = os.environ["HERMES_POSTGRESQL_URL"]
POSTGRESQL_PORT = int(os.environ["HERMES_POSTGRESQL_PORT"])
POSTGRESQL_IDENTIFIER = os.environ["HERMES_POSTGRESQL_IDENTIFIER"]
POSTGRESQL_PASSWORD = os.environ["HERMES_POSTGRESQL_PASSWORD"]
POSTGRESQL_DATABASE = os.environ["HERMES_POSTGRESQL_DATABASE"]

# MQTT connection details
MQTT_URL = os.environ["MQTT_URL"]
MQTT_PORT = int(os.environ["MQTT_PORT"])
MQTT_IDENTIFIER = os.environ["MQTT_IDENTIFIER"]
MQTT_PASSWORD = os.environ["MQTT_PASSWORD"]
MQTT_URL = os.environ["HERMES_MQTT_URL"]
MQTT_PORT = int(os.environ["HERMES_MQTT_PORT"])
MQTT_IDENTIFIER = os.environ["HERMES_MQTT_IDENTIFIER"]
MQTT_PASSWORD = os.environ["HERMES_MQTT_PASSWORD"]
30 changes: 15 additions & 15 deletions server/scripts/develop
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ set -o errexit -o pipefail -o nounset
cd "$(dirname "$0")/.."

# Set our environment variables
export ENVIRONMENT="development"
export COMMIT_SHA=$(git rev-parse --verify HEAD)
export BRANCH_NAME=$(git branch --show-current)
export POSTGRESQL_URL="localhost"
export POSTGRESQL_PORT="5432"
export POSTGRESQL_IDENTIFIER="postgres"
export POSTGRESQL_PASSWORD="12345678"
export POSTGRESQL_DATABASE="database"
export MQTT_URL="localhost"
export MQTT_PORT="1883"
export MQTT_IDENTIFIER="server"
export MQTT_PASSWORD="password"
export HERMES_ENVIRONMENT="development"
export HERMES_COMMIT_SHA=$(git rev-parse --verify HEAD)
export HERMES_BRANCH_NAME=$(git branch --show-current)
export HERMES_POSTGRESQL_URL="localhost"
export HERMES_POSTGRESQL_PORT="5432"
export HERMES_POSTGRESQL_IDENTIFIER="postgres"
export HERMES_POSTGRESQL_PASSWORD="12345678"
export HERMES_POSTGRESQL_DATABASE="database"
export HERMES_MQTT_URL="localhost"
export HERMES_MQTT_PORT="1883"
export HERMES_MQTT_IDENTIFIER="server"
export HERMES_MQTT_PASSWORD="password"

# Path to our Mosquitto configuation
MOSQUITTO_CONFIGURATION="$(pwd)/tests/mosquitto.conf"
HERMES_MOSQUITTO_CONFIGURATION="$(pwd)/tests/mosquitto.conf"

# Start PostgreSQL via docker in the background
docker run -td --rm --name postgres -p 127.0.0.1:5432:5432 -e POSTGRES_USER="${POSTGRESQL_IDENTIFIER}" -e POSTGRES_PASSWORD="${POSTGRESQL_PASSWORD}" -e POSTGRES_DB="${POSTGRESQL_DATABASE}" timescale/timescaledb:latest-pg15 >/dev/null
docker run -td --rm --name postgres -p 127.0.0.1:5432:5432 -e POSTGRES_USER="${HERMES_POSTGRESQL_IDENTIFIER}" -e POSTGRES_PASSWORD="${HERMES_POSTGRESQL_PASSWORD}" -e POSTGRES_DB="${HERMES_POSTGRESQL_DATABASE}" timescale/timescaledb:latest-pg15 >/dev/null
# Start the Mosquitto MQTT broker via docker in the background
docker run -td --rm --name mosquitto -p 127.0.0.1:1883:1883 -v "${MOSQUITTO_CONFIGURATION}:/mosquitto/config/mosquitto.conf" eclipse-mosquitto:latest >/dev/null
docker run -td --rm --name mosquitto -p 127.0.0.1:1883:1883 -v "${HERMES_MOSQUITTO_CONFIGURATION}:/mosquitto/config/mosquitto.conf" eclipse-mosquitto:latest >/dev/null
# Wait for services to be ready
sleep 4
# Run the database initialization script
Expand Down
24 changes: 12 additions & 12 deletions server/scripts/jupyter
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ set -o errexit -o pipefail -o nounset
cd "$(dirname "$0")/.."

# Set our environment variables
export ENVIRONMENT="development"
export COMMIT_SHA=$(git rev-parse --verify HEAD)
export BRANCH_NAME=$(git branch --show-current)
export POSTGRESQL_URL="localhost"
export POSTGRESQL_PORT="5432"
export POSTGRESQL_IDENTIFIER="postgres"
export POSTGRESQL_PASSWORD="12345678"
export POSTGRESQL_DATABASE="database"
export MQTT_URL="localhost"
export MQTT_PORT="1883"
export MQTT_IDENTIFIER="server"
export MQTT_PASSWORD="password"
export HERMES_ENVIRONMENT="development"
export HERMES_COMMIT_SHA=$(git rev-parse --verify HEAD)
export HERMES_BRANCH_NAME=$(git branch --show-current)
export HERMES_POSTGRESQL_URL="localhost"
export HERMES_POSTGRESQL_PORT="5432"
export HERMES_POSTGRESQL_IDENTIFIER="postgres"
export HERMES_POSTGRESQL_PASSWORD="12345678"
export HERMES_POSTGRESQL_DATABASE="database"
export HERMES_MQTT_URL="localhost"
export HERMES_MQTT_PORT="1883"
export HERMES_MQTT_IDENTIFIER="server"
export HERMES_MQTT_PASSWORD="password"

# Enable importing local modules
export PYTHONPATH=$(pwd)
Expand Down
26 changes: 13 additions & 13 deletions server/scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ set -o errexit -o pipefail -o nounset
cd "$(dirname "$0")/.."

# Set our environment variables
export ENVIRONMENT="test"
export COMMIT_SHA=$(git rev-parse --verify HEAD)
export BRANCH_NAME=$(git branch --show-current)
export POSTGRESQL_URL="localhost"
export POSTGRESQL_PORT="5432"
export POSTGRESQL_IDENTIFIER="postgres"
export POSTGRESQL_PASSWORD="12345678"
export POSTGRESQL_DATABASE="database"
export MQTT_URL="localhost"
export MQTT_PORT="1883"
export MQTT_IDENTIFIER="server"
export MQTT_PASSWORD="password"
export HERMES_ENVIRONMENT="test"
export HERMES_COMMIT_SHA=$(git rev-parse --verify HEAD)
export HERMES_BRANCH_NAME=$(git branch --show-current)
export HERMES_POSTGRESQL_URL="localhost"
export HERMES_POSTGRESQL_PORT="5432"
export HERMES_POSTGRESQL_IDENTIFIER="postgres"
export HERMES_POSTGRESQL_PASSWORD="12345678"
export HERMES_POSTGRESQL_DATABASE="database"
export HERMES_MQTT_URL="localhost"
export HERMES_MQTT_PORT="1883"
export HERMES_MQTT_IDENTIFIER="server"
export HERMES_MQTT_PASSWORD="password"

# Path to our Mosquitto configuation
MOSQUITTO_CONFIGURATION="$(pwd)/tests/mosquitto.conf"

# Start PostgreSQL via docker in the background
docker run -td --rm --name postgres -p 127.0.0.1:5432:5432 -e POSTGRES_USER="${POSTGRESQL_IDENTIFIER}" -e POSTGRES_PASSWORD="${POSTGRESQL_PASSWORD}" -e POSTGRES_DB="${POSTGRESQL_DATABASE}" timescale/timescaledb:latest-pg15 >/dev/null
docker run -td --rm --name postgres -p 127.0.0.1:5432:5432 -e POSTGRES_USER="${HERMES_POSTGRESQL_IDENTIFIER}" -e POSTGRES_PASSWORD="${HERMES_POSTGRESQL_PASSWORD}" -e POSTGRES_DB="${HERMES_POSTGRESQL_DATABASE}" timescale/timescaledb:latest-pg15 >/dev/null
# Start the Mosquitto MQTT broker via docker in the background
docker run -td --rm --name mosquitto -p 127.0.0.1:1883:1883 -v "${MOSQUITTO_CONFIGURATION}:/mosquitto/config/mosquitto.conf" eclipse-mosquitto:latest >/dev/null
# Wait for services to be ready
Expand Down
10 changes: 5 additions & 5 deletions server/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ async def _connection():
"""Provide a connection to the database that's properly closed afterwards."""
try:
connection = await asyncpg.connect(
host=os.environ["POSTGRESQL_URL"],
port=os.environ["POSTGRESQL_PORT"],
user=os.environ["POSTGRESQL_IDENTIFIER"],
password=os.environ["POSTGRESQL_PASSWORD"],
database=os.environ["POSTGRESQL_DATABASE"],
host=os.environ["HERMES_POSTGRESQL_URL"],
port=os.environ["HERMES_POSTGRESQL_PORT"],
user=os.environ["HERMES_POSTGRESQL_IDENTIFIER"],
password=os.environ["HERMES_POSTGRESQL_PASSWORD"],
database=os.environ["HERMES_POSTGRESQL_DATABASE"],
)
await database.initialize(connection)
yield connection
Expand Down

0 comments on commit 6a0b65f

Please sign in to comment.