def process_cluster_attributes(cls, cluster, attributes): """Generate Cluster-Plugins relation based on attributes Iterates through plugins attributes, creates or deletes Cluster <-> Plugins relation if plugin is enabled or disabled. :param cluster: A cluster instance :type cluster: nailgun.objects.cluster.Cluster :param attributes: Cluster attributes :type attributes: dict """ def _convert_attrs(plugin_id, attrs): prefix = "#{0}_".format(plugin_id) result = dict((title[len(prefix):], attrs[title]) for title in attrs if title.startswith(prefix)) for attr in six.itervalues(result): if 'restrictions' not in attr: continue if len(attr['restrictions']) == 1: attr.pop('restrictions') else: attr['restrictions'].pop() return result for attrs in six.itervalues(attributes): if not isinstance(attrs, dict): continue plugin_versions = attrs.pop('plugin_versions', None) if plugin_versions is None: continue metadata = attrs.pop('metadata', {}) plugin_enabled = metadata.get('enabled', False) default = metadata.get('default', False) for version in plugin_versions['values']: pid = version.get('data') plugin = Plugin.get_by_uid(pid) if not plugin: logger.warning('Plugin with id "%s" is not found, skip it', pid) continue enabled = plugin_enabled and\ pid == plugin_versions['value'] ClusterPlugins.set_attributes( cluster.id, plugin.id, enabled=enabled, attrs=_convert_attrs(plugin.id, attrs) if enabled or default else None)
def process_cluster_attributes(cls, cluster, attributes): """Generate Cluster-Plugins relation based on attributes Iterates through plugins attributes, creates or deletes Cluster <-> Plugins relation if plugin is enabled or disabled. :param cluster: A cluster instance :type cluster: nailgun.objects.cluster.Cluster :param attributes: Cluster attributes :type attributes: dict """ def _convert_attrs(plugin_id, attrs): prefix = "#{0}_".format(plugin_id) result = dict((title[len(prefix):], attrs[title]) for title in attrs if title.startswith(prefix)) for attr in six.itervalues(result): if 'restrictions' not in attr: continue if len(attr['restrictions']) == 1: attr.pop('restrictions') else: attr['restrictions'].pop() return result for attrs in six.itervalues(attributes): if not isinstance(attrs, dict): continue plugin_versions = attrs.pop('plugin_versions', None) if plugin_versions is None: continue metadata = attrs.pop('metadata', {}) plugin_enabled = metadata.get('enabled', False) default = metadata.get('default', False) for version in plugin_versions['values']: pid = version.get('data') plugin = Plugin.get_by_uid(pid) if not plugin: logger.warning( 'Plugin with id "%s" is not found, skip it', pid) continue enabled = plugin_enabled and\ pid == plugin_versions['value'] ClusterPlugins.set_attributes( cluster.id, plugin.id, enabled=enabled, attrs=_convert_attrs(plugin.id, attrs) if enabled or default else None )
def enable_plugins_by_components(cls, cluster): """Enable plugin by components. :param cluster: A cluster instance :type cluster: Cluster model """ cluster_components = set(cluster.components) plugin_ids = [p.id for p in PluginCollection.all_newest()] for plugin in ClusterPlugins.get_connected_plugins( cluster, plugin_ids): plugin_adapter = wrap_plugin(plugin) plugin_components = set( component['name'] for component in plugin_adapter.components_metadata) if cluster_components & plugin_components: ClusterPlugins.set_attributes( cluster.id, plugin.id, enabled=True)
def enable_plugins_by_components(cls, cluster): """Enable plugin by components. :param cluster: A cluster instance :type cluster: Cluster model """ cluster_components = set(cluster.components) plugin_ids = [p.id for p in PluginCollection.all_newest()] for plugin in ClusterPlugins.get_connected_plugins( cluster, plugin_ids): plugin_adapter = wrap_plugin(plugin) plugin_components = set( component['name'] for component in plugin_adapter.components_metadata) if cluster_components & plugin_components: ClusterPlugins.set_attributes(cluster.id, plugin.id, enabled=True)
def process_cluster_attributes(cls, cluster, attributes): """Generate Cluster-Plugins relation based on attributes. Iterates through plugins attributes, creates or deletes Cluster <-> Plugins relation if plugin is enabled or disabled. :param cluster: A cluster instance :type cluster: nailgun.db.sqlalchemy.models.cluster.Cluster :param attributes: Cluster attributes :type attributes: dict """ plugins = {} # Detach plugins data for k in list(attributes): if cls.is_plugin_data(attributes[k]): plugins[k] = attributes.pop(k)['metadata'] cluster.attributes.editable.pop(k, None) for container in six.itervalues(plugins): default = container.get('default', False) for attrs in container.get('versions', []): version_metadata = attrs.pop('metadata') plugin_id = version_metadata['plugin_id'] plugin = Plugin.get_by_uid(plugin_id) if not plugin: logger.warning('Plugin with id "%s" is not found, skip it', plugin_id) continue enabled = container['enabled']\ and plugin_id == container['chosen_id'] ClusterPlugins.set_attributes( cluster.id, plugin.id, enabled=enabled, attrs=attrs if enabled or default else None)
def process_cluster_attributes(cls, cluster, attributes): """Generate Cluster-Plugins relation based on attributes. Iterates through plugins attributes, creates or deletes Cluster <-> Plugins relation if plugin is enabled or disabled. :param cluster: A cluster instance :type cluster: nailgun.db.sqlalchemy.models.cluster.Cluster :param attributes: Cluster attributes :type attributes: dict """ plugins = {} # Detach plugins data for k in list(attributes): if cls.is_plugin_data(attributes[k]): plugins[k] = attributes.pop(k)['metadata'] cluster.attributes.editable.pop(k, None) for container in six.itervalues(plugins): default = container.get('default', False) for attrs in container.get('versions', []): version_metadata = attrs.pop('metadata') plugin_id = version_metadata['plugin_id'] plugin = Plugin.get_by_uid(plugin_id) if not plugin: logger.warning( 'Plugin with id "%s" is not found, skip it', plugin_id) continue enabled = container['enabled']\ and plugin_id == container['chosen_id'] ClusterPlugins.set_attributes( cluster.id, plugin.id, enabled=enabled, attrs=attrs if enabled or default else None )