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

updated readme file #1494

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions 0-setup_web_static.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# 0-setup_web_static.sh

# Install Nginx
if ! command -v nginx &> /dev/null; then
sudo apt-get update
sudo apt-get install -y nginx
fi

# Create necessary directories
sudo mkdir -p /data/web_static/releases/test/
sudo mkdir -p /data/web-static/shared/

# Create a fake HTML file for testing
echo "<html>
<head>
</head>
<body>
ALX
</body>
</html>" | sudo tee /data/web_static/releases/test/index.html > /dev/null

# Create or recreate the symbolic link
sudo ln -sf /data/web_static/releases/test/ /data/web_static/current

# Give ownership of the /data/ folder to the ubuntu user and group
sudo chown -R ubuntu:ubuntu /data/

# Update the Nginx configuration
nginx_config="/etc/nginx/sites-available/default"
sudo sed -i '/listen 80 default_server;/a \\n\tlocation /hbnb_static/ {\n\t\talias /data/web_static/current/;\n\t}' $nginx_config

# Restart Nginx to apply changes
sudo service nginx restart

# Exit successfully
exit 0
34 changes: 34 additions & 0 deletions 1-pack_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
from fabric.api import local
from datetime import datetime
import os

def do_pack():
"""
Generates a .tgz archive from the contents of the web_static folder.
"""
try:
# Create the versions folder if it doesn't exist
if not os.path.exists("versions"):
local("mkdir -p versions")

# Generate the archive name using the current timestamp
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
archive_name = f"web_static_{timestamp}.tgz"
archive_path = f"versions/{archive_name}"

# Create the .tgz archive
print(f"Packing web_static to {archive_path}")
result = local(f"tar -cvzf {archive_path} web_static")

# Check if the archive was created successfully
if result.succeeded:
archive_size = os.path.getsize(archive_path)
print(f"web_static packed: {archive_path} -> {archive_size}Bytes")
return archive_path
else:
return None
except Exception as e:
print(f"An error occurred: {e}")
return None
pass
50 changes: 50 additions & 0 deletions 2-do_deploy_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
from fabric.api import env, put, run, local
from os.path import exists

# Define the list of web servers
env.hosts = ['<IP web-01>', '<IP web-02>'] # Replace with your actual server IPs
env.user = 'ubuntu' # Replace with your SSH username
env.key_filename = 'my_ssh_private_key' # Replace with your SSH private key path

def do_deploy(archive_path):
"""
Distributes an archive to your web servers.
"""
if not exists(archive_path):
return False

try:
# Upload the archive to the /tmp/ directory on the web server
archive_filename = archive_path.split('/')[-1]
archive_no_ext = archive_filename.split('.')[0]
remote_tmp_path = f"/tmp/{archive_filename}"
put(archive_path, remote_tmp_path)

# Create the target directory for the release
release_path = f"/data/web_static/releases/{archive_no_ext}"
run(f"mkdir -p {release_path}")

# Uncompress the archive to the release directory
run(f"tar -xzf {remote_tmp_path} -C {release_path}")

# Remove the uploaded archive from the web server
run(f"rm {remote_tmp_path}")

# Move the contents of the web_static folder to the release directory
run(f"mv {release_path}/web_static/* {release_path}/")

# Remove the now-empty web_static folder
run(f"rm -rf {release_path}/web_static")

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

# Create a new symbolic link to the new release
run(f"ln -s {release_path} /data/web_static/current")

print("New version deployed!")
return True
except Exception as e:
print(f"An error occurred: {e}")
return False
93 changes: 93 additions & 0 deletions 3-deploy_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python3
from fabric.api import env, local, put, run
from datetime import datetime
from os.path import exists

# Define the list of web servers
env.hosts = ['<IP web-01>', '<IP web-02>'] # Replace with your actual server IPs
env.user = 'ubuntu' # Replace with your SSH username
env.key_filename = 'my_ssh_private_key' # Replace with your SSH private key path

def do_pack():
"""
Generates a .tgz archive from the contents of the web_static folder.
"""
try:
# Create the versions folder if it doesn't exist
if not exists("versions"):
local("mkdir -p versions")

# Generate the archive name using the current timestamp
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
archive_name = f"web_static_{timestamp}.tgz"
archive_path = f"versions/{archive_name}"

# Create the .tgz archive
print(f"Packing web_static to {archive_path}")
result = local(f"tar -cvzf {archive_path} web_static")

# Check if the archive was created successfully
if result.succeeded:
archive_size = local(f"stat -c%s {archive_path}", capture=True)
print(f"web_static packed: {archive_path} -> {archive_size}Bytes")
return archive_path
else:
return None
except Exception as e:
print(f"An error occurred: {e}")
return None

def do_deploy(archive_path):
"""
Distributes an archive to your web servers.
"""
if not exists(archive_path):
return False

