def get_machines(self, instance_ids=()): """List machines running in the provider. :param list instance_ids: ids of instances you want to get. Leave empty to list every :class:`juju.providers.maas.MAASMachine` owned by this provider. :return: a list of :class:`juju.providers.maas.MAASMachine` :rtype: :class:`twisted.internet.defer.Deferred` :raises: :exc:`juju.errors.MachinesNotFound` """ instances = yield self.maas_client.get_nodes(instance_ids) machines = [MAASMachine.from_dict(i) for i in instances] if instance_ids: instance_ids_expected = set(instance_ids) instance_ids_returned = set( machine.instance_id for machine in machines) instance_ids_missing = ( instance_ids_expected - instance_ids_returned) instance_ids_unexpected = ( instance_ids_returned - instance_ids_expected) if instance_ids_missing: raise MachinesNotFound(sorted(instance_ids_missing)) if instance_ids_unexpected: raise ProviderError( "Machines not requested returned: %s" % ( ", ".join(sorted(instance_ids_unexpected)))) returnValue(machines)
def test_from_dict(self): """ Given a dict of node data from the MAAS API, `from_dict()` returns a `MAASMachine` instance representing that machine. """ data = { "resource_uri": "/an/example/uri", "hostname": "machine.example.com", } machine = MAASMachine.from_dict(data) self.assertEqual("/an/example/uri", machine.instance_id) self.assertEqual("machine.example.com", machine.dns_name) self.assertEqual("machine.example.com", machine.private_dns_name)
def start_machine(self, machine_id, zookeepers): """Start an instance with MAAS. :param str machine_id: the juju machine ID to assign :param zookeepers: the machines currently running zookeeper, to which the new machine will need to connect :type zookeepers: list of :class:`juju.providers.maas.provider.MAASMachine` :return: a singe-entry list containing a :class:`juju.providers.maas.provider.MAASMachine` representing the newly-launched machine :rtype: :class:`twisted.internet.defer.Deferred` """ maas_client = self._provider.maas_client instance_data = yield maas_client.acquire_node(self._constraints) instance_uri = instance_data["resource_uri"] # If anything goes wrong after the acquire and before the launch # actually happens, we attempt to release the node. try: cloud_init = self._create_cloud_init(machine_id, zookeepers) cloud_init.set_provider_type("maas") cloud_init.set_instance_id_accessor(instance_uri) node_data = yield maas_client.start_node( instance_uri, cloud_init.render()) machine = MAASMachine.from_dict(node_data) except Exception: log.exception( "Failed to launch machine %s; attempting to release.", instance_uri) exc_info = sys.exc_info() yield maas_client.release_node(instance_uri) # Use three-expression form to ensure that the error with its # traceback is correctly propagated. raise exc_info[0], exc_info[1], exc_info[2] else: returnValue([machine])