VenkateshRoshan commited on
Commit
9cc1b28
·
1 Parent(s): 157ba12

Automatic deployer script added

Browse files
Files changed (2) hide show
  1. automatic_deployer.py +58 -0
  2. setup.sh +54 -47
automatic_deployer.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import os
3
+ import time
4
+
5
+ PORT=22060
6
+ HOST='paffenroth-23.dyn.wpi.edu'
7
+
8
+ def deploy():
9
+ print(f'Deploying the app...')
10
+ try:
11
+ # Run the setup.sh script
12
+ result = subprocess.run(
13
+ ['./setup.sh'], # Command to execute
14
+ stdout=subprocess.PIPE,
15
+ stderr=subprocess.PIPE,
16
+ check=True # Raise an exception for non-zero exit codes
17
+ )
18
+ print(result.stdout.decode()) # Print standard output from the script
19
+ except subprocess.CalledProcessError as e:
20
+ print(f"Error occurred while deploying: {e.stderr.decode()}")
21
+
22
+ def checkStatus(HOST, PORT):
23
+ print(f'Checking the status of the app...')
24
+ try:
25
+ # Run the netcat (nc) command to check if the port is open
26
+ result = subprocess.run(
27
+ ['nc', '-vz', HOST, str(PORT)],
28
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE
29
+ )
30
+
31
+ # If the return code is 0, the connection was successful
32
+ if result.returncode == 0:
33
+ print(f"Connection to {HOST}:{PORT} successful.")
34
+ return True
35
+ else:
36
+ print(f"Connection to {HOST}:{PORT} refused.")
37
+ return False
38
+ except Exception as e:
39
+ print(f"Error occurred: {e}")
40
+ return False
41
+
42
+ def monitorStatus(HOST, PORT, checkInterval=5): # for 5 seconds will wait if the port is not available
43
+ print(f'Monitoring the status of the app...')
44
+ status=False
45
+ while True:
46
+ if checkStatus(HOST, PORT):
47
+ # If the connection is successful, run FUN and exit the loop
48
+ if not status:
49
+ deploy()
50
+ status=True
51
+ else:
52
+ # If connection is refused, wait and retry
53
+ print(f"Waiting for the server {HOST}:{PORT} to become available...")
54
+ time.sleep(checkInterval)
55
+ status = False
56
+
57
+ # checkStatus(HOST, PORT)
58
+ monitorStatus(HOST, PORT)
setup.sh CHANGED
@@ -40,38 +40,42 @@ SSH_CONNECTION="ssh -i my_key -p $PORT $USER@$HOST"
40
 
41
  # TODO : Comment the old ssh key in the authorized_keys file
42
  # Use the new SSH key to connect to the server and install Docker
43
- $SSH_CONNECTION << EOF
44
- # Update existing package list
45
- sudo apt-get update
46
-
47
- # Install required packages for Docker
48
- sudo apt-get install -y ca-certificates curl gnupg lsb-release
49
-
50
- # Add Docker's official GPG key
51
- sudo mkdir -p /etc/apt/keyrings
52
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
53
-
54
- # Set up the Docker repository
55
- echo \
56
- "deb [arch=\$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
57
- \$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
58
-
59
- # Update the package list again
60
- sudo apt-get update
61
-
62
- # Install Docker Engine, Docker CLI, and containerd
63
- sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
64
-
65
- # Start and enable Docker service
66
- sudo systemctl start docker
67
- sudo systemctl enable docker
68
-
69
- # Add current user to docker group (to run Docker commands without sudo)
70
- sudo usermod -aG docker $USER
71
-
72
- # Verify Docker installation
73
- docker --version
74
- EOF
 
 
 
 
75
 
76
  echo "Docker has been installed on the server."
77
 
@@ -79,25 +83,28 @@ echo "Docker has been installed on the server."
79
 
80
  DOCKER_NAME=mlopscs2
81
 
82
- # Build Docker image locally
83
- echo "Building Docker image..."
84
- # docker build -t $DOCKER_NAME .
85
- docker build --build-arg HF_TOKEN=$HF_TOKEN -t $DOCKER_NAME .
86
 
87
- # Save the Docker image to a tar file
88
- docker save -o $DOCKER_NAME.tar $DOCKER_NAME
 
 
89
 
