From e16765c5f4ae04ce26904b3942cf4ab0f3246405 Mon Sep 17 00:00:00 2001 From: Kevin Reynolds Date: Tue, 20 Aug 2024 14:55:52 -0400 Subject: [PATCH] add state for petname --- base/app/app.py | 25 ++++++++++++++++++++++--- orijen-udf-base-install.sh | 5 +++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/base/app/app.py b/base/app/app.py index 71f47bd..367b9c8 100644 --- a/base/app/app.py +++ b/base/app/app.py @@ -12,13 +12,28 @@ import petname from flask import Flask, jsonify +state_base = "/var/lib/private/orijen-udf-base/" + def run_flask(app): """Function to run the Flask app on a separate thread.""" app.run(host='0.0.0.0', port=5123) +def save_state(file, state): + with open(file, 'w') as f: + json.dump(state, f) + +def load_state(file): + try: + with open(file, 'r') as f: + return json.load(f) + except FileNotFoundError: + return {} + def generate_petname(): """Generates a pet name in the format 'adjective-animal'.""" - return petname.Generate() + name = petname.Generate() + save_state(state_base + "petname.json", name) + return name def b64_lazy_decode(s: str) -> str|None: """ @@ -40,7 +55,7 @@ def fetch_metadata(url: str, max_retries=5) -> dict|None: retry_delay = 1 for attempt in range(max_retries): try: - response = requests.get(url) + response = requests.get(url, timeout=5) response.raise_for_status() return response.json() except requests.RequestException as e: @@ -212,10 +227,14 @@ def main(): """ Main Function """ + metadata = query_metadata() labInfo = get_lab_info(metadata) sqsMeta = build_sqs_meta(metadata, labInfo) - petName = generate_petname() + + petName = load_state(state_base + "petname.json") + if not petName: + petName = generate_petname() app = Flask(__name__) diff --git a/orijen-udf-base-install.sh b/orijen-udf-base-install.sh index 3ac689f..8792ced 100755 --- a/orijen-udf-base-install.sh +++ b/orijen-udf-base-install.sh @@ -18,7 +18,7 @@ systemctl enable docker systemctl start docker # Variable Declarations -IMAGE="ghcr.io/f5devcentral/orijen-udf-service/orijen-udf-base:dev" +IMAGE="ghcr.io/f5devcentral/orijen-udf-service/orijen-udf-base:petname-api" SERVICE="orijen-udf-base.service" CONTAINER="orijen-udf-base" @@ -35,8 +35,9 @@ Restart=always ExecStartPre=-/usr/bin/docker stop $CONTAINER ExecStartPre=-/usr/bin/docker rm $CONTAINER ExecStartPre=/usr/bin/docker pull $IMAGE -ExecStart=/usr/bin/docker run --rm --name $CONTAINER $IMAGE +ExecStart=/usr/bin/docker run -p 5123:5123 --rm --name $CONTAINER $IMAGE ExecStop=/usr/bin/docker stop $CONTAINER +StateDirectory=$CONTAINER [Install] WantedBy=multi-user.target