from fabric.api import *

env.hosts = [
env.user = 'eric'
env.key_filename = '/path/to/key.pem'

# Deploy the project
def deploy():
    for host in env.hosts:
        local('rsync -vrltD --checksum --chmod=+rwx --delete -e "ssh -i /path/to/key.pem" --exclude ".git" --exclude "logs" /local/path/to/website/. www-data@' + host + ':/remove/path/to/website/.')

# Execute: git pull
def git_pull():
    with cd('/path/to/website'):
        sudo('git pull')

# Execute: npm install
def npm_install():
    with cd('/path/to/website'):
        sudo('npm install')

# Execute: npm update
def npm_update():
    with cd('/path/to/website'):
        sudo('npm update')

# Run the migration
def migration():
    with cd('/path/to/website'):
        sudo('grunt migration')

# Check the node status
def node_status():
    sudo('service mywebsite status')

# Start the node server
def node_start():
    sudo('service mywebsite start')

# Stop the node server
def node_stop():
    sudo('service mywebsite stop')

# Restart the node server
def node_restart():
    sudo('service mywebsite restart')

# Reload nginx
def nginx_reload():
    sudo('service nginx reload')

# Tail the error log
def tail_error(index):
    host_list = [env.hosts[int(index)]]
    execute(tail_error_internal, hosts=host_list)

def tail_error_internal():
    run('tail -f /path/to/logs/error.log')