def do_rename_service(cmd_options=None): '''Rename a service. Note: Errors that occur during the various rename stages are printed, but the other stages will continue, with the hopes of leaving the final product as close to functional as possible ''' # check that we are root if os.geteuid() != 0: raise SystemExit(_("Error: Root privileges are required for this " "command.\n")) (svcname, newsvcname) = parse_options(cmd_options) # Ensure the service to rename is a valid service if not config.is_service(svcname): raise SystemExit(_("\nFailed to find service %s\n") % svcname) # Ensure the new name is not already a service if config.is_service(newsvcname): raise SystemExit(_("\nService or alias already exists: %s\n") % newsvcname) # Don't allow renaming to/from the 'default-<arch>' aliases if svcname in DEFAULT_ARCH: raise SystemExit(_('\nYou may not rename the "%s" service.\n') % svcname) if newsvcname in DEFAULT_ARCH: raise SystemExit(cw(_('\nYou may not rename a service to be the ' 'default service for an architecture. To create ' 'the default-sparc or default-i386 service ' 'aliases, use "installadm create-service ' '-t|--aliasof."\n'))) # Unmount old service was_mounted = False try: oldservice = AIService(svcname) if oldservice.mounted(): was_mounted = True logging.debug("disabling %s", svcname) oldservice.disable(force=True) except (MountError, ImageError) as err: raise SystemExit(err) # remove old mountpoint try: os.rmdir(oldservice.mountpoint) except OSError as err: # Just make a note if unable to cleanup mountpoint logging.debug(err) # Remove clients whose base service has been renamed clients = config.get_clients(svcname) for clientid in clients.keys(): clientctrl.remove_client(clientid) oldservice.rename(newsvcname) # Update aliases whose base service has been renamed aliases = config.get_aliased_services(svcname) failures = list() for alias in aliases: props = {config.PROP_ALIAS_OF: newsvcname} config.set_service_props(alias, props) # Mount the renamed service if it was mounted newservice = AIService(newsvcname) if was_mounted: try: logging.debug("enabling %s", newsvcname) newservice.enable() except (MountError, ImageError) as err: failures.append(err) print >> sys.stderr, err # Re-add clients whose base service has been renamed arch = newservice.arch for clientid in clients.keys(): # strip off leading '01' client = clientid[2:] bootargs = None if config.BOOTARGS in clients[clientid]: bootargs = clients[clientid][config.BOOTARGS] create_client.create_new_client(arch, newservice, client, bootargs=bootargs) if failures: return 1 else: return 0
def do_disable_service(cmd_options=None): ''' Disable a service Disable the specified service and optionally update the service's properties to reflect the new status. Input: List of command line options Return: None Raises: SystemExit if missing permissions, invalid service name, or if attempt to place smf service in maintenance fails. ''' logging.debug('**** START do_disable_service ****') # check for authorization and euid try: check_auth_and_euid(SERVICE_AUTH) except UnauthorizedUserError as err: raise SystemExit(err) usage = '\n' + get_disable_usage() parser = OptionParser(usage=usage) (options, args) = parser.parse_args(cmd_options) # Check for correct number of args if len(args) != 1: if len(args) == 0: parser.error(_("Missing required argument, <svcname>")) else: parser.error(_("Too many arguments: %s") % args) svcname = args[0] # validate service name try: validate_service_name(svcname) except ValueError as err: raise SystemExit(err) if not config.is_service(svcname): err_msg = _("The service does not exist: %s\n") % svcname parser.error(err_msg) prop_data = config.get_service_props(svcname) if prop_data and config.PROP_STATUS not in prop_data: err_msg = _("The property, status, is missing for %s.\n") % svcname parser.error(err_msg) if prop_data[config.PROP_STATUS] == config.STATUS_OFF: err_msg = _("The service is not running: %s\n") % svcname parser.error(err_msg) try: logging.debug("Disabling install service %s", svcname) service = AIService(svcname) service.disable(force=True) except (config.ServiceCfgError, aismf.ServicesError, MountError) as err: raise SystemExit(err) except CalledProcessError: return 1