Exemplo n.º 1
0
    def get_package_information(cls):
        """
        Retrieve the installed and candidate versions of all packages relevant for this repository (See PackageFactory.get_package_info)
        If installed version is lower than candidate version, this information is stored
        If installed version is equal or higher than candidate version we verify whether all relevant services have the correct binary active
        Whether a service has the correct binary version in use, we use the ServiceFactory.get_service_update_versions functionality

        In this function the services for each component / package combination are defined
        This service information consists out of:
            * Services to stop (before update) and start (after update of packages) -> 'services_stop_start'
            * Services to restart after update (post-update logic)                  -> 'services_post_update'
            * Down-times which will be caused due to service restarts               -> 'downtime'
            * Prerequisites that have not been met                                  -> 'prerequisites'

        The installed vs candidate version which is displayed always gives priority to the versions effectively installed on the system
        and not the versions as reported by the service files

        This combined information is then stored in the 'package_information' of the ALBA Node DAL object
        :return: Update information
        :rtype: dict
        """
        cls._logger.info('Refreshing update information')

        binaries = cls._package_manager.get_binary_versions(client=cls._local_client)
        update_info = {}
        package_info = PackageFactory.get_packages_to_update(client=cls._local_client)  # {'alba': {'openvstorage-sdm': {'installed': 'ee-1.6.1', 'candidate': 'ee-1.6.2'}}}
        cls._logger.debug('Binary versions found: {0}'.format(binaries))
        cls._logger.debug('Package info found: {0}'.format(package_info))
        for component, package_names in PackageFactory.get_package_info()['names'].iteritems():
            package_names = sorted(package_names)
            cls._logger.debug('Validating component {0} and related packages: {1}'.format(component, package_names))
            if component not in update_info:
                update_info[component] = copy.deepcopy(ServiceFactory.DEFAULT_UPDATE_ENTRY)
            svc_component_info = update_info[component]
            pkg_component_info = package_info.get(component, {})

            for package_name in package_names:
                cls._logger.debug('Validating package {0}'.format(package_name))
                if package_name in [PackageFactory.PKG_ALBA, PackageFactory.PKG_ALBA_EE]:
                    for service_name in sorted(list(ASDController.list_asd_services())) + sorted(list(MaintenanceController.get_services())):
                        service_version = ServiceFactory.get_service_update_versions(client=cls._local_client, service_name=service_name, binary_versions=binaries)
                        cls._logger.debug('Service {0} has version: {1}'.format(service_name, service_version))
                        # If package_name in pkg_component_info --> update available (installed <--> candidate)
                        # If service_version is not None --> service is running an older binary version
                        if package_name in pkg_component_info or service_version is not None:
                            svc_component_info['services_post_update'][20].append(service_name)
                            if service_version is not None and package_name not in svc_component_info['packages']:
                                svc_component_info['packages'][package_name] = service_version

                # Extend the service information with the package information related to this repository for current ALBA Node
                if package_name in pkg_component_info and package_name not in svc_component_info['packages']:
                    cls._logger.debug('Adding package {0} because it has an update available'.format(package_name))
                    svc_component_info['packages'][package_name] = pkg_component_info[package_name]
        cls._logger.info('Refreshed update information')
        return update_info
Exemplo n.º 2
0
 def get_service_status(name):
     # type: (str) -> Union[Tuple[bool, str], None]
     """
     Retrieve the status of the service specified
     :param name: Name of the service to check
     :type name: str
     :return: Status of the service
     :rtype: dict
     """
     client = SSHClient(endpoint='127.0.0.1', username='******')
     service_manager = ServiceFactory.get_manager()
     if service_manager.has_service(name=name, client=client):
         status = service_manager.get_service_status(name=name,
                                                     client=client)
         return status == 'active', status
     return None
