Skip to content
Snippets Groups Projects
Commit 0e4900db authored by mohamad.moussa's avatar mohamad.moussa
Browse files

Initial Commit

parents
Branches
No related tags found
No related merge requests found
Showing with 456 additions and 0 deletions
FROM python:3.10-slim
WORKDIR /app
#RUN pip install --no-cache-dir -r requirements.txt
COPY sock.py /app/sock.py
EXPOSE 8000
CMD ["python", "sock.py"]
\ No newline at end of file
version: '3.8'
services:
node0:
image: comlocal:1.0
container_name: node0
environment:
- RANK=0
- BASE_PORT=8000
- IP_ADDRESS=node0
volumes:
- ./neighbors/neighbors0.txt:/app/neighbors.txt
networks:
- my_network
node1:
image: comlocal:1.0
container_name: node1
environment:
- RANK=1
- BASE_PORT=8000
- IP_ADDRESS=node1
volumes:
- ./neighbors/neighbors1.txt:/app/neighbors.txt
networks:
- my_network
node2:
image: comlocal:1.0
container_name: node2
environment:
- RANK=2
- BASE_PORT=8000
- IP_ADDRESS=node2
volumes:
- ./neighbors/neighbors2.txt:/app/neighbors.txt
networks:
- my_network
node3:
image: comlocal:1.0
container_name: node3
environment:
- RANK=3
- BASE_PORT=8000
- IP_ADDRESS=node3
volumes:
- ./neighbors/neighbors3.txt:/app/neighbors.txt
networks:
- my_network
networks:
my_network:
driver: bridge
import sys
image = 'mohamadmoussa/'
def generate_docker_compose(num_nodes):
docker_compose_template = """\
version: '3.8'
services:
"""
service_template = """
node{i}:
image: {img}
container_name: node{i}
environment:
- RANK={i}
- BASE_PORT=8000
- IP_ADDRESS=node{i}
volumes:
- ./neighbors/neighbors{file_suffix}.txt:/app/neighbors.txt
networks:
- my_network
"""
network_section = """
networks:
my_network:
driver: bridge
"""
docker_compose_content = docker_compose_template
for i in range(0, num_nodes):
file_suffix = i
docker_compose_content += service_template.format(i=i, file_suffix=file_suffix,img=image)
docker_compose_content += network_section
with open('docker-compose.yml', 'w') as file:
file.write(docker_compose_content)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python generate_docker_compose.py <num_nodes>")
sys.exit(1)
num_nodes = int(sys.argv[1])
generate_docker_compose(num_nodes)
1
3
\ No newline at end of file
0
2
\ No newline at end of file
1
3
\ No newline at end of file
0
2
\ No newline at end of file
import socket
import time
import sys
import json
import select
import os
import logging
NB_ITERATIONS = 20
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', handlers=[logging.StreamHandler(sys.stdout)])
logger = logging.getLogger(__name__)
def mainProg(neighbors, rank, base_port, ip_address):
c = 0
iteration = 0
received_dict = {neighbor: False for neighbor in neighbors}
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((ip_address, base_port))
server_socket.listen(1000)
time.sleep(10)
logger.info(f"Server {rank} listening on {ip_address} and port {base_port}")
while iteration < NB_ITERATIONS:
if iteration < 3:
for neighbor in neighbors:
try:
neighbor_ip = f"node{neighbor}" # Use Docker service name
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((neighbor_ip, base_port))
message = "REQUEST_WEIGHT"
data = json.dumps((message, rank))
client_socket.send(data.encode())
logger.info(f"Rank {rank} sent {message} to {neighbor} at iteration {iteration}")
time.sleep(3)
client_socket.close()
except ConnectionRefusedError as e:
logger.error(f"Exception {e}\nConnection refused when {rank} tried to connect to {neighbor}'s service {neighbor_ip}.")
continue
logger.info(f"I am at -- iteration {iteration}")
readable, _, _ = select.select([server_socket], [], [], 1) # x seconds timeout
if readable:
conn, addr = server_socket.accept()
data = conn.recv(1024).decode()
message_rcvd, sender = json.loads(data)
conn.close()
if message_rcvd == "REQUEST_WEIGHT":
try:
sender_ip = f"node{sender}" # Use Docker service name
cl_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cl_socket.connect((sender_ip, base_port))
message = "WEIGHTS"
data = json.dumps((message, rank))
cl_socket.send(data.encode())
time.sleep(3)
cl_socket.close()
except ConnectionRefusedError as e:
logger.error(f"Exception {e}\nConnection refused when {rank} tried to send WEIGHTS to {sender}'s service {sender_ip}.")
continue
if message_rcvd == "WEIGHTS":
received_dict[sender] = True
logger.info(f"I am here -- iteration {iteration} and message received {message_rcvd} from {sender}")
iteration += 1
return
if __name__ == "__main__":
rank = int(os.getenv('RANK'))
base_port = int(os.getenv('BASE_PORT'))
ip_address = os.getenv('IP_ADDRESS')
if rank <0 or not base_port or not ip_address:
logger.error("Environment variables RANK, BASE_PORT, and IP_ADDRESS must be set.")
sys.exit(1)
neighbors_file = '/app/neighbors.txt'
with open(neighbors_file, "r") as file:
neighbors = [int(line.strip()) for line in file]
logger.info(f"I am rank {rank} running on port {base_port} and neighbors are {neighbors}")
mainProg(neighbors, rank, base_port, ip_address)
\ No newline at end of file
FROM python:3.10-slim
WORKDIR /App
COPY comSimple.py /App/
CMD ["python", "comSimple.py"]
\ No newline at end of file
import socket
import time
import sys
import json
import select
import os
import logging
NB_ITERATIONS = 20
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s:%(message)s')
logger = logging.getLogger()
def mainProg(neighbors, NODE_ID):
c = 0
iteration = 0
received_dict = {neighbor: False for neighbor in neighbors}
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', base_port))
server_socket.listen(1000)
time.sleep(60)
logger.info(f"Server {NODE_ID} listening on {NODE_ID} and port {base_port}")
while iteration < NB_ITERATIONS:
if iteration < 3:
for neighbor in neighbors:
try:
neighbor_ip = neighbor
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logger.info(f"The neighbor_ip is {neighbor_ip}")
client_socket.connect((neighbor_ip, base_port))
message = "REQUEST_WEIGHT"
data = json.dumps((message, NODE_ID))
client_socket.send(data.encode())
logger.info(f"Rank {NODE_ID} sent {message} to {neighbor_ip} at iteration {iteration}")
time.sleep(3)
client_socket.close()
except ConnectionRefusedError as e:
logger.error(f"Exception {e} \n Connection refused when {NODE_ID} tried to connect to {neighbor_ip}'s port {base_port}.")
continue
logger.info(f"I am at -- \t iteration {iteration}")
readable, _, _ = select.select([server_socket], [], [], 1) # x seconds timeout
if readable:
conn, addr = server_socket.accept()
data = conn.recv(1024).decode()
message_rcvd, sender = json.loads(data)
conn.close()
if message_rcvd == "REQUEST_WEIGHT":
try:
sender_ip = sender
cl_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cl_socket.connect((sender_ip, base_port))
message = "WEIGHTS"
data = json.dumps((message, NODE_ID))
cl_socket.send(data.encode())
time.sleep(3)
cl_socket.close()
except ConnectionRefusedError as e:
logger.error(f"Exception {e} \n Connection refused when {NODE_ID} tried to send WEIGHTS to {sender_ip}'s port {base_port}.")
continue
if message_rcvd == "WEIGHTS":
logger.info(f"I am here -- iteration {iteration} and message received {message_rcvd} from {sender_ip}")
received_dict[sender] = True
iteration += 1
return
if __name__ == "__main__":
base_port = 12345
NODE_ID = os.getenv('NODE_ID')
NEIGHBORHOOD_CONFIG = "/etc/neighbors.json"
with open(NEIGHBORHOOD_CONFIG) as f:
neighborhood_data = json.load(f)
NEIGHBORHOOD = neighborhood_data.get(NODE_ID, [])
logger.info(f"Node {NODE_ID} has neighbors: {NEIGHBORHOOD}")
mainProg(list(NEIGHBORHOOD), NODE_ID)
version: '3.8'
services:
comm0:
image: mohamadmoussa/comm_image:2.0
configs:
- source: neighborhood_config
target: /etc/neighbors.json
networks:
- commNetwork
deploy:
placement:
constraints:
- node.hostname == ip-10-0-0-42
environment:
- NODE_ID=comm0
comm1:
image: mohamadmoussa/comm_image:2.0
configs:
- source: neighborhood_config
target: /etc/neighbors.json
networks:
- commNetwork
deploy:
placement:
constraints:
- node.hostname == ip-10-0-0-163
environment:
- NODE_ID=comm1
comm2:
image: mohamadmoussa/comm_image:2.0
configs:
- source: neighborhood_config
target: /etc/neighbors.json
networks:
- commNetwork
deploy:
placement:
constraints:
- node.hostname == ip-10-0-0-253
environment:
- NODE_ID=comm2
comm3:
image: mohamadmoussa/comm_image:2.0
configs:
- source: neighborhood_config
target: /etc/neighbors.json
networks:
- commNetwork
deploy:
placement:
constraints:
- node.hostname == ip-10-0-0-74
environment:
- NODE_ID=comm3
networks:
commNetwork:
driver: overlay
configs:
neighborhood_config:
external: true
input_file = 'instance_privateIPs.txt'
output_file = 'docker-compose.yml'
with open(input_file, 'r') as file:
ip_addresses = [line.strip() for line in file if line.strip()]
service_template = """
comm{i}:
image: mohamadmoussa/comm_image:2.0
configs:
- source: neighborhood_config
target: /etc/neighbors.json
networks:
- commNetwork
deploy:
placement:
constraints:
- node.hostname == ip-{ip}
environment:
- NODE_ID=comm{i}
"""
compose_content = """version: '3.8'
services:"""
for i, ip in enumerate(ip_addresses):
formatted_ip = ip.replace('.', '-')
compose_content += service_template.format(i=i, ip=formatted_ip)
compose_content += """
networks:
commNetwork:
driver: overlay
configs:
neighborhood_config:
external: true
"""
# Write the content to the docker-compose.yml file
with open(output_file, 'w') as file:
file.write(compose_content)
print(f"Docker-compose file generated successfully: {output_file}")
10.0.0.42
10.0.0.163
10.0.0.253
10.0.0.74
\ No newline at end of file
{
"comm0": [
"comm1",
"comm3"
],
"comm1": [
"comm0",
"comm2"
],
"comm2": [
"comm1",
"comm3"
],
"comm3": [
"comm0",
"comm2"
]
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment