File size: 2,277 Bytes
9cc1b28
 
191f3b0
9cc1b28
 
8591509
9cc1b28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191f3b0
 
 
 
 
 
9cc1b28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8591509
9cc1b28
8591509
9cc1b28
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import subprocess
import os
import socket
import time

PORT=22013
HOST='paffenroth-23.dyn.wpi.edu'

def deploy():
    print(f'Deploying the app...')
    try:
        # Run the setup.sh script
        result = subprocess.run(
            ['./setup.sh'],  # Command to execute
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True  # Raise an exception for non-zero exit codes
        )
        print(result.stdout.decode())  # Print standard output from the script
    except subprocess.CalledProcessError as e:
        print(f"Error occurred while deploying: {e.stderr.decode()}")

def is_port_busy(host, port):
    """Check if the specified port on the host is busy."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.settimeout(1)  # 1 second timeout
        return sock.connect_ex((host, port)) == 0  # Returns True if port is busy

def checkStatus(HOST, PORT):
    print(f'Checking the status of the app...')
    try:
        # Run the netcat (nc) command to check if the port is open
        result = subprocess.run(
            ['nc', '-vz', HOST, str(PORT)],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        
        # If the return code is 0, the connection was successful
        if result.returncode == 0:
            print(f"Connection to {HOST}:{PORT} successful.")
            return True
        else:
            print(f"Connection to {HOST}:{PORT} refused.")
            return False
    except Exception as e:
        print(f"Error occurred: {e}")
        return False

def monitorStatus(HOST, PORT, checkInterval=5): # for 5 seconds will wait if the port is not available
    print(f'Monitoring the status of the app...')
    status=False
    while True:
        if checkStatus(HOST, PORT):
            # If the connection is successful, run deploy and exit the loop
            if not status:
                print('Deploying the app...')
                deploy()
            status=True
        else:
            # If connection is refused, wait and retry
            print(f"Waiting for the server {HOST}:{PORT} to become available...")
            time.sleep(checkInterval)
            status = False

# checkStatus(HOST, PORT)
monitorStatus(HOST, PORT)