Exemplo n.º 3
0
class PostUpdate(object):

    logger = Logger('update')
    service_manager = ServiceFactory.get_manager()

    CURRENT_VERSION = 7

    @staticmethod
    def ensure_directory(file_path):
        """
        Ensures the directory for a given file
        :param file_path: Path to the file
        :return: None
        :rtype: NoneType
        """
        directory = os.path.dirname(file_path)
        try:
            # Safer to capture the exception than to check if the directory exists (which can have race condition problems).
            os.makedirs(directory)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

    @classmethod
    def update(cls):
        """
        Perform an update
        :return: None
        :rtype: NoneType
        """
        try:
            cls.migrate_arakoon_cacc()
            cls.migrate()
        except Exception:
            log_path = cls.logger.get_sink_path('update',
                                                forced_target_type='file')
            msg = 'Exception while updating the package. Please check the logging within {0}'.format(
                log_path)
            print msg  # Print to stdout
            cls.logger.exception(msg)
            exit(1)

    @classmethod
    def migrate_arakoon_cacc(cls):
        # type: () -> None
        """
        Migrate the Arakoon config file. It was moved away from /opt/asd-manager in order to unify all paths
        This is done during the post install as updating the code will point the new configuration to /opt/OpenvStorage/
        If the setup did not run, there is no problem
        :return: None
        :rtype: NoneType
        :raises: AssertionException if the file under /opt/OpenvStorage would not match the one under /opt/asd-manager
        This means that the asd-manager was added to another cluster different to the one that the framework/other components are on
        A machine can only be part of one cluster now.
        """
        if CACC_LOCATION_OLD == CACC_LOCATION:
            # Manual intervention changed the constants
            return
        if os.path.exists(CACC_LOCATION_OLD):
            if os.path.exists(CACC_LOCATION):
                if filecmp.cmp(CACC_LOCATION_OLD, CACC_LOCATION):
                    os.remove(CACC_LOCATION_OLD)
                else:
                    raise AssertionError(
                        'File {0} and {1} are not identical.'
                        'The ASD-manager belongs to a different cluster than the other component on this host.'
                        'This ASD-Manager won\'t be able to resolve Arakoon URL generated by any other component'
                        'If you wish to continue using the file in this location, '
                        'update the /opt/asd-manager/constants/asd.py and change `CACC_LOCATION=CACC_LOCATION_OLD`'
                        'and run '.format(CACC_LOCATION_OLD, CACC_LOCATION))
            else:
                cls.ensure_directory(CACC_LOCATION)
                shutil.copyfile(CACC_LOCATION_OLD, CACC_LOCATION)
                os.remove(CACC_LOCATION_OLD)

    @classmethod
    def migrate(cls):
        # type: () -> None
        """
        Execute the migration logic.
        :return: None
        :rtype: NoneType
        """
        with file_mutex('package_update_pu'):
            local_client = SSHClient(endpoint='127.0.0.1', username='******')

            # Override the created openvstorage_sdm_id during package install, with currently available SDM ID
            if local_client.file_exists(BOOTSTRAP_FILE):
                with open(BOOTSTRAP_FILE) as bstr_file:
                    node_id = json.load(bstr_file)['node_id']
                local_client.file_write(filename='/etc/openvstorage_sdm_id',
                                        contents=node_id + '\n')
            else:
                with open('/etc/openvstorage_sdm_id', 'r') as id_file:
                    node_id = id_file.read().strip()

            key = '{0}/versions'.format(
                ASD_NODE_CONFIG_LOCATION.format(node_id))
            version = Configuration.get(key) if Configuration.exists(
                key) else 0

            asd_manager_service_name = 'asd-manager'
            if cls.service_manager.has_service(
                    asd_manager_service_name,
                    local_client) and cls.service_manager.get_service_status(
                        asd_manager_service_name, local_client) == 'active':
                cls.logger.info('Stopping asd-manager service')
                cls.service_manager.stop_service(asd_manager_service_name,
                                                 local_client)

            # @TODO: Move these migrations to alba_node.client.update_execute_migration_code()
            if version < cls.CURRENT_VERSION:
                try:
                    # DB migrations
                    from source.controllers.asd import ASDController
                    from source.controllers.disk import DiskController
                    from source.dal.asdbase import ASDBase
                    from source.dal.lists.asdlist import ASDList
                    from source.dal.lists.disklist import DiskList
                    from source.dal.objects.asd import ASD

                    if not local_client.file_exists('{0}/main.db'.format(
                            ASDBase.DATABASE_FOLDER)):
                        local_client.dir_create([ASDBase.DATABASE_FOLDER])

                    asd_map = dict(
                        (asd.asd_id, asd) for asd in ASDList.get_asds())
                    DiskController.sync_disks()
                    for disk in DiskList.get_usable_disks():
                        if disk.state == 'MISSING' or disk.mountpoint is None:
                            continue
                        for asd_id in local_client.dir_list(disk.mountpoint):
                            if asd_id in asd_map:
                                asd = asd_map[asd_id]
                            else:
                                asd = ASD()

                            asd.disk = disk
                            asd.asd_id = asd_id
                            asd.folder = asd_id
                            if asd.has_config:
                                if asd.port is None or asd.hosts is None:
                                    config = Configuration.get(
                                        key=asd.config_key)
                                    asd.port = config['port']
                                    asd.hosts = config.get('ips', [])
                                asd.save()

                    # Adjustment of open file descriptors for ASD/maintenance services to 8192
                    asd_service_names = list(ASDController.list_asd_services())
                    maintenance_service_names = list(
                        MaintenanceController.get_services())
                    for service_name in asd_service_names + maintenance_service_names:
                        if cls.service_manager.has_service(
                                name=service_name, client=local_client):
                            if cls.service_manager.__class__ == Systemd:
                                path = '/lib/systemd/system/{0}.service'.format(
                                    service_name)
                                check = 'LimitNOFILE=8192'
                            else:
                                path = '/etc/init/{0}.conf'.format(
                                    service_name)
                                check = 'limit nofile 8192 8192'

                            restart_required = False
                            if os.path.exists(path):
                                with open(path, 'r') as system_file:
                                    if check not in system_file.read():
                                        restart_required = True

                            if restart_required is False:
                                continue

                            configuration_key = ServiceFactory.SERVICE_CONFIG_KEY.format(
                                node_id, service_name)
                            if Configuration.exists(configuration_key):
                                # Rewrite the service file
                                cls.service_manager.add_service(
                                    name=ASDController.ASD_PREFIX
                                    if service_name in asd_service_names else
                                    MaintenanceController.MAINTENANCE_PREFIX,
                                    client=local_client,
                                    params=Configuration.get(
                                        configuration_key),
                                    target_name=service_name)

                                # Let the update know that the ASD / maintenance services need to be restarted
                                # Inside `if Configuration.exists`, because useless to rapport restart if we haven't rewritten service file
                                ExtensionsToolbox.edit_version_file(
                                    client=local_client,
                                    package_name='alba',
                                    old_run_file='{0}/{1}.version'.format(
                                        ServiceFactory.RUN_FILE_DIR,
                                        service_name))
                        if cls.service_manager.__class__ == Systemd:
                            local_client.run(['systemctl', 'daemon-reload'])

                    # Version 3: Addition of 'ExecReload' for ASD/maintenance SystemD services
                    if cls.service_manager.__class__ == Systemd:  # Upstart does not have functionality to reload a process' configuration
                        reload_daemon = False
                        asd_service_names = list(
                            ASDController.list_asd_services())
                        maintenance_service_names = list(
                            MaintenanceController.get_services())
                        for service_name in asd_service_names + maintenance_service_names:
                            if not cls.service_manager.has_service(
                                    name=service_name, client=local_client):
                                continue

                            path = '/lib/systemd/system/{0}.service'.format(
                                service_name)
                            if os.path.exists(path):
                                with open(path, 'r') as system_file:
                                    if 'ExecReload' not in system_file.read():
                                        reload_daemon = True
                                        configuration_key = ServiceFactory.SERVICE_CONFIG_KEY.format(
                                            node_id, service_name)
                                        if Configuration.exists(
                                                configuration_key):
                                            # No need to edit the service version file, since this change only requires a daemon-reload
                                            cls.service_manager.add_service(
                                                name=ASDController.ASD_PREFIX
                                                if service_name
                                                in asd_service_names else
                                                MaintenanceController.
                                                MAINTENANCE_PREFIX,
                                                client=local_client,
                                                params=Configuration.get(
                                                    configuration_key),
                                                target_name=service_name)
                        if reload_daemon is True:
                            local_client.run(['systemctl', 'daemon-reload'])

                    # Version 6: Introduction of Active Drive
                    all_local_ips = OSFactory.get_manager().get_ip_addresses(
                        client=local_client)
                    for asd in ASDList.get_asds():
                        if asd.has_config:
                            asd_config = Configuration.get(asd.config_key)
                            if 'multicast' not in asd_config:
                                asd_config['multicast'] = None
                            if 'ips' in asd_config:
                                asd_ips = asd_config['ips'] or all_local_ips
                            else:
                                asd_ips = all_local_ips
                            asd.hosts = asd_ips
                            asd_config['ips'] = asd_ips
                            Configuration.set(asd.config_key, asd_config)
                            asd.save()

                    # Version 7: Moving flask certificate files to config dir
                    for file_name in [
                            'passphrase', 'server.crt', 'server.csr',
                            'server.key'
                    ]:
                        if local_client.file_exists(
                                '/opt/asd-manager/source/{0}'.format(
                                    file_name)):
                            local_client.file_move(
                                source_file_name='/opt/asd-manager/source/{0}'.
                                format(file_name),
                                destination_file_name=
                                '/opt/asd-manager/config/{0}'.format(
                                    file_name))
                except:
                    cls.logger.exception(
                        'Error while executing post-update code on node {0}'.
                        format(node_id))
            Configuration.set(key, cls.CURRENT_VERSION)

            if cls.service_manager.has_service(
                    asd_manager_service_name,
                    local_client) and cls.service_manager.get_service_status(
                        asd_manager_service_name, local_client) != 'active':
                cls.logger.info('Starting asd-manager service')
                cls.service_manager.start_service(asd_manager_service_name,
                                                  local_client)

        cls.logger.info('Post-update logic executed')
