Exemplo n.º 1
0
    def init_connection(self, recipe_name):
        """ Initialize the slave connection

            Calling this method will initialize the rpc connection to the
            machine and initialize all the interfaces. Note, that it will
            *not* configure the interfaces. They need to be configured
            individually later on.
        """
        hostname = self._hostname
        port = self._port
        m_id = self._id

        logging.info("Connecting to RPC on machine %s (%s)", m_id, hostname)
        connection = CtlSecSocket(socket.create_connection((hostname, port)))
        connection.handshake(self._security)

        self._msg_dispatcher.add_slave(self, connection)

        hello, slave_desc = self._rpc_call("hello", recipe_name)
        if hello != "hello":
            msg = "Unable to establish RPC connection " \
                  "to machine %s, handshake failed!" % hostname
            raise MachineError(msg)

        self._slave_desc = slave_desc

        for iface in self._interfaces:
            iface.initialize()

        self._configured = True
Exemplo n.º 2
0
    def remove_saved_machine_config(cls):
        #removes previously saved configuration
        cfg = None
        try:
            with open("/tmp/.lnst_machine_conf", "rb") as f:
                cfg = cPickle.load(f)
        except:
            logging.info("No previous configuration found.")
            return

        if cfg:
            logging.info("Cleaning up leftover configuration from previous "\
                         "config_only run.")
            for hostname, machine in cfg["machines"].iteritems():
                port = lnst_config.get_option("environment", "rpcport")
                if test_tcp_connection(hostname, port):
                    s = socket.create_connection((hostname, port))
                    rpc_con = CtlSecSocket(s)
                    try:
                        rpc_con.handshake(machine["security"])
                    except SecSocketException:
                        logging.error("Failed authentication for machine %s" %\
                                      hostname)
                        continue

                    rpc_msg = {
                        "type": "command",
                        "method_name": "machine_cleanup",
                        "args": []
                    }

                    logging.debug("Calling cleanup on slave '%s'" % hostname)
                    rpc_con.send_msg(rpc_msg)
                    while True:
                        msg = rpc_con.recv_msg()
                        if msg['type'] == 'result':
                            break
                    rpc_con.close()

                if "libvirt_dom" in machine:
                    libvirt_dom = machine["libvirt_dom"]
                    domain_ctl = VirtDomainCtl(libvirt_dom)
                    logging.info("Detaching dynamically created interfaces.")
                    for i in machine["interfaces"]:
                        try:
                            domain_ctl.detach_interface(i)
                        except:
                            pass

            logging.info("Removing dynamically created bridges.")
            for br in cfg["bridges"]:
                try:
                    net_ctl = VirtNetCtl(br)
                    net_ctl.cleanup()
                except:
                    pass

            os.remove("/tmp/.lnst_machine_conf")
Exemplo n.º 3
0
    def init_connection(self, recipe_name):
        """ Initialize the slave connection

            Calling this method will initialize the rpc connection to the
            machine and initialize all the interfaces. Note, that it will
            *not* configure the interfaces. They need to be configured
            individually later on.
        """
        hostname = self._hostname
        port = self._port
        m_id = self._id

        logging.info("Connecting to RPC on machine %s (%s)", m_id, hostname)
        connection = CtlSecSocket(socket.create_connection((hostname, port)))
        connection.handshake(self._security)

        self._msg_dispatcher.add_slave(self, connection)

        hello, slave_desc = self._rpc_call("hello", recipe_name)
        if hello != "hello":
            msg = "Unable to establish RPC connection " \
                  "to machine %s, handshake failed!" % hostname
            raise MachineError(msg)

        slave_version = slave_desc["lnst_version"]
        slave_is_git = self.is_git_version(slave_version)
        ctl_version = lnst_config.version
        ctl_is_git = self.is_git_version(ctl_version)
        if slave_version != ctl_version:
            if ctl_is_git and slave_is_git:
                msg = "Controller and Slave '%s' git versions are different"\
                                                                    % hostname
                logging.warning(len(msg) * "=")
                logging.warning(msg)
                logging.warning(len(msg) * "=")
            else:
                msg = "Controller and Slave '%s' versions are not compatible!"\
                                                                    % hostname
                raise MachineError(msg)

        self._slave_desc = slave_desc

        devices = self._rpc_call("get_devices")
        for if_index, dev in devices.items():
            self._device_database[if_index] = Device(dev, self)

        for iface in self._interfaces:
            iface.initialize()

        self._configured = True
Exemplo n.º 4
0
    def init_connection(self, recipe_name):
        """ Initialize the slave connection

            Calling this method will initialize the rpc connection to the
            machine and initialize all the interfaces. Note, that it will
            *not* configure the interfaces. They need to be configured
            individually later on.
        """
        hostname = self._hostname
        port = self._port
        m_id = self._id

        logging.info("Connecting to RPC on machine %s (%s)", m_id, hostname)
        connection = CtlSecSocket(socket.create_connection((hostname, port)))
        connection.handshake(self._security)

        self._msg_dispatcher.add_slave(self, connection)

        hello, slave_desc = self._rpc_call("hello", recipe_name)
        if hello != "hello":
            msg = "Unable to establish RPC connection " \
                  "to machine %s, handshake failed!" % hostname
            raise MachineError(msg)

        slave_version = slave_desc["lnst_version"]
        slave_is_git = self.is_git_version(slave_version)
        ctl_version = lnst_config.version
        ctl_is_git = self.is_git_version(ctl_version)
        if slave_version != ctl_version:
            if ctl_is_git and slave_is_git:
                msg = "Controller and Slave '%s' git versions are different"\
                                                                    % hostname
                logging.warning(len(msg)*"=")
                logging.warning(msg)
                logging.warning(len(msg)*"=")
            else:
                msg = "Controller and Slave '%s' versions are not compatible!"\
                                                                    % hostname
                raise MachineError(msg)

        self._slave_desc = slave_desc

        devices = self._rpc_call("get_devices")
        for if_index, dev in devices.items():
            self._device_database[if_index] = Device(dev, self)

        for iface in self._interfaces:
            iface.initialize()

        self._configured = True
