D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
imh-python
/
lib
/
python2.7
/
site-packages
/
cmu_ded
/
Libs
/
Filename :
cmuded.py
back
Copy
import os import json import common import cmumain import traceback # Local imports import NSetup import MySQLTransfer import WHMPluginTransfer import EasyApacheTransfer class CMUDed(cmumain.CMUMain): """ This is specifically for dedicated server moves. """ def __init__(self, dev_mode=False, logger=None): super(CMUDed, self).__init__(dev_mode=dev_mode, logger=logger) # Variables needed between execution. self.mysqltransfer = None self.dedi_ips = None self.php_transfered = None self.nameservers_transfered = None self.urls_checked = None self.plugin_checked = None def start_move(self): """ Void function that run's the whole move process. :return: Nothing. """ # cPanel license should be valid before we start. if self.pre_flight_check(): super(CMUDed, self).start_move() if self.mysqltransfer is None: try: self.mysqltransfer = str(self.migrate_mysql()) except: self.mysqltransfer = None self.error(traceback.format_exc()) pass self.dump() if self.dedi_ips is None: try: self.dedi_ips = self.grab_dedi_ips() except: self.dedi_ips = None self.error(traceback.format_exc()) pass self.dump() if self.php_transfered is None: try: self.php_transfered = str(self.migrate_apache()) except: self.php_transfered = None self.critical(traceback.format_exc()) pass self.dump() if self.nameservers_transfered is None: try: self.nameservers_transfered = str(self.migrate_nameservers()) except: self.nameservers_transfered = None self.critical(traceback.format_exc()) pass self.dump() if self.plugin_checked is None: try: self.plugin_checked = str(self.migrate_whm_plugins()) except: self.plugin_checked = None self.critical(traceback.format_exc()) pass self.dump() if self.urls_checked is None: try: self.urls_checked = self.check_urls() except: self.urls_checked = None self.critical(traceback.format_exc()) pass self.dump() self.str_content = self.ded_genstr( mysqltransfer=self.mysqltransfer, dedi_ips=self.dedi_ips, trans_php=self.php_transfered, custom_name=self.nameservers_transfered, url_check=self.urls_checked, whm_plugins=self.plugin_checked ) return True else: return False def dump(self): """ Overloaded funcion from cmumain that incorperates specific variables in v/ded to v/ded transfers. :return: Dumps variables into env.setup. """ json_data = { 'mysqltransfer': self.mysqltransfer, 'users': self.users, 'reseller_user': self.reseller_user, 'generated_backups': self.generated_backups, 'downloaded_backups': self.downloaded_backups, 'restored_backups': self.restored_backups, 'files_downloaded': self.files_downloaded, 'users_setup': self.users_setup, 'ssls_installed': self.ssls_installed, 'dedi_ips': self.dedi_ips, 'php_transfered': self.php_transfered, 'nameservers_transfered': self.nameservers_transfered, 'urls_checked': self.urls_checked, 'whm_plugins': self.plugin_checked } if os.path.exists(self.base_dir + "env.setup"): with open(self.base_dir + "env.setup", 'w') as file: json.dump(json_data, file, sort_keys=True, indent=4) def load(self): """ Overloaded function from cmumain that incorperates spceific variables in v/ded to v/ded transfers. :return: Loads the varables into the class. """ json_data = None if os.path.exists(self.base_dir + "env.setup"): with open(self.base_dir + "env.setup", 'r') as file: try: json_data = json.load(file) except ValueError: self.error("[!!] Doesn't look like cmu_ded was running. Please just run --start.") exit(0) if json_data is not None: self.users = json_data['users'] self.reseller_user = json_data['reseller_user'] self.generated_backups = json_data['generated_backups'] self.downloaded_backups = json_data['downloaded_backups'] self.restored_backups = json_data['restored_backups'] self.files_downloaded = json_data['files_downloaded'] self.users_setup = json_data['users_setup'] self.ssls_installed = json_data['ssls_installed'] self.mysqltransfer = json_data['mysqltransfer'] self.dedi_ips = json_data['dedi_ips'] self.php_transfered = json_data['php_transfered'] self.nameservers_transfered = json_data['nameservers_transfered'] self.urls_checked = json_data['urls_checked'] self.plugin_checked = json_data['whm_plugins'] # @TODO, should check to see if resuming or not. def pre_flight_check(self): """ Couple of checks before we go nuts on the move. Checks cPanel license, disk space and if they are spamming. :return: True if everything is kosher, false if not. """ return self.check_cpanel_license() and self.check_disk_space() and self.check_if_spamming() def check_cpanel_license(self): """ If you don't check the cPanel license then the whm api goes wonky. :return: True if everything is good. False if not. """ if not os.path.exists("/root/.accesshash"): self.error("Access hash does not exist. Attempting to run /usr/local/cpanel/bin/mkaccesshash") self.local_cmd_exec("/usr/local/cpanel/bin/mkaccesshash") response = cmumain.whm_api("run_cpkeyclt", version=1) if response is not None and response['metadata'] is not None and response['metadata']['result'] == 1: return True else: self.error("[!!] Update the cPanel License before starting!") return False def check_disk_space(self): """ Free_bytes is the free amount of KB on the pulling server. Used_bytes is the amount of KB used on the pushing server. If there is more used bytes than free bytes, then we can't transfer. :return: True if there is space, false if there is not. """ system_statvfs = os.statvfs('/') # Converting to KB cause df is a pain. free_bytes = system_statvfs.f_frsize * system_statvfs.f_bfree / 1024 used_bytes = self.ssh_connection.string_exec("df / | grep /").split()[2] if int(used_bytes) > free_bytes: self.error("[!!] The account won't fit on the new VPS.") return False else: return True def check_if_spamming(self): """ Part of Pre-Check, see's if the exim_mainlog is above 200 since that is what we do on shared. :return: True if everything is kosher. """ emails_in_queue = self.ssh_connection.string_exec("exim -bpc | wc -l") if int(emails_in_queue) > 200: self.error("[!!] It appears the customer is spamming. Please check the account before proceeding") return False else: return True def ded_genstr(self, **kwargs): """ Kwargs into kwargs is a bad idea. ( For overloading ) It ends up sending an empty list into the super's genstr :param kwargs: List of args for the string. :return: STR string specific to v/ded to v/ded """ return_string = self.str_content return_string += "[8] Errors in MySQL transfer: %s\n" % common.to_string(kwargs['mysqltransfer']) return_string += "[9] IP's that need to be transferred: %s\n" % common.to_string(kwargs['dedi_ips']) return_string += "[10] The status of the PHP transfer: %s\n" % kwargs['trans_php'] return_string += "[11] Were custom name servers transferred: %s\n" % kwargs['custom_name'] return_string += "[12] WHM Plugins Transfered: %s\n" % kwargs['whm_plugins'] return_string += "[13] URL's that mismatched: %s\n" % common.to_string(kwargs['url_check']) return_string += "Domains that failed are the following:\n" for domain in self.urls_checked.iterkeys(): ipaddr = self.urls_checked[domain][0].split(":") return_string += "https://%s/ with the IP address of %s and %s.\n" % \ (domain, ipaddr[0], ipaddr[1]) return return_string def grab_dedi_ips(self): """ Grabs the IPs on the other server that arn't the main IP. This will be sent in an email to STR@ so that they can move them manually. :return: List of IP's to users. """ self.ssh_connection.check_connection() self.info("[*] Entering Grab Dedi IP") user_to_ip = {} mainip = self.ssh_connection.string_exec('cat /var/cpanel/mainip') for user in self.users: ip = self.ssh_connection.string_exec('cat /var/cpanel/users/%s | grep IP' % user) user_ip = ip.split('=')[1] if mainip not in user_ip and '127.0.0.1' not in user_ip: user_to_ip[user] = user_ip return user_to_ip def migrate_apache(self): """ Function that migrated apache. :return: String for STR. """ self.info("Entering migrate_apache") self.ssh_connection.check_connection() ea = EasyApacheTransfer.EasyApacheTransfer(self.logger) # Passing variables. ea.ssh_connection = self.ssh_connection ea.from_server = self.from_server ea.to_server = self.to_server ea.dev_mode = self.dev_mode ea.check_ea4() result = ea.load() and \ ea.main() and \ ea.dump_file() and \ ea.install_profile() if result: return "EasyApache has been migrated" else: return "Errors in EasyApache Migration." def migrate_mysql(self): """ Function that runs the migration of MySQL. :return: String for str. """ self.info("Entering migrate_mysql") self.ssh_connection.check_connection() mysql = MySQLTransfer.MySQLTransfer(self.logger) # Passing variables. mysql.ssh_connection = self.ssh_connection mysql.from_server = self.from_server mysql.to_server = self.to_server mysql.dev_mode = self.dev_mode result = self.ssh_connection.string_exec('cat /etc/my.cnf') if mysql.load(result) and \ mysql.pre_flight_check() and \ mysql.convert() and \ mysql.dump_file('/etc/my.cnf') and \ mysql.check_configurations(): return "Setup MySQL" else: return "Errors in setting up MySQL" def migrate_nameservers(self): """ Function that migrates the nameservers :return: String for str. """ self.info("Entering migrate_mysql") self.ssh_connection.check_connection() ns = NSetup.NSetup(self.logger) # Passing variables. ns.ssh_connection = self.ssh_connection ns.from_server = self.from_server ns.to_server = self.to_server ns.dev_mode = self.dev_mode if ns.check_nameservers() and ns.install_nameservers(): return "Custom name servers have been setup." else: return "Errors in migrating nameservers." def migrate_whm_plugins(self): """ Function for migrating WHM plugins. :return: String for str. """ self.info("Entering migrate_whm_plugins") self.ssh_connection.check_connection() whm = WHMPluginTransfer.WHMPluginTransfer(self.logger) # Passing variables. whm.ssh_connection = self.ssh_connection whm.from_server = self.from_server whm.to_server = self.to_server whm.dev_mode = self.dev_mode if whm.main(): self.debug("Plugin Special Modules are %s" % whm.special_modules) return "Transfered WHM Plugins" else: return "Errors in Transfering WHM Plugins"