Exemplo n.º 4
0
class SDMUpdateController(object):
    """
    Update Controller class for SDM package
    """
    _local_client = SSHClient(endpoint='127.0.0.1', username='******')
    _logger = Logger(name='update', forced_target_type='file')
    _package_manager = PackageFactory.get_manager()
    _service_manager = ServiceFactory.get_manager()

    @classmethod
    def get_package_information(cls):
        """
        Retrieve the installed and candidate versions of all packages relevant for this repository (See PackageFactory.get_package_info)
        If installed version is lower than candidate version, this information is stored
        If installed version is equal or higher than candidate version we verify whether all relevant services have the correct binary active
        Whether a service has the correct binary version in use, we use the ServiceFactory.get_service_update_versions functionality

        In this function the services for each component / package combination are defined
        This service information consists out of:
            * Services to stop (before update) and start (after update of packages) -> 'services_stop_start'
            * Services to restart after update (post-update logic)                  -> 'services_post_update'
            * Down-times which will be caused due to service restarts               -> 'downtime'
            * Prerequisites that have not been met                                  -> 'prerequisites'

        The installed vs candidate version which is displayed always gives priority to the versions effectively installed on the system
        and not the versions as reported by the service files

        This combined information is then stored in the 'package_information' of the ALBA Node DAL object
        :return: Update information
        :rtype: dict
        """
        cls._logger.info('Refreshing update information')

        binaries = cls._package_manager.get_binary_versions(client=cls._local_client)
        update_info = {}
        package_info = PackageFactory.get_packages_to_update(client=cls._local_client)  # {'alba': {'openvstorage-sdm': {'installed': 'ee-1.6.1', 'candidate': 'ee-1.6.2'}}}
        cls._logger.debug('Binary versions found: {0}'.format(binaries))
        cls._logger.debug('Package info found: {0}'.format(package_info))
        for component, package_names in PackageFactory.get_package_info()['names'].iteritems():
            package_names = sorted(package_names)
            cls._logger.debug('Validating component {0} and related packages: {1}'.format(component, package_names))
            if component not in update_info:
                update_info[component] = copy.deepcopy(ServiceFactory.DEFAULT_UPDATE_ENTRY)
            svc_component_info = update_info[component]
            pkg_component_info = package_info.get(component, {})

            for package_name in package_names:
                cls._logger.debug('Validating package {0}'.format(package_name))
                if package_name in [PackageFactory.PKG_ALBA, PackageFactory.PKG_ALBA_EE]:
                    for service_name in sorted(list(ASDController.list_asd_services())) + sorted(list(MaintenanceController.get_services())):
                        service_version = ServiceFactory.get_service_update_versions(client=cls._local_client, service_name=service_name, binary_versions=binaries)
                        cls._logger.debug('Service {0} has version: {1}'.format(service_name, service_version))
                        # If package_name in pkg_component_info --> update available (installed <--> candidate)
                        # If service_version is not None --> service is running an older binary version
                        if package_name in pkg_component_info or service_version is not None:
                            svc_component_info['services_post_update'][20].append(service_name)
                            if service_version is not None and package_name not in svc_component_info['packages']:
                                svc_component_info['packages'][package_name] = service_version

                # Extend the service information with the package information related to this repository for current ALBA Node
                if package_name in pkg_component_info and package_name not in svc_component_info['packages']:
                    cls._logger.debug('Adding package {0} because it has an update available'.format(package_name))
                    svc_component_info['packages'][package_name] = pkg_component_info[package_name]
        cls._logger.info('Refreshed update information')
        return update_info

    @classmethod
    def update(cls, package_name):
        """
        Update the package on the local node
        :return: None
        :rtype: NoneType
        """
        cls._logger.info('Installing package {0}'.format(package_name))
        cls._package_manager.install(package_name=package_name, client=cls._local_client)
        cls._logger.info('Installed package {0}'.format(package_name))

    @classmethod
    def get_installed_version_for_package(cls, package_name):
        """
        Retrieve the currently installed package version
        :param package_name: Name of the package to retrieve the version for
        :type package_name: str
        :return: Version of the currently installed package
        :rtype: str
        """
        installed_version = cls._package_manager.get_installed_versions(client=None, package_names=[package_name])
        if package_name in installed_version:
            return str(installed_version[package_name])

    @classmethod
    def restart_services(cls, service_names):
        """
        Restart the services specified
        :param service_names: Names of the services to restart
        :type service_names: list[str]
        :return: None
        :rtype: NoneType
        """
        if len(service_names) == 0:
            service_names = [service_name for service_name in ASDController.list_asd_services()]
            service_names.extend([service_name for service_name in MaintenanceController.get_services()])

        for service_name in service_names:
            cls._logger.warning('Verifying whether service {0} needs to be restarted'.format(service_name))
            if cls._service_manager.get_service_status(service_name, cls._local_client) != 'active':
                cls._logger.warning('Found stopped service {0}. Will not start it.'.format(service_name))
                continue

            cls._logger.info('Restarting service {0}'.format(service_name))
            try:
                cls._service_manager.restart_service(service_name, cls._local_client)
            except CalledProcessError:
                cls._logger.exception('Failed to restart service {0}'.format(service_name))

    @classmethod
    def execute_migration_code(cls):
        # type: () -> None
        """
        Run some migration code after an update has been done
        :return: None
        :rtype: NoneType
        """
        cls._logger.info('Starting out of band migrations for SDM nodes')

        ###########################
        # Start crucial migration #
        ###########################

        # Removal of bootstrap file and store API IP, API port and node ID in SQLite DB
        try:
            if cls._local_client.file_exists(BOOTSTRAP_FILE):
                cls._logger.info('Bootstrap file still exists. Retrieving node ID')
                with open(BOOTSTRAP_FILE) as bstr_file:
                    node_id = json.load(bstr_file)['node_id']
            else:
                node_id = SettingList.get_setting_by_code(code='node_id').value
        except Exception:
            cls._logger.exception('Unable to determine the node ID, cannot migrate')
            raise

        try:
            api_settings_map = {'api_ip': 'ip', 'api_port': 'port'}  # Map settings code to keys in the Config management
            required_settings = ['node_id', 'migration_version'] + api_settings_map.keys()
            for settings_code in required_settings:
                try:
                    _ = SettingList.get_setting_by_code(settings_code)
                except ObjectNotFoundException:
                    cls._logger.info('Missing required settings: {0}'.format(settings_code))
                    if settings_code == 'node_id':
                        value = node_id
                    elif settings_code in api_settings_map.keys():
                        # Information must be extracted from Configuration
                        main_config = Configuration.get(ASD_NODE_CONFIG_MAIN_LOCATION.format(node_id))
                        value = main_config[api_settings_map[settings_code]]
                    elif settings_code == 'migration_version':
                        # Introduce version for ASD Manager migration code
                        value = 0
                    else:
                        raise NotImplementedError('No action implemented for setting {0}'.format(settings_code))

                    cls._logger.info('Modeling Setting with code {0} and value {1}'.format(settings_code, value))
                    setting = Setting()
                    setting.code = settings_code
                    setting.value = value
                    setting.save()

            if cls._local_client.file_exists(BOOTSTRAP_FILE):
                cls._logger.info('Removing the bootstrap file')
                cls._local_client.file_delete(BOOTSTRAP_FILE)
        except Exception:
            cls._logger.exception('Error during migration of code settings. Unable to proceed')
            raise

        ###############################
        # Start non-crucial migration #
        ###############################

        errors = []
        migration_setting = SettingList.get_setting_by_code(code='migration_version')
        # Add installed package_name in version files and additional string replacements in service files
        try:
            if migration_setting.value < 1:
                cls._logger.info('Adding additional information to service files')
                edition = Configuration.get_edition()
                if edition == PackageFactory.EDITION_ENTERPRISE:
                    for version_file_name in cls._local_client.file_list(directory=ServiceFactory.RUN_FILE_DIR):
                        version_file_path = '{0}/{1}'.format(ServiceFactory.RUN_FILE_DIR, version_file_name)
                        contents = cls._local_client.file_read(filename=version_file_path)
                        if '{0}='.format(PackageFactory.PKG_ALBA) in contents:
                            contents = contents.replace(PackageFactory.PKG_ALBA, PackageFactory.PKG_ALBA_EE)
                            cls._local_client.file_write(filename=version_file_path, contents=contents)

                    node_id = SettingList.get_setting_by_code(code='node_id').value
                    asd_services = list(ASDController.list_asd_services())
                    maint_services = list(MaintenanceController.get_services())
                    for service_name in asd_services + maint_services:
                        config_key = ServiceFactory.SERVICE_CONFIG_KEY.format(node_id, service_name)
                        if Configuration.exists(key=config_key):
                            config = Configuration.get(key=config_key)
                            if 'RUN_FILE_DIR' in config:
                                continue
                            config['RUN_FILE_DIR'] = ServiceFactory.RUN_FILE_DIR
                            config['ALBA_PKG_NAME'] = PackageFactory.PKG_ALBA_EE
                            config['ALBA_VERSION_CMD'] = PackageFactory.VERSION_CMD_ALBA
                            Configuration.set(key=config_key, value=config)
                            cls._service_manager.regenerate_service(name=ASDController.ASD_PREFIX if service_name in asd_services else MaintenanceController.MAINTENANCE_PREFIX,
                                                                    client=cls._local_client,
                                                                    target_name=service_name)
        except Exception as ex:
            cls._logger.exception('Failed to regenerate the ASD and Maintenance services')
            errors.append(ex)

        try:
            if migration_setting.value < 2:
                if System.get_component_identifier() not in Configuration.get(Configuration.get_registration_key(), default=[]):
                    Configuration.register_usage(System.get_component_identifier())
        except Exception as ex:
            cls._logger.exception('Failed to register the asd-manager')
            errors.append(ex)

        if len(errors) == 0:
            cls._logger.info('No errors during non-crucial migration. Saving the migration setting')
            # Save migration settings when no errors occurred
            migration_setting = SettingList.get_setting_by_code(code='migration_version')
            migration_setting.value = 2
            migration_setting.save()

        cls._logger.info('Finished out of band migrations for SDM nodes')
