-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-deploy_web_static.py
211 lines (187 loc) · 6.25 KB
/
3-deploy_web_static.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python3
"""
This module makes use of fabric version 1.14.2
and automates the creation of archive files
on a remote host
"""
# Import Fabric's API module
from datetime import datetime
import os
import platform
from fabric.api import task, run, env
from fabric.operations import local, put
# env.hosts = [
# '108999f0b0e4.a73c91be.alx-cod.online',
#'54.210.108.11',
#'54.158.179.90'
# 'web-02.eldoret.tech',
#]
# Set the username
# env.user = "108999f0b0e4"
# Set the password [NOT RECOMMENDED]
# env.password = "785b0035e30507820c46"
# env.hosts = ['localhost']
# env.host="108999f0b0e4.a73c91be.alx-cod.online"
@task
def do_pack():
"""
compresses web folder files and
versions them based on timestamp.
"""
retval = None
try:
# Create the appropriate directory tree using native python.
result = local("mkdir -p ./versions")
# print("After Creating Dir result: ",type(str(result)))
fileName = "{}".format(datetime.now().strftime('%Y%m%d%H%M%S'))
result = local(
"tar -czvf versions/web_static_{}.tgz\
./web_static".format(fileName))
# print("Result of Tar: ",result)
# Unix -bash implementation
# fileSize = local(
# 'stat -c "%s" versions/web_static_{}.tgz'.format(fileName))
fileSize = os.stat(
os.path.join(
'versions',
'web_static_{}.tgz'.format(fileName))).st_size
retval = "web_static packed: \
versions/web_static_{}.tgz -> {}Bytes".format(fileName, fileSize)
return retval
except BaseException:
return retval
@task
def deploy():
"""
creates and distributes an archive to your web servers
"""
# Call the do_pack() function and store the path of the created archive
path_of_new_archive = do_pack()
# if successfully returned folder path returns stringpath
if path_of_new_archive:
# archive path returned
# Call the do_deploy(archive_path) function, using the new path of the
# new archive
result_status = do_deploy(path_of_new_archive)
# Return the return value of do_deploy
return result_status
else:
# No archive path returned
return False
@task
def do_deploy(archive_path):
"""
deploys a web project to a remote server's
root page
Return: True on Success Else False
"""
# Verify that versions directory exists and deploy the latest Version
# Returns False if the file at the path archive_path doesn’t exist
retval = False
if not local_file_exists(file_to_check=archive_path):
# file not found - return false
retval = False
# Upload the archive to the /tmp/ directory of the web server
remote_path = "/tmp"
separator = platform.system()
if separator == 'Linux' or separator == 'Darwin':
separator = '/'
else:
separator = '\\'
uploaded_file_name = archive_path.split(separator)[-1]
upload_success = upload_file_to_remote(
file_to_upload=archive_path,
remote_path=remote_path)
if upload_success:
# Unpack to remote directory
post_success = do_post_unpack(
remote_path=remote_path,
uploaded_file_name=uploaded_file_name,
file_separator=separator)
if post_success:
retval = True
else:
retval = False
else:
retval = False
# Returns True if all operations have been done correctly, otherwise
# returns False
return retval
def local_file_exists(file_to_check):
"""
the test -e command checks if file exists
Return:
True : if file exists
False : if file not found
"""
try:
local("test -e {}".format(file_to_check))
except BaseException:
return False
else:
return True
def remote_directory_exists(directory_to_check):
"""
the test -e command checks if directory exists
Return:
True : if directory exists
False : if directory not found
"""
try:
run("test -d {}".format(directory_to_check))
except BaseException:
return False
else:
return True
def upload_file_to_remote(file_to_upload, remote_path):
"""
Uploads a local file to a remote path
Verifies that remote directory exists be4 upload
"""
# verify that the remote path exists
if remote_directory_exists(directory_to_check=remote_path):
# remote path found continue
try:
put(local_path=file_to_upload, remote_path=remote_path)
except BaseException:
return False
else:
return True
else:
return False
def do_post_unpack(remote_path, uploaded_file_name, file_separator):
"""
Unpacks a Remote File To a Remote directory
"""
try:
# filename without extension> on the web server
file_without_extension = uploaded_file_name.split(".")[0]
# Make target Directories
run("mkdir -p /data/web_static/releases/{}/".format(
file_without_extension))
# Uncompress the archive to the folder
# /data/web_static/releases/<archive
run("tar -xzf {}{}{} -C /data/web_static/releases/{}/".format(
remote_path,
file_separator, uploaded_file_name, file_without_extension))
# Delete the archive from the web server
run("rm {}{}{}".format(remote_path, file_separator,
uploaded_file_name))
run("mv /data/web_static/releases/{}/web_static/* /data/web_static/releases/{}/".format(
file_without_extension, file_without_extension))
run("rm -rf /data/web_static/releases/{}/web_static".format(
file_without_extension))
# Delete the symbolic link /data/web_static/current from the web server
run("rm -rf /data/web_static/current")
# Create a new the symbolic link /data/web_static/current
run("ln -s /data/web_static/releases/{} /data/web_static/current".format(
file_without_extension))
except BaseException:
# Error Occurred
return False
else:
return True
if __name__ == '__main__':
# result = do_pack()
# print(result)
print(local_file_exists("/tmp/tim"))