コード例 #1
0
ファイル: cephadmservice.py プロジェクト: yanest/ceph
    def create_realm_zonegroup_zone(self, spec: RGWSpec, rgw_id: str) -> None:
        if utils.get_cluster_health(self.mgr) != 'HEALTH_OK':
            raise OrchestratorError(
                'Health not ok, will try again when health ok')

        # get keyring needed to run rados commands and strip out just the keyring
        keyring = self.get_keyring(rgw_id).split('key = ', 1)[1].rstrip()

        # We can call radosgw-admin within the container, cause cephadm gives the MGR the required keyring permissions

        def get_realms() -> List[str]:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'realm', 'list', '--format=json'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            out = result.stdout
            if not out:
                return []
            try:
                j = json.loads(out)
                return j.get('realms', [])
            except Exception:
                raise OrchestratorError('failed to parse realm info')

        def create_realm() -> None:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'realm', 'create',
                '--rgw-realm=%s' % spec.rgw_realm, '--default'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            if result.returncode:
                err = 'failed to create RGW realm "%s": %r' % (spec.rgw_realm,
                                                               result.stderr)
                raise OrchestratorError(err)
            self.mgr.log.info('created realm: %s' % spec.rgw_realm)

        def get_zonegroups() -> List[str]:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'zonegroup', 'list', '--format=json'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            out = result.stdout
            if not out:
                return []
            try:
                j = json.loads(out)
                return j.get('zonegroups', [])
            except Exception:
                raise OrchestratorError('failed to parse zonegroup info')

        def create_zonegroup() -> None:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'zonegroup', 'create',
                '--rgw-zonegroup=default', '--master', '--default'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            if result.returncode:
                err = 'failed to create RGW zonegroup "%s": %r' % (
                    'default', result.stderr)
                raise OrchestratorError(err)
            self.mgr.log.info('created zonegroup: default')

        def create_zonegroup_if_required() -> None:
            zonegroups = get_zonegroups()
            if 'default' not in zonegroups:
                create_zonegroup()

        def get_zones() -> List[str]:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'zone', 'list', '--format=json'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            out = result.stdout
            if not out:
                return []
            try:
                j = json.loads(out)
                return j.get('zones', [])
            except Exception:
                raise OrchestratorError('failed to parse zone info')

        def create_zone() -> None:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'zone', 'create', '--rgw-zonegroup=default',
                '--rgw-zone=%s' % spec.rgw_zone, '--master', '--default'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            if result.returncode:
                err = 'failed to create RGW zone "%s": %r' % (spec.rgw_zone,
                                                              result.stderr)
                raise OrchestratorError(err)
            self.mgr.log.info('created zone: %s' % spec.rgw_zone)

        changes = False
        realms = get_realms()
        if spec.rgw_realm not in realms:
            create_realm()
            changes = True

        zones = get_zones()
        if spec.rgw_zone not in zones:
            create_zonegroup_if_required()
            create_zone()
            changes = True

        # update period if changes were made
        if changes:
            cmd = [
                'radosgw-admin',
                '--key=%s' % keyring, '--user',
                'rgw.%s' % rgw_id, 'period', 'update',
                '--rgw-realm=%s' % spec.rgw_realm, '--commit'
            ]
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            if result.returncode:
                err = 'failed to update RGW period: %r' % (result.stderr)
                raise OrchestratorError(err)
            self.mgr.log.info('updated period')
コード例 #2
0
    def create_realm_zonegroup_zone(self, spec: RGWSpec, rgw_id: str):
        if utils.get_cluster_health(self.mgr) != 'HEALTH_OK':
            raise OrchestratorError(
                'Health not ok, will try agin when health ok')

        #get keyring needed to run rados commands and strip out just the keyring
        keyring = self.get_keyring(rgw_id).split('key = ', 1)[1].rstrip()

        # We can call radosgw-admin within the container, cause cephadm gives the MGR the required keyring permissions
        # get realms
        cmd = [
            'radosgw-admin',
            '--key=%s' % keyring, '--user',
            'rgw.%s' % rgw_id, 'realm', 'list', '--format=json'
        ]
        result = subprocess.run(cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        # create realm if needed
        cmd = [
            'radosgw-admin',
            '--key=%s' % keyring, '--user',
            'rgw.%s' % rgw_id, 'realm', 'create',
            '--rgw-realm=%s' % spec.rgw_realm, '--default'
        ]
        if not result.stdout:
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            self.mgr.log.info('created realm: %s' % spec.rgw_realm)
        else:
            try:
                j = json.loads(result.stdout)
                if 'realms' not in j or spec.rgw_realm not in j['realms']:
                    result = subprocess.run(cmd,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
                    self.mgr.log.info('created realm: %s' % spec.rgw_realm)
            except Exception as e:
                raise OrchestratorError('failed to parse realm info')

        # get zonegroup
        cmd = [
            'radosgw-admin',
            '--key=%s' % keyring, '--user',
            'rgw.%s' % rgw_id, 'zonegroup', 'list', '--format=json'
        ]
        result = subprocess.run(cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        #create zonegroup if needed
        cmd = [
            'radosgw-admin',
            '--key=%s' % keyring, '--user',
            'rgw.%s' % rgw_id, 'zonegroup', 'create',
            '--rgw-zonegroup=default', '--master', '--default'
        ]
        if not result.stdout:
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            self.mgr.log.info('created zonegroup: default')
        else:
            try:
                j = json.loads(result.stdout)
                if 'zonegroups' not in j or 'default' not in j['zonegroups']:
                    result = subprocess.run(cmd,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
                    self.mgr.log.info('created zonegroup: default')
            except Exception as e:
                raise OrchestratorError('failed to parse zonegroup info')

        #get zones
        cmd = [
            'radosgw-admin',
            '--key=%s' % keyring, '--user',
            'rgw.%s' % rgw_id, 'zone', 'list', '--format=json'
        ]
        result = subprocess.run(cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        #create zone if needed
        cmd = [
            'radosgw-admin',
            '--key=%s' % keyring, '--user',
            'rgw.%s' % rgw_id, 'zone', 'create', '--rgw-zonegroup=default',
            '--rgw-zone=%s' % spec.rgw_zone, '--master', '--default'
        ]
        if not result.stdout:
            result = subprocess.run(cmd,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            self.mgr.log.info('created zone: %s' % spec.rgw_zone)
        else:
            try:
                j = json.loads(result.stdout)
                if 'zones' not in j or spec.rgw_zone not in j['zones']:
                    result = subprocess.run(cmd,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
                    self.mgr.log.info('created zone: %s' % spec.rgw_zone)
            except Exception as e:
                raise OrchestratorError('failed to parse zone info')