Exemplo n.º 5
0
class ASDController(object):
    """
    ASD Controller class
    """
    ASD_PREFIX = 'alba-asd'
    _logger = Logger('controllers')
    _local_client = SSHClient(endpoint='127.0.0.1', username='******')
    _service_manager = ServiceFactory.get_manager()

    @staticmethod
    def calculate_rocksdb_cache_size(is_ssd):
        """
        Calculate the cache size for the RocksDB
        :param disk: disk on which the asd is running
        :type disk: source.dal.objects.disk.Disk
        :return: None or int
        """
        if is_ssd:  # No cache size is required to be specified for ASDs
            return None
        else:
            return 128 * 1024 * 1024  # 128 MiB

    @staticmethod
    def create_asd(disk):
        """
        Creates and starts an ASD on a given disk
        :param disk: Disk on which to create an ASD
        :type disk: source.dal.objects.disk.Disk
        :return: None
        :rtype: NoneType
        """
        # Validations
        if disk.state == 'MISSING':
            raise RuntimeError(
                'Cannot create an ASD on missing disk {0}'.format(disk.name))

        _node_id = SettingList.get_setting_by_code(code='node_id').value
        ipaddresses = Configuration.get('{0}|ips'.format(
            ASD_NODE_CONFIG_NETWORK_LOCATION.format(_node_id)))
        if len(ipaddresses) == 0:
            ipaddresses = OSFactory.get_manager().get_ip_addresses(
                client=ASDController._local_client)
            if len(ipaddresses) == 0:
                raise RuntimeError('Could not find any IP on the local node')

        alba_pkg_name, alba_version_cmd = PackageFactory.get_package_and_version_cmd_for(
            component='alba'
        )  # Call here, because this potentially raises error, which should happen before actually making changes

        # Fetch disk information
        disk_size = int(
            ASDController._local_client.run(
                ['df', '-B', '1', '--output=size', disk.mountpoint],
                timeout=5).splitlines()[1])

        # Find out appropriate disk size
        asd_size = int(math.floor(disk_size / (len(disk.asds) + 1)))
        for asd in disk.asds:
            if asd.has_config:
                config = Configuration.get(asd.config_key)
                config['capacity'] = asd_size
                cache_size = ASDController.calculate_rocksdb_cache_size(
                    is_ssd=disk.is_ssd)
                if cache_size:
                    config.update({'rocksdb_block_cache_size': cache_size})
                Configuration.set(asd.config_key, config)
                try:
                    ASDController._service_manager.send_signal(
                        asd.service_name, signal.SIGUSR1,
                        ASDController._local_client)
                except Exception as ex:
                    ASDController._logger.info(
                        'Could not send signal to ASD for reloading the quota: {0}'
                        .format(ex))

        used_ports = []
        for asd in ASDList.get_asds():
            if asd.has_config:
                config = Configuration.get(asd.config_key)
                used_ports.append(config['port'])
                if 'rora_port' in config:
                    used_ports.append(config['rora_port'])

        # Prepare & start service
        ASDController._logger.info('Setting up service for disk {0}'.format(
            disk.name))
        asd_id = ''.join(
            random.choice(string.ascii_letters + string.digits)
            for _ in range(32))
        homedir = '{0}/{1}'.format(disk.mountpoint, asd_id)
        base_port = Configuration.get('{0}|port'.format(
            ASD_NODE_CONFIG_NETWORK_LOCATION.format(_node_id)))

        asd_port = base_port
        rora_port = base_port + 1
        while asd_port in used_ports:
            asd_port += 1
        used_ports.append(asd_port)
        while rora_port in used_ports:
            rora_port += 1

        asd_config = {
            'ips': ipaddresses,
            'home': homedir,
            'port': asd_port,
            'asd_id': asd_id,
            'node_id': _node_id,
            'capacity': asd_size,
            'multicast': None,
            'transport': 'tcp',
            'log_level': 'info'
        }
        cache_size = ASDController.calculate_rocksdb_cache_size(
            is_ssd=disk.is_ssd)
        if cache_size:
            asd_config.update({'rocksdb_block_cache_size': cache_size})
        if Configuration.get('/ovs/framework/rdma'):
            asd_config['rora_port'] = rora_port
            asd_config['rora_transport'] = 'rdma'

        if Configuration.exists('{0}/extra'.format(
                ASD_NODE_CONFIG_LOCATION.format(_node_id))):
            data = Configuration.get('{0}/extra'.format(
                ASD_NODE_CONFIG_LOCATION.format(_node_id)))
            asd_config.update(data)

        asd = ASD()
        asd.disk = disk
        asd.port = asd_port
        asd.hosts = ipaddresses
        asd.asd_id = asd_id
        asd.folder = asd_id
        asd.save()

        Configuration.set(asd.config_key, asd_config)
        params = {
            'LOG_SINK': Logger.get_sink_path('alba-asd_{0}'.format(asd_id)),
            'CONFIG_PATH':
            Configuration.get_configuration_path(asd.config_key),
            'SERVICE_NAME': asd.service_name,
            'ALBA_PKG_NAME': alba_pkg_name,
            'ALBA_VERSION_CMD': alba_version_cmd
        }
        os.mkdir(homedir)
        ASDController._local_client.run(['chown', '-R', 'alba:alba', homedir])
        ASDController._service_manager.add_service(
            name=ASDController.ASD_PREFIX,
            client=ASDController._local_client,
            params=params,
            target_name=asd.service_name)
        ASDController.start_asd(asd)

    @staticmethod
    def update_asd(asd, update_data):
        """
        Updates an ASD with the 'update_data' provided
        :param asd: ASD to update
        :type asd: source.dal.objects.asd.ASD
        :param update_data: Data to update
        :type update_data: dict
        :raises ValueError: - When ASD configuration key is not present
                            - When an unsupported key is passed in via 'update_data'
        :return: None
        :rtype: NoneType
        """
        key_map = {'ips': 'hosts'}
        if not Configuration.exists(asd.config_key):
            raise ValueError(
                'Failed to the configuration at location {0}'.format(
                    asd.config_key))

        config = Configuration.get(asd.config_key)
        for key, value in update_data.iteritems():
            if key not in key_map:  # Only updating IPs is supported for now
                raise ValueError(
                    'Unsupported property provided: {0}. Only IPs can be updated for now'
                    .format(key))
            setattr(asd, key_map[key], value)
            config[key] = value
        asd.save()
        Configuration.set(key=asd.config_key, value=config)

    @staticmethod
    def remove_asd(asd):
        """
        Remove an ASD
        :param asd: ASD to remove
        :type asd: source.dal.objects.asd.ASD
        :return: None
        :rtype: NoneType
        """
        if ASDController._service_manager.has_service(
                asd.service_name, ASDController._local_client):
            ASDController._service_manager.stop_service(
                asd.service_name, ASDController._local_client)
            ASDController._service_manager.remove_service(
                asd.service_name, ASDController._local_client)
        try:
            ASDController._local_client.dir_delete('{0}/{1}'.format(
                asd.disk.mountpoint, asd.asd_id))
        except Exception:
            ASDController._logger.exception('Could not clean ASD data')
        Configuration.delete(asd.config_key)
        asd.delete()

    @staticmethod
    def start_asd(asd):
        """
        Start an ASD
        :param asd: ASD to start
        :type asd: source.dal.objects.asd.ASD
        :return: None
        :rtype: NoneType
        """
        if ASDController._service_manager.has_service(
                asd.service_name, ASDController._local_client):
            ASDController._service_manager.start_service(
                asd.service_name, ASDController._local_client)

    @staticmethod
    def stop_asd(asd):
        """
        Stop an ASD
        :param asd: ASD to stop
        :type asd: source.dal.objects.asd.ASD
        :return: None
        :rtype: NoneType
        """
        if ASDController._service_manager.has_service(
                asd.service_name, ASDController._local_client):
            ASDController._service_manager.stop_service(
                asd.service_name, ASDController._local_client)

    @staticmethod
    def restart_asd(asd):
        """
        Restart an ASD
        :param asd: ASD to restart
        :type asd: source.dal.objects.asd.ASD
        :return: None
        :rtype: NoneType
        """
        if ASDController._service_manager.has_service(
                asd.service_name, ASDController._local_client):
            ASDController._service_manager.restart_service(
                asd.service_name, ASDController._local_client)

    @staticmethod
    def list_asd_services():
        """
        Retrieve all ASD services
        :return: The ASD Services present on this ALBA Node
        :rtype: generator
        """
        for service_name in ASDController._service_manager.list_services(
                ASDController._local_client):
            if service_name.startswith(ASD.ASD_SERVICE_PREFIX.format('')):
                yield service_name
