def polish_blueprint(self, names, polishers): """ Walks a named blueprint for this facility and polish related resources :param names: the name(s) of the blueprint(s) to polish :type names: ``str`` or ``list`` of ``str`` :param polishers: polishers to be applied :type polishers: list of :class:`plumbery.PlumberyPolisher` """ self.power_on() infrastructure = PlumberyInfrastructure(self) nodes = PlumberyNodes(self) if isinstance(names, str): names = names.split(' ') for name in names: blueprint = self.get_blueprint(name) if blueprint is None: continue container = infrastructure.get_container(blueprint) for polisher in polishers: polisher.shine_container(container) nodes.polish_blueprint(blueprint, polishers, container)
def destroy_all_blueprints(self): """ Destroys all blueprints at this facility """ self.power_on() nodes = PlumberyNodes(self) infrastructure = PlumberyInfrastructure(self) basement = self.list_basement() for name in self.list_blueprints(): if name in basement: continue blueprint = self.get_blueprint(name) logging.debug("Destroying blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint) infrastructure.destroy_blueprint(blueprint) for name in basement: blueprint = self.get_blueprint(name) logging.debug("Destroying blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint) infrastructure.destroy_blueprint(blueprint)
def stop_blueprint(self, names): """ Stops nodes of the given blueprint at this facility :param names: the name(s) of the target blueprint(s) :type names: ``str`` or ``list`` of ``str`` You can use the following setting to prevent plumbery from stopping a node:: - sql: domain: *vdc1 ethernet: *data nodes: - slaveSQL: running: always """ nodes = PlumberyNodes(self) for name in self.expand_blueprint(names): blueprint = self.get_blueprint(name) if 'nodes' not in blueprint: continue nodes.stop_blueprint(blueprint)
def stop_nodes(self, names): """ Stops nodes of the given blueprint at this facility :param names: the name(s) of the target blueprint(s) :type names: ``str`` or ``list`` of ``str`` You can use the following setting to prevent plumbery from stopping a node:: - sql: domain: *vdc1 ethernet: *data nodes: - slaveSQL: running: always """ nodes = PlumberyNodes(self) if isinstance(names, str): names = names.split(' ') for name in names: blueprint = self.get_blueprint(name) if blueprint is None: continue if 'nodes' not in blueprint: continue nodes.stop_blueprint(blueprint)
def stop_nodes(self, name): """ Stops nodes of the given blueprint at this facility :param name: the name of the target blueprint :type name: ``str`` You can use the following setting to prevent plumbery from stopping a node:: - sql: domain: *vdc1 ethernet: *data nodes: - slaveSQL: running: always """ blueprint = self.get_blueprint(name) if not blueprint: return if 'nodes' not in blueprint: return nodes = PlumberyNodes(self) nodes.stop_blueprint(blueprint)
def polish_blueprint(self, names, polishers): """ Walks a named blueprint for this facility and polish related resources :param names: the name(s) of the blueprint(s) to polish :type names: ``str`` or ``list`` of ``str`` :param polishers: polishers to be applied :type polishers: list of :class:`plumbery.PlumberyPolisher` """ if isinstance(polishers, str): polishers = PlumberyPolisher.filter(self.plumbery.polishers, polishers) self.power_on() infrastructure = PlumberyInfrastructure(self) nodes = PlumberyNodes(self) for name in self.expand_blueprint(names): blueprint = self.get_blueprint(name) container = infrastructure.get_container(blueprint) for polisher in polishers: polisher.shine_container(container) nodes.polish_blueprint(blueprint, polishers, container)
def build_blueprint(self, names): """ Builds a named blueprint for this facility :param names: the name(s) of the blueprint(s) to build :type names: ``str`` or ``list`` of ``str`` This function builds the named blueprint in two steps: the infrastructure comes first, and then the nodes themselves. >>>facility.build_blueprint('sql') If the keyword ``basement`` mentions one or several blueprints, then network domains of these special blueprints are built before the actual target blueprint. Example ``fittings.yaml``:: --- basement: admin blueprints: - admin: ethernet: control - sql: ethernet: data nodes: - server1: glue: control In this example, the node ``server1``has two network interfaces. The main network interface is connected to the network ``data``, and the secondary network interface is connected to the network ``control``. """ self.power_on() infrastructure = PlumberyInfrastructure(self) nodes = PlumberyNodes(self) basement = self.list_basement() for name in basement: blueprint = self.get_blueprint(name) infrastructure.build(blueprint) for name in self.expand_blueprint(names): blueprint = self.get_blueprint(name) if name not in basement: infrastructure.build(blueprint) nodes.build_blueprint( blueprint, infrastructure.get_container(blueprint))
def lookup(self, token): """ Retrieves the value attached to a token :param token: the token, e.g., 'node.ipv6' :type token: ``str`` :return: the value attached to this token, or `None` """ if token in self.cache: return str(self.cache[token]) value = None if self.context is not None: value = self.context.lookup(token) if value is not None: return value if self.container is None: return None tokens = token.split('.') if len(tokens) < 2: tokens.append('private') nodes = PlumberyNodes(self.container.facility) node = nodes.get_node(tokens[0]) if node is None: return None if self.context is not None: self.context.remember(tokens[0], node.private_ips[0]) self.context.remember(tokens[0] + '.private', node.private_ips[0]) self.context.remember(tokens[0] + '.ipv6', node.extra['ipv6']) if len(node.public_ips) > 0: self.context.remember(tokens[0] + '.public', node.public_ips[0]) if tokens[1] == 'private': return node.private_ips[0] if tokens[1] == 'ipv6': return node.extra['ipv6'] if tokens[1] == 'public': if len(node.public_ips) > 0: return node.public_ips[0] else: return '' return None
def lookup(self, token): """ Retrieves the value attached to a token :param token: the token, e.g., 'node.ipv6' :type token: ``str`` :return: the value attached to this token, or `None` """ if token in self.cache: return str(self.cache[token]) value = None if self.context is not None: value = self.context.lookup(token) if value is not None: return value if self.container is None: return None tokens = token.split('.') if len(tokens) < 2: tokens.append('private') nodes = PlumberyNodes(self.container.facility) node = nodes.get_node(tokens[0]) if node is None: return None if self.context is not None: self.context.remember(tokens[0], node.private_ips[0]) self.context.remember(tokens[0]+'.private', node.private_ips[0]) self.context.remember(tokens[0]+'.ipv6', node.extra['ipv6']) if len(node.public_ips) > 0: self.context.remember(tokens[0]+'.public', node.public_ips[0]) if tokens[1] == 'private': return node.private_ips[0] if tokens[1] == 'ipv6': return node.extra['ipv6'] if tokens[1] == 'public': if len(node.public_ips) > 0: return node.public_ips[0] else: return '' return None
def destroy_all_nodes(self): """ Destroys all nodes at this facility """ self.power_on() nodes = PlumberyNodes(self) for name in self.list_blueprints(): blueprint = self.get_blueprint(name) if blueprint is not None: logging.info("Destroying nodes of blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint)
def list_nodes(self): """ Retrieves the list of nodes that have been defined across blueprints for this facility :return: names of nodes defined for this facility :rtype: ``list`` of ``str`` or ``[]`` Nodes are defined in blueprints. """ labels = set() for blueprint in self.fittings.blueprints: name = blueprint.keys()[0] if 'nodes' in blueprint[name]: for item in blueprint[name]['nodes']: if type(item) is dict: label = item.keys()[0] else: label = item for label in PlumberyNodes.expand_labels(label): labels.add(label) return list(labels)
def list_nodes(self): """ Retrieves the list of nodes that have been defined across blueprints for this facility :return: names of nodes defined for this facility :rtype: ``list`` of ``str`` or ``[]`` Nodes are defined in blueprints. """ labels = [] for blueprint in self.fittings.blueprints: name = blueprint.keys()[0] if 'nodes' in blueprint[name]: for item in blueprint[name]['nodes']: if type(item) is dict: label = item.keys()[0] else: label = item for label in PlumberyNodes.expand_labels(label): if label in labels: logging.warning("Duplicate node name '{}'" .format(label)) else: labels.append(label) return labels
def list_nodes(self): """ Retrieves the list of nodes that have been defined across blueprints for this facility :return: names of nodes defined for this facility :rtype: ``list`` of ``str`` or ``[]`` Nodes are defined in blueprints. """ labels = [] for blueprint in self.blueprints: name = blueprint.keys()[0] if 'nodes' in blueprint[name]: for item in blueprint[name]['nodes']: if type(item) is dict: label = item.keys()[0] else: label = item for label in PlumberyNodes.expand_labels(label): if label in labels: logging.warning("Duplicate node name '{}'" .format(label)) else: labels.append(label) return labels
def wipe_blueprint(self, names): """ Destroys nodes of a given blueprint at this facility :param names: the names of the blueprint to destroy :type names: ``str`` or ``list`` of ``str`` """ self.power_on() nodes = PlumberyNodes(self) for name in self.expand_blueprint(names): blueprint = self.get_blueprint(name) nodes.destroy_blueprint(blueprint)
def destroy_nodes(self, name): """ Destroys nodes of a given blueprint at this facility :param name: the name of the blueprint to destroy :type name: ``str`` """ self.power_on() blueprint = self.get_blueprint(name) if not blueprint: return nodes = PlumberyNodes(self) nodes.destroy_blueprint(blueprint)
def build_all_blueprints(self): """ Builds all blueprints defined for this facility This function builds all network domains across all blueprints, then it builds all nodes across all blueprints. If the keyword ``basement`` mentions one or several blueprints, then these are built before the other blueprints. """ self.power_on() infrastructure = PlumberyInfrastructure(self) nodes = PlumberyNodes(self) basement = self.list_basement() for name in basement: logging.debug("Building infrastructure of blueprint '{}'" .format(name)) blueprint = self.get_blueprint(name) infrastructure.build(blueprint) blueprints = self.list_blueprints() for name in blueprints: if name not in basement: logging.debug("Building infrastructure of blueprint '{}'" .format(name)) blueprint = self.get_blueprint(name) infrastructure.build(blueprint) for name in basement: logging.debug("Building nodes of blueprint '{}'" .format(name)) blueprint = self.get_blueprint(name) container = infrastructure.get_container(blueprint) nodes.build_blueprint(blueprint, container) for name in blueprints: if name not in basement: logging.debug("Building nodes of blueprint '{}'" .format(name)) blueprint = self.get_blueprint(name) container = infrastructure.get_container(blueprint) nodes.build_blueprint(blueprint, container)
def destroy_blueprint(self, names): """ Destroys a given blueprint at this facility :param names: the name(s) of the blueprint(s) to destroy :type names: ``str`` or ``list`` of ``str`` """ self.power_on() nodes = PlumberyNodes(self) infrastructure = PlumberyInfrastructure(self) for name in self.expand_blueprint(names): blueprint = self.get_blueprint(name) nodes.destroy_blueprint(blueprint) infrastructure.destroy_blueprint(blueprint)
def build_blueprint(self, name): """ Builds a named blueprint for this facility :param name: the name of the blueprint to build :type name: ``str`` """ blueprint = self.get_blueprint(name) if not blueprint: return domain = PlumberyDomain(self) domain.build(blueprint) nodes = PlumberyNodes(self) nodes.build_blueprint(blueprint, domain)
def start_nodes(self, name): """ Starts nodes from a given blueprint at this facility :param name: the name of the target blueprint :type name: ``str`` """ blueprint = self.get_blueprint(name) if not blueprint: return if 'nodes' not in blueprint: return nodes = PlumberyNodes(self) nodes.start_blueprint(blueprint)
def start_blueprint(self, names): """ Starts nodes from a given blueprint at this facility :param names: the name(s) of the target blueprint(s) :type names: ``str`` or ``list`` of ``str`` """ nodes = PlumberyNodes(self) for name in self.expand_blueprint(names): blueprint = self.get_blueprint(name) if 'nodes' not in blueprint: continue nodes.start_blueprint(blueprint)
def polish_blueprint(self, name, polishers): """ Walks a named blueprint for this facility and polish related resources :param name: the name of the blueprint to polish :type name: ``str`` :param polishers: polishers to be applied :type polishers: list of :class:`plumbery.PlumberyPolisher` """ blueprint = self.get_blueprint(name) if not blueprint: return if 'nodes' not in blueprint: return nodes = PlumberyNodes(self) nodes.polish_blueprint(blueprint, polishers)
def lookup(self, token): if token in self.cache: return str(self.cache[token]) if self.container is not None: if self.inventory is None: self.inventory = self.container.facility.list_nodes() tokens = token.split('.') if len(tokens) < 2: tokens.append('private') if tokens[0] in self.inventory: nodes = PlumberyNodes(self.container.facility) node = nodes.get_node(tokens[0]) self.cache[node.name] = node.private_ips[0] self.cache[node.name+'.private'] = node.private_ips[0] self.cache[node.name+'.ipv6'] = node.extra['ipv6'] self.cache[node.name+'.public'] = node.public_ips[0] if tokens[1] == 'private': return node.private_ips[0] if tokens[1] == 'ipv6': return node.extra['ipv6'] if tokens[1] == 'public': if len(node.public_ips) > 0: return node.public_ips[0] else: return '' if self.context is not None: return self.context.lookup(token) return None
def destroy_all_blueprints(self): """ Destroys all blueprints at this facility """ self.power_on() nodes = PlumberyNodes(self) infrastructure = PlumberyInfrastructure(self) basement = self.list_basement() for name in self.expand_blueprint('*'): if name in basement: continue blueprint = self.get_blueprint(name) logging.debug("Destroying blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint) infrastructure.destroy_blueprint(blueprint) for name in basement: blueprint = self.get_blueprint(name) logging.debug("Destroying blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint) infrastructure.destroy_blueprint(blueprint)
def destroy_nodes(self, names): """ Destroys nodes of a given blueprint at this facility :param names: the names of the blueprint to destroy :type names: ``str`` or ``list`` of ``str`` """ self.power_on() nodes = PlumberyNodes(self) if isinstance(names, str): names = names.split(' ') for name in names: blueprint = self.get_blueprint(name) if blueprint is None: continue nodes.destroy_blueprint(blueprint)
def wipe_all_blueprints(self): """ Destroys all nodes at this facility """ self.power_on() nodes = PlumberyNodes(self) basement = self.list_basement() for name in self.list_blueprints(): if name in basement: continue blueprint = self.get_blueprint(name) logging.debug("Wiping blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint) for name in basement: blueprint = self.get_blueprint(name) logging.debug("Wiping blueprint '{}'".format(name)) nodes.destroy_blueprint(blueprint)
def start_nodes(self, names): """ Starts nodes from a given blueprint at this facility :param names: the name(s) of the target blueprint(s) :type names: ``str`` or ``list`` of ``str`` """ nodes = PlumberyNodes(self) if isinstance(names, str): names = names.split(' ') for name in names: blueprint = self.get_blueprint(name) if blueprint is None: continue if 'nodes' not in blueprint: continue nodes.start_blueprint(blueprint)
def build_all_blueprints(self): """ Builds all blueprints defined for this facility This function builds all network domains across all blueprints, then it builds all nodes across all blueprints. If the keyword ``basement`` mentions one or several blueprints, then these are built before the other blueprints. """ self.power_on() infrastructure = PlumberyInfrastructure(self) nodes = PlumberyNodes(self) for name in self.list_basement(): blueprint = self.get_blueprint(name) if blueprint is not None: infrastructure.build(blueprint) for name in self.list_blueprints(): if name not in self.list_basement(): blueprint = self.get_blueprint(name) if blueprint is not None: infrastructure.build(blueprint) for name in self.list_basement(): blueprint = self.get_blueprint(name) if blueprint is not None: container = infrastructure.get_container(blueprint) nodes.build_blueprint(blueprint, container) for name in self.list_blueprints(): if name not in self.list_basement(): blueprint = self.get_blueprint(name) if blueprint is not None: container = infrastructure.get_container(blueprint) nodes.build_blueprint(blueprint, container)