Skip to content

Commit

Permalink
implement launch_gateway.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Sep 6, 2024
1 parent eb9c9be commit b086079
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ RUN apt update && apt install -y net-tools tcpdump iproute2 iputils-ping vim

# install python libraries for set_addr_list.py
RUN pip3 install setuptools scapy kubernetes ipython
COPY launch_gateway.py /launch_gateway.py

ENTRYPOINT ["bash"]
CMD ["-c", "/epics/ca-gateway/bin/linux_x86-64/gateway"]
CMD ["-c", "/epics/ca-gateway/bin/linux-x86_64/gateway"]

## ======================================
# 3rd stage: "dockerize" the application - copy executable, lib dependencies
Expand Down
45 changes: 40 additions & 5 deletions set_addr_list.py → launch_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import queue
import threading
import time
import os
import subprocess


from collections import namedtuple
from typing import Generator, List, Set, Tuple
Expand Down Expand Up @@ -106,6 +109,22 @@ def handle_events(eventq: 'queue.Queue[ServiceEvent]',
else:
log.error("Incorrect service event type: %s", event.type)

def get_ioc_ips(v1: client.CoreV1Api, namespace: str):
"""Get the list cluster IPs of IOCs running in a namespace
Args:
v1: kubernetes client
namespace: namespace to get the IOCs from
"""
ips = set()
ret = v1.list_namespaced_pod(namespace)
for pod in ret.items:
if "is_ioc" in pod.metadata.labels:
ips.add(pod.status.pod_ip)

return ips



def parse_args():
parser = argparse.ArgumentParser()
Expand All @@ -119,11 +138,27 @@ def main():
args = parse_args()
search_endpoints = set()
logging.basicConfig(level=level2num.get(args.loglevel.lower(), "info"))
eventq = queue.Queue()
threading.Thread(None, services_events_task, "services_events",
args=(args.namespace, args.port, eventq)).start()
while True:
handle_events(eventq, search_endpoints)

# configure K8S and make a Core API client
config.load_incluster_config()
v1 = client.CoreV1Api()

namespace = os.getenv("MY_POD_NAMESPACE", "default")
port = os.getenv("GATEWAY_SERVER_PORT", 5064)

ips = get_ioc_ips(v1, namespace)
ipstr = ",".join(ips)

command = f"/epics/ca-gateway/bin/linux-x86_64/gateway -sport {port} -cip {ipstr} -pvlist /config/pvlist -access /config/access -log /dev/stdout -debug 1"

print(f"Running command: {command}")
subprocess.run(["bash", "-c", command], check=True)
# eventq = queue.Queue()
# threading.Thread(None, services_events_task, "services_events",
# args=(args.namespace, args.port, eventq)).start()

# while True:
# handle_events(eventq, search_endpoints)


if __name__ == "__main__":
Expand Down

0 comments on commit b086079

Please sign in to comment.