def mysql_dump_auto(config, magento_root_path, backup_path, menu_return): x = datetime.datetime.now() current_date = x.strftime("%d%B%y") # Check if backups directory exists locally if not os.path.exists(backup_path): action = "Create Backup Folder" shell.run_bash_command(config, magento_root_path, action, "mkdir -p " + backup_path, "Backup Directory Created") if os.path.exists(backup_path + "/" + current_date + "_" + config["db"]["connection"]["default"]["dbname"] + ".sql"): os.remove(backup_path + "/" + current_date + "_" + config["db"]["connection"]["default"]["dbname"] + ".sql") action = "Database Backup" shell.run_bash_command_popen( config, magento_root_path, action, "mysqldump --no-tablespaces --skip-lock-tables --opt --single-transaction --max_allowed_packet=512M -h mysql -u " + config["db"]["connection"]["default"]["username"] + " -p" + config["db"]["connection"]["default"]["password"] + " " + config["db"]["connection"]["default"]["dbname"] + " > " + backup_path + "/" + current_date + "_" + config["db"]["connection"]["default"]["dbname"] + ".sql", 1) print(Colors.FG.LightGreen + Colors.Bold + "MySQL Database Backed up to: " + backup_path + "/" + current_date + "_" + config["db"]["connection"]["default"]["dbname"] + ".sql" + Colors.Reset) if menu_return == 1: menu.mysql_menu(config, magento_root_path)
def install_mmcron(config, path, menu_return): action = "Install MageMojo Cron Module" print(Colors.FG.LightGreen + "Adding magemojo/m2-me-cron to Composer") shell.run_bash_command( config, path, action, "cd " + path + " && composer require magemojo/m2-ce-cron", "Installed MageMojo Cron With Composer") print(Colors.FG.LightGreen + "Enabling Module in Magento.") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento module:enable MageMojo_Cron", "Enabled MageMojo_Cron in Magento 2") print(Colors.FG.LightGreen + "Running Magento setup:upgrade") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento setup:upgrade", "Ran Magento 2 setup:upgrade.") print(Colors.FG.LightGreen + "Starting Magento Compile") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento setup:di:compile", "Magento 2 Compile Complete") print(Colors.FG.LightGreen + "Starting Static Content Deploy") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento setup:static-content:deploy -f", "Magento 2 Static Content Deploy Complete") print(Colors.FG.LightGreen + Colors.Bold + action + " Completed." + Colors.Reset) time.sleep(1.5) if menu_return == 1: menu.cron_menu(config, path)
def clear_redis(config, path, menu_return): action = "Clear Redis Cache" print(Colors.FG.LightGreen + "Clearing Redis Cache" + Colors.Reset) shell.run_bash_command(config, path, action, "redis-cli -h redis flushall", "Cleared Redis Cache") if menu_return == 1: menu.cache_menu(config, path)
def config_varnish(config, path, menu_return): action = "Configure Varnish" shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set system/full_page_cache/caching_application 2", ".") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set system/full_page_cache/varnish/access_list localhost", "..") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set system/full_page_cache/varnish/backend_host nginx", "...") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set system/full_page_cache/varnish/backend_port 8080", "....") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento setup:config:set --http-cache-hosts=varnish", ".....") print(Colors.FG.LightGreen + Colors.Bold + action + " Completed!" + Colors.Reset) if menu_return == 1: menu.varnish_menu(config, path)
def clear_cloudfront(config, path, menu_return): action = "Clear CloudFront Cache" print(Colors.FG.LightGreen + "Clearing CloudFront Cache" + Colors.Reset) shell.run_bash_command(config, path, action, "/usr/share/stratus/cli cache.all.clear", "Cleared CloudFront Cache") if menu_return == 1: menu.cache_menu(config, path)
def purge_varnish(config, path, menu_return): action = "Purge Varnish" shell.run_bash_command( config, path, action, "curl -X 'PURGE' -H'X-Magento-Tags-Pattern: .*' varnish", "...") print(Colors.FG.LightGreen + Colors.Bold + action + " Completed!" + Colors.Reset) if menu_return == 1: menu.varnish_menu(config, path)
def backup_magento_no_media(config, magento_root_path, backup_path, menu_return): action = "Magento Root Backup" x = datetime.datetime.now() current_date = x.strftime("%d%B%y") print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) if not os.path.exists(backup_path): action = "Create Backup Folder" shell.run_bash_command(config, magento_root_path, action, "mkdir -p " + backup_path, "Backup Directory Created") shell.run_bash_command_popen(config, magento_root_path, action, "tar --exclude=" + magento_root_path + "/var/* --exclude=" + magento_root_path + "/pub/media/* -zcf " + backup_path + "/backup_" + current_date + ".tar.gz " + magento_root_path, 1) if menu_return == 1: menu.magento_menu(config, magento_root_path)
def reinit_as(config, path, menu_return): action = "Reinitialize Autoscaling" print(Colors.FG.LightGreen + "Reinitializing Autoscaling" + Colors.Reset) shell.run_bash_command(config, path, action, "/usr/share/stratus/cli zerodowntime.init", "New Autoscale Pods Deployed") shell.run_bash_command(config, path, action, "/usr/share/stratus/cli zerodowntime.switch", "Autoscaling Pods Switched") if menu_return == 1: menu.cache_menu(config, path)
def clear_magento(config, path, menu_return): action = "Clear Magento Cache" print(Colors.FG.LightGreen + "Clearing Magento Cache" + Colors.Reset) shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento cache:flush", "Magento Cache Flush Complete") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento cache:clean", "Magento Cache Clean Complete") if menu_return == 1: menu.cache_menu(config, path)
def mysql_dump_manual(config, magento_root_path, menu_return): x = datetime.datetime.now() current_date = x.strftime("%d%B%y") backup_path = input(Colors.FG.Yellow + "Path to dump database to? Default: /srv/backups: " + Colors.Reset) if backup_path == "": backup_path = "/srv/backups" elif backup_path.endswith("/"): path_length = len(backup_path) backup_path = backup_path[:path_length - 1] db_name = input(Colors.FG.Yellow + "MySQL Database: " + Colors.Reset) db_username = input(Colors.FG.Yellow + "MySQL Username: "******"MySQL Password: "******"Create Backup Folder" shell.run_bash_command(config, magento_root_path, action, "mkdir -p " + backup_path, "Backup Directory Created") if os.path.exists(backup_path + "/" + current_date + "_" + db_name + ".sql"): os.remove(backup_path + "/" + current_date + "_" + db_name + ".sql") action = "Database Backup" shell.run_bash_command_popen( config, magento_root_path, action, "mysqldump --no-tablespaces --skip-lock-tables --opt --single-transaction --max_allowed_packet=512M -h mysql -u " + db_username + " -p" + db_password + " " + db_name + " > " + backup_path + "/" + current_date + "_" + db_name + ".sql", 1) print(Colors.FG.LightGreen + Colors.Bold + "MySQL Database Backed up to: " + backup_path + "/" + current_date + "_" + db_name + ".sql" + Colors.Reset) if menu_return == 1: menu.mysql_menu(config, magento_root_path)
def clear_all(config, path, menu_return): action = "Clear All Caches" print(Colors.FG.LightGreen + "Clearing All Caches" + Colors.Reset) shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento cache:flush", "Magento Cache Flush Complete") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento cache:clean", "Magento Cache Clean Complete") shell.run_bash_command(config, path, action, "redis-cli -h redis flushall", "Cleared Redis Cache") shell.run_bash_command(config, path, action, "/usr/share/stratus/cli cache.all.clear", "Cleared CloudFront Cache") if menu_return == 1: menu.cache_menu(config, path)
def reset_crons(config, path, menu_return): action = "Reset Crons" shell.run_bash_command(config, path, "Stop Crons", "/usr/share/stratus/cli crons.stop", "Stopped Cron Jobs") shell.run_bash_command_popen( config, path, "Delete Cron Schedule from Database", "mysql -h mysql -u " + config["db"]["connection"]["default"]["username"] + " -p\"" + config["db"]["connection"]["default"]["password"] + "\" " + config["db"]["connection"]["default"]["dbname"] + " -e 'delete from " + config["db"]["table_prefix"] + "cron_schedule'", 1) shell.run_bash_command(config, path, action, "rm -rf " + path + "/var/cron/*", "Cleared " + path + "/var/cron/") shell.run_bash_command(config, path, action, "/usr/share/stratus/cli crons.start", "Started Cron Jobs") print(Colors.FG.LightGreen + Colors.Bold + action + " Completed." + Colors.Reset) time.sleep(1.5) if menu_return == 1: menu.cron_menu(config, path)
def set_index_to_schedule(config, path): action = "Set Indexes to Run on Schedule" print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento indexer:set-mode schedule", action + " Completed")
def static_content_deploy(config, path): action = "Deploy Static Content" print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento setup:static-content:deploy -f", action + " Completed")
def magento_setup_upgrade(config, path): action = "Magento Setup Upgrade Database" print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento setup:upgrade", action + " Completed")
def clear_all_reinit(config, path, menu_return): action = "Clear All Caches and Reinitialize Autoscaling" print(Colors.FG.LightGreen + "Clearing All Caches and Reinitializing Autoscaling" + Colors.Reset) shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento cache:flush", "Magento Cache Flush Complete") shell.run_bash_command( config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento cache:clean", "Magento Cache Clean Complete") shell.run_bash_command(config, path, action, "redis-cli -h redis flushall", "Cleared Redis Cache") shell.run_bash_command(config, path, action, "/usr/share/stratus/cli cache.all.clear", "Cleared CloudFront Cache") shell.run_bash_command(config, path, action, "/usr/share/stratus/cli zerodowntime.init", "New Autoscale Pods Deployed") shell.run_bash_command(config, path, action, "/usr/share/stratus/cli zerodowntime.switch", "Autoscaling Pods Switched") if menu_return == 1: menu.cache_menu(config, path)
def rsync_production_files(settings_dict): action = "RSYNC Prod Files to Dev." # remove the root path on dev instance if os.path.exists(settings_dict["prod_public_html"]): shell.run_bash_command_popen(False, False, action, "rm -rf " + settings_dict["prod_public_html"], "") print(Colors.FG.LightGreen + Colors.Bold + "Starting " + action + Colors.Reset) if settings_dict["root_is_symlink"] is True: # remove last portion of link path for rsync target new = settings_dict["root_symlink"]["link_path"].split("/") for l in new: if l == "": new.remove(l) new_path = "/" list_length = len(new) - 1 for l in range(list_length): new_path += new[l] + "/" if not os.path.exists(new_path): child = subprocess.Popen("mkdir -p " + new_path, shell=True, stdout=subprocess.PIPE) streamdata = child.communicate()[0] rc = child.returncode # rsync link path to dev from production shell.run_bash_command_popen(False, False, action, "rsync -Pavl -e 'ssh -p " + settings_dict["prod_ssh_port"] + " -i " + settings_dict[ "prod_ssh_privkey_path"] + "' " + settings_dict["prod_ssh_user"] + "@" + settings_dict["prod_ssh_host"] + ":" + settings_dict["root_symlink"]["link_path"] + " " + new_path, ".") # recreate symlink shell.run_bash_command(False, False, action, "ln -s " + settings_dict["root_symlink"]["link_path"] + " " + settings_dict["prod_public_html"], "..") else: # remove last portion of root path for rsync target new = settings_dict["prod_public_html"].split("/") for l in new: if l == "": new.remove(l) new_path = "/" list_length = len(new) - 1 for l in range(list_length): new_path += new[l] + "/" if not os.path.exists(new_path): child = subprocess.Popen("mkdir -p " + new_path, shell=True, stdout=subprocess.PIPE) streamdata = child.communicate()[0] rc = child.returncode # rsync root from production to dev using rsync target shell.run_bash_command_popen(False, False, action, "rsync -Pavl -e 'ssh -p " + settings_dict["prod_ssh_port"] + " -i " + settings_dict[ "prod_ssh_privkey_path"] + "' " + settings_dict["prod_ssh_user"] + "@" + settings_dict["prod_ssh_host"] + ":" + settings_dict["prod_public_html"] + " " + new_path, ".") # print second set of . to keep visual progress. print(Colors.FG.LightGreen + Colors.Bold + ".." + Colors.Reset) # check if we have folder inside root that are symlinks if settings_dict["symlink_folders"] is not False: count = 3 folder_list = list(settings_dict["symlink_folders"]) for f in folder_list: # remove folder on dev if exists if os.path.exists(f): shell.run_bash_command_popen(False, False, action, "rm -rf " + f, "") # remove last folder of path ex "/srv/public_html/var" to "/srv/public_html" new = f.split("/") for l in new: if l == "": new.remove(l) new_path = "/" list_length = len(new) - 1 for p in range(list_length): new_path += new[p] + "/" success_message = "." * count # create directory if it doesn't exist if not os.path.exists(new_path): shell.run_bash_command_popen(False, False, action, "mkdir -p " + new_path, success_message) count += 1 success_message = "." * count shell.run_bash_command_popen(False, False, action, "rsync -Pavl -e 'ssh -p " + settings_dict["prod_ssh_port"] + " -i " + settings_dict[ "prod_ssh_privkey_path"] + "' " + settings_dict["prod_ssh_user"] + "@" + settings_dict["prod_ssh_host"] + ":" + f + " " + new_path, success_message) count += 1 print(Colors.FG.LightGreen + Colors.Bold + action + " Complete!" + Colors.Reset)
def reindex_one_index(config, path, index): action = "Reindex: " + index.replace("_", " ").capitalize() print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento indexer:reindex " + index, action + " Completed")
def reindex_all_index(config, path): action = "Reindex all Indexes" print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento indexer:reindex", action + " Completed")
def configure_elasticsearch(config, path, menu_return): action = "Configure Database for Elastic Search" version = input("ElasticSearch version? 5, 6 or 7 (Default 7):") if version == "": version = "elasticsearch7" elif version == "5": version = "elasticsearch5" elif version == "6": version = "elasticsearch6" elif version == "7": version = "elasticsearch7" else: print(Colors.FG.Yellow + "Versions can only be 5, 6, or 7: " + version + Colors.Reset) menu.main_menu(path) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set catalog/search/engine " + version, ".") shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set catalog/search/" + version + "_server_hostname elasticsearch", "..") shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set catalog/search/" + version + "_server_port 9200", "...") shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set catalog/search/" + version + "_index_prefix magento2", "....") shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set catalog/search/" + version + "_enable_auth 0", ".....") shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento config:set catalog/search/" + version + "_server_timeout 15", "......") print(Colors.FG.LightGreen + Colors.Bold + action + " Completed." + Colors.Reset) action = "Reindex Search Catalog" print(Colors.FG.LightGreen + Colors.Bold + action + " Started." + Colors.Reset) shell.run_bash_command(config, path, action, "php -ddisplay_errors=on " + path + "/bin/magento indexer:reindex catalogsearch_fulltext", ".......") print(Colors.FG.LightGreen + Colors.Bold + action + " Completed." + Colors.Reset) print(Colors.FG.LightGreen + Colors.Bold + "ElasticSearch Configured" + Colors.Reset) if menu_return == 1: menu.elasticsearch_menu(config, path)
def save_config(config, path): action = "Save Config" with open('/srv/webscale_toolkit/var/config.json', 'w+') as outfile: json.dump(config, outfile) shell.run_bash_command(config, path, action, "php -d display_errors=on ./lib/save_config.php " + path, "")