def run(self): domain_name = self.domain_name() cmpnt = self.get_component(domain_name) if not cmpnt: raise error.NotFound( "Could not find `{}`. Maybe wrong directory?".format( domain_name)) user = self.user_name(cmpnt) ip = cmpnt.domain_get_ip(domain_name) self.say("{} has IP {}".format(domain_name, ip)) null = open(os.devnull, 'w') retry = self.get("global/retry_ssh", 20) # scan key first subprocess.call("ssh-keygen -R {}".format(ip), shell=True, stdout=null, stderr=null) subprocess.call("ssh-keyscan -H {} >> ~/.ssh/known_hosts".format(ip), shell=True, stdout=null, stderr=null) self._run_ssh_cmd(domain_name, user, ip, retry)
def validate(self): AnsibleAttribute.validate(self) if not os.path.exists(self.settings()): raise error.NotFound("Could not find ansible playbook: " "{} no such file or directory".format( self.settings()))
def _initialize_attributes(instance, ext_mgr): non_attributes = ["count", "type", "settings", "basename"] def add_attr(name): attr = ext_mgr.get_attribute(instance.ctype, name) if not attr: raise error.NotFound("Invalid attribute `{}` for component " "{}. Maybe Missspelled?".format( name, instance.entity())) instance.add_attribute(attr["class"](instance, attr["templates"])) # load defined attributes for n in instance.store().values().keys(): if n not in non_attributes: add_attr(n) # add defaults for name in instance.default_attributes: if not instance.has_attribute(name): add_attr(name) # check if all required attributes are set for name in instance.required_attributes: if not instance.has_attribute(name): raise error.NotFound("{} needs a {} attribute but not found, " "add one!".format(instance.ctype, name))
def _run_shell(self, script, shell="/bin/bash"): if not os.path.isfile(script): raise error.NotFound( "Could not find privisioner shell script ({})".format(script)) ssh = self.default_ssh() script_location = ssh.copy_to_tmp(script) ssh.run(shell + " " + script_location)
def file_read(path): try: with open(path, 'r') as stream: return stream.read() except IOError: raise error.NotFound("Could not open `{}`: No such file or " "directory".format(path))
def add_attr(name): attr = ext_mgr.get_attribute(instance.ctype, name) if not attr: raise error.NotFound("Invalid attribute `{}` for component " "{}. Maybe Missspelled?".format( name, instance.entity())) instance.add_attribute(attr["class"](instance, attr["templates"]))
def spawn(self): network = self._get_delayed_network(self.network_name()) if not network: raise error.NotFound("Network {} for domain " "{}".format(self.network_name(), self.component_entity())) if not network.isActive(): self.start() self.add_xml('devices', self._gen_xml())
def _check_sshfs_compability(self): sshfs_locations = ["/usr/bin/sshfs"] self.say("checking sshfs compability") for location in sshfs_locations: if self.guest().exists(location): return raise error.NotFound("Image for {} seems to not support sshfs.".format( self.component_entity()))
def domain_name(self): if self.args().domain is not None: return self.args().domain nodes = self.get("components/node") if nodes is None: raise error.NotFound("This definition does not define any " "nodes. Checkout your definition file") # return the first node in the definition return nodes.keys()[0]
def validate(self, required_children=None): self.reorder_childs() if required_children: for required in required_children: child = self.get_child(required) if not child: raise error.NotFound("Could not find required `{}` " "in {}".format( required, self.entity())) for child in self._childs: child.validate()
def _get_mac_address(self): def _uses_network(iface): return iface.find("source").attrib["network"] == self.network_name() node = self.get_domain(self.component_entity()) desc = etree.fromstring(node.XMLDesc()) ifaces = filter(_uses_network, desc.findall("devices/interface")) if len(ifaces) == 0: raise error.NotFound("Could not find domain interface") # FIXME: Add multiple interface support # When multiple interfaces using the same network mac = ifaces[0].find("mac") if mac is None: raise error.NotFound("Could not find interface mac address") return mac.attrib["address"]
def from_definition(definition, command, ext_mgr): for component_type in definition: for name in definition[component_type].keys(): cmpnt = ext_mgr.get_component(component_type) if not cmpnt: raise error.NotFound( "Could not find component type {}!".format(component_type)) instance = cmpnt["class"](name, command, cmpnt["templates"]) _initialize_attributes(instance, ext_mgr) yield instance
def get_resource(self, typ, *args, **kwargs): """returns a libvirt resource search a libvirt resource. `typ` defines which type should be searched. Currently supported is: * domain * pool * network * volume Args: typ: Resource type args: name or the resource or in case of volume name of the pool and name of the volume kwargs: either throw or raise_exception set to boolean Returns: Returns the searched resource or None if set non throwing Throws: If throw or raise_exception is not set to False a exception is raised if the resource could not be found """ r = True if "raise_exception" in kwargs: r = kwargs["raise_exception"] if "throw" in kwargs: r = kwargs["throw"] mapping = { "domain": "lookupByName", "pool": "storagePoolLookupByName", "network": "networkLookupByName", } try: if typ == "volume": pool = self.get_resource("pool", args[0], throw=r) if not pool: return None return pool.storageVolLookupByName(args[1]) return getattr(self.virt(), mapping[typ])(*args) except libvirt.libvirtError: if r: raise error.NotFound("Could not find {} " "({})".format(typ, args[0])) return None
def _get_delayed_network(self, name): network = self.get_network(name, raise_exception=False) if not network: if not self.has_component("network", name): raise error.NotFound("Could not find network ({})" .format(name)) # wait for network to become ready for _ in range(self.global_get("global/retry_network", 20)): network = self.get_network(name, raise_exception=False) if network: return network sleep(self.global_get("global/wait", 3)) raise error.ExecError("Network {} has not become ready in " "time. Giving up".format(name)) return network
def used_pool(self): pool = self.get_pool(self.settings(), raise_exception=False) if not pool: if not self.has_component("pool", self.settings()): raise error.NotFound("pool {} does not exist.") for i in range(self.global_get("global/retry_pool", 20)): pool = self.get_pool(name, raise_exception=False) if pool: break self.counted( i, "Waiting for pool {} to become ready.".format( self.settings())) sleep(self.global_get("global/wait", 3)) raise error.ExecError("Coult not start pool {}!".format( self.settings())) return pool
def validate(self): if not self.is_ansible_installed(): raise error.NotFound("Need ansible installed to provision nodes") Component.validate(self)
def add_path(self, path): if not os.path.exists(path): raise error.NotFound( "Could not find extension path: {}".format(path)) self._paths.add(path)