← azuredeploy

adding primary and seconadry test files

75aa9c8e591799c85fc80b7b0a38fbf05f8542b8 · ovidiucs · Feb 28, 2024 18:10

+154 · 8 files changed

  • codefiles/src/primary/config.ini +3 −0
  • codefiles/src/primary/primary.py +33 −0
  • codefiles/src/secondary/config.ini +7 −0
  • codefiles/src/secondary/secondary.py +61 −0
  • container/Dockerfile +0 −0
  • container/README.md +3 −0
  • container/primary/Dockerfile +23 −0
  • container/secondary/Dockerfile +24 −0
diff --git a/codefiles/src/primary/config.ini b/codefiles/src/primary/config.ini
new file mode 100644
index 0000000..1389063
--- /dev/null
+++ b/codefiles/src/primary/config.ini
@@ -0,0 +1,3 @@
+[PRIMARY]
+HOST = 0.0.0.0
+PORT = 12345
diff --git a/codefiles/src/primary/primary.py b/codefiles/src/primary/primary.py
new file mode 100644
index 0000000..e28ae80
--- /dev/null
+++ b/codefiles/src/primary/primary.py
@@ -0,0 +1,33 @@
+import socket
+import configparser
+from threading import Thread
+
+# Load configuration
+config = configparser.ConfigParser()
+config.read('config.ini')
+HOST = config['PRIMARY']['HOST']
+PORT = int(config['PRIMARY']['PORT'])
+
+def handle_client(connection, address):
+    try:
+        print(f"Connection from {address} has been established.")
+        while True:
+            data = connection.recv(1024)
+            if not data:
+                break  # Connection closed by the client
+            # Discard data explicitly
+    finally:
+        connection.close()
+
+def main():
+    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
+        s.bind((HOST, PORT))
+        s.listen()
+        print(f"Listening on {HOST}:{PORT}...")
+        while True:
+            conn, addr = s.accept()
+            client_thread = Thread(target=handle_client, args=(conn, addr))
+            client_thread.start()
+
+if __name__ == "__main__":
+    main()
diff --git a/codefiles/src/secondary/config.ini b/codefiles/src/secondary/config.ini
new file mode 100644
index 0000000..0493850
--- /dev/null
+++ b/codefiles/src/secondary/config.ini
@@ -0,0 +1,7 @@
+[SECONDARY]
+HOST = 0.0.0.0
+PORT = 23456
+PRIMARY_HOST = primary_container
+PRIMARY_PORT = 12345
+CHECK_INTERVAL = 5
+
diff --git a/codefiles/src/secondary/secondary.py b/codefiles/src/secondary/secondary.py
new file mode 100644
index 0000000..f06ce3e
--- /dev/null
+++ b/codefiles/src/secondary/secondary.py
@@ -0,0 +1,61 @@
+import socket
+import configparser
+import sys
+import time
+from threading import Thread
+
+# Load configuration
+config = configparser.ConfigParser()
+config.read('config.ini')
+PRIMARY_HOST = config['SECONDARY']['PRIMARY_HOST']
+PRIMARY_PORT = int(config['SECONDARY']['PRIMARY_PORT'])
+SECONDARY_PORT = int(config['SECONDARY']['PORT'])
+CHECK_INTERVAL = int(config['SECONDARY']['CHECK_INTERVAL'])
+
+primary_online = True
+
+def check_primary():
+    global primary_online
+    while True:
+        try:
+            with socket.create_connection((PRIMARY_HOST, PRIMARY_PORT), timeout=2):
+                if not primary_online:
+                    print("Primary is back online.")
+                primary_online = True
+        except socket.error:
+            if primary_online:
+                print("Primary is offline.")
+            primary_online = False
+        time.sleep(CHECK_INTERVAL)
+
+def handle_client(connection, address):
+    if primary_online:
+        print(f"Rejecting connection from {address} because primary is online.")
+        connection.close()
+    else:
+        print(f"Handling connection from {address} because primary is offline.")
+        try:
+            while True:
+                data = connection.recv(1024)
+                if not data:
+                    break  # Connection closed by the client
+                # Discard data explicitly
+        finally:
+            connection.close()
+
+def main():
+    check_thread = Thread(target=check_primary)
+    check_thread.start()
+
+    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
+        s.bind(('0.0.0.0', SECONDARY_PORT))
+        s.listen()
+        print(f"Secondary listening on port {SECONDARY_PORT}...")
+        while True:
+            conn, addr = s.accept()
+            client_thread = Thread(target=handle_client, args=(conn, addr))
+            client_thread.start()
+
+if __name__ == "__main__":
+    main()
+
diff --git a/container/Dockerfile b/container/Dockerfile
new file mode 100644
index 0000000..e69de29
diff --git a/container/README.md b/container/README.md
new file mode 100644
index 0000000..fb1ece4
--- /dev/null
+++ b/container/README.md
@@ -0,0 +1,3 @@
+BUILD ARGS
+
+RUNTIME ARGS
diff --git a/container/primary/Dockerfile b/container/primary/Dockerfile
new file mode 100644
index 0000000..377198b
--- /dev/null
+++ b/container/primary/Dockerfile
@@ -0,0 +1,23 @@
+# Use an official Python runtime as a parent image
+FROM python:3.8-slim
+
+# Set the working directory in the container
+WORKDIR /app
+
+# Copy the current directory contents into the container at /app
+COPY ../../codefiles/src/primary /app
+
+# Install any needed packages specified in requirements.txt
+# Assuming you might have a requirements.txt for any Python dependencies
+# For this example, it's likely empty but included for completeness
+COPY requirements.txt /app
+RUN pip install --no-cache-dir -r requirements.txt
+
+# Make port 12345 available to the world outside this container
+EXPOSE 12345
+
+# Define environment variable
+ENV NAME Primary
+
+# Run primary.py when the container launches
+CMD ["python", "primary.py"]
diff --git a/container/secondary/Dockerfile b/container/secondary/Dockerfile
new file mode 100644
index 0000000..4437318
--- /dev/null
+++ b/container/secondary/Dockerfile
@@ -0,0 +1,24 @@
+# Use an official Python runtime as a parent image
+FROM python:3.8-slim
+
+# Set the working directory in the container
+WORKDIR /app
+
+# Copy the current directory contents into the container at /app
+COPY ../../codefiles/src/secondary /app
+
+# Install any needed packages specified in requirements.txt
+# Assuming you might have a requirements.txt for any Python dependencies
+# For this example, it's likely empty but included for completeness
+COPY requirements.txt /app
+RUN pip install --no-cache-dir -r requirements.txt
+
+# Make port 23456 available to the world outside this container
+EXPOSE 23456
+
+# Define environment variable
+ENV NAME Secondary
+
+# Run secondary.py when the container launches
+CMD ["python", "secondary.py"]
+