Exemplo n.º 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)

        # 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
Exemplo n.º 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)

        # Show cluster configuration
        try:

            # Load config
            oClusterConfig = KiscCluster_config(self._oArguments.config)
            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
            oClusterConfig.VERBOSE(self._oArguments.verbose)

            # Loop through hosts/resources
            if self._oArguments.what == 'hosts':
                if self._oArguments.host:
                    lsHosts_ids = [self._oArguments.host]
                else:
                    lsHosts_ids = sorted(oClusterConfig.getHostsIDs())
                for sHost_id in lsHosts_ids:
                    oClusterHost = KiscCluster_host(oClusterConfig, sHost_id)
                    oClusterHost.VERBOSE(self._oArguments.verbose)
                    iStatus = oClusterHost.status(False)
                    asResources_ids = oClusterHost.host().getResourcesIDs()
                    if self._oArguments.resource is None or self._oArguments.resource in asResources_ids:
                        sResources_ids = ','.join(asResources_ids)
                        sHostRegistration_id = oClusterHost.host().registerTo()
                        if sHostRegistration_id is not None:
                            sResources_ids = '> %s' % sHostRegistration_id
                        elif not len(sResources_ids):
                            sResources_ids = '-'
                        sys.stdout.write(
                            '%s %s %s\n' %
                            (sHost_id, KiscRuntime.STATUS_MESSAGE[iStatus],
                             sResources_ids))
            elif self._oArguments.what == 'resources':
                if self._oArguments.resource:
                    lsResources_ids = [self._oArguments.resource]
                else:
                    lsResources_ids = sorted(oClusterConfig.getResourcesIDs())
                for sResource_id in lsResources_ids:
                    oClusterResource = KiscCluster_resource(
                        oClusterConfig, None, sResource_id, False)
                    oClusterResource.VERBOSE(self._oArguments.verbose)
                    iStatus = oClusterResource.status(False)
                    asHosts_ids = oClusterResource.resource().getHostsIDs()
                    if self._oArguments.host is None or self._oArguments.host in asHosts_ids:
                        sHosts_ids = ','.join(asHosts_ids)
                        if not len(sHosts_ids): sHosts_ids = '-'
                        sys.stdout.write(
                            '%s %s %s\n' %
                            (sResource_id, KiscRuntime.STATUS_MESSAGE[iStatus],
                             sHosts_ids))

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

        # Done
        return 0