示例#1
0
    def status(self, _bStateful=True, _iIntent=None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_STARTED

        if _bStateful:

            # Exists ?
            # NOTE: look only for a mountpoint match, independently from potentially mismatching
            #       fstype, device or options
            try:
                KiscRuntime.shell([
                    'awk',
                    'BEGIN{e=22}; {if($2=="%s") {e=0; exit}}; END {exit e}' %
                    self._dsConfig['mountpoint'], '/proc/mounts'
                ],
                                  _bTrace=self._iVerbose >=
                                  KiscRuntime.VERBOSE_TRACE)
            except OSError as e:
                if e.filename == 0 and e.errno == 22:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus

        # Done
        self._iStatus = iStatus
        if self._iVerbose:
            self._INFO('Status is %s' %
                       KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#2
0
    def stop(self):
        if self._iVerbose: self._INFO('Stopping')
        lsErrors = list()

        # Be idempotent
        if self.status(True, KiscRuntime.STATUS_STOPPED) == KiscRuntime.STATUS_STOPPED:
            if self._iVerbose: self._INFO('Already stopped')
            return lsErrors

        # Stop the resource

        # ... DOWN!
        try:
            KiscRuntime.shell(['ip', 'link', 'set', self._dsConfig['name'], 'down'], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
        except OSError as e:
            if self._iVerbose: self._WARNING(str(e))
            lsErrors.append(str(e))

        # ... delete device
        try:
            KiscRuntime.shell(['ip', 'link', 'delete', self._dsConfig['name']], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STOPPED
            if self._iVerbose: self._INFO('Stopped')
        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#3
0
    def status(self, _bStateful = True, _iIntent = None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_UNKNOWN

        if _bStateful:

            # Exists ?
            try:
                KiscRuntime.shell(['invoke-rc.d', '--quiet', self._dsConfig['name'], 'status'], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                iStatus = KiscRuntime.STATUS_STARTED
            except OSError as e:
                # REF: http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
                if e.filename == 0 and e.errno == 3:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus 

        # Done
        self._iStatus = iStatus
        if self._iVerbose: self._INFO('Status is %s' % KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#4
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Restart ?
        bRestart = KiscRuntime.parseBool(self._dsConfig.get('restart', False))

        # Be idempotent
        if self.status(True, KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if not bRestart:
                if self._iVerbose: self._INFO('Already started')
                return lsErrors
            else:
                if self._iVerbose: self._INFO('Restarting')
        else:
            bRestart = False

        # Start the resource
        try:
            if bRestart:
                KiscRuntime.shell(['systemctl', '-q', 'restart', self._dsConfig['name']], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            else:
                KiscRuntime.shell(['systemctl', '-q', 'start', self._dsConfig['name']], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')
        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#5
0
    def status(self, _bStateful = True, _iIntent = None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_STARTED

        if _bStateful:

            # Exists ?
            # NOTE: look only for a name match, independently from potentially mismatching
            #       type, VLAN ID, physical device or options
            try:
                KiscRuntime.shell(['test', '-e', '/sys/class/net/%s' % self._dsConfig['name']], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            except OSError as e:
                if e.filename == 0 and e.errno == 1:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

            # UP ?
            if iStatus == KiscRuntime.STATUS_STARTED and _iIntent == KiscRuntime.STATUS_STARTED:
                try:
                    KiscRuntime.shell(['grep', '-Fq', 'up', '/sys/class/net/%s/operstate' % self._dsConfig['name']], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                except OSError as e:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus 

        # Done
        self._iStatus = iStatus
        if self._iVerbose: self._INFO('Status is %s' % KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#6
0
    def stop(self):
        if self._iVerbose: self._INFO('Stopping')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STOPPED) == KiscRuntime.STATUS_STOPPED:
            if self._iVerbose: self._INFO('Already stopped')
            return lsErrors

        # Stop the resource

        # ... unmount device
        try:
            KiscRuntime.shell(
                ['umount', self._dsConfig['mountpoint']],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STOPPED
            if self._iVerbose: self._INFO('Stopped')
        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#7
0
    def stop(self):
        if self._iVerbose: self._INFO('Stopping')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STOPPED) == KiscRuntime.STATUS_STOPPED:
            if self._iVerbose: self._INFO('Already stopped')
            return lsErrors

        # Stop the resource

        # ... delete address
        try:
            KiscRuntime.shell(
                [
                    'ip', '-4', 'address', 'delete',
                    '%s/%s' %
                    (self._dsConfig['address'], self._dsConfig['mask']), 'dev',
                    self._dsConfig['device']
                ],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Stopped')
        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#8
0
    def status(self, _bStateful=True, _iIntent=None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_STARTED

        if _bStateful:

            # Exists ?
            # NOTE: look only for an address match, independently from potentially mismatching
            #       network mask, device or options
            try:
                KiscRuntime.shell(
                    [['ip', '-4', 'address', 'show'],
                     ['grep', '-Fq',
                      'inet %s/' % self._dsConfig['address']]],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            except OSError as e:
                if e.filename == 0 and e.errno == 1:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus

        # Done
        self._iStatus = iStatus
        if self._iVerbose:
            self._INFO('Status is %s' %
                       KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#9
0
    def stop(self):
        if self._iVerbose: self._INFO('Stopping')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STOPPED) == KiscRuntime.STATUS_STOPPED:
            if self._iVerbose: self._INFO('Already stopped')
            return lsErrors

        # Stop the resource
        try:

            # ... timeout
            try:
                iTimeout = int(self._dsConfig.get('timeout_stop', 15))
            except ValueError:
                raise RuntimeError('Invalid timeout value (%s)' %
                                   self._dsConfig['timeout_stop'])

            # ... stop domain
            KiscRuntime.shell(
                ['virsh', '-q', 'shutdown', self._dsConfig['name']],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... wait for domain to stop
            while iTimeout >= 0:
                iTimeout -= 1
                try:
                    if KiscRuntime.shell(
                        ['virsh', 'domstate', self._dsConfig['name']],
                            _bTrace=self._iVerbose >=
                            KiscRuntime.VERBOSE_TRACE).strip() == 'shut off':
                        break
                except OSError as e:
                    if e.filename == 0:
                        break
                    else:
                        raise e
                if iTimeout < 0:
                    KiscRuntime.shell(
                        ['virsh', '-q', 'destroy', self._dsConfig['name']],
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                    raise RuntimeError('Domain did not stop')
                time.sleep(1)

            # ... done
            self._iStatus = KiscRuntime.STATUS_STOPPED
            if self._iVerbose: self._INFO('Stopped')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#10
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(True, KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... add device
            lsCommand = [
                'ip', 'link', 'add',
                'link', self._dsConfig['device'],
                'name', self._dsConfig['name'],
            ]
            for sSetting in ['address', 'mtu', 'txqueuelen', 'numtxqueues', 'numrxqueues']:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])
            lsCommand.extend(['type', 'vlan'])

            # ... protocol
            for sSetting in ['protocol']:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])

            # ... VLAN ID
            lsCommand.extend(['id', self._dsConfig['vlan']])

            # ... options
            for sSetting in ['reorder_hdr', 'gvrp', 'mvrp', 'loose_binding']:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])

            # ... QoS configuration
            for sSetting in ['ingress_qos_map', 'egress_qos_map']:
                if sSetting in self._dsConfig:
                    lsCommand.append(sSetting.replace('_', '-'))
                    for sMapping in self._dsConfig[sSetting].split(','):
                        lsCommand.append(sMapping)
            KiscRuntime.shell(lsCommand, _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... UP!
            KiscRuntime.shell(['ip', 'link', 'set', self._dsConfig['name'], 'up'], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#11
0
    def resume(self):
        if self._iVerbose: self._INFO('Resuming')
        lsErrors = list()

        # Be idempotent
        iStatus = self.status(True, KiscRuntime.STATUS_STARTED)
        if iStatus == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Domain is running')
            return lsErrors

        # Resuming the resource
        if iStatus != KiscRuntime.STATUS_SUSPENDED:
            raise RuntimeError('Domain not suspended')
        try:

            # ... timeout
            try:
                iTimeout = int(self._dsConfig.get('timeout_resume', 5))
            except ValueError:
                raise RuntimeError('Invalid timeout value (%s)' %
                                   self._dsConfig['timeout_resume'])

            # ... resume domain
            KiscRuntime.shell(
                ['virsh', '-q', 'resume', self._dsConfig['name']],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... wait for domain to resume
            while iTimeout >= 0:
                iTimeout -= 1
                try:
                    if KiscRuntime.shell(
                        ['virsh', 'domstate', self._dsConfig['name']],
                            _bTrace=self._iVerbose >=
                            KiscRuntime.VERBOSE_TRACE).strip() == 'running':
                        break
                except OSError as e:
                    if e.filename == 0:
                        break
                    else:
                        raise e
                if iTimeout < 0:
                    raise RuntimeError('Domain did not resume')
                time.sleep(1)

            # ... done
            self._iStatus = KiscRuntime.STATUS_RESUMED
            if self._iVerbose: self._INFO('Resumed')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#12
0
    def migrate(self, _oHost):
        if self._iVerbose: self._INFO('Migrating')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) != KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Not started')
            return lsErrors

        # Migrate the resource
        try:

            # ... remote URI
            sRemoteUri = self._dsConfig.get('remote_uri',
                                            'qemu://%{host}/system')
            sRemoteUri = sRemoteUri.replace('%{host}', _oHost.id())
            sRemoteUri = sRemoteUri.replace('%{hostname}',
                                            _oHost.getHostname())

            # ... timeout
            try:
                iTimeout = int(self._dsConfig.get('timeout_migrate', 60))
            except ValueError:
                raise RuntimeError('Invalid timeout value (%s)' %
                                   self._dsConfig['timeout_migrate'])

            # ... migrate domain
            lsCommand = ['virsh', '-q', 'migrate', '--live']
            if iTimeout > 0:
                lsCommand.extend(
                    ['--timeout',
                     str(iTimeout), '--timeout-suspend'])
            lsCommand.extend([self._dsConfig['name'], sRemoteUri])
            KiscRuntime.shell(
                lsCommand, _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... done
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self.status(True, KiscRuntime.STATUS_SUSPENDED)
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#13
0
    def status(self, _bStateful=True, _iIntent=None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_UNKNOWN

        if _bStateful:

            # Locate resource on Pacekamer cluster
            try:
                sOutput = KiscRuntime.shell(
                    ['crm_resource', '-Q', '-r', self._dsConfig['name'], '-W'],
                    _bTrace=self._iVerbose >=
                    KiscRuntime.VERBOSE_TRACE).strip()
                if sOutput is None or not len(sOutput):
                    if '$PACEMAKER_NODES' in self._dsConfig:
                        del self._dsConfig['$PACEMAKER_NODES']
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    self._dsConfig['$PACEMAKER_NODES'] = sOutput
                    iStatus = KiscRuntime.STATUS_STARTED
            except OSError as e:
                if e.filename == 0 and e.errno == 6:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus

        # Done
        self._iStatus = iStatus
        if self._iVerbose:
            self._INFO('Status is %s' %
                       KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#14
0
    def start(self):
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        if self._iVerbose: self._INFO('Starting')
        try:

            # ... stonith command
            lsCommand = [
                'stonith', '-s', '-S', '-t', self._dsConfig['device_type']
            ]

            # ... arguments
            lsCommand.extend(['-c', self._dsConfig.get('count', '1')])

            # ... parameters
            if 'parameters' in self._dsConfig:
                dsParameters = KiscRuntime.parseDictionary(
                    self._dsConfig['parameters'], _sAssignmentOperator='=')
                for sParameter_name in dsParameters:
                    lsCommand.append(
                        '%s=%s' %
                        (sParameter_name, dsParameters[sParameter_name]))

            # ... do it !
            KiscRuntime.shell(
                lsCommand, _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#15
0
 def _cleanup(self):
     if KiscRuntime.parseBool(self._dsConfig.get('cleanup', False)):
         if 'constraint_file' in self._dsConfig:
             KiscRuntime.shell(
                 [
                     'cibadmin', '-o', 'constraints', '-d', '-f', '-A',
                     '//rsc_location[@rsc=\'%s\']' % self._dsConfig['name']
                 ],
                 _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
             time.sleep(3)
         if 'resource_file' in self._dsConfig:
             KiscRuntime.shell(
                 [
                     'cibadmin', '-o', 'resources', '-D', '-A',
                     '//primitive[@id=\'%s\'] | //group[@id=\'%s\']' %
                     (self._dsConfig['name'], self._dsConfig['name'])
                 ],
                 _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
             time.sleep(3)
示例#16
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... add address
            lsCommand = [
                'ip', '-4', 'address', 'add',
                '%s/%s' % (self._dsConfig['address'], self._dsConfig['mask'])
            ]

            # ... options
            for sSetting in ['broadcast', 'anycast', 'label', 'scope']:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])
            lsCommand.extend(['dev', self._dsConfig['device']])

            # ... DO IT!
            KiscRuntime.shell(
                lsCommand, _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#17
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(True, KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... add device
            lsCommand = [
                'ip', 'tuntap', 'add',
                'dev', self._dsConfig['name'],
                'mode', self._dsConfig['mode'],
            ]

            # ... options
            for sSetting in ['user', 'group']:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])

            # ... DO IT!
            KiscRuntime.shell(lsCommand, _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#18
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... mkdir ?
            if KiscRuntime.parseBool(self._dsConfig.get('mkdir', True)):
                os.makedirs(self._dsConfig['mountpoint'], exist_ok=True)

            # ... mount device
            lsCommand = ['mount', '-t', self._dsConfig['fstype']]
            if 'options' in self._dsConfig:
                lsCommand.extend(['-o', self._dsConfig['options']])
            lsCommand.extend(
                [self._dsConfig['device'], self._dsConfig['mountpoint']])
            KiscRuntime.shell(
                lsCommand, _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#19
0
    def status(self, _bStateful = True, _iIntent = None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_UNKNOWN

        if _bStateful:

            # Exists ?
            try:
                KiscRuntime.shell(['systemctl', '-q', 'is-active', self._dsConfig['name']], _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                iStatus = KiscRuntime.STATUS_STARTED
            except OSError as e:
                if e.filename == 0:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus 

        # Done
        self._iStatus = iStatus
        if self._iVerbose: self._INFO('Status is %s' % KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#20
0
    def status(self, _bStateful=True, _iIntent=None):
        if self._iVerbose: self._INFO('Querying status')
        iStatus = KiscRuntime.STATUS_UNKNOWN

        if _bStateful:

            # Query domain state
            try:
                sOutput = KiscRuntime.shell(
                    ['virsh', 'domstate', self._dsConfig['name']],
                    _bTrace=self._iVerbose >=
                    KiscRuntime.VERBOSE_TRACE).strip()
                if sOutput is None or not len(sOutput):
                    iStatus = KiscRuntime.STATUS_ERROR
                elif sOutput == 'shut off':
                    iStatus = KiscRuntime.STATUS_STOPPED
                elif sOutput == 'paused':
                    iStatus = KiscRuntime.STATUS_SUSPENDED
                else:
                    iStatus = KiscRuntime.STATUS_STARTED
            except OSError as e:
                if e.filename == 0:
                    iStatus = KiscRuntime.STATUS_STOPPED
                else:
                    if self._iVerbose: self._ERROR(str(e))
                    iStatus = KiscRuntime.STATUS_ERROR

        else:
            iStatus = self._iStatus

        # Done
        self._iStatus = iStatus
        if self._iVerbose:
            self._INFO('Status is %s' %
                       KiscRuntime.STATUS_MESSAGE[self._iStatus])
        return self._iStatus
示例#21
0
    def stop(self):
        if self._iVerbose: self._INFO('Stopping')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STOPPED) == KiscRuntime.STATUS_STOPPED:
            if self._iVerbose: self._INFO('Already stopped')
            try:
                # ... clean-up configuration
                self._cleanup()
            except OSError as e:
                if self._iVerbose: self._ERROR(str(e))
                self._iStatus = KiscRuntime.STATUS_ERROR
                lsErrors.append(str(e))
            return lsErrors

        # Stop the resource
        try:

            # ... timeout
            try:
                iTimeout = int(self._dsConfig['timeout_stop'])
            except (KeyError, ValueError):
                iTimeout = 60

            # ... stop resource
            KiscRuntime.shell(
                [
                    'crm_resource', '-Q', '-r', self._dsConfig['name'], '-m',
                    '-p', 'target-role', '-v', 'Stopped'
                ],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... wait for resource to stop
            while iTimeout >= 0:
                iTimeout -= 1
                if not KiscRuntime.shell(
                    ['crm_resource', '-Q', '-r', self._dsConfig['name'], '-W'],
                        _bTrace=self._iVerbose >=
                        KiscRuntime.VERBOSE_TRACE).strip():
                    break
                if iTimeout < 0:
                    raise RuntimeError('Resource did not stop')
                time.sleep(1)

            # ... clean-up configuration
            self._cleanup()

            # ... done
            self._iStatus = KiscRuntime.STATUS_STOPPED
            if self._iVerbose: self._INFO('Stopped')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#22
0
    def start(self):
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        if self._iVerbose: self._INFO('Starting')
        try:

            # ... satisfy ?
            try:
                iSatisfy = int(self._dsConfig['satisfy'])
            except KeyError:
                iSatisfy = None
            except ValueError:
                raise RuntimeError('Invalid "satisfy" setting (%s)' %
                                   self._dsConfig['satisfy'])
            iSatisfied = 0

            # ... ping command
            lsCommand = ['ping6', '-q', '-n']

            # ... arguments
            lsCommand.extend(['-c', self._dsConfig.get('count', '1')])
            lsCommand.extend(['-i', self._dsConfig.get('interval', '1')])
            lsCommand.extend(['-W', self._dsConfig.get('timeout', '5')])

            # ... options
            if 'deadline' in self._dsConfig:
                lsCommand.extend(['-w', self._dsConfig['deadline']])
            if 'interface' in self._dsConfig:
                lsCommand.extend(['-I', self._dsConfig['interface']])
            if 'mark' in self._dsConfig:
                lsCommand.extend(['-m', self._dsConfig['mark']])
            if 'flow' in self._dsConfig:
                lsCommand.extend(['-F', self._dsConfig['flow']])

            # ... loop through IP address(es)
            iAddresses = 0
            for sAddress in self._dsConfig['address'].split(','):
                sAddress = sAddress.strip()
                if not len(sAddress): continue
                iAddresses += 1
                lsCommand_sub = copy.deepcopy(lsCommand)
                lsCommand_sub.append(sAddress)
                try:
                    KiscRuntime.shell(
                        lsCommand_sub,
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                    iSatisfied += 1
                except OSError as e:
                    if e.filename == 0 and e.errno == 1:
                        # address not reachable
                        pass
                    else:
                        raise e

            # ... satisfied ?
            if iSatisfy is None:
                if iSatisfied < iAddresses:
                    raise RuntimeError('Ping failed (%d<%d)' %
                                       (iSatisfied, iAddresses))
            else:
                if iSatisfied < iSatisfy:
                    raise RuntimeError('Ping failed (%d<%d)' %
                                       (iSatisfied, iSatisfy))
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#23
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... timeout
            try:
                iTimeout = int(self._dsConfig.get('timeout_start', 5))
            except ValueError:
                raise RuntimeError('Invalid timeout value (%s)' %
                                   self._dsConfig['timeout_start'])

            # ... check cache
            if self._sCachedConfigFile is None and 'config_file' in self._dsConfig:
                raise RuntimeError('Configuration file not cached')

            # ... start/create domain
            if 'config_file' not in self._dsConfig:
                KiscRuntime.shell(
                    ['virsh', '-q', 'start', self._dsConfig['name']],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            else:
                KiscRuntime.shell(
                    ['virsh', '-q', 'create', self._sCachedConfigFile],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... wait for domain to start
            while iTimeout >= 0:
                iTimeout -= 1
                try:
                    if KiscRuntime.shell(
                        ['virsh', 'domstate', self._dsConfig['name']],
                            _bTrace=self._iVerbose >=
                            KiscRuntime.VERBOSE_TRACE).strip() == 'running':
                        break
                except OSError as e:
                    if e.filename == 0:
                        pass
                    else:
                        raise e
                if iTimeout < 0:
                    KiscRuntime.shell(
                        ['virsh', '-q', 'destroy', self._dsConfig['name']],
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                    raise RuntimeError('Domain did not stop')
                time.sleep(1)

            # ... done
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#24
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... mkdir ?
            if KiscRuntime.parseBool(self._dsConfig.get('mkdir', True)):
                os.makedirs(os.path.dirname(self._dsConfig['destination']),
                            exist_ok=True)

            # ... pre-cache command ?
            if 'command_pre' in self._dsConfig:
                KiscRuntime.shell(
                    self._dsConfig['command_pre'].split(' '),
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... permissions
            mUser = self._dsConfig.get('user', None)
            mGroup = self._dsConfig.get('group', None)
            try:
                mMode = int(self._dsConfig['mode'], 8)
            except KeyError:
                mMode = None

            # ... cache file
            if 'config_file' in self._dsConfig:
                oClusterConfig = KiscCluster_config(
                    self._dsConfig['config_file'])
                sHost_id = oClusterConfig.getHostByHostname().id()
                oClusterConfig.resolveFile(self._dsConfig['source'],
                                           self._dsConfig['destination'],
                                           _sHost_id=sHost_id,
                                           _tPermissions=(mUser, mGroup,
                                                          mMode))
            else:
                if self._iVerbose:
                    self._DEBUG('Reading file (%s)' % self._dsConfig['source'])
                with open(self._dsConfig['source'], 'r') as oFile:
                    sFile = oFile.read()
                if self._iVerbose:
                    self._DEBUG('Writing file (%s)' %
                                self._dsConfig['destination'])
                iUmask = os.umask(0o077)
                oFile = None
                try:
                    oFile = open(self._dsConfig['destination'], 'w')
                    KiscRuntime.perms(
                        oFile.fileno(),
                        mUser,
                        mGroup,
                        mMode,
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                    oFile.write(sFile)
                finally:
                    if oFile: oFile.close()
                    os.umask(iUmask)

            # ... post-cache command ?
            if 'command_post' in self._dsConfig:
                KiscRuntime.shell(
                    self._dsConfig['command_post'].split(' '),
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... done
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#25
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... load bonding driver (but do not create any default device)
            KiscRuntime.shell(
                ['modprobe', 'bonding', 'max_bonds=0'],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... add device
            lsCommand = [
                'ip',
                'link',
                'add',
                'name',
                self._dsConfig['name'],
            ]
            for sSetting in [
                    'address', 'mtu', 'txqueuelen', 'numtxqueues',
                    'numrxqueues'
            ]:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])
            lsCommand.extend(['type', 'bond', 'mode', self._dsConfig['mode']])
            KiscRuntime.shell(
                lsCommand, _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... options
            for sSetting in [
                    'miimon', 'updelay', 'downdelay', 'use_carrier',
                    'arp_interval', 'arp_ip_target', 'arp_all_targets',
                    'arp_validate', 'primary_reselect', 'all_slaves_active',
                    'fail_over_mac', 'xmit_hash_policy', 'packets_per_slave',
                    'tlb_dynamic_lb', 'lacp_rate', 'ad_select', 'num_grat_arp',
                    'num_unsol_na', 'lp_interval', 'resend_igmp'
            ]:
                if sSetting in self._dsConfig:
                    KiscRuntime.echo(
                        self._dsConfig[sSetting],
                        '/sys/class/net/%s/bonding/%s' %
                        (self._dsConfig['name'], sSetting),
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... attach slaves
            for sDevice in self._dsConfig['devices'].split(','):
                KiscRuntime.shell(
                    [
                        'ip', 'link', 'set', sDevice, 'master',
                        self._dsConfig['name'], 'up'
                    ],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... default/active slave
            for sSetting in ['active_slave', 'primary']:
                if sSetting in self._dsConfig:
                    KiscRuntime.echo(
                        self._dsConfig[sSetting],
                        '/sys/class/net/%s/bonding/%s' %
                        (self._dsConfig['name'], sSetting),
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... UP!
            KiscRuntime.shell(
                ['ip', 'link', 'set', self._dsConfig['name'], 'up'],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#26
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... add device
            lsCommand = [
                'ip',
                'link',
                'add',
                'name',
                self._dsConfig['name'],
            ]
            for sSetting in [
                    'address', 'mtu', 'txqueuelen', 'numtxqueues',
                    'numrxqueues'
            ]:
                if sSetting in self._dsConfig:
                    lsCommand.extend([sSetting, self._dsConfig[sSetting]])
            lsCommand.extend(['type', 'bridge'])
            KiscRuntime.shell(
                lsCommand, _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... options
            for sSetting in [
                    'ageing_time', 'stp_state', 'priority', 'hello_time',
                    'forward_delay', 'max_age'
            ]:
                if sSetting in self._dsConfig:
                    KiscRuntime.echo(
                        self._dsConfig[sSetting],
                        '/sys/class/net/%s/bridge/%s' %
                        (self._dsConfig['name'], sSetting),
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... attach slaves
            for sDevice in self._dsConfig['devices'].split(','):
                KiscRuntime.shell(
                    [
                        'ip', 'link', 'set', sDevice, 'master',
                        self._dsConfig['name'], 'up'
                    ],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... UP!
            KiscRuntime.shell(
                ['ip', 'link', 'set', self._dsConfig['name'], 'up'],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except OSError as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors
示例#27
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... timeout
            try:
                iTimeout = int(self._dsConfig['timeout_start'])
            except KeyError:
                iTimeout = 15
            except ValueError:
                raise RuntimeError('Invalid timeout value (%s)' %
                                   self._dsConfig['timeout_start'])

            # ... check cache
            if self._sCachedResourceFile is None and 'resource_file' in self._dsConfig:
                raise RuntimeError('Resource configuration file not cached')
            if self._sCachedConstraintFile is None and 'constraint_file' in self._dsConfig:
                raise RuntimeError('Constraint configuration file not cached')

            # ... update Pacemaker resource/constraint configuration
            if self._sCachedResourceFile is not None:
                KiscRuntime.shell(
                    [
                        'cibadmin', '-o', 'resources', '-M', '-c', '-x',
                        self._sCachedResourceFile
                    ],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                time.sleep(3)
            if self._sCachedConstraintFile is not None:
                KiscRuntime.shell(
                    [
                        'cibadmin', '-o', 'constraints', '-M', '-c', '-x',
                        self._sCachedConstraintFile
                    ],
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                time.sleep(3)

            # ... start resource
            KiscRuntime.shell(
                [
                    'crm_resource', '-Q', '-r', self._dsConfig['name'], '-m',
                    '-p', 'target-role', '-v', 'Started'
                ],
                _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... wait for resource to start
            while iTimeout >= 0:
                iTimeout -= 1
                if KiscRuntime.shell(
                    ['crm_resource', '-Q', '-r', self._dsConfig['name'], '-W'],
                        _bTrace=self._iVerbose >=
                        KiscRuntime.VERBOSE_TRACE).strip():
                    break
                if iTimeout < 0:
                    raise RuntimeError('Resource did not start')
                time.sleep(1)

            # ... done
            self._iStatus = KiscRuntime.STATUS_STARTED

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors