#!/usr/bin/env python 
import __future__

import io
import os
import datetime
import time
import subprocess
from pwd import getpwnam
from tarfile import is_tarfile
import requests
from base64 import urlsafe_b64encode as base64
import json

from ftplib import FTP

from paramiko.client import AutoAddPolicy, SSHClient
from paramiko.sftp_client import SFTPClient

from paramiko.ssh_exception import SSHException

import ftplib

import logging
import argparse

from selenium import webdriver

def compare_sites(domain, local_ip):
    driver = webdriver.PhantomJS('./phantomjs') 
    driver.set_window_size(1024, 768)
    driver.get("http://" + domain)
    driver.implicitly_wait(5)
    remote_version = driver.get_screenshot_as_base64()
    driver.quit()

    with open("/etc/hosts", 'a') as f: 
        f.write(local_ip + " www." + domain + " " + domain + "\n")

    driver = webdriver.PhantomJS('./phantomjs')
    driver.set_window_size(1024, 768)
    driver.get("http://" + domain)
    driver.implicitly_wait(5)
    local_version = driver.get_screenshot_as_base64()
    driver.quit() 

    with open("/etc/hosts", "r+") as f: 
        lines = f.readlines()
        f.seek(0)
        for line in lines: 
            if domain not in line: 
                f.write(line)
        f.truncate()
    if local_version == remote_version: 
        return True
    else: 
        return False 

class cPanel:

    def __init__(self, host, user, passwd, version=1): 
        self.url = "https://%s:2083/json-api/cpanel" % host
        requests.packages.urllib3.disable_warnings()
        self.auth = { 'Authorization' : 'Basic ' + base64( user + ':' + passwd) } 
        self.payload = { 'api.version' : version, 'cpanel_jsonapi_user' : user }  

    def execute(self, module, func, cp_version=2, **kwargs):
        payload = self.payload.copy()
        payload.update({ 'cpanel_jsonapi_version' : cp_version, 'cpanel_jsonapi_module': module,  'cpanel_jsonapi_func' : func})
        payload.update({ item:kwargs[item] for item in kwargs })  
        res = requests.post(self.url, verify=False, headers=self.auth, data=payload)
        
        if cp_version == 2: 
            # convert version 2 to json 
            res = res.json()
            # report an error
            if u'reason' in res[u'cpanelresult']:
                raise Exception("cPanel API Failure: %s" % res[u'cpanelresult'][u'reason']) 
            elif  u'error' in res[u'cpanelresult']:
                raise Exception("cPanel API Failure: %s" % res[u'cpanelresult'][u'error']) 
        
        return res

class FileTransfer(FTP):
    
    def put_file(self, filename): 
        with open(filename, 'rb') as ofile: 
            self.storbinary('STOR ' + filename, ofile) 

    def get_file(self, filename): 
        self.retrbinary('RETR ' + filename, open(filename, 'wb').write) 

    def write_to_file(self, filename, string): 
        fp = io.BytesIO(str.encode(string))
        self.storbinary('STOR ' + filename, fp)

class Log:
    
    def __init__(self, log_location): 
        self.logger = logging.getLogger('logger')
        self.logger.setLevel(logging.INFO)

        self.handler = logging.FileHandler(log_location)
        self.console = logging.StreamHandler()
        formatter = logging.Formatter('[%(asctime)s] %(levelname)s : %(message)s') 
        self.handler.setFormatter(formatter)
        self.console.setFormatter(formatter)
        self.logger.addHandler(self.console)
        self.logger.addHandler(self.handler)

    def close(self):
        self.logger.removeHandler(self.console)
        self.logger.removeHandler(self.handler)
        self.handler.close()

