class RemoveLogicalNVDIMMRegionCommand(RdmcCommandBase):
    """ Main removelogicalnvdimm command class """
    def __init__(self, rdmcObj):
        RdmcCommandBase.__init__(self, \
            name='removelogicalnvdimm', \
            usage='removelogicalnvdimm (--processor=NUMBER --index=INDEX | '\
            '--pair=PAIR)\n\n\tRemove a logical NVDIMM. All data will be lost.\n'\
            '\n\texample: removelogicalnvdimm --processor=1 --index=1'\
            '\n\texample: removelogicalnvdimm --processors=1,2', \
            summary='Remove an existing logical NVDIMM.', \
            aliases=['lnvdimm-remove', 'lnr'], \
            optparser=OptionParser())
        self.definearguments(self.parser)
        self._rdmc = rdmcObj
        self._helpers = Helpers()
        self._restHelpers = RestHelpers(self._rdmc)
        self._chif_lib = self._helpers.gethprestchifhandle()

    def removeRegion(self, options):
        """ Removes the Logical NVDIMM specified

        :param socketIdx: the socket of the NUMA region, or None for Non-NUMA
        :type socketIdx: string or int

        :param regionIdx: the index of the region
        :type regionIdx: string or int
        """

        validator = LogicalNvdimmValidator()

        scalable_pmem_config = ScalablePersistentMemoryConfig(self._restHelpers,\
                                                     validator, self._chif_lib)
        scalable_pmem_config.refresh()

        # pre-validation
        self._helpers.validateAllConfigurationPolicies(scalable_pmem_config)

        matchingRegion = None
        if options.processorPair:
            matchingPair = next((p for _, p in scalable_pmem_config.regions.\
                                 socketPairs.items() if p.labelString == options.\
                                                            processorPair), None)
            if matchingPair:
                matchingRegion = matchingPair.nonNumaRegion
        else:
            matchingSocket = next((s for _, s in scalable_pmem_config.regions.\
                                   sockets.items() if s.labelString == options.\
                                                        processorNumber), None)
            if matchingSocket:
                matchingRegion = matchingSocket.numaRegions.get(options.index)

        if matchingRegion and matchingRegion.isConfigured:
            if self._rdmc.interactive:
                if matchingRegion.isActivelyConfigured:
                    self._helpers.confirmBeforeConfigCausesDataLoss(scalable_pmem_config)

            patchAttributes = {
                matchingRegion.settingName : 0
            }
            _ = self._restHelpers.patchScalablePmemSettingAttributes(patchAttributes)
        else:
            self._helpers.displayRegionConfiguration(scalable_pmem_config)
            raise InvalidCommandLineError(u"Unable to identify an existing logical NVDIMM")

        # display the new state
        scalable_pmem_config.refresh()
        self._helpers.displayRegionConfiguration(scalable_pmem_config)
        sys.stdout.write(u"\n")



    def run(self, line):
        """ Wrapper function for the Remove logical NVDIMM command

        :param line: command line input
        :type line: string.
        """
        LOGGER.info("Scalable PMEM: {}".format(self.name))
        try:
            (options, _) = self._parse_arglist(line)
        except:
            if ("-h" in line) or ("--help" in line):
                return ReturnCodes.SUCCESS
            else:
                raise InvalidCommandLineErrorOPTS("")

        LOGGER.info("Options: {}".format(options))

        if not self._chif_lib:
            self._helpers.failNoChifLibrary()

        if not options.processorNumber and not options.processorPair:
            self.parser.print_help()
            raise InvalidCommandLineError(u"One of --processor or --processors"\
                                                                " is required")

        if options.processorNumber and options.processorPair:
            self.parser.print_help()
            raise InvalidCommandLineError(u"--processor and --processors may not"\
                                                        " be used at the same time")

        if options.processorNumber:
            if options.index is None:
                self.parser.print_help()
                raise InvalidCommandLineError(u"--index must be specified with "\
                                                                    "--processor")
            self.removeRegion(options)

        if options.processorPair:
            if not options.index is None:
                self.parser.print_help()
                raise InvalidCommandLineError(u"--index is not a valid option to"\
                                                        " use with --processors")
            self.removeRegion(options)

        #Return code
        return ReturnCodes.SUCCESS

    def definearguments(self, customparser):
        """ Define the arguments in the remove region function

        :param customparser: command line input
        :type customparser: parser.
        """

        groupDanger = OptionGroup(customparser, "Dangerous Options", \
            "Use of these options will alter the backup storage devices configured\n" \
            "for use with Scalable Persistent Memory and could cause data loss.  "\
            "Back up all\ndata first.")

        groupDanger.add_option(
            '--proc',
            '--processor',
            action="store",
            type="string",
            default=None,
            dest="processorNumber",
            metavar="NUMBER",
            help="Specify the processor number of the logical NVDIMM to remove (1, 2)."
        )

        groupDanger.add_option(
            '-i',
            '--index',
            type="int",
            default=None,
            dest="index",
            help="Specify the index of the logical NVDIMM to remove (use with --processor).")

        groupDanger.add_option(
            '--pair',
            '--processors',
            action="store",
            type="string",
            default=None,
            dest="processorPair",
            metavar="PAIR",
            help="Specify the pair of processors of the spanned logical NVDIMM to remove (1,2)."
            )

        customparser.add_option_group(groupDanger)
