'''
Created on Oct 17, 2020

@fabric.author: esdev
'''
import os
import fabric
from fabric import task
# from fabric import api, operations
# from fabric.api import env,hosts,local,run,serial,settings, task
#from fabric.context_managers import hide

class env():
    # Standard fabric configuration
    shell = "/bin/bash -l -i -c"
    parallel = True            # Changes default behavior to parallel
    use_ssh_config = False     # Tells fabric to use the user's ssh config
    disable_known_hosts = True # Ignore warnings about known_hosts
    user = 'slacm'
    # File transfer directories
    localPath = os.getcwd() + '/' # Path on localhost
    nodePath = '/home/slacm/'  # Path on target
    hosts = ['rpi4car']
    version = '0.0.1'

@task
def run(ctx,cmd):
    """Execute command as user:<command>"""
    ctx.user = env.user
    print("["+ctx.host+"] " + cmd)
    result = ctx.run(cmd,shell=env.shell,hide=True,warn=True)
    print(result)
    return result

@task
def sudo(ctx,cmd):
    """Execute command as sudo:<command>"""
    ctx.user = env.user
    print("["+ctx.host+"] " + cmd)
    result = ctx.sudo(cmd,shell=env.shell,hide=True,warn=True)
    print(result)
    return result


@task(hosts=env.hosts)
def check(c):
    """Test that hosts are communicating"""
    run(c,'hostname && uname -a')

@task
def build(c):
    """Build package locally"""
    c.run("python3 setup.py sdist bdist_wheel")
 
@task
def install(c):
    """ Install package locally - must be run w/ sudo"""
    c.sudo("python3 setup.py install")
    
@task
def uninstall(c):
    """ Uninstall package locally - must be run w/ sudo"""
    package = 'slacm'
    c.sudo('pip3 uninstall -y %s' % package)
    
@task
def get(ctx,fileName, local_path='.'):
    """Download file from host:<file name>,[local path]"""
    ctx.local("scp %s@%s:%s %s" % (env.user,ctx.host,fileName,local_path))
 
@task
def put(ctx,fileName, remote_path=''):
    """Upload file to hosts:<file name>,[remote path]"""
    ctx.local("scp %s %s@%s:%s" % (fileName,env.user,ctx.host,remote_path))
     
@task(hosts=env.hosts)
def deploy(c):
    """Deploy package on remote host(s)"""
    package = 'slacm-' + str(env.version) + '.tar.gz'
    package_path = 'dist/' + package
    put(c,package_path)
    sudo(c,'pip3 install %s' % package)
    sudo(c,'rm -f %s' %(package))
     
@task(hosts=env.hosts)
def undeploy(c):
    """Uninstall package on remote hosts(s)"""
    package = 'slacm'
    sudo(c,'pip3 uninstall -y %s' % package)

@task(hosts=env.hosts)
def wipe(c):
    """Wipe all user files on remote host(s)"""
    sudo(c,'rm -fr /home/%s/[a-zA-Z]*' % env.user)
    
@task(hosts=env.hosts)
def requires(c):
    """Install requirements om remote host(s)"""
    put(c,"requirements.txt")
    sudo(c,"pip3 install -r requirements.txt")
    run(c,'rm -f requirements.txt')
     
@task(hosts=env.hosts)
def kill(c):
    """Kill any hanging processes"""
    sudo(c,"pkill -SIGKILL slacm_run")
     
@task
def stop(c):
    """Stop local slacm process"""
    c.run("pkill -SIGTERM -f slacm_run")
    
@task(hosts=env.hosts)
def shutdown(c):
    """Shutdown all hosts"""
    sudo(c,"shutdown now")
        
     
'''
Created on Oct 17, 2020

@fabric.author: esdev
'''
import os
import fabric
from fabric import task
# from fabric import api, operations
# from fabric.api import env,hosts,local,run,serial,settings, task
#from fabric.context_managers import hide