Exemplo n.º 6
0
class ASD(ASDBase):
    """
    Represents an ASD that has been deployed.
    """

    ASD_CONFIG = '/ovs/alba/asds/{0}/config'
    ASD_SERVICE_PREFIX = 'alba-asd-{0}'
    _local_client = SSHClient(endpoint='127.0.0.1', username='******')
    _service_manager = ServiceFactory.get_manager()

    _table = 'asd'
    _properties = [
        Property(name='port', property_type=int, unique=True, mandatory=True),
        Property(name='hosts',
                 property_type=list,
                 unique=False,
                 mandatory=True),
        Property(name='asd_id', property_type=str, unique=True,
                 mandatory=True),
        Property(name='folder',
                 property_type=str,
                 unique=False,
                 mandatory=False)
    ]
    _relations = [['disk', Disk, 'asds']]
    _dynamics = ['service_name', 'config_key', 'has_config']

    def _service_name(self):
        return ASD.ASD_SERVICE_PREFIX.format(self.asd_id)

    def _config_key(self):
        return ASD.ASD_CONFIG.format(self.asd_id)

    def _has_config(self):
        return Configuration.exists(self.config_key)

    def export(self):
        """
        Exports the ASD information to a dict structure
        :return: Representation of the ASD as dict
        :rtype: dict
        """
        if not self.has_config:
            raise RuntimeError('No configuration found for ASD {0}'.format(
                self.asd_id))
        data = Configuration.get(self.config_key)
        for prop in self._properties:
            if prop.name == 'hosts':
                data['ips'] = getattr(self, prop.name)
            else:
                data[prop.name] = getattr(self, prop.name)
        if self.disk.state == 'MISSING':
            data.update({'state': 'error', 'state_detail': 'missing'})
        else:
            output, error = ASD._local_client.run(
                ['ls', '{0}/{1}/'.format(self.disk.mountpoint, self.folder)],
                allow_nonzero=True,
                return_stderr=True)
            output += error
            if 'Input/output error' in output:
                data.update({'state': 'error', 'state_detail': 'io_error'})
            elif ASD._service_manager.has_service(self.service_name,
                                                  ASD._local_client):
                service_state = ASD._service_manager.get_service_status(
                    self.service_name, ASD._local_client)
                if service_state == 'activating':
                    data.update({
                        'state': 'warning',
                        'state_detail': 'service_activating'
                    })
                elif service_state == 'active':
                    data.update({'state': 'ok', 'state_detail': None})
                else:
                    data.update({
                        'state': 'error',
                        'state_detail': 'service_failure'
                    })
            else:
                data.update({
                    'state': 'error',
                    'state_detail': 'service_failure'
                })
        return data
