-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_dev.py
113 lines (91 loc) · 3.3 KB
/
deploy_dev.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python3
"""
Fabric script that creates and distributes an archive to the web servers
"""
from fabric import task
from fabric.api import env
from datetime import datetime
from os.path import exists, isdir
# Replace these with your actual server IP addresses and SSH credentials
env.hosts = ["54.144.46.194"]
user = "ubuntu"
key_path = "/path/to/your/private/key.pem"
remote_app_path = "/home/ubuntu/"
gunicorn_service_name = "cariba_app"
@task
def deploy(c):
# Step 1: Archive the app folder
archive_path = archive_app(c)
# Step 2: Check that archive exists
if exists(archive_path) is False:
return False
# Loop through the servers and deploy to each one
for host in env.hosts:
c.host = "{}@{}".format(user, host)
c.connect_kwargs.key_filename = [key_path]
try:
file_n = archive_path.split("/")[-1]
no_ext = file_n.split(".")[0]
# Step 3: Copy the app archive to the remote host
copy_archive_app_to_remote(c, archive_path)
# Step 4: Stop gunicorn app
stop_gunicorn(c)
# Step 5: Delete previous gunicorn app path if and recreate it
create_remote_directory(c, no_ext)
# Step 6: Extract the app archive on the remote host
extract_app(c, file_n, remote_app_path, no_ext)
# Step 7: Remove the app archive on the remote host
remove_app_archive(c, file_n)
# Step 8: Restart the Gunicorn service
start_gunicorn(c)
# Step 9: Reload Nginx configuration
reload_nginx(c)
return True
except:
return False
@task
def archive_app(c):
# Archive the app folder
"""generates a tgz archive"""
try:
date = datetime.now().strftime("%Y%m%d%H%M%S")
if isdir("versions") is False:
c.local("mkdir versions")
file_name = "versions/cariba_{}.tgz".format(date)
c.local("tar -cvzf {} app".format(file_name))
return file_name
except:
return None
@task
def remove_app_archive(c, file_n):
# Copy the app archive to the remote host
c.run('rm /tmp/{}'.format(file_n))
@task
def create_remote_directory(c, no_ext):
# Copy the app archive to the remote host
c.run('rm -rf {}{}/'.format(remote_app_path, no_ext))
c.run('mkdir -p {}{}/'.format(remote_app_path, no_ext))
@task
def copy_archive_app_to_remote(c, archive_path):
# Copy the app archive to the remote host
c.put(archive_path, '/tmp/')
@task
def extract_app(c, file_n, path, no_ext):
# Extract the app archive on the remote server
c.run('tar -xzf /tmp/{} -C {}{}/'.format(file_n, path, no_ext))
@task
def remove_extract_app(c, file_n):
# Extract the app archive on the remote server
c.run('rm /tmp/{}'.format(file_n))
@task
def start_gunicorn(c):
# Restart the Gunicorn service on the remote server
c.sudo(f"systemctl start {gunicorn_service_name}", pty=True)
@task
def stop_gunicorn(c):
# Restart the Gunicorn service on the remote server
c.sudo(f"systemctl stop {gunicorn_service_name}", pty=True)
@task
def reload_nginx(c):
# Reload Nginx configuration on the remote server
c.sudo("systemctl reload nginx", pty=True)