Пример #1
0
Файл: ssh.py Проект: jloehel/xii
    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)
Пример #2
0
    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()))
Пример #3
0
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))
Пример #4
0
 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)
Пример #5
0
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))
Пример #6
0
    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"]))
Пример #7
0
    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())
Пример #8
0
    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()))
Пример #9
0
Файл: ssh.py Проект: jloehel/xii
    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]
Пример #10
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()
Пример #11
0
    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"]
Пример #12
0
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
Пример #13
0
    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
Пример #14
0
    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
Пример #15
0
    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
Пример #16
0
 def validate(self):
     if not self.is_ansible_installed():
         raise error.NotFound("Need ansible installed to provision nodes")
     Component.validate(self)
Пример #17
0
 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)