#!/usr/bin/python3
""" This module creates a tar.gz file"""
import datetime
from fabric.api import local
from fabric.api import run
from fabric.api import put
from fabric.api import env
from fabric.api import sudo
import os
env.hosts = ['35.237.30.147', '54.82.115.78']
env.user = "ubuntu"


def do_pack():
    """This function creates the thing"""
    local("mkdir -p versions")
    time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    file = "versions/web_static_{}.tgz".format(time)
    res = local("tar -czvf {} web_static".format(file))
    if res.failed:
        return None
    return (file)


def do_deploy(archive_path):
    """This function deploys the file to the server"""
    if os.path.exists(archive_path) is False:
        return False
    fn = archive_path.split("/", 2)[1]
    dwr = '/data/web_static/releases/{}'.format(
        fn.split(".", 1)[0])
    tmp = '/tmp/{}'.format(fn)
    cur = '/data/web_static/current'
    try:
        put(archive_path, '/tmp/')
        sudo("mkdir -p {}/".format(dwr))
        sudo("tar -xzf {} -C {}/".format(tmp, dwr))
        sudo("rm {}".format(tmp))
        sudo("mv {}/web_static/* {}".format(dwr, dwr))
        sudo("rm -rf {}/web_static".format(dwr))
        sudo("rm -rf {}".format(cur))
        sudo("ln -s {}/ {}".format(dwr, cur))
        return True
    except:
        return False
