def clone(backup_name, archive_dir): """ Do Cloning """ validate_controller_state() LOG.info("Cloning [{}] at [{}]".format(backup_name, archive_dir)) check_size(archive_dir) isolinux_dir = os.path.join(archive_dir, 'isolinux') clone_archive_dir = os.path.join(isolinux_dir, CLONE_ARCHIVE_DIR) if os.path.exists(isolinux_dir): LOG.info("deleting old iso_dir %s" % isolinux_dir) shutil.rmtree(isolinux_dir, ignore_errors=True) os.makedirs(clone_archive_dir, 0o644) try: backup_restore.backup(backup_name, clone_archive_dir, clone=True) LOG.info("system backup done") update_backup_archive(backup_name + '_system', clone_archive_dir) create_iso(backup_name, archive_dir) except BackupFail as e: raise CloneFail(e.message) except CloneFail as e: raise finally: if not DEBUG: shutil.rmtree(isolinux_dir, ignore_errors=True)
def create_simplex_backup(software_upgrade): """Creates the upgrade metadata and creates the system backup""" backup_data = {} upgrade_data = software_upgrade.as_dict() if upgrade_data['created_at']: upgrade_data['created_at'] = \ upgrade_data['created_at'].replace( microsecond=0).replace(tzinfo=None).isoformat() if upgrade_data['updated_at']: upgrade_data['updated_at'] = \ upgrade_data['updated_at'].replace( microsecond=0).replace(tzinfo=None).isoformat() backup_data['upgrade'] = upgrade_data json_data = json.dumps(backup_data) metadata_path = os.path.join(tsc.CONFIG_PATH, 'upgrades') os.mkdir(metadata_path) metadata_filename = os.path.join(metadata_path, 'metadata') with open(metadata_filename, 'w') as metadata_file: metadata_file.write(json_data) backup_filename = get_upgrade_backup_filename(software_upgrade) backup_restore.backup(backup_filename, constants.BACKUPS_PATH) LOG.info("Create simplex backup complete")
def main(): options = {} answerfile = None backup_name = None archive_dir = constants.BACKUPS_PATH do_default_config = False do_backup = False do_system_restore = False include_storage_reinstall = False do_clone = False do_non_interactive = False do_provision = False system_config_file = "/home/sysadmin/system_config" allow_ssh = False # Disable completion as the default completer shows python commands readline.set_completer(no_complete) # remove any previous config fail flag file if os.path.exists(constants.CONFIG_FAIL_FILE) is True: os.remove(constants.CONFIG_FAIL_FILE) if os.environ.get('CGCS_LABMODE'): options['labmode'] = True arg = 1 while arg < len(sys.argv): if sys.argv[arg] == "--answerfile": arg += 1 if arg < len(sys.argv): answerfile = sys.argv[arg] else: print("--answerfile option requires a file to be specified") exit(1) elif sys.argv[arg] == "--backup": arg += 1 if arg < len(sys.argv): backup_name = sys.argv[arg] else: print("--backup requires the name of the backup") exit(1) do_backup = True elif sys.argv[arg] == "--restore-system": arg += 1 if arg < len(sys.argv): if sys.argv[arg] in [ "include-storage-reinstall", "exclude-storage-reinstall" ]: if sys.argv[arg] == "include-storage-reinstall": include_storage_reinstall = True arg += 1 if arg < len(sys.argv): backup_name = sys.argv[arg] else: print( textwrap.fill( "--restore-system requires the filename " " of the backup", 80)) exit(1) else: backup_name = sys.argv[arg] else: print( textwrap.fill( "--restore-system requires the filename " "of the backup", 80)) exit(1) do_system_restore = True elif sys.argv[arg] == "--archive-dir": arg += 1 if arg < len(sys.argv): archive_dir = sys.argv[arg] else: print("--archive-dir requires a directory") exit(1) elif sys.argv[arg] == "--clone-iso": arg += 1 if arg < len(sys.argv): backup_name = sys.argv[arg] else: print("--clone-iso requires the name of the image") exit(1) do_clone = True elif sys.argv[arg] == "--clone-status": clone.clone_status() exit(0) elif sys.argv[arg] == "--default": do_default_config = True elif sys.argv[arg] == "--config-file": arg += 1 if arg < len(sys.argv): system_config_file = sys.argv[arg] else: print("--config-file requires the filename of the config file") exit(1) do_non_interactive = True elif sys.argv[arg] in ["--help", "-h", "-?"]: show_help() exit(1) elif sys.argv[arg] == "--labhelp": show_help_lab_only() exit(1) elif sys.argv[arg] == "--provision": do_provision = True elif sys.argv[arg] == "--allow-ssh": allow_ssh = True elif sys.argv[arg] == "--kubernetes": # This is a temporary flag for use during development. Once things # are stable, we will remove it and make kubernetes the default. options['kubernetes'] = True else: print("Invalid option. Use --help for more information.") exit(1) arg += 1 if [ do_backup, do_system_restore, do_clone, do_default_config, do_non_interactive ].count(True) > 1: print("Invalid combination of options selected") exit(1) if answerfile and [ do_backup, do_system_restore, do_clone, do_default_config, do_non_interactive ].count(True) > 0: print("The --answerfile option cannot be used with the selected " "option") exit(1) log.configure() if not do_backup and not do_clone: # Check if that the command is being run from the console if utils.is_ssh_parent(): if allow_ssh: print(textwrap.fill(constants.SSH_WARNING_MESSAGE, 80)) print('') else: print(textwrap.fill(constants.SSH_ERROR_MESSAGE, 80)) exit(1) # Reduce the printk console log level to avoid noise during configuration printk_levels = '' with open('/proc/sys/kernel/printk', 'r') as f: printk_levels = f.readline() temp_printk_levels = '3' + printk_levels[1:] with open('/proc/sys/kernel/printk', 'w') as f: f.write(temp_printk_levels) try: if do_backup: backup_restore.backup(backup_name, archive_dir) print("\nBackup complete") elif do_system_restore: backup_restore.restore_system(backup_name, include_storage_reinstall) print("\nSystem restore complete") elif do_clone: clone.clone(backup_name, archive_dir) print("\nCloning complete") elif do_provision: assistant = ConfigAssistant(**options) assistant.provision(answerfile) else: print( textwrap.fill( "Please use bootstrap playbook to configure the " "first controller.", 80)) exit(1) if do_non_interactive: if not os.path.isfile(system_config_file): raise ConfigFail("Config file %s does not exist." % system_config_file) if (os.path.exists(constants.CGCS_CONFIG_FILE) or os.path.exists(constants.CONFIG_PERMDIR) or os.path.exists( constants.INITIAL_CONFIG_COMPLETE_FILE)): raise ConfigFail("Configuration has already been done " "and cannot be repeated.") configure_system(system_config_file) answerfile = TEMP_CGCS_CONFIG_FILE assistant = ConfigAssistant(**options) assistant.configure(answerfile, do_default_config) print("\nConfiguration was applied\n") print( textwrap.fill( "Please complete any out of service commissioning steps " "with system commands and unlock controller to proceed.", 80)) assistant.check_required_interfaces_status() except KeyboardInterrupt: print("\nAborting configuration") except BackupFail as e: print("\nBackup failed: {}".format(e)) except RestoreFail as e: print("\nRestore failed: {}".format(e)) except ConfigFail as e: print("\nConfiguration failed: {}".format(e)) except CloneFail as e: print("\nCloning failed: {}".format(e)) except UserQuit: print("\nAborted configuration") finally: if os.path.isfile(TEMP_CGCS_CONFIG_FILE): os.remove(TEMP_CGCS_CONFIG_FILE) # Restore the printk console log level with open('/proc/sys/kernel/printk', 'w') as f: f.write(printk_levels)