def get_default_arch_image_in_nodegroup(self, nodegroup, series, purpose): """Return the first image available for any architecture in the nodegroup/series supplied. """ images = BootImage.objects.filter( release=series, nodegroup=nodegroup, purpose=purpose) images = images.order_by('architecture') return get_first(images)
def test_get_first_does_not_retrieve_beyond_first_item(self): class SecondItemRetrieved(Exception): """Second item as retrieved. It shouldn't be.""" def multiple_items(): yield "Item 1" raise SecondItemRetrieved() self.assertEqual("Item 1", get_first(multiple_items()))
def get_default_commissioning_resource(self, osystem, series): """Return best guess `BootResource` for the given osystem and series. Prefers `i386` then `amd64` resources if available. Returns `None` if none match requirements. """ commissionable = list(self.get_commissionable_resource( osystem, series)) for resource in commissionable: # Prefer i386. It will work for most cases where we don't # know the actual architecture. arch, subarch = resource.split_arch() if arch == "i386": return resource for resource in commissionable: # Prefer amd64. It has a much better chance of working than # say arm or ppc. arch, subarch = resource.split_arch() if arch == "amd64": return resource return get_first(commissionable)
def acquire(self, request): """Acquire an available node for deployment. Constraints parameters can be used to acquire a node that possesses certain characteristics. All the constraints are optional and when multiple constraints are provided, they are combined using 'AND' semantics. :param name: Hostname of the returned node. :type name: unicode :param arch: Architecture of the returned node (e.g. 'i386/generic', 'amd64', 'armhf/highbank', etc.). :type arch: unicode :param cpu_count: The minium number of CPUs the returned node must have. :type cpu_count: int :param mem: The minimum amount of memory (expressed in MB) the returned node must have. :type mem: float :param tags: List of tags the returned node must have. :type tags: list of unicodes :param not_tags: List of tags the acquired node must not have. :type tags: List of unicodes. :param connected_to: List of routers' MAC addresses the returned node must be connected to. :type connected_to: unicode or list of unicodes :param networks: List of networks (defined in MAAS) to which the node must be attached. A network can be identified by the name assigned to it in MAAS; or by an `ip:` prefix followed by any IP address that falls within the network; or a `vlan:` prefix followed by a numeric VLAN tag, e.g. `vlan:23` for VLAN number 23. Valid VLAN tags must be in the range of 1 to 4095 inclusive. :type networks: list of unicodes :param not_networks: List of networks (defined in MAAS) to which the node must not be attached. The returned noded won't be attached to any of the specified networks. A network can be identified by the name assigned to it in MAAS; or by an `ip:` prefix followed by any IP address that falls within the network; or a `vlan:` prefix followed by a numeric VLAN tag, e.g. `vlan:23` for VLAN number 23. Valid VLAN tags must be in the range of 1 to 4095 inclusive. :type not_networks: list of unicodes :param not_connected_to: List of routers' MAC Addresses the returned node must not be connected to. :type connected_to: list of unicodes :param zone: An optional name for a physical zone the acquired node should be located in. :type zone: unicode :type not_in_zone: Optional list of physical zones from which the node should not be acquired. :type not_in_zone: List of unicodes. :param agent_name: An optional agent name to attach to the acquired node. :type agent_name: unicode Returns 409 if a suitable node matching the constraints could not be found. """ form = AcquireNodeForm(data=request.data) maaslog.info( "Request from user %s to acquire a node with constraints %s", request.user.username, request.data) if not form.is_valid(): raise ValidationError(form.errors) # This lock prevents a node we've picked as available from # becoming unavailable before our transaction commits. with locks.node_acquire: nodes = Node.objects.get_available_nodes_for_acquisition( request.user) nodes = form.filter_nodes(nodes) node = get_first(nodes) if node is None: constraints = form.describe_constraints() if constraints == '': # No constraints. That means no nodes at all were # available. message = "No node available." else: message = ( "No available node matches constraints: %s" % constraints) raise NodesNotAvailable(message) agent_name = request.data.get('agent_name', '') node.acquire( request.user, get_oauth_token(request), agent_name=agent_name) return node
def test_get_first_accepts_any_sequence(self): item = factory.getRandomString() self.assertEqual(item, get_first(repeat(item)))
def test_get_first_returns_first_item(self): items = [factory.getRandomString() for counter in range(10)] self.assertEqual(items[0], get_first(items))
def test_get_first_returns_None_for_empty_list(self): self.assertIsNone(get_first([]))