Skip to content

Commit

Permalink
add state for petname
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Reynolds committed Aug 20, 2024
1 parent 1eea915 commit e16765c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
25 changes: 22 additions & 3 deletions base/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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:
Expand Down Expand Up @@ -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__)

Expand Down
5 changes: 3 additions & 2 deletions orijen-udf-base-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down

0 comments on commit e16765c

Please sign in to comment.