コード例 #2
0
class EnableScalablePmemCommand(RdmcCommandBase):
    """ Enable Scalable Pmem command """
    def __init__(self, rdmcObj):
        RdmcCommandBase.__init__(self, \
            name='enablescalablepmem', \
            usage='enablescalablepmem [OPTIONS]\n\n' \
                '\tEnables or disables the Scalable Persistent Memory feature.\n'\
                '\n\texample: enablescalablepmem', \
            summary='Enable or disable the Scalable Persistent Memory feature.', \
            aliases=['spmem-enable', 'spmemen'], \
            optparser=OptionParser())
        self.definearguments(self.parser)
        self._rdmc = rdmcObj
        self._helpers = Helpers()
        self._restHelpers = RestHelpers(self._rdmc)
        self._chif_lib = self._helpers.gethprestchifhandle()

    def enableOrDisableFeature(self, enable):
        """ Enables or disables the feature

        :param enable: a flag whether to enable or disable the feature
        :type enable: boolean
        """

        validator = LogicalNvdimmValidator()

        scalable_pmem_config = ScalablePersistentMemoryConfig(self._restHelpers,\
                                                     validator, self._chif_lib)
        scalable_pmem_config.refresh()

        # pre-validation
        self._helpers.validateFeatureIsSupported(scalable_pmem_config)
        self._helpers.validateFunctionalityIsEnabled(scalable_pmem_config)

        if enable is False:
            # If user disables Scalable PMEM, revert any pending changes to
            # prevent data or configuration loss
            if self._rdmc.interactive:
                message = u"Warning: disabling Scalable Persistent Memory will "\
                                    "revert any pending configuration changes.\n"
                self._helpers.confirmChanges(message=message)
            self._restHelpers.revertSettings()

        patchAttributes = {
            "FeatureEnabled" : enable
        }
        _ = self._restHelpers.patchScalablePmemSettingAttributes(patchAttributes)

        sys.stdout.write(u"\nThe Scalable Persistent Memory feature has been "\
                    "set to: {}\n".format("Enabled" if enable else "Disabled"))

        self._helpers.noticeRestartRequired(scalable_pmem_config)

        sys.stdout.write("\n\n")

    def run(self, line):
        """ Wrapper function for the Remove logical NVDIMM command

        :param line: command line input
        :type line: string.
        """
        LOGGER.info("Scalable PMEM: {}".format(self.name))
        try:
            (options, _) = self._parse_arglist(line)
        except:
            if ("-h" in line) or ("--help" in line):
                return ReturnCodes.SUCCESS
            else:
                raise InvalidCommandLineErrorOPTS("")

        if len(args):
            InvalidCommandLineError("This command takes no parameters.")

        LOGGER.info("Options: {}".format(options))

        if not self._chif_lib:
            self._helpers.failNoChifLibrary()

        enable = True
        if options.enableFeature is False:
            enable = False

        self.enableOrDisableFeature(enable)

        #Return code
        return ReturnCodes.SUCCESS

    def definearguments(self, customparser):
        """ Define arguments for the command

        :param customparser: command line input
        :type customparser: parser.
        """

        customparser.add_option(
            '--disable',
            action="store_false",
            dest="enableFeature",
            help="Disable the Scalable Persistent Memory feature. Warning: "\
                                "any pending configuration changes will be lost."
        )