def main(argv): from optparse import OptionParser parser = OptionParser(usage="%prog [options] backup|uninstall|restore|restore-engage [backup_directory]") parser.add_option("--compress", "-c", action="store_true", dest="compress", default=False, help="If specified, compress backup files") cmdline_script_utils.add_standard_cmdline_options(parser, running_deployment=False) (options, args) = parser.parse_args() if not (len(args)==2 or (len(args)==1 and args[0]=="uninstall")): parser.error("Wrong number of args, expecting 1 or 2") cmd = args[0] valid_commands = ["backup", "uninstall", "restore", "restore-engage"] if not (cmd in valid_commands): parser.error("Command must be one of %s" % valid_commands) (file_layout, dh) = cmdline_script_utils.process_standard_options(options, parser) if cmd != "uninstall": backup_directory = os.path.abspath(os.path.expanduser(args[1])) if cmd=="backup": if not os.path.isdir(backup_directory): os.makedirs(backup_directory) elif (cmd=="restore") or (cmd=="restore-engage"): if not os.path.isdir(backup_directory): parser.error("Backup directory %s does not exist" % backup_directory) if cmd == "restore-engage": # for restore engage, we don't try to get resources, as they aren't there yet restore_engage_files(backup_directory) return 0 # skip the rest mgr_pkg_list = cmdline_script_utils.get_mgrs_and_pkgs(file_layout, dh, options) logger = log_setup.setup_engine_logger(__name__) if cmd=="backup": for (m, p) in reversed(mgr_pkg_list): if m.is_service() and m.is_running(): logger.info("stopping resource %s" % m.id) m.stop() backup_resources(backup_directory, mgr_pkg_list, logger, options.compress) save_engage_files(backup_directory, dh, logger, options.compress) elif cmd=="uninstall": for (m, p) in reversed(mgr_pkg_list): if m.is_service() and m.is_running(): logger.info("stopping resource %s" % m.id) m.stop() uninstall_resources(mgr_pkg_list, logger) else: # command == restore restore_resources(backup_directory, mgr_pkg_list, logger) return 0
def upgrade(backup_dir, file_layout, deployment_home, options, atomic_upgrade=True): old_resources = get_old_resources(backup_dir) mgrs_and_pkgs = cmdline_script_utils.get_mgrs_and_pkgs(file_layout, deployment_home, options, file_layout.get_install_script_file()) undo_list = [] for (m, p) in mgrs_and_pkgs: id = m.metadata.id try: if old_resources.has_key(id) and old_resources[id].key['name']==m.metadata.key['name'] and \ m.metadata.key['version']>=old_resources[id].key['version']: logger.info("Calling upgrade for resource %s" % id) m.upgrade(p, old_resources[id], backup_dir) else: if not m.is_installed(): logger.info("Installing resource %s" % id) m.install(p) else: logger.info("Resource %s already installed" % id) m.metadata.set_installed() if m.is_service() and not m.is_running(): m.start() undo_list.append(m) except: if not atomic_upgrade: raise exc_info = (exc_class, exc_val, exc_tb) = sys.exc_info() logger.exception("Upgrade of %s failed, will attempt undo" % m.id) _undo_bad_install(m, undo_list, deployment_home, "%s(%s)" % (exc_class.__name__, exc_val)) user_error = convert_exc_to_user_error(exc_info, errors[ERR_UPGRADE_ROLLBACK], msg_args={'id':m.id}, user_error_class=UpgradeRollbackInProgress) user_error.resource_id = m raise user_error install_target_mgr = get_install_target_mgr(mgrs_and_pkgs) managers = [mgr for (mgr, pkg) in mgrs_and_pkgs] install_target_mgr.write_resources_to_file(managers) if options.mgt_backends: import mgt_registration import install_context mgt_registration.register_with_mgt_backends(options.mgt_backends, managers, deployment_home, sudo_password=install_context.get_sudo_password(), upgrade=True) return 0
def main(): parser = OptionParser(usage=usage_msg) add_standard_cmdline_options(parser, running_deployment=False, default_log_level="WARNING") parser.add_option("-r", "--resource-file", dest="resource_file", default=None, help="Name of resource file (defaults to <deployment_home>/config/installed_resources.json)") parser.add_option("--force", dest='force', default=False, action="store_true", help="If stop or restart, try to force everything to stop") parser.add_option("--dry-run", dest="dry_run", default=False, action="store_true", help="If specified, just print what would be done") (options, args) = parser.parse_args() if len(args)==0: parser.print_help() sys.exit(0) if len(args)<1: parser.error("Missing command name. Must be one of %s" % valid_commands) (file_layout, deployment_home) = process_standard_options(options, parser, allow_overrides_of_dh=True, rotate_logfiles=False) if options.resource_file: installed_resources_file = os.path.abspath(os.path.expanduser(options.resource_file)) else: installed_resources_file = file_layout.get_installed_resources_file(deployment_home) if not os.path.exists(installed_resources_file): sys.stderr.write("Error: Installed resources file '%s does not exist\n" % installed_resources_file) sys.exit(1) logger = setup_engage_logger(__name__) mgr_pkg_list = get_mgrs_and_pkgs(file_layout, deployment_home, options, installed_resources_file) resource_map = {} for (mgr, pkg) in mgr_pkg_list: resource_map[mgr.id] = mgr command = args[0] if command not in commands.keys(): sys.stderr.write("Error: invalid command %s\n" % command) parser.print_help() sys.exit(1) if options.force and command not in ['stop', 'restart']: sys.stderr.write("Error: --force option not valid for command %s\n" % command) parser.print_help() sys.exit(1) command_args = args[1:] cmd_fn = commands[command] try: if command in ['stop', 'restart']: rc = cmd_fn(command, command_args, mgr_pkg_list, resource_map, logger, options.dry_run, force=options.force) else: rc = cmd_fn(command, command_args, mgr_pkg_list, resource_map, logger, options.dry_run) except CommandError, msg: sys.stderr.write("Error: %s\n" % msg) sys.exit(1)