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

AirBnB_clone_v2 #1449

Open
wants to merge 8 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
19 changes: 19 additions & 0 deletions 0-setup_web_static.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Installs Nginx if not installed
#+ Creates folders /data/web_static/shared
#+ and /data/releases/test if not exists
#+ /data/web_static/current linked to
#+ /data/web_static/releases/test/
#+ Creates an /data/web_static/releases/test/index.html
#+ Configures Nginx to serve /data/web_static/current/
#+ to hbnb_static

sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install nginx
sudo mkdir -p /data/web_static/releases/test /data/web_static/shared
echo "Welcome to ALX Interns home" | sudo tee /data/web_static/releases/test/index.html
sudo ln -sf /data/web_static/releases/test/ /data/web_static/current
sudo chown -hR ubuntu:ubuntu /data/
sudo sed -i '38i\\tlocation /hbnb_static/ {\n\t\talias /data/web_static/current/;\n\t}\n' /etc/nginx/sites-available/default
sudo service nginx start
16 changes: 16 additions & 0 deletions 1-pack_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python3
"""Create funtion for generate tgz file."""
from fabric.api import local
import time


def do_pack():
"""Gerenate tgz."""
timestamp = time.strftime("%Y%m%d%H%M%S")
try:
local("mkdir -p versions")
local("tar -cvzf versions/web_static_{:s}.tgz web_static/".
format(timestamp))
return ("versions/web_static_{:s}.tgz".format(timestamp))
except FileNotFoundError:
return None
29 changes: 29 additions & 0 deletions 100-clean_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/python3
"""
Deletes out of date archives
"""

import os
from fabric.api import *

env.hosts = ["3.235.239.225", "35.170.64.18"]
env.user = 'ubuntu'
env.identinty = '~/.ssh/school'


def do_clean(number=0):
"""
Delete out-of-date archives.
"""
number = 1 if int(number) == 0 else int(number)

archives = sorted(os.listdir("versions"))
[archives.pop() for i in range(number)]
with lcd("versions"):
[local("rm ./{}".format(a)) for a in archives]

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]
87 changes: 87 additions & 0 deletions 101-setup_web_static.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Configures web server to deploy static web
# Nginx configuration file
$nginx_conf = "server {
listen 80 default_server;
listen [::]:80 default_server;
add_header X-Served-By ${hostname};
root /var/www/html;
index index.html index.htm;
location /hbnb_static {
alias /data/web_static/current;
index index.html index.htm;
}
location /redirect_me {
rewrite ^ https://www.youtube.com/watch?v=QH2-TGUlw\u4? permanent;
}
error_page 404 /404.html;
location /404 {
root /var/www/html;
internal;
}
}"

package { 'nginx':
ensure => 'present',
provider => 'apt'
} ->

file { '/data':
ensure => 'directory'
} ->

file { '/data/web_static':
ensure => 'directory'
} ->

file { '/data/web_static/releases':
ensure => 'directory'
} ->

file { '/data/web_static/releases/test':
ensure => 'directory'
} ->

file { '/data/web_static/shared':
ensure => 'directory'
} ->

file { '/data/web_static/releases/test/index.html':
ensure => 'present',
content => "Welcome to ALX Internship Program\n"
} ->

file { '/data/web_static/current':
ensure => 'link',
target => '/data/web_static/releases/test'
} ->

exec { 'chown -R ubuntu:ubuntu /data/':
path => '/usr/bin/:/usr/local/bin/:/bin/'
}

file { '/var/www':
ensure => 'directory'
} ->

file { '/var/www/html':
ensure => 'directory'
} ->

file { '/var/www/html/index.html':
ensure => 'present',
content => "Welcome to ALX Internship Program\n"
} ->

file { '/var/www/html/404.html':
ensure => 'present',
content => "Ceci n'est pas une page\n"
} ->

file { '/etc/nginx/sites-available/default':
ensure => 'present',
content => $nginx_conf
} ->

exec { 'nginx restart':
path => '/etc/init.d/'
}
39 changes: 39 additions & 0 deletions 2-do_deploy_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/python3
"""
Distributes an archive to our web servers,
using the function do_deploy
def do_deploy(archive_path):
Return False iff archive path doesn't exist
"""

from fabric.api import put, run, env
from os.path import exists
env.hosts = ['3.235.239.225', '35.170.64.18']
env.user = 'ubuntu'
env.identity = '~/.ssh/school'
env.password = None