try:
# Upload the archive to the /tmp/ directory on the web server
archive_filename = archive_path.split('/')[-1]
archive_no_ext = archive_filename.split('.')[0]
remote_tmp_path = f"/tmp/{archive_filename}"
put(archive_path, remote_tmp_path)

# Create the target directory for the release
release_path = f"/data/web_static/releases/{archive_no_ext}"
run(f"mkdir -p {release_path}")

# Uncompress the archive to the release directory
run(f"tar -xzf {remote_tmp_path} -C {release_path}")

# Remove the uploaded archive from the web server
run(f"rm {remote_tmp_path}")

# Move the contents of the web_static folder to the release directory
run(f"mv {release_path}/web_static/* {release_path}/")

# Remove the now-empty web_static folder
run(f"rm -rf {release_path}/web_static")

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

# Create a new symbolic link to the new release
run(f"ln -s {release_path} /data/web_static/current")

print("New version deployed!")
return True
except Exception as e:
print(f"An error occurred: {e}")
return False

def deploy():
"""
Creates and distributes an archive to your web servers.
"""
# Step 1: Create the archive
archive_path = do_pack()
if not archive_path:
print("Failed to create archive.")
return False

# Step 2: Deploy the archive
return do_deploy(archive_path)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ Usage: <class_name>.update(<_id>, <dictionary>)
(hbnb) User.all()
(hbnb) ["[User] (98bea5de-9cb0-4d78-8a9d-c4de03521c30) {'updated_at': datetime.datetime(2020, 2, 19, 21, 47, 29, 134362), 'name': 'Fred the Frog', 'age': 9, 'id': '98bea5de-9cb0-4d78-8a9d-c4de03521c30', 'created_at': datetime.datetime(2020, 2, 19, 21, 47, 29, 134343)}"]
```
(TBD - contributions will be updated once known).
<br>
36 changes: 29 additions & 7 deletions console.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python3
""" Console Module """
from ast import Name
import cmd
import sys
from models.base_model import BaseModel
Expand Down Expand Up @@ -73,7 +74,7 @@ def precmd(self, line):
pline = pline[2].strip() # pline is now str
if pline:
# check for *args or **kwargs
if pline[0] is '{' and pline[-1] is'}'\
if pline[0] == '{' and pline[-1] is'}'\
and type(eval(pline)) is dict:
_args = pline
else:
Expand Down Expand Up @@ -115,16 +116,37 @@ def emptyline(self):

def do_create(self, args):
""" Create an object of any class"""
if not args:
try:
class_name = args.split(" ")[0]
except IndexError:
pass
if not class_name:
print("** class name missing **")
return
elif args not in HBNBCommand.classes:
elif class_name not in HBNBCommand.classes:
print("** class doesn't exist **")
return
new_instance = HBNBCommand.classes[args]()
storage.save()
# create place city_id="0001" user_id="0001" name="My_liitle_house"
all_list = args.split(" ")

new_instance = eval(class_name)()

for i in range(1, len(all_list)):
key, value = tuple(all_list[i].split("-"))
if value.startswith('""'):
value.strip('""').replace("_", " ")
else:
try:
value = eval(value)
except Exception:
print(f"couldn't evaluate {value}")
pass
if hasattr(new_instance, key):
setattr(new_instance, key, value)

storage.new(new_instance)
print(new_instance.id)
storage.save()
new_instance.save()

def help_create(self):
""" Help information for the create method """
Expand Down Expand Up @@ -272,7 +294,7 @@ def do_update(self, args):
args.append(v)
else: # isolate args
args = args[2]
if args and args[0] is '\"': # check for quoted arg
if args and args[0] == '\"': # check for quoted arg
second_quote = args.find('\"', 1)
att_name = args[1:second_quote]
args = args[second_quote + 1:]
Expand Down
1 change: 1 addition & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Binary file added models/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/city.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/place.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/review.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/state.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/user.cpython-310.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions setup_mysql_dev.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- script that prepares a MySQL server for this project
-- A database hbnb_dev_db
-- A new user hbnb_dev (in localhost)
-- The password of hbnb_dev should be set to hbnb_dev_pwd
-- hbnb_dev should have all privileges on the database hbnb_dev_db...
-- ...(and only this database)
-- hbnb_dev should have SELECT privilege on the database performance_schema...
-- ...(and only this database)
-- If the database hbnb_dev_db or the user hbnb_dev already exists, your...
-- ...script should not fail

CREATE DATABASE IF NOT EXISTS hbnb_dev_db;
CREATE USER IF NOT EXISTS 'hbnb_dev'@'localhost' IDENTIFIED BY 'hbnb_dev_pwd';
GRANT ALL PRIVILEGES ON hbnb_dev_db.* TO 'hbnb_dev'@'localhost';
GRANT SELECT ON performance_schema.* TO 'hbnb_dev'@'localhost';
FLUSH PRIVILEGES;