90
- # Copy the Docker image tar to the server
91
- echo "Copying Docker image to the server..."
92
- scp -i "$old_ssh_key_name" -P "$PORT" $DOCKER_NAME.tar "$USER@$HOST:/home/$USER/"
93
 
94
- # Load the Docker image on the server
95
- $SSH_CONNECTION "docker load -i /home/$USER/$DOCKER_NAME.tar"
 
96
 
97
- # Run the Docker container on the server with restart policy
98
- $SSH_CONNECTION "docker run -d --restart unless-stopped -p 5000:5000 $DOCKER_NAME"
99
 
100
- # Clean up the Docker image tar file
101
- $SSH_CONNECTION "rm /home/$USER/$DOCKER_NAME.tar"
 
 
 
 
 
102
 
103
  echo "Docker container is running on the server."
 
40
 
41
  # TODO : Comment the old ssh key in the authorized_keys file
42
  # Use the new SSH key to connect to the server and install Docker
43
+
44
+ echo "Installing Docker on the server..."
45
+
46
+ $SSH_CONNECTION "sudo apt-get update"
47
+
48
+ # Install required packages for Docker
49
+ echo "Installing required packages for Docker..."
50
+ $SSH_CONNECTION "sudo apt-get install -y ca-certificates curl gnupg lsb-release"
51
+
52
+ # Add Docker's official GPG key
53
+ echo "Adding Docker's official GPG key..."
54
+ $SSH_CONNECTION "sudo mkdir -p /etc/apt/keyrings"
55
+ $SSH_CONNECTION "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg"
56
+
57
+ # Set up the Docker repository
58
+ echo "Setting up the Docker repository..."
59
+ $SSH_CONNECTION "echo \"deb [arch=\$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null"
60
+
61
+ # Update the package list again
62
+ $SSH_CONNECTION "sudo apt-get update"
63
+
64
+ # Install Docker Engine, Docker CLI, and containerd
65
+ echo "Installing Docker Engine, Docker CLI, and containerd..."
66
+ $SSH_CONNECTION "sudo apt-get install -y docker-ce docker-ce-cli containerd.io"
67
+
68
+ # Start and enable Docker service
69
+ echo "Starting and enabling Docker service..."
70
+ $SSH_CONNECTION "sudo systemctl start docker"
71
+ $SSH_CONNECTION "sudo systemctl enable docker"
72
+
73
+ # Add current user to docker group (to run Docker commands without sudo)
74
+ echo "Adding current user to docker group..."
75
+ $SSH_CONNECTION "sudo usermod -aG docker $USER"
76
+
77
+ # Verify Docker installation
78
+ $SSH_CONNECTION "docker --version"
79
 
80
  echo "Docker has been installed on the server."
81
 
 
83
 
84
  DOCKER_NAME=mlopscs2
85
 
 
 
 
 
86
 
87
+ # # Build Docker image locally
88
+ # echo "Building Docker image..."
89
+ # # docker build -t $DOCKER_NAME .
90
+ # docker build --build-arg HF_TOKEN=$HF_TOKEN -t $DOCKER_NAME .
91
 
92
+ # # Save the Docker image to a tar file
93
+ # docker save -o $DOCKER_NAME.tar $DOCKER_NAME
 
94
 
95
+ # # Copy the Docker image tar to the server
96
+ # echo "Copying Docker image to the server..."
97
+ # scp -i "$old_ssh_key_name" -P "$PORT" $DOCKER_NAME.tar "$USER@$HOST:/home/$USER/"
98
 
99
+ # # Load the Docker image on the server
100
+ # $SSH_CONNECTION "docker load -i /home/$USER/$DOCKER_NAME.tar"
101
 
102
+ # pull the docker image from venkateshroshan/mlops-case-study2:latest
103
+ $SSH_CONNECTION "docker pull venkateshroshan/mlops-case-study2:latest"
104
+
105
+ echo "Docker image has been copied to the server."
106
+
107
+ # Run the Docker container on the server with restart policy
108
+ $SSH_CONNECTION "docker run -d --restart unless-stopped -p 8013:5000 $DOCKER_NAME"
109
 
110
  echo "Docker container is running on the server."