Skip to content

Commit

Permalink
Working on v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdalertdk committed Jun 19, 2024
1 parent 9f00f26 commit a122003
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore everything in this directory
*

# Whitelist these files
!healthcheck.py
!__init__.py
15 changes: 11 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ ENV PATH="/app/.opentakserver_venv/bin:$PATH"
# Install Opentakserver
RUN pip3 install --no-cache-dir opentakserver==${BUILD_VERSION}

# Fix for https://github.com/brian7704/OpenTAKServer/issues/15
RUN pip3 uninstall -y bcrypt && pip3 install --no-cache-dir bcrypt==4.0.1

# ************************************************************
# Second stage: runtime
# ************************************************************
Expand Down Expand Up @@ -63,6 +60,15 @@ WORKDIR /app/ots
# Copy opentakserver from build image
COPY --from=builder /app/.opentakserver_venv /app/.opentakserver_venv

RUN pip3 uninstall -y bcrypt && pip3 install bcrypt==4.0.1

# Add Healthcheck for OTS
COPY __init__.py healthcheck.py /app/scripts/
RUN chmod +x /app/scripts/*

#HEALTHCHECK --interval=1m --start-period=1m CMD python3 /app/scripts/healthcheck.py

# Run as OTS user
USER ots

# Flask will stop gracefully on SIGINT (Ctrl-C).
Expand All @@ -78,4 +84,5 @@ EXPOSE 8088/tcp
# 8089 SSL CoT streaming port
EXPOSE 8089/tcp

CMD [ "python3", "-m" , "opentakserver.app"]
#ENTRYPOINT [ "python3" ]
CMD [ "python3", "/app/scripts/__init__.py" ]
65 changes: 65 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
import os, yaml, subprocess;
from opentakserver.defaultconfig import DefaultConfig;
from flask.config import Config as FlaskConfig;

config_file = os.path.join( os.environ.get("DOCKER_OTS_DATA_FOLDER", "/app/ots/"), "config.yml" );
config = FlaskConfig(config_file);

def save_config(config):
global config_file
try:
with open(config_file, "w") as config_file:
yaml.safe_dump(dict(config), config_file)
print("Container init | Saving config file...")
except BaseException as e:
print("Container init | Failed to save config.yml: {}".format(e))

# Get config file,
# Load config.yml if it exists
if not os.path.exists(config_file) or os.environ.get("DOCKER_CONFIG_OVERWRITE", False):
print("Container init | Creating config.yml")

# Get default config from opentakserver
config.from_object(DefaultConfig);

# Override settings to make OTS work in a container
config.update(
OTS_LISTENER_ADDRESS = os.environ.get("DOCKER_OTS_LISTENER_ADDRESS", "0.0.0.0"),
OTS_RABBITMQ_SERVER_ADDRESS = os.environ.get("DOCKER_OTS_RABBITMQ_SERVER_ADDRESS", "rabbitmq")
)

# Get env variables with the prefix 'DOCKER_'
# Used so we can override variables from the docker-compose file
config.from_prefixed_env('DOCKER');

save_config(config)
else:
print('Container init | Found existing config.yml')

print('Container init | Checking environment variables...')
init_config_file = FlaskConfig(config_file)
init_config_file.from_file(config_file, load=yaml.safe_load);

init_config_env = FlaskConfig(config_file)
init_config_env.from_prefixed_env('DOCKER');

init_config_diff = set(init_config_file).intersection(set(init_config_env))

if bool(init_config_diff):
init_config_updated = dict()
for value in init_config_diff:
if init_config_file[value] != init_config_env[value]:
print("Container init | Found changed environment variable ['{}'] old value: '{}' new value: '{}'".format(value, init_config_file[value], init_config_env[value]))
init_config_updated[value] = init_config_env[value]

if bool(init_config_updated):
init_config_file.update(init_config_updated)
save_config(init_config_file)
else:
print('Container init | No changed environment variables found')

# Start the OpenTAKServer app
print('Container init | Starting OpenTAKServer...')
ots = subprocess.Popen( ['python3', '-m', 'opentakserver.app'], start_new_session=True)
ots.wait()
13 changes: 13 additions & 0 deletions healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import requests, sys;

URL = "http://localhost:8081/api/health"

try:
response = requests.head(URL)
except Exception as e:mak
sys.exit(1)
else:
if response.status_code == 200:
sys.exit(0)
else:
sys.exit(1)

0 comments on commit a122003

Please sign in to comment.