Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My contribution #1447

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d8696ff
Console improvements
derickmokua Jun 26, 2024
3bdd594
Update AUTHORS
derickmokua Jun 26, 2024
ac56385
Create setup_mysql_dev.sql
derickmokua Jun 26, 2024
d3fb8b4
Create setup_mysql_test.sql
derickmokua Jun 26, 2024
6913414
Update file_storage.py
derickmokua Jun 26, 2024
3741839
Create 0-setup_web_static.sh
derickmokua Jul 11, 2024
d09480a
Create 1-pack_web_static.py
derickmokua Jul 11, 2024
724e69b
Create 2-do_deploy_web_static.py
derickmokua Jul 11, 2024
1e84201
Create 3-deploy_web_static.py
derickmokua Jul 11, 2024
ca37a3e
Create 100-clean_web_static.py
derickmokua Jul 11, 2024
bcf2adb
Create 101-setup_web_static.pp
derickmokua Jul 11, 2024
96d29e0
Create README.md
derickmokua Jul 28, 2024
47c2e3f
Update README.md
derickmokua Jul 28, 2024
d6f912f
start a Flask web app
derickmokua Jul 28, 2024
040ac34
Create __init__.py
derickmokua Jul 28, 2024
ae97316
Create 1-hbnb_route.py
derickmokua Jul 28, 2024
376ab2b
Create 10-hbnb_filters.py
derickmokua Jul 28, 2024
bfedc02
Create 2-c_route.py
derickmokua Jul 28, 2024
069c00c
Create 3-python_route.py
derickmokua Jul 28, 2024
6b53ddc
Create 4-number_route.p
derickmokua Jul 28, 2024
1b604ed
Create 5-number_template.py
derickmokua Jul 28, 2024
94d22d6
Create 6-number_odd_or_even.py
derickmokua Jul 28, 2024
46521da
Create 7-states_list.py
derickmokua Jul 28, 2024
2a49b6a
Create 8-cities_by_states.py
derickmokua Jul 28, 2024
afe63f5
Create 9-states.py
derickmokua Jul 28, 2024
9184013
Create 10-hbnb_filters.html
derickmokua Jul 28, 2024
0e39621
Create 5-number.html
derickmokua Jul 28, 2024
4997c68
Create 6-number_odd_or_even.html
derickmokua Jul 28, 2024
67b5de1
Create 7-states_list.html
derickmokua Jul 28, 2024
6138469
Create 8-cities_by_states.html
derickmokua Jul 28, 2024
8d0804d
Create 9-states.html
derickmokua Jul 28, 2024
0908d58
Create 100-hbnb.html
derickmokua Jul 28, 2024
59ea6f5
Create 7-states_list.cpython-310.py
derickmokua Jul 28, 2024
8d56312
Create db_storage.py
derickmokua Jul 28, 2024
991800f
Update file_storage.py
derickmokua Jul 28, 2024
694fe98
Update state.py
derickmokua Jul 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions 0-setup_web_static.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Shell script for configuring web servers to deploy web_static content

sudo apt-get update
sudo apt-get -y install nginx
sudo ufw allow 'Nginx HTTP'

sudo mkdir -p /data/
sudo mkdir -p /data/web_static/
sudo mkdir -p /data/web_static/releases/
sudo mkdir -p /data/web_static/shared/
sudo mkdir -p /data/web_static/releases/test/
sudo touch /data/web_static/releases/test/index.html
sudo echo "<html>
<head>
</head>
<body>
Holberton School
</body>
</html>" | sudo tee /data/web_static/releases/test/index.html

sudo ln -s -f /data/web_static/releases/test/ /data/web_static/current

sudo chown -R ubuntu:ubuntu /data/

sudo sed -i '/listen 80 default_server/a location /hbnb_static { alias /data/web_static/current/;}' /etc/nginx/sites-enabled/default

sudo service nginx restart
19 changes: 19 additions & 0 deletions 1-pack_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/python3
from fabric.api import local
from time import strftime
from datetime import date


def do_pack():
"""Script to create an archive of the contents in the web_static folder"""

filename = strftime("%Y%m%d%H%M%S")
try:
local("mkdir -p versions")
local("tar -czvf versions/web_static_{}.tgz web_static/"
.format(filename))

return "versions/web_static_{}.tgz".format(filename)

except Exception as e:
return None
62 changes: 62 additions & 0 deletions 100-clean_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/python3
import os.path
from datetime import datetime
from fabric.api import env, local, put, run

env.hosts = ['100.25.19.204', '54.157.159.85']

def do_pack():
"""Create a tar gzipped archive of the web_static directory."""
dt = datetime.utcnow()
file = "versions/web_static_{}{}{}{}{}{}.tgz".format(dt.year,
dt.month,
dt.day,
dt.hour,
dt.minute,
dt.second)
if not os.path.isdir("versions"):
if local("mkdir -p versions").failed:
return None
if local("tar -cvzf {} web_static".format(file)).failed:
return None
return file

def do_deploy(archive_path):
"""Deploy an archive to the web server.

Args:
archive_path (str): Path to the archive file to deploy.
Returns:
bool: False if the file doesn't exist or an error occurs, True otherwise.
"""
if not os.path.isfile(archive_path):
return False
file = os.path.basename(archive_path)
name = file.split(".")[0]