def do_deploy(archive_path):
"""
Deploys an archive to a server
"""
if exists(archive_path) is False:
return False
try:
file_N = archive_path.split("/")[-1]
n = file_N.split(".")[0]
path = "/data/web_static/releases/"
put(archive_path, '/tmp/')
run('sudo mkdir -p {}{}/'.format(path, n))
run('sudo tar -xzf /tmp/{} -C {}{}/'.format(file_N, path, n))
run('sudo rm /tmp/{}'.format(file_N))
run('sudo mv {0}{1}/web_static/* {0}{1}/'.format(path, n))
run('sudo rm -rf {}{}/web_static'.format(path, n))
run('sudo rm -rf /data/web_static/current')
run('sudo ln -s {}{}/ /data/web_static/current'.format(path, n))
run('sudo chmod -R 755 /data/')
print("New version deployed!")
return True
except FileNotFoundError:
return False
53 changes: 53 additions & 0 deletions 3-deploy_web_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/python3
"""
Creates and distributes an archive to the web servers
"""

from fabric.api import env, local, put, run
from datetime import datetime
from os.path import exists, isdir
env.hosts = ['3.235.239.225', '35.170.64.18']
env.user = 'ubuntu'
env.identity = '~/.ssh/school'


def do_pack():
"""generates a tgz archive"""
try:
date = datetime.now().strftime("%Y%m%d%H%M%S")
if isdir("versions") is False:
local("mkdir versions")
file_name = "versions/web_static_{}.tgz".format(date)
local("tar -cvzf {} web_static".format(file_name))
return file_name
except FileNotFoundError:
return None


def do_deploy(archive_path):
"""distributes an archive to the web servers"""
if exists(archive_path) is False:
return False
try:
file_n = archive_path.split("/")[-1]
no_ext = file_n.split(".")[0]
path = "/data/web_static/releases/"
put(archive_path, '/tmp/')
run('mkdir -p {}{}/'.format(path, no_ext))
run('tar -xzf /tmp/{} -C {}{}/'.format(file_n, path, no_ext))
run('rm /tmp/{}'.format(file_n))
run('mv {0}{1}/web_static/* {0}{1}/'.format(path, no_ext))
run('rm -rf {}{}/web_static'.format(path, no_ext))
run('rm -rf /data/web_static/current')
run('ln -s {}{}/ /data/web_static/current'.format(path, no_ext))
return True
except FileNotFoundError:
return False


def deploy():
"""creates and distributes an archive to the web servers"""
archive_path = do_pack()
if archive_path is None:
return False
return do_deploy(archive_path)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ 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)}"]
```
<br>
<br>
16 changes: 16 additions & 0 deletions web_flask/0-hello_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python3
"""
start Flask application
"""

from flask import Flask
app = Flask(__name__)


@app.route('/', strict_slashes=False)
def index():
"""returns Hello HBNB!"""
return 'Hello HBNB!'

if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
22 changes: 22 additions & 0 deletions web_flask/1-hbnb_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/python3
"""
start Flask application
"""

from flask import Flask
app = Flask(__name__)


@app.route('/', strict_slashes=False)
def index():
"""returns Hello HBNB!"""
return 'Hello HBNB!'


@app.route('/hbnb', strict_slashes=False)
def hbnb():
"""returns HBNB"""
return 'HBNB'

if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
28 changes: 28 additions & 0 deletions web_flask/2-c_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/python3
"""
start Flask application
"""

from flask import Flask
app = Flask(__name__)


@app.route('/', strict_slashes=False)
def index():
"""returns Hello HBNB!"""
return 'Hello HBNB!'


@app.route('/hbnb', strict_slashes=False)
def hbnb():
"""returns HBNB"""
return 'HBNB'


@app.route('/c/<text>', strict_slashes=False)
def cisfun(text):
"""display “C ” followed by the value of the text variable"""
return 'C ' + text.replace('_', ' ')

if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
35 changes: 35 additions & 0 deletions web_flask/3-python_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python3
"""
start Flask application
"""

from flask import Flask
app = Flask(__name__)


@app.route('/', strict_slashes=False)
def index():
"""returns Hello HBNB!"""
return 'Hello HBNB!'


@app.route('/hbnb', strict_slashes=False)
def hbnb():
"""returns HBNB"""
return 'HBNB'


@app.route('/c/<text>', strict_slashes=False)
def cisfun(text):
"""display “C ” followed by the value of the text variable"""
return 'C ' + text.replace('_', ' ')


@app.route('/python', strict_slashes=False)
@app.route('/python/<text>', strict_slashes=False)
def pythoniscool(text='is cool'):
"""display “Python ”, followed by the value of the text variable"""
return 'Python ' + text.replace('_', ' ')

if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
Loading