Exemplo n.º 7
0
def setup():
    """
    Interactive setup part for initial asd manager configuration
    """
    _print_and_log(message=Interactive.boxed_message(['ASD Manager setup']))

    # Gather information
    ipaddresses = OSFactory.get_manager().get_ip_addresses()
    if not ipaddresses:
        _print_and_log(
            level='error',
            message='\n' + Interactive.boxed_message(
                ['Could not retrieve IP information on local node']))
        sys.exit(1)
    validation_ip_addresses = copy.deepcopy(ipaddresses)

    local_client = SSHClient(endpoint='127.0.0.1', username='******')
    service_manager = ServiceFactory.get_manager()
    if service_manager.has_service(MANAGER_SERVICE, local_client):
        _print_and_log(level='error',
                       message='\n' + Interactive.boxed_message(
                           ['The ASD Manager is already installed.']))
        sys.exit(1)

    config = _validate_and_retrieve_pre_config()
    interactive = len(config) == 0
    ipmi_info = {'ip': None, 'username': None, 'pwd': None}

    if interactive is False:
        api_ip = config['api_ip']
        api_port = config.get('api_port', 8500)
        asd_ips = config.get('asd_ips', [])
        asd_start_port = config.get('asd_start_port', 8600)
        configuration_store = config.get('configuration_store', 'arakoon')
        ipmi_info = config.get('ipmi', ipmi_info)
    else:
        api_ip = Interactive.ask_choice(
            choice_options=ipaddresses,
            question='Select the public IP address to be used for the API',
            sort_choices=True)
        api_port = Interactive.ask_integer(
            question="Select the port to be used for the API",
            min_value=1025,
            max_value=65535,
            default_value=8500)
        asd_ips = []
        add_ips = True
        ipaddresses.append('All')
        while add_ips:
            current_ips = ' - Current selected IPs: {0}'.format(asd_ips)
            new_asd_ip = Interactive.ask_choice(
                choice_options=ipaddresses,
                question=
                "Select an IP address to be used for the ASDs or 'All' (All current and future interfaces: 0.0.0.0){0}"
                .format(current_ips if len(asd_ips) > 0 else ''),
                default_value='All')
            if new_asd_ip == 'All':
                ipaddresses.remove('All')
                asd_ips = [
                ]  # Empty list maps to all IPs - checked when configuring ASDs
                add_ips = False
            else:
                asd_ips.append(new_asd_ip)
                ipaddresses.remove(new_asd_ip)
                add_ips = Interactive.ask_yesno(
                    "Do you want to add another IP?")
        asd_start_port = Interactive.ask_integer(
            question="Select the port to be used for the ASDs",
            min_value=1025,
            max_value=65435,
            default_value=8600)
        configuration_store = 'arakoon'

        message = 'Do you want to set IPMI configuration keys?'
        proceed = Interactive.ask_yesno(message=message, default_value=False)
        if proceed is True:
            ipmi_info['ip'] = Interactive.ask_string(
                message='Enter the IPMI IP address',
                regex_info={'regex': ExtensionsToolbox.regex_ip})
            ipmi_info['username'] = Interactive.ask_string(
                message='Enter the IPMI username')
            ipmi_info['pwd'] = Interactive.ask_password(
                message='Enter the IPMI password')

    if api_ip not in validation_ip_addresses:
        _print_and_log(
            level='error',
            message='\n' + Interactive.boxed_message(lines=[
                'Invalid API IP {0} specified. Please choose from:'.format(
                    api_ip)
            ] + ['  * {0}'.format(ip) for ip in ipaddresses]))
        sys.exit(1)
    different_ips = set(asd_ips).difference(set(validation_ip_addresses))
    if different_ips:
        _print_and_log(
            level='error',
            message='\n' + Interactive.boxed_message(lines=[
                'Invalid ASD IPs {0} specified. Please choose from:'.format(
                    asd_ips)
            ] + ['  * {0}'.format(ip) for ip in ipaddresses]))
        sys.exit(1)

    if api_port in range(asd_start_port, asd_start_port + 100):
        _print_and_log(
            level='error',
            message='\n' + Interactive.boxed_message(
                ['API port cannot be in the range of the ASD port + 100']))
        sys.exit(1)

    if interactive is True:
        while not local_client.file_exists(CACC_LOCATION):
            _print_and_log(
                level='warning',
                message=
                ' - Please place a copy of the Arakoon\'s client configuration file at: {0}'
                .format(CACC_LOCATION))
            Interactive.ask_continue()

    local_client.file_write(filename=CONFIG_STORE_LOCATION,
                            contents=json.dumps(
                                {'configuration_store': configuration_store},
                                indent=4))

    node_id = Configuration.initialize(
        config={
            'api_ip': api_ip,
            'asd_ips': asd_ips,
            'api_port': api_port,
            'asd_start_port': asd_start_port,
            'ipmi': ipmi_info
        })

    # Model settings
    _print_and_log(message=' - Store settings in DB')
    for code, value in {
            'api_ip': api_ip,
            'api_port': api_port,
            'configuration_store': configuration_store,
            'node_id': node_id
    }.iteritems():
        setting = Setting()
        setting.code = code
        setting.value = value
        setting.save()

    # Deploy/start services
    _print_and_log(message=' - Deploying and starting services')
    service_manager.add_service(name=MANAGER_SERVICE, client=local_client)
    service_manager.add_service(name=WATCHER_SERVICE, client=local_client)
    _print_and_log(message=' - Starting watcher service')
    try:
        service_manager.start_service(name=WATCHER_SERVICE,
                                      client=local_client)
    except Exception:
        Configuration.uninitialize()
        _print_and_log(level='exception',
                       message='\n' +
                       Interactive.boxed_message(['Starting watcher failed']))
        sys.exit(1)

    _print_and_log(message='\n' +
                   Interactive.boxed_message(['ASD Manager setup completed']))