if put(archive_path, "/tmp/{}".format(file)).failed:
return False
if run("rm -rf /data/web_static/releases/{}/".format(name)).failed:
return False
if run("mkdir -p /data/web_static/releases/{}/".format(name)).failed:
return False
if run("tar -xzf /tmp/{} -C /data/web_static/releases/{}/".format(file, name)).failed:
return False
if run("rm /tmp/{}".format(file)).failed:
return False
if run("mv /data/web_static/releases/{}/web_static/* /data/web_static/releases/{}/".format(name, name)).failed:
return False
if run("rm -rf /data/web_static/releases/{}/web_static".format(name)).failed:
return False
if run("rm -rf /data/web_static/current").failed:
return False
if run("ln -s /data/web_static/releases/{}/ /data/web_static/current".format(name)).failed:
return False
return True

def deploy():
"""Create and deploy an archive to the web server."""
file = do_pack()
if file is None:
return False
return do_deploy(file)
31 changes: 31 additions & 0 deletions 101-setup_web_static.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python3
import os
from fabric.api import *

env.hosts = ['100.25.19.204', '54.157.159.85']

def do_clean(number=0):
"""Remove outdated archives.

Args:
number (int): The number of archives to retain.

If number is 0 or 1, keeps only the most recent archive. If
number is 2, keeps the two most recent archives, and so on.
"""
number = 1 if int(number) == 0 else int(number)

# List and sort archives in the versions directory
archives = sorted(os.listdir("versions"))
[archives.pop() for i in range(number)]

# Remove older archives locally
with lcd("versions"):
[local("rm ./{}".format(a)) for a in archives]

# Remove older archives on the remote server
with cd("/data/web_static/releases"):
archives = run("ls -tr").split()
archives = [a for a in archives if "web_static_" in a]
[archives.pop() for i in range(number)]
[run("rm -rf ./{}".format(a)) for a in archives]
48 changes: 48 additions & 0 deletions 2-do_deploy_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/python3
"""Script to compress the web static package
"""
from fabric.api import *
from datetime import datetime
from os import path

# Define the hosts, user, and key for connecting to the servers
env.hosts = ['100.25.19.204', '54.157.159.85']
env.user = 'ubuntu'
env.key_filename = '~/.ssh/id_rsa'

def do_deploy(archive_path):
"""Deploy web files to the server
"""
try:
if not path.exists(archive_path):
return False

# Upload the archive to the server
put(archive_path, '/tmp/')

# Create the target directory
timestamp = archive_path[-18:-4]
run('sudo mkdir -p /data/web_static/releases/web_static_{}/'.format(timestamp))

# Uncompress the archive and delete the .tgz file
run('sudo tar -xzf /tmp/web_static_{}.tgz -C /data/web_static/releases/web_static_{}/'.format(timestamp, timestamp))

# Remove the archive from the server
run('sudo rm /tmp/web_static_{}.tgz'.format(timestamp))

# Move the contents to the web_static directory on the server
run('sudo mv /data/web_static/releases/web_static_{}/web_static/* /data/web_static/releases/web_static_{}/'.format(timestamp, timestamp))

# Remove the extra web_static directory
run('sudo rm -rf /data/web_static/releases/web_static_{}/web_static'.format(timestamp))

# Delete the existing symbolic link
run('sudo rm -rf /data/web_static/current')

# Create a new symbolic link
run('sudo ln -s /data/web_static/releases/web_static_{}/ /data/web_static/current'.format(timestamp))
except:
return False

# Return True if the deployment is successful
return True
62 changes: 62 additions & 0 deletions 3-deploy_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/python3
import os.path
from datetime import datetime
from fabric.api import env, local, put, run

env.hosts = ['100.25.19.204', '54.157.159.85']

def do_pack():
"""Create a tar gzipped archive of the web_static directory."""
dt = datetime.utcnow()
file = "versions/web_static_{}{}{}{}{}{}.tgz".format(dt.year,
dt.month,
dt.day,
dt.hour,
dt.minute,
dt.second)
if not os.path.isdir("versions"):
if local("mkdir -p versions").failed:
return None
if local("tar -cvzf {} web_static".format(file)).failed:
return None
return file

def do_deploy(archive_path):
"""Deploy an archive to the web server.

Args:
archive_path (str): Path to the archive file to deploy.
Returns:
bool: False if the file doesn't exist or an error occurs, True otherwise.
"""
if not os.path.isfile(archive_path):
return False
file = os.path.basename(archive_path)
name = file.split(".")[0]

if put(archive_path, "/tmp/{}".format(file)).failed:
return False
if run("rm -rf /data/web_static/releases/{}/".format(name)).failed:
return False
if run("mkdir -p /data/web_static/releases/{}/".format(name)).failed:
return False
if run("tar -xzf /tmp/{} -C /data/web_static/releases/{}/".format(file, name)).failed:
return False
if run("rm /tmp/{}".format(file)).failed:
return False
if run("mv /data/web_static/releases/{}/web_static/* /data/web_static/releases/{}/".format(name, name)).failed:
return False
if run("rm -rf /data/web_static/releases/{}/web_static".format(name)).failed:
return False
if run("rm -rf /data/web_static/current").failed:
return False
if run("ln -s /data/web_static/releases/{}/ /data/web_static/current".format(name)).failed:
return False
return True

def deploy():
"""Create and deploy an archive to the web server."""
file = do_pack()
if file is None:
return False
return do_deploy(file)
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

Ezra Nobrega <[email protected]>
Justin Majetich <[email protected]>
Derick Mookua <[email protected]>
Loading