Exemplo n.º 5
0
    def remove_saved_machine_config(cls):
        #removes previously saved configuration
        cfg = None
        try:
            with open("/tmp/.lnst_machine_conf", "rb") as f:
                cfg = cPickle.load(f)
        except:
            logging.info("No previous configuration found.")
            return

        if cfg:
            logging.info("Cleaning up leftover configuration from previous "\
                         "config_only run.")
            for hostname, machine in cfg["machines"].iteritems():
                port = lnst_config.get_option("environment", "rpcport")
                if test_tcp_connection(hostname, port):
                    s = socket.create_connection((hostname, port))
                    rpc_con = CtlSecSocket(s)
                    try:
                        rpc_con.handshake(machine["security"])
                    except SecSocketException:
                        logging.error("Failed authentication for machine %s" %\
                                      hostname)
                        continue

                    rpc_msg= {"type": "command",
                              "method_name": "machine_cleanup",
                              "args": []}

                    logging.debug("Calling cleanup on slave '%s'" % hostname)
                    rpc_con.send_msg(rpc_msg)
                    while True:
                        msg = rpc_con.recv_msg()
                        if msg['type'] == 'result':
                            break
                    rpc_con.close()

                if "libvirt_dom" in machine:
                    libvirt_dom = machine["libvirt_dom"]
                    domain_ctl = VirtDomainCtl(libvirt_dom)
                    logging.info("Detaching dynamically created interfaces.")
                    for i in machine["interfaces"]:
                        try:
                            domain_ctl.detach_interface(i)
                        except:
                            pass

            logging.info("Removing dynamically created bridges.")
            for br in cfg["bridges"]:
                try:
                    net_ctl = VirtNetCtl(br)
                    net_ctl.cleanup()
                except:
                    pass

            os.remove("/tmp/.lnst_machine_conf")
Exemplo n.º 6
0
 def _get_connection(self, hostname, port, sec_params):
     """ Connects to machine
     @param hostname Hostname of the machine
     @param port Port of the machine
     @return Connected socket if connection was successful, None otherwise
     """
     try:
         sock = socket.create_connection((hostname, port))
         ret = CtlSecSocket(sock)
         ret.handshake(sec_params)
         return ret
     except SecSocketException:
         sys.stderr.write("Couldn't connect to host %s:%s, because "\
                          "security negotiation failed.\n" %
                          (hostname, port))
         return None
     except socket.error:
         sys.stderr.write("Connection to remote host '%s:%s' failed\n"
                          % (hostname, port))
         return None
Exemplo n.º 7
0
    def init_connection(self, timeout=None):
        """ Initialize the slave connection

        This will connect to the Slave, get it's description (should be
        usable for matching), and checks version compatibility
        """
        hostname = self._hostname
        port = self._port
        m_id = self._id

        logging.info("Connecting to RPC on machine %s (%s)", m_id, hostname)
        connection = CtlSecSocket(socket.create_connection((hostname, port),
                                                           timeout))
        connection.handshake(self._security)

        self._msg_dispatcher.add_slave(self, connection)

        hello, slave_desc = self.rpc_call("hello")
        if hello != "hello":
            msg = "Unable to establish RPC connection " \
                  "to machine %s, handshake failed!" % hostname
            raise MachineError(msg)

        slave_version = slave_desc["lnst_version"]

        if lnst_version != slave_version:
            if lnst_version.is_git_version:
                msg = ("Controller ({}) and Slave '{}' ({}) versions "
                       "are different".format(lnst_version, hostname,
                                              slave_version))
                logging.warning(len(msg)*"=")
                logging.warning(msg)
                logging.warning(len(msg)*"=")
            else:
                msg = ("Controller ({}) and Slave '{}' ({}) versions "
                       "are not compatible!".format(lnst_version, hostname,
                                                    slave_version))
                raise MachineError(msg)

        self._slave_desc = slave_desc
Exemplo n.º 8
0
    def _init_connection(self):
        """ Initialize the slave connection

        This will connect to the Slave, get it's description (should be
        usable for matching), and checks version compatibility
        """
        hostname = self._hostname
        port = self._port
        m_id = self._id

        logging.info("Connecting to RPC on machine %s (%s)", m_id, hostname)
        connection = CtlSecSocket(socket.create_connection((hostname, port)))
        connection.handshake(self._security)

        self._msg_dispatcher.add_slave(self, connection)

        hello, slave_desc = self.rpc_call("hello")
        if hello != "hello":
            msg = "Unable to establish RPC connection " \
                  "to machine %s, handshake failed!" % hostname
            raise MachineError(msg)

        slave_version = slave_desc["lnst_version"]
        slave_is_git = self.is_git_version(slave_version)
        ctl_version = self._ctl_config.version
        ctl_is_git = self.is_git_version(ctl_version)
        if slave_version != ctl_version:
            if ctl_is_git and slave_is_git:
                msg = "Controller and Slave '%s' git versions are different"\
                                                                    % hostname
                logging.warning(len(msg)*"=")
                logging.warning(msg)
                logging.warning(len(msg)*"=")
            else:
                msg = "Controller and Slave '%s' versions are not compatible!"\
                                                                    % hostname
                raise MachineError(msg)

        self._slave_desc = slave_desc