Exemplo n.º 8
0
def remove(silent=None):
    """
    Interactive removal part for the ASD manager
    :param silent: If silent == '--force-yes' no question will be asked to confirm the removal
    :type silent: str
    :return: None
    :rtype: NoneType
    """
    _print_and_log(message='\n' +
                   Interactive.boxed_message(['ASD Manager removal']))

    local_client = SSHClient(endpoint='127.0.0.1', username='******')
    if not local_client.file_exists(
            filename='{0}/main.db'.format(Setting.DATABASE_FOLDER)):
        _print_and_log(level='error',
                       message='\n' + Interactive.boxed_message(
                           ['The ASD Manager has already been removed']))
        sys.exit(1)

    _print_and_log(message=' - Validating configuration management')
    try:
        Configuration.list(key='ovs')
    except:
        _print_and_log(
            level='exception',
            message='\n' +
            Interactive.boxed_message(['Could not connect to Arakoon']))
        sys.exit(1)

    _print_and_log(message='  - Retrieving ASD information')
    all_asds = {}
    try:
        all_asds = ASDList.get_asds()
    except:
        _print_and_log(level='exception',
                       message='  - Failed to retrieve the ASD information')

    interactive = silent != '--force-yes'
    if interactive is True:
        message = 'Are you sure you want to continue?'
        if len(all_asds) > 0:
            _print_and_log(message='\n\n+++ ALERT +++\n', level='warning')
            message = 'DATA LOSS possible if proceeding! Continue?'

        proceed = Interactive.ask_yesno(message=message, default_value=False)
        if proceed is False:
            _print_and_log(level='error',
                           message='\n' +
                           Interactive.boxed_message(['Abort removal']))
            sys.exit(1)

    if len(all_asds) > 0:
        _print_and_log(message=' - Removing disks')
        for disk in DiskList.get_disks():
            if disk.available is True:
                continue
            try:
                _print_and_log(
                    message='    - Retrieving ASD information for disk {0}'.
                    format(disk.name))
                for asd in disk.asds:
                    _print_and_log(
                        message='      - Removing ASD {0}'.format(asd.name))
                    ASDController.remove_asd(asd)
                DiskController.clean_disk(disk)
            except Exception:
                _print_and_log(level='exception',
                               message='    - Deleting ASDs failed')

    _print_and_log(message=' - Removing services')
    service_manager = ServiceFactory.get_manager()
    for service in MaintenanceController.get_services():
        service_name = service
        _print_and_log(
            message='    - Removing service {0}'.format(service_name))
        guid = None
        for alba_backend_guid in Configuration.list(key='/ovs/alba/backends'):
            for maintenance_service_name in Configuration.list(
                    key='/ovs/alba/backends/{0}/maintenance/'.format(
                        alba_backend_guid)):
                if maintenance_service_name == service_name:
                    guid = alba_backend_guid
                    break
        MaintenanceController.remove_maintenance_service(
            name=service_name, alba_backend_guid=guid)

    for service_name in [WATCHER_SERVICE, MANAGER_SERVICE]:
        if service_manager.has_service(name=service_name, client=local_client):
            _print_and_log(
                message='   - Removing service {0}'.format(service_name))
            service_manager.stop_service(name=service_name,
                                         client=local_client)
            service_manager.remove_service(name=service_name,
                                           client=local_client)

    _print_and_log(message=' - Removing from configuration management')
    remaining_users = Configuration.uninitialize()
    if not remaining_users:
        local_client.file_delete(filenames=CACC_LOCATION)

    local_client.file_delete(
        filenames='{0}/main.db'.format(Setting.DATABASE_FOLDER))
    _print_and_log(
        message='\n' +
        Interactive.boxed_message(['ASD Manager removal completed']))
