def get_machine(self, instance_id): """Retrieve a machine by provider machine id. """ for machine in self._machines: if instance_id == machine.instance_id: return succeed(machine) return fail(MachinesNotFound([instance_id]))
def _update_system(self, instance_id, info): """Set an attribute on a Cobbler system. :param str instance_id: the Cobbler uid of the system :param dict info: names and desired values of system attributes to set :raises: :exc:`juju.errors.ProviderError` on invalid cobbler state :raises: :exc:`juju.errors.MachinesNotFound` when `instance_id` is not acquired """ name = yield self._get_name(instance_id) try: handle = yield self._caller.call( "get_system_handle", (name,), auth=True) except Fault as e: if "unknown system name" in str(e): raise MachinesNotFound([instance_id]) raise for (key, value) in sorted(info.items()): yield self._caller.check_call( "modify_system", (handle, key, value), auth=True, expect=True) yield self._caller.check_call( "save_system", (handle,), auth=True, expect=True) returnValue(name)
def describe_systems(self, *instance_ids): """Get all available information about systems. :param instance_ids: Cobbler uids of requested systems; leave blank to return information about all acquired systems. :return: a list of dictionaries describing acquired systems :rtype: :class:`twisted.internet.defer.Deferred` :raises: :exc:`juju.errors.MachinesNotFound` if any requested instance_id doesn't exist """ all_systems = yield self._caller.call("get_systems") acquired_systems = [s for s in all_systems if self._acquired_class in s["mgmt_classes"]] if not instance_ids: returnValue(acquired_systems) keyed_systems = dict(((system["uid"], system) for system in acquired_systems)) result_systems = [] missing_instance_ids = [] for instance_id in instance_ids: if instance_id in keyed_systems: result_systems.append(keyed_systems[instance_id]) else: missing_instance_ids.append(instance_id) if missing_instance_ids: raise MachinesNotFound(missing_instance_ids) returnValue(result_systems)
def extract_name(systems): if len(systems) > 1: raise ProviderError( "Got multiple names for machine %s: %s" % (instance_id, ", ".join(systems))) if not systems: raise MachinesNotFound([instance_id]) return systems[0]
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.ec2.machine.EC2ProviderMachine` owned by this provider. :return: a list of :class:`juju.providers.ec2.machine.EC2ProviderMachine` instances :rtype: :class:`twisted.internet.defer.Deferred` :raises: :exc:`juju.errors.MachinesNotFound` """ group_name = "juju-%s" % self.environment_name try: instances = yield self.ec2.describe_instances(*instance_ids) except EC2Error as error: code = error.get_error_codes() message = error.get_error_messages() if code == "InvalidInstanceID.NotFound": message = error.get_error_messages() raise MachinesNotFound( re.findall(r"\bi-[0-9a-f]{3,15}\b", message)) raise ProviderInteractionError( "Unexpected EC2Error getting machines %s: %s" % (", ".join(instance_ids), message)) machines = [] for instance in instances: if instance.instance_state not in ("running", "pending"): continue if group_name not in instance.reservation.groups: continue machines.append(machine_from_instance(instance)) if instance_ids: # We were asked for a specific list of machines, and if we can't # completely fulfil that request we should blow up. found_instance_ids = set(m.instance_id for m in machines) missing = set(instance_ids) - found_instance_ids if missing: raise MachinesNotFound(missing) returnValue(machines)
def test_bad_machines(self): provider = self.get_provider( {"zookeeper-instances": ["porter", "carter"]}) provider.get_machines(["porter"]) self.mocker.result(fail(MachinesNotFound(["porter"]))) provider.get_machines(["carter"]) self.mocker.result(fail(MachinesNotFound(["carter"]))) self.mocker.replay() d = provider.get_zookeeper_machines() self.assertFailure(d, EnvironmentNotFound) def check(error): self.assertEquals( str(error), "juju environment not found: machines are not running " "(porter, carter)") d.addCallback(check) return d
def test_gets_all_good_machines(self): provider = self.get_provider( {"zookeeper-instances": ["porter", "carter", "miller", "baker"]}) provider.get_machines(["porter"]) self.mocker.result(fail(MachinesNotFound(["porter"]))) provider.get_machines(["carter"]) carter = object() self.mocker.result(succeed([carter])) provider.get_machines(["miller"]) self.mocker.result(fail(MachinesNotFound(["miller"]))) provider.get_machines(["baker"]) baker = object() self.mocker.result(succeed([baker])) self.mocker.replay() d = provider.get_zookeeper_machines() def verify_machine(result): self.assertEquals(result, [carter, baker]) d.addCallback(verify_machine) return d
def get_machines(self, instance_ids=()): """List all the machine running in the provider.""" if not instance_ids: return succeed(self._machines[:]) machines_by_id = dict(((m.instance_id, m) for m in self._machines)) machines = [] missing_instance_ids = [] for instance_id in instance_ids: if instance_id in machines_by_id: machines.append(machines_by_id[instance_id]) else: missing_instance_ids.append(instance_id) if missing_instance_ids: return fail(MachinesNotFound(missing_instance_ids)) return succeed(machines)
def test_MachinesNotFoundPlural(self): error = MachinesNotFound(("i-disappeared", "i-exploded")) self.assertEquals(error.instance_ids, ["i-disappeared", "i-exploded"]) self.assertEquals(str(error), "Cannot find machines: i-disappeared, i-exploded")
def test_MachinesNotFoundSingular(self): error = MachinesNotFound(("i-sublimed", )) self.assertEquals(error.instance_ids, ["i-sublimed"]) self.assertEquals(str(error), "Cannot find machine: i-sublimed")