Пример #1
0
    def execute(self, _sCommand=None, _lArguments=None):
        """
        Execute the command

        @param str  _sCommand    Command name
        @param list _lArguments  Command arguments

        @return int  0 on success, non-zero in case of failure
        """

        # Arguments
        self._initArgumentParser(_sCommand)
        self._initArguments(_lArguments)

        # Show cluster configuration
        try:

            # Load config
            oClusterConfig = KiscCluster_config(self._oArguments.config)
            oClusterConfig.VERBOSE(self._oArguments.verbose)
            lsErrors = oClusterConfig.load()
            if lsErrors:
                if self._oArguments.verbose >= KiscRuntime.VERBOSE_DEBUG:
                    for sError in lsErrors:
                        sys.stderr.write('%s\n' % sError)
                else:
                    sys.stderr.write('%s\n' % lsErrors[-1])
                return 255

            # Show config
            if self._oArguments.host:
                oHost = oClusterConfig.getHost(self._oArguments.host)
                sys.stdout.write(oHost.toString())
            elif self._oArguments.resource:
                oResource = oClusterConfig.getResource(
                    self._oArguments.resource, self._oArguments.bootstrap)
                sys.stdout.write(oResource.toString())
            else:
                sys.stdout.write(oClusterConfig.toString())

        except (OSError, RuntimeError) as e:
            sys.stderr.write('%s\n' % str(e))
            return 255

        # Done
        return 0
Пример #2
0
    def execute(self, _sCommand=None, _lArguments=None):
        """
        Execute the command

        @param str  _sCommand    Command name
        @param list _lArguments  Command arguments

        @return int  0 on success, non-zero in case of failure
        """

        # Arguments
        self._initArgumentParser(_sCommand)
        self._initArguments(_lArguments)

        # List cluster hosts/resources
        try:

            # Filter
            # ... exclude
            self._ltFilters_exclude = list()
            for sFilter in self._oArguments.exclude:
                try:
                    (sKey, sValue) = sFilter.split('=', 1)
                    if sKey[-1] == '~':
                        sKey = sKey[:-1]
                        sValue = re.compile(sValue)
                except ValueError:
                    sKey = sFilter
                    sValue = None
                self._ltFilters_exclude.append((sKey, sValue))
            # ... include
            self._ltFilters_include = list()
            for sFilter in self._oArguments.include:
                try:
                    (sKey, sValue) = sFilter.split('=', 1)
                    if sKey[-1] == '~':
                        sKey = sKey[:-1]
                        sValue = re.compile(sValue)
                except ValueError:
                    sKey = sFilter
                    sValue = None
                self._ltFilters_include.append((sKey, sValue))

            # Load config
            oClusterConfig = KiscCluster_config(self._oArguments.config)
            lsErrors = oClusterConfig.load()
            if lsErrors:
                sys.stderr.write('%s\n' % lsErrors[-1])
                return 255

            # List IDs
            if self._oArguments.what == 'hosts':
                if not len(self._ltFilters_include) and not len(
                        self._ltFilters_exclude):
                    sys.stdout.write(
                        '\n'.join(sorted(oClusterConfig.getHostsIDs())) + '\n')
                else:
                    for sHost_id in sorted(oClusterConfig.getHostsIDs()):
                        if self._match(
                                oClusterConfig.getHost(sHost_id).config()):
                            sys.stdout.write('%s\n' % sHost_id)
            elif self._oArguments.what == 'resources':
                if not len(self._ltFilters_include) and not len(
                        self._ltFilters_exclude):
                    sys.stdout.write('\n'.join(
                        sorted(
                            oClusterConfig.getResourcesIDs(
                                self._oArguments.bootstrap))) + '\n')
                else:
                    for sResource_id in sorted(
                            oClusterConfig.getResourcesIDs(
                                self._oArguments.bootstrap)):
                        if self._match(
                                oClusterConfig.getResource(
                                    sResource_id).config()):
                            sys.stdout.write('%s\n' % sResource_id)

        except (OSError, RuntimeError) as e:
            sys.stderr.write('%s\n' % str(e))
            return 255

        # Done
        return 0