class MigrationError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class Migration:

    def __init__(self, host, remote_user, local_user, password, ticket_id, ssh_port=2222, ftp_port=21):
        self.ticket = ticket_id
        self.luser = local_user
        self.ruser = remote_user
        self.password = password
        self.host = host

        self.working_dir = "/home1/hgtransfer/" + self.ticket 
        try: 
            os.makedirs(self.working_dir)
        except OSError: 
            print("[!] Working directory already exists... " + self.working_dir + ".... Moving") 
            try: 
                time = datetime.datetime.utcnow().strftime("%s")
                os.rename(self.working_dir, self.working_dir + ".bak-" + time )
            except OSError: 
                print("[!] Failed to move working dirctory. Exiting") 
                raise MigrationError("Failed to move the working dir")
            os.makedirs(self.working_dir)

        self.log_path = self.working_dir + "/log/"
        try: 
            os.makedirs(self.log_path)
        except OSError:
            print("[!] Log path already exists... " + self.log_path)
            print("[!] This should already be moved. Exiting")
            raise MigrationError("Path should already have been moved")

        self.log = Log(self.log_path + "account.log")
        logger = self.log.logger

        logger.warn("This Tool does not check if any content should be saved first")
        logger.warn("If an account exists is will be backed up prior to migration")
        logger.warn("This is your final warning.")

	self.pre_transfer = self.working_dir + "/pre-transfer/"
        logger.info("Creating pre-transfer directory: " + self.pre_transfer)
        try: 
            os.makedirs(self.pre_transfer)
        except OSError:
            logger.error("Pre-transfer directory still exists")
            logger.error("Something has gone wrong as this should already be moved out of the way")
            logger.error("Exiting")
            self.log.close()
            raise MigrationError("Path Should have already been moved")
        
        if self.luser != self.ruser: 
            try: 
                getpwnam(self.ruser)
                logger.error('User already exists. Failing')
                self.log.close()
                raise MigrationError("User already exists")
            except KeyError: 
                logger.info('Remote user does not exist') 
                pass

        self.source_dir = self.working_dir + "/source/"
        logger.info("Creating source directory: " + self.source_dir)
        try: 
            os.makedirs(self.source_dir)
        except OSError:
            logger.error("Source directory still exists")
            logger.error("Something has gone wrong as this should already be moved out of the way")
            logger.error("Exiting")
            self.log.close()
            raise MigrationError("Path should already have been moved")

        logger.info("Gathering user data for: %s" % self.luser)
        self.user = self.__check_for_user_data()

        logger.info("Checking for remote cPanel access")
        self.cPanel = self.__check_for_remote_cpanel()

	logger.info("Checking if this account should be migrated")
	self.remote_size = self.__get_remote_size()

        logger.info("Checking for ssh access")
        self.ssh, self.sport = self.__check_for_ssh_access(ssh_port)

        logger.info("Checking for ftp access")
        self.ftp = self.__check_for_ftp_access(ftp_port)

        if self.cPanel == None and self.ssh == None: 
            logger.error("Unable to create a cpmove automatically. Proceed with Migration Manually") 
            self.log.close()
            raise MigrationError("Cannot get cpmove")
	
    def __get_remote_size(self):
	logger = self.log.logger
	total = 0 
        try: 
            diskusage = self.cPanel.execute('DiskUsage', 'fetchdiskusagewithextras')['cpanelresult']['data']
            total = diskusage[0]['homedir']['user_contained_usage']
        except Exception: 
            logger.error("Failed to get the disk usage")

        homes = ['/home/']
        if os.stat('/home1'): 
            homes = ['/home1/', '/home2/', '/home3/', '/home4/']
        for home in homes: 
            try: 
                stats = os.statvfs(home) 
                remaining_on_home = stats.f_frsize * stats.f_bavail 
                if int((stats.f_frsize * stats.f_bavail * 100) / (stats.f_frsize * stats.f_blocks)) < 10: 
                    logger.error('Remaining disk space is less than 10% on %s' % home)
                if total == 0: 
                    logger.warn("Unable to determine the remote disk space size")

                if int(remaining_on_home < total):
                    logger.error('Not enough disk space to migrate')
                else: 
                    return total
            except OSError as e: 
                print(e)
                continue 
        self.log.close()
        raise MigrationError('Unexpected condition while gathering disk informaiton')

    def __check_for_user_data(self): 
        logger = self.log.logger
        user = {'user' : self.luser} 

        logger.info("Checking for users file")
        if os.path.isfile('/var/cpanel/users/' + self.luser):
            user.update({'exists' : True})
            logger.info("User file found. Gathering data....")
            # At some point we may want to switch to using a 
            # more robust tool, like `mi`
            with open('/var/cpanel/users/' + self.luser) as f: 
                for line in f: 
                    if '=' in line: 
                        var, val = line.split('=', 2) 
                        if var == 'OWNER':
                            user.update({'owner':val.strip()})
                        if var == 'DNS':
                            user.update({'u-domain':val.strip()})
                        if var == 'PLAN':
                            user.update({'plan':val.strip()})
                        if var == 'IP':
                            user.update({'ip':val.strip()})

            if user['owner'] == 'root': 
                user.update({'acct':'Shared'})
            elif user['owner'] == user['user']:
                user.update({'acct':'Reseller'}) 
            else:
                user.update({'acct':'Resold'})
        else: 
            logger.info("Failed to locate user file. Proceeding as new account...")
            user.update({'exists':False})

        return user 

    def __check_for_remote_cpanel(self):
        logger = self.log.logger
        cpanel = cPanel(self.host, self.ruser, self.password)
        try: 
            cpanel.execute('Cron', 'listcron')
            logger.info("Successfully connected via cPanel API")
            return cpanel 
        except Exception: 
            logger.error("Failed to execute a remote cPanel function")
            return None

    def __check_for_ssh_access(self, port): 
        logger = self.log.logger
        ssh = None
        sport = port
        try: 
            ssh = SSHClient()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(self.host, port=port, username=self.ruser, password=self.password)
            stdin, stdout, stderr = ssh.exec_command('echo test')
            if 'test' not in stdout.readline():
                return None, sport
            logger.info("Successfully connected via SSH")
        except SSHException:
            logger.warn("Provided port, %s, did not work. Trying port 22" % port)
            try:
                ssh = SSHClient()
                ssh.set_missing_host_key_policy(AutoAddPolicy())
                ssh.connect(self.host, port=22, username=self.ruser, password=self.password)
                stdin, stdout, stderr = ssh.exec_command('echo test')
                if 'test' not in stdin:
                    return None, sport
                logger.info("Successfully connected via SSH")
                sport = 22
            except:
                logger.error("Failed to connect via SSH")
                return None, sport
        #except: 
            #logger.error("SSH Timeout") 
            #return None, sport
        return ssh, sport
    
    def __check_for_ftp_access(self, port): 
        logger = self.log.logger
        try: 
            ftp = FileTransfer(self.host, self.ruser, self.password)
            logger.info("Successfully connected via FTP")
            return ftp 
        except ftplib.all_errors:
            logger.error("Failed to connect via FTP")
            return None

    def create_and_transfer_account(self):
        logger = self.log.logger
        logger.info("Starting remote account backup and transfer")

        remote_home_dir = "/home/" + self.ruser

        uniq = datetime.datetime.utcnow().strftime('%s')
        backup_dir = "xfer." + uniq
        full_backup_dir = remote_home_dir + "/" + backup_dir

        logger.info("Remote backup being saved to: %s" % backup_dir)

        remote_pass = "REMOTE_PASSWORD='%s'" % self.password
        io_redirect = "&>%s/log" % full_backup_dir
        pkgacct = "/usr/local/cpanel/scripts/pkgacct --skiphomedir %s %s" % (self.ruser, full_backup_dir) 

        command = "%s %s %s" % (remote_pass, pkgacct, io_redirect) 
        logger.info("Remotely Executing: %s" % command) 

        if self.ssh: 
            logger.info("Using SSH to run the backup")
            with open(self.log_path + "ssh.log", "a") as f: 
                try: 
                    stdin, stdout, stderr = self.ssh.exec_command('mkdir -p %s' % full_backup_dir)
                    f.write(stdout.read())
                    f.write(stderr.read())
                except Exception: 
                    logger.error("Failed to create the remote dir: %s" % full_backup_dir)
                    self.log.close()
                    raise MigrationError("Failed to create the remote dir: %s" % full_backup_dir)
                try: 
                    stdin, stdout, stderr = self.ssh.exec_command(command) 
                    logger.info("Executing %s" % command)
                    f.write(stdout.read())
                    f.write(stderr.read())
                except Exception: 
                    logger.error("Failed to execute the remote command: %s" % command)
                    self.log.close()
                    raise MigrationError("Failed to execute the remote command: %s" % command)
                try: 
                    sftp = self.ssh.open_sftp()
                    cpmove = 'cpmove-' + self.ruser + '.tar.gz'
                    sftp.get(full_backup_dir + "/" + cpmove, self.source_dir + "/" + cpmove)
                    sftp.get(full_backup_dir + "/log", self.source_dir + "/log")
                except Exception: 
                    logger.error("Failed to copy the remote source")
                    self.log.close()
                    raise MigrationError("Failed to copy the remote source")
                try:  
                    sftp.remove(full_backup_dir + "/" + cpmove)
                    sftp.remove(full_backup_dir + "/log")

                    sftp.rmdir(full_backup_dir) 
                except Exception:
                    logger.error("Failed to remove the backup")
                    self.log.close()
                    raise MigrationError("Failed to remove the backup")

            self.ssh.close() # We are going to use sftp over lftp for now

        else: 
            logger.info("No SSH, using cPanel API and Cron")
            logger.info("Attempting to create backup dir") 
            try: 
                output = self.cPanel.execute('Fileman', 'mkdir', path='', name=backup_dir) 
                logger.info("Sucessfully created Backup dir") 
            except Exception:
                logger.error("Failed to create Backup dir")
                logger.error("Something is wrong with the cPanel API, Migrate Manually")
                self.log.close()
                raise MigrationError("Something is wrong with the cPanel API, Migrate Manually")
            
            logger.info("Attempting to create Cron Job")
            try: 
                self.cPanel.execute('Cron', 'add_line', weekday='*', minute='*/5', hour='*', day='*', month='*', command=command)
                logger.info("Cron created Sucessfully")
            except: 
                logger.error("Failed to create cron")
                logger.error("Something is wrong with the cPanel API, Migrate Manually")
                self.log.close
                raise MigrationError("Something is wrong with the cPanel API, Migrate Manually")

            os.chdir(self.source_dir)
            self.ftp.cwd(backup_dir)

            cpmove = 'cpmove-' + self.ruser + '.tar.gz'
            completed = False 

            for i in range(0,120,1): # We need a time out cycle of once every minute and then at 2 hours fail
                time.sleep(60)
                try: 
                    # the goal here is to get the cPmove file
                    # however we have no easy way to determine if it has completed. 
                    # instead we'll try to download the tar, if we can and it is a tar
                    # we will move on, if we can't we'll wait
                    self.ftp.get_file(cpmove) 
                    if is_tarfile(cpmove):
                        self.ftp.get_file('log')
                        completed = True
                        break
                    os.remove(cpmove)
                except ftplib.all_errors:
                        logger.info("Backup not ready yet. Waiting...")

            if not completed:
                logger.error("Waited for two hours to complete cpmove")
                logger.error("Something occured on the remote server, or the backup is too large")
                logger.error("Migrate Manually")
                self.log.close()
                raise MigrationError("Waited for two hours to complete cpmove")

            logger.info("Cleaning up the remote content") 
            try: 
                crons = self.cPanel.execute('Cron', 'fetchcron')['cpanelresult']['data']
                for cron in crons:
                    if 'command' in cron and backup_dir in cron['command']:
                        try: 
                            self.cPanel.execute('Cron', 'remove_line', line=cron['commandnumber'])
                        except Exception: 
                            logger.error("Could not remove the cron a the provided line: %s" % cron['commandnumber'])
                            logger.error("This will not cause the migraiton to fail, but will leave the backup cron in place")
            except Exception: 
                logger.error("Could not fetch a list of the remote crons")
                logger.error("This will not cause the migraiton to fail, but will leave the backup cron in place")
            
            logger.info("Deleting remote files")
            try: 
                self.ftp.delete(cpmove)
                self.ftp.delete('log')
                self.ftp.rmd(self.ftp.pwd())
                self.ftp.close()
            except ftplib.all_errors:
                logger.error("Failed to correctly delete all generated content")
                logger.error("This will not cause the migration to fail, but artifacts will exist post migraiton")

            try: 
                os.stat(self.source_dir + "/" + cpmove)
            except OSError: 
                logger.error("There is a problem with the cpmove file. You will need to proceed with the Migration Manually")
                self.log.close()
                raise MigrationError("There is a problem with the cpmove file. You will need to proceed with the Migration Manually")
        
        logger.info("Starting account restore process")
        if self.user['exists']: 
            #os.chdir(self.source_dir)
            # We'll eventually want to be able to make a user name change
            '''
            with open(self.log_path + "/pre-transfer.user-change.log") as f:
                try:
                    subprocess.check_call(['/root/bin/cpchuser', self.ruser, self.luser], stdout=f, stderr=f)
                except subprocess.CalledProcessError:
                    logger.error('cpchuser failure. Check log: %s' % (self.log_path + "/pre-transfer.user-change.log")
                    exit(1)
            '''
            logger.info('Account already exists. Backing up')
            with open(self.log_path + "pre-transfer-backup.log", "a") as f:
                try: 
                    subprocess.check_call(['/scripts/pkgacct', self.luser, self.pre_transfer], stdout=f, stderr=f) 
                except subprocess.CalledProcessError: 
                    logger.error('Failed to package the account. It may not be safe to continue!')
                    logger.error('Check output in %s' % (self.log_path + "pre-transfer.backup.log"))
                    self.log.close()
                    raise MigrationError('Failed to package the account. It may not be safe to continue!')
            logger.info('Account backup completed')
            logger.info('Removing old account')
            with open(self.log_path + "pre-transfer-removal.log", "a") as f:
                try: 
                    subprocess.check_call(['/scripts/removeacct', '--force', self.luser], stdout=f, stderr=f)
                except subprocess.CalledProcessError:
                    logger.error('Account Removal Failure, unable to proceed. Complete Migration Manually')
                    self.log.close()
                    raise MigrationError('Account Removal Failure, unable to proceed. Complete Migration Manually')
            logger.info('Account removed')

        logger.info('Restoring account')
        with open(self.log_path + "pre-transfer-restorepkg.log", "a") as f: 
            try: 
                subprocess.check_call(['/scripts/restorepkg', self.source_dir + "/cpmove-" + self.ruser + '.tar.gz'], stdout=f, stderr=f)
            except subprocess.CalledProcessError:
                logger.error('Account restore Failure. Attempting to revert and exit')
                with open(self.log_path + "post-transfer-failure.log", "a") as d: 
                    try: 
                        subprocess.check_call(['/scripts/restorepkg', self.pre_transfer + "/cpmove-" + self.luser + '.tar.gz'], stdout=d, stderr=d)
                        logger.info('Old account restored')
                        self.log.close()
                        raise MigrationError('Old account restored')
                    except subprocess.CalledProcessError:
                        logger.error('Either the previous account did not exist or we are otherwise unable to restore it. Exiting')
                        self.log.close()
                        raise MigrationError('Either the previous account did not exist or we are otherwise unable to restore it. Exiting')
        logger.info('Account data successfully migrated. Files need to be migrated still')

    def transfer_files(self):
        logger = self.log.logger
	host = self.host
	if self.ssh:
            host = "sftp://" + self.host + ":" + str(self.sport)
        with open(self.log_path + "file-transfer.log", "a") as f: 
            logger.info("Starting file transfer")
            root_id = getpwnam('root')
            user_id = getpwnam(self.ruser)
            try: 
                path = "/home/" + self.ruser + "/" 
                user_pass = self.ruser + ',' + self.password
                mirror = 'set cmd:move-background-detach false ; set xfer:log true ; set ftp:list-options -a ; set ftp:ssl-allow no ;'
                mirror = mirror + 'set net:connection-limit 1 ; set net:limit-total-rate 3276800 ; set cache:enable false;'
                mirror = mirror + 'mirror -c --allow-suid --allow-chown . ' + path + '; exit'
                try: 
                    subprocess.check_call(['lftp', '-u', user_pass, '-e', mirror, host], stdout=f, stderr=f)
                except subprocess.CalledProcessError:
                    logger.error("Not all files appear to have moved over. Manually migraiton may be needed")
                    with open(self.log_path + "file-transfer.log") as d:
                        for line in d: 
                            logger.error(line) 
            except KeyError:
                logger.error("The remote user hasn't been restored to this account.")
                self.log.close()
                raise MigrationError("The remote user hasn't been restored to this account.")
            logger.info("File Transfer complete")
            logger.info("Correcting file ownership")
            for root, dirs, files in os.walk('/home/' + self.ruser + '/'):
                for f in files + dirs: 
                    gid = -1
                    uid = -1 
                    fl = root + "/" + f
                    try: 
                        f_stat = os.stat(fl)
                        if f_stat.st_uid == root_id.pw_uid: 
                            uid = user_id.pw_uid 
                        if f_stat.st_gid == root_id.pw_gid: 
                            gid = user_id.pw_gid 
                        os.chown(fl, uid, gid) 
                    except OSError:
                        logger.error("File not found %s" % fl)
            logger.info("Ownership fixed")
                     
    def complete(self): 
        logger = self.log.logger
        logger.info("Attempting to set previous account values")
        with open(self.log_path + "cppc.log", "a") as f:
            try:
                subprocess.check_call(['/root/bin/cppc', '--setowner', self.user['owner'], '--setpkg', self.user['plan'], self.ruser], stdout=f, stderr=f)
                logger.info("Previous account values set")
            except subprocess.CalledProcessError:
                logger.error("Failed to correctly set the previous account values")
            except KeyError:
                logger.info("No previous account information")

        with open(self.log_path + "chgacctip.log", "a") as f: 
            try: 
            	subprocess.check_call(['/root/bin/chgacctip', self.ruser, self.user['ip'], 'y'], stdout=f, stderr=f)
            except subprocess.CalledProcessError: 
                logger.error("Failed to correctly set the IP for the account")
            except KeyError:
                logger.info("No previous account information")

	os.chdir("/home/hgtransfer/xfertools/")
        logger.info("Comparing sites")
        with open('/var/cpanel/users/' + self.ruser) as f: 
                ip = ''
                domain = ''
                for line in f: 
                    if 'IP' in line: 
                        ip = line.split('=', 2)[1].strip()
                f.seek(0)
                for line in f: 
                    if 'DNS' in line: 
                        domain = line.split('=', 2)[1].strip() 
			if compare_sites(domain, ip):
                            logger.info("Remote and local versions of %s MATCH" % domain)
                        else: 
                            logger.error("Remote and local versions of %s DON'T MATCH" % domain)
                            logger.error("Site will need to be verified manually")

        logger.info("Migration complete")
        self.log.close()

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("luser", help="Local Username") 
    parser.add_argument("host", help="Remote Domain")
    parser.add_argument("ruser", help="Remote Username")
    parser.add_argument("password", help="Remote User's Password")
    parser.add_argument("ticket", help="Ticket ID of the transfer")
    args = parser.parse_args()

    migration = Migration(args.host, args.ruser, args.luser, args.password, args.ticket)
    migration.create_and_transfer_account()
    migration.transfer_files()
    migration.complete()

if __name__ == "__main__":
    main()           

