<commit_before>#todo
#zip up the files (duh)
#scp it to the remote machine
#ssh to remote machine
#blow away old location
#unzip and redeploy it over there
#drop in the build number and revision number?

#get username, password, hostname, location from environmental variables

import os
import sys
import tarfile
import gzip

import sys, os, select
import paramiko

debug = True

hostport = 22

def make_archive(path, target_filename):
    print "Making archive %s : %s" % (path, target_filename)    
    tar = tarfile.open(target_filename, "w:gz")
    tar.add(path)
    tar.close()
    print "archive created successfully"

def putfile(t,filename):
    pass

def run(t, cmd):
    'Open channel on transport, run command, capture output and return'
    global debug
    out = ''

    if debug: print 'DEBUG: Running cmd:', cmd
    chan = t.open_session()
    
    #chan.setblosesscking(0)

    try:
        chan.exec_command(cmd)
    except SSHException:
        print "Error running remote command, yo", SSHException        
        sys.exit(1)
    

    ### Read when data is available
#    while select.select([chan,], [], []):
#        x = chan.recv(1024)
#        if not x: break
#        out += x
#        select.select([],[],[],.1)
#
#    if debug: print 'DEBUG: cmd results:', out
    chan.close()
    return out


def do_deploy(hostname, username, password, target_abs_path, target_deploy_path, build_number, revision_number):
    ### Unbuffered sys.stdout
    #COMMCARE VARS
    #CCHQ_BUILD_NUMBER=-1
    #CCHQ_REVISION_NUMBER=-1
    #CCHQ_BUILD_DATE=''
    
    
    #make the archive    
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    archive_to_deploy = os.path.join('../../../','deploy-b%s-rev%s.tar.gz' % (build_number, revision_number))
    
    basedir = os.path.basename(os.path.abspath('../../'))    
    curdir = os.getcwd()
    os.chdir('../../../')
    make_archive(basedir,os.path.basename(archive_to_deploy))
    os.chdir(curdir)
    
    
    sys.stdout = os.fdopen(1, 'w', 0)    
    if debug:
        print 'DEBUG: Writing log to ssh-cmd.log'
        paramiko.util.log_to_file('ssh-cmd.log')
    
    ### Open SSH transport
    
    transport = paramiko.Transport((hostname, hostport))
    transport.connect(username=username, password=password)
    
    print "starting sftp session"
    sftp = paramiko.SFTPClient.from_transport(transport)
    basename = os.path.basename(archive_to_deploy)
    sftp.put(archive_to_deploy,target_abs_path + "/builds/" + basename)
    sftp.close()
    
    print "sftp file transferred, remoting in to deploy archive"    
    
    #print run(transport, 'cd %s' %(target_abs_path))
    print run(transport, 'rm -rf %s/%s' % (target_abs_path,target_deploy_path)) 
    print run(transport,'gunzip %s/%s' % (target_abs_path+"/builds",basename))
    print run(transport,'tar -xf %s/%s' % (target_abs_path+"/builds",basename[0:-3]))    
    
    print run(transport,'echo CCHQ_BUILD_DATE=\\"`date`\\" >> %s/projects/cchq_main/settings.py' % (basedir))
    print run(transport,'echo CCHQ_BUILD_NUMBER=%s >> %s/projects/cchq_main/settings.py' % (build_number,basedir))
    print run(transport,'echo CCHQ_REVISION_NUMBER=%s >> %s/projects/cchq_main/settings.py' % (revision_number,basedir))
    
    print run(transport,'touch %s/projects/cchq_main/media/version.txt' % (basedir))
    print run(transport,'echo CCHQ_BUILD_DATE=\\"`date`\\" >> %s/projects/cchq_main/media/version.txt' % (basedir))
    print run(transport,'echo CCHQ_BUILD_NUMBER=%s >> %s/projects/cchq_main/media/version.txt' % (build_number,basedir))
    print run(transport,'echo CCHQ_REVISION_NUMBER=%s >> %s/projects/cchq_main/media/version.txt' % (revision_number,basedir))
    
    
    print run(transport,'mkdir %s/projects/cchq_main/%s' % (basedir, 'xform-data'))
    print run(transport,'mkdir %s/projects/cchq_main/%s' % (basedir, 'schemas'))    
     
    print run(transport,'chmod 777 %s/projects/cchq_main/' % (basedir))
    print run(transport,'chmod -R 777 %s/projects/cchq_main/' % (basedir))
    print run(transport,'chmod 777 %s/projects/cchq_main/cchq.db' % (basedir))
    #print run(transport,'rm -rf /var/commcarehq-test')<commit_msg>fix deploy script so that it actually copies all files over correctly<commit_after>#todo
#zip up the files (duh)
#scp it to the remote machine
#ssh to remote machine
#blow away old location
#unzip and redeploy it over there
#drop in the build number and revision number?

#get username, password, hostname, location from environmental variables

import os
import sys
import tarfile
import gzip

import sys, os, select
import paramiko

debug = True

hostport = 22

def make_archive(path, target_filename):
    print "Making archive %s : %s" % (path, target_filename)    
    tar = tarfile.open(target_filename, "w:gz")
    tar.add(path)
    tar.close()
    print "archive created successfully"

def putfile(t,filename):
    pass

def run(t, cmd):
    'Open channel on transport, run command, capture output and return'
    global debug
    out = ''

    if debug: print 'DEBUG: Running cmd:', cmd
    chan = t.open_session()
    
    #chan.setblosesscking(0)

    try:
        chan.exec_command(cmd)
    except SSHException:
        print "Error running remote command, yo", SSHException        
        sys.exit(1)
    

    ### Read when data is available
#    while select.select([chan,], [], []):
#        x = chan.recv(1024)
#        if not x: break
#        out += x
#        select.select([],[],[],.1)
#
#    if debug: print 'DEBUG: cmd results:', out
    chan.close()
    return out


def do_deploy(hostname, username, password, target_abs_path, target_deploy_path, build_number, revision_number):
    ### Unbuffered sys.stdout
    #COMMCARE VARS
    #CCHQ_BUILD_NUMBER=-1
    #CCHQ_REVISION_NUMBER=-1
    #CCHQ_BUILD_DATE=''
    
    
    #make the archive    
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    archive_to_deploy = os.path.join('../../../','deploy-b%s-rev%s.tar.gz' % (build_number, revision_number))
    
    basedir = os.path.basename(os.path.abspath('../../'))    
    curdir = os.getcwd()
    os.chdir('../../../')
    make_archive(basedir,os.path.basename(archive_to_deploy))
    os.chdir(curdir)
    
    
    sys.stdout = os.fdopen(1, 'w', 0)    
    if debug:
        print 'DEBUG: Writing log to ssh-cmd.log'
        paramiko.util.log_to_file('ssh-cmd.log')
    
    ### Open SSH transport
    
    transport = paramiko.Transport((hostname, hostport))
    transport.connect(username=username, password=password)
    
    print "starting sftp session"
    sftp = paramiko.SFTPClient.from_transport(transport)
    basename = os.path.basename(archive_to_deploy)
    sftp.put(archive_to_deploy,target_abs_path + "/builds/" + basename)
    sftp.close()
    
    print "sftp file transferred, remoting in to deploy archive"    
    
    #print run(transport, 'cd %s' %(target_abs_path))
    print run(transport, 'rm -rf %s/%s' % (target_abs_path,target_deploy_path)) 
    print run(transport,'gunzip %s/%s' % (target_abs_path+"/builds",basename))
    print run(transport,'tar -xf %s/%s' % (target_abs_path+"/builds",basename[0:-3]))    
    
    print run(transport,'echo CCHQ_BUILD_DATE=\\"`date`\\" >> %s/projects/cchq_main/settings.py' % (basedir))
    print run(transport,'echo CCHQ_BUILD_NUMBER=%s >> %s/projects/cchq_main/settings.py' % (build_number,basedir))
    print run(transport,'echo CCHQ_REVISION_NUMBER=%s >> %s/projects/cchq_main/settings.py' % (revision_number,basedir))
    
    print run(transport,'touch %s/projects/cchq_main/media/version.txt' % (basedir))
    print run(transport,'echo CCHQ_BUILD_DATE=\\"`date`\\" >> %s/projects/cchq_main/media/version.txt' % (basedir))
    print run(transport,'echo CCHQ_BUILD_NUMBER=%s >> %s/projects/cchq_main/media/version.txt' % (build_number,basedir))
    print run(transport,'echo CCHQ_REVISION_NUMBER=%s >> %s/projects/cchq_main/media/version.txt' % (revision_number,basedir))
    
    
    print run(transport,'mkdir %s/projects/cchq_main/%s' % (basedir, 'xform-data'))
    print run(transport,'mkdir %s/projects/cchq_main/%s' % (basedir, 'schemas'))    
     
    print run(transport,'chmod 777 %s/projects/cchq_main/' % (basedir))
    print run(transport,'chmod -R 777 %s/projects/cchq_main/' % (basedir))
    print run(transport,'chmod 777 %s/projects/cchq_main/cchq.db' % (basedir))
    print run(transport,'rm -rf /var/django-sites/commcarehq-test')
{'fraction_non_alphanumeric': 0.1416903409090909, 'fraction_numerical': 0.005681818181818182, 'mean_word_length': 3.446842105263158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1352096', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2896, 'n_words': 614}