Exemplo n.º 9
0
 def _get_service_manager(cls):
     return ServiceFactory.get_manager()
Exemplo n.º 10
0
class MaintenanceController(object):
    """
    Maintenance controller class
    """
    MAINTENANCE_KEY = '/ovs/alba/backends/{0}/maintenance/{1}'
    MAINTENANCE_PREFIX = 'alba-maintenance'
    _local_client = SSHClient(endpoint='127.0.0.1', username='******')
    _service_manager = ServiceFactory.get_manager()

    @staticmethod
    def get_services():
        """
        Retrieve all configured maintenance services running on this node for each backend
        :return: The maintenance services present on this ALBA Node
        :rtype: generator
        """
        for service_name in MaintenanceController._service_manager.list_services(
                MaintenanceController._local_client):
            if service_name.startswith(
                    MaintenanceController.MAINTENANCE_PREFIX):
                yield service_name

    @staticmethod
    def add_maintenance_service(name,
                                alba_backend_guid,
                                abm_name,
                                read_preferences=None):
        """
        Add a maintenance service with a specific name
        :param name: Name of the maintenance service to add
        :type name: str
        :param alba_backend_guid: ALBA Backend GUID for which the maintenance service needs to run
        :type alba_backend_guid: str
        :param abm_name: Name of the ABM cluster
        :type abm_name: str
        :param read_preferences: List of ALBA Node IDs (LOCAL) or ALBA IDs of linked ALBA Backends (GLOBAL) for the maintenance services where they should prioritize the READ actions
        :type read_preferences: list[str]
        :return: None
        :rtype: NoneType
        """
        if MaintenanceController._service_manager.has_service(
                name, MaintenanceController._local_client) is False:
            alba_pkg_name, alba_version_cmd = PackageFactory.get_package_and_version_cmd_for(
                component=PackageFactory.COMP_ALBA)
            config_location = '{0}/config'.format(
                MaintenanceController.MAINTENANCE_KEY.format(
                    alba_backend_guid, name))
            params = {
                'LOG_SINK':
                Logger.get_sink_path('alba_maintenance'),
                'ALBA_CONFIG':
                Configuration.get_configuration_path(config_location),
                'ALBA_PKG_NAME':
                alba_pkg_name,
                'ALBA_VERSION_CMD':
                alba_version_cmd
            }
            Configuration.set(
                key=config_location,
                value={
                    'log_level':
                    'info',
                    'albamgr_cfg_url':
                    Configuration.get_configuration_path(
                        '/ovs/arakoon/{0}/config'.format(abm_name)),
                    'read_preference':
                    [] if read_preferences is None else read_preferences,
                    'multicast_discover_osds':
                    False
                })

            MaintenanceController._service_manager.add_service(
                name=MaintenanceController.MAINTENANCE_PREFIX,
                client=MaintenanceController._local_client,
                params=params,
                target_name=name)
        MaintenanceController._service_manager.start_service(
            name, MaintenanceController._local_client)

    @staticmethod
    def remove_maintenance_service(name, alba_backend_guid=None):
        """
        Remove a maintenance service with a specific name
        :param name: Name of the service to remove
        :type name: str
        :param alba_backend_guid: ALBA Backend GUID for which the maintenance service needs to be removed
                                  Defaults to None for backwards compatibility
        :type alba_backend_guid: str
        :return: None
        :rtype: NoneType
        """
        if MaintenanceController._service_manager.has_service(
                name, MaintenanceController._local_client):
            MaintenanceController._service_manager.stop_service(
                name, MaintenanceController._local_client)
            MaintenanceController._service_manager.remove_service(
                name, MaintenanceController._local_client)

        if alba_backend_guid is not None:
            key = MaintenanceController.MAINTENANCE_KEY.format(
                alba_backend_guid, name)
            if Configuration.dir_exists(key=key):
                Configuration.delete(key=key)