class env():
    # Standard fabric configuration
    shell = "/bin/bash -l -i -c"
    parallel = True            # Changes default behavior to parallel
    use_ssh_config = False     # Tells fabric to use the user's ssh config
    disable_known_hosts = True # Ignore warnings about known_hosts
    user = 'slacm'
    # File transfer directories
    localPath = os.getcwd() + '/' # Path on localhost
    nodePath = '/home/slacm/'  # Path on target
    hosts = ['rpi4car']
    version = '0.0.1'

@task
def run(ctx,cmd):
    """Execute command as user:<command>"""
    ctx.user = env.user
    print("["+ctx.host+"] " + cmd)
    result = ctx.run(cmd,shell=env.shell,hide=True,warn=True)
    print(result)
    return result

@task
def sudo(ctx,cmd):
    """Execute command as sudo:<command>"""
    ctx.user = env.user
    print("["+ctx.host+"] " + cmd)
    result = ctx.sudo(cmd,shell=env.shell,hide=True,warn=True)
    print(result)
    return result


@task(hosts=env.hosts)
def check(c):
    """Test that hosts are communicating"""
    run(c,'hostname && uname -a')

@task
def build(c):
    """Build package locally"""
    c.run("python3 setup.py sdist bdist_wheel")
 
@task
def install(c):
    """ Install package locally - must be run w/ sudo"""
    c.sudo("python3 setup.py install")
    
@task
def uninstall(c):
    """ Uninstall package locally - must be run w/ sudo"""
    package = 'slacm'
    c.sudo('pip3 uninstall -y %s' % package)
    
@task
def get(ctx,fileName, local_path='.'):
    """Download file from host:<file name>,[local path]"""
    ctx.local("scp %s@%s:%s %s" % (env.user,ctx.host,fileName,local_path))
 
@task
def put(ctx,fileName, remote_path=''):
    """Upload file to hosts:<file name>,[remote path]"""
    ctx.local("scp %s %s@%s:%s" % (fileName,env.user,ctx.host,remote_path))
     
@task(hosts=env.hosts)
def deploy(c):
    """Deploy package on remote host(s)"""
    package = 'slacm-' + str(env.version) + '.tar.gz'
    package_path = 'dist/' + package
    put(c,package_path)
    sudo(c,'pip3 install %s' % package)
    sudo(c,'rm -f %s' %(package))
     
@task(hosts=env.hosts)
def undeploy(c):
    """Uninstall package on remote hosts(s)"""
    package = 'slacm'
    sudo(c,'pip3 uninstall -y %s' % package)

@task(hosts=env.hosts)
def wipe(c):
    """Wipe all user files on remote host(s)"""
    sudo(c,'rm -fr /home/%s/[a-zA-Z]*' % env.user)
    
@task(hosts=env.hosts)
def requires(c):
    """Install requirements om remote host(s)"""
    put(c,"requirements.txt")
    sudo(c,"pip3 install -r requirements.txt")
    run(c,'rm -f requirements.txt')
     
@task(hosts=env.hosts)
def kill(c):
    """Kill any hanging processes"""
    sudo(c,"pkill -SIGKILL slacm_run")
     
@task
def stop(c):
    """Stop local slacm process"""
    c.run("pkill -SIGTERM -f slacm_run")
    
@task(hosts=env.hosts)
def shutdown(c):
    """Shutdown all hosts"""
    sudo(c,"shutdown now")
        
     
Created on Oct 17, 2020  @fabric.author: esdev # from fabric import api, operations # from fabric.api import env,hosts,local,run,serial,settings, task #from fabric.context_managers import hide # Standard fabric configuration # Changes default behavior to parallel # Tells fabric to use the user's ssh config # Ignore warnings about known_hosts # File transfer directories # Path on localhost # Path on target Execute command as user:<command> Execute command as sudo:<command> Test that hosts are communicating Build package locally Install package locally - must be run w/ sudo Uninstall package locally - must be run w/ sudo Download file from host:<file name>,[local path] Upload file to hosts:<file name>,[remote path] Deploy package on remote host(s) Uninstall package on remote hosts(s) Wipe all user files on remote host(s) Install requirements om remote host(s) Kill any hanging processes Stop local slacm process Shutdown all hosts