Esempio n. 1
0
    def run_machine(self, username, name):
        """ Asks VM provider to run VM with provided name.
        """
        if not self.__select_vm_provider():
            return

        if not username:
            return
        user_machines = User.get_user_machines_by_name(username)
        if user_machines != [True]:
            if name not in user_machines:
                return
        return self.conn.run_machine(name)
Esempio n. 2
0
    def destroy_machine(self, username, name):
        """ Asks VM provider to force stop of VM with provided name.
        This can be called by UI after calling `stop_machine` function, which can be ignored by VM.
        """
        if not self.__select_vm_provider():
            return

        if not username:
            return
        user_machines = User.get_user_machines_by_name(username)
        if user_machines != [True]:
            if name not in user_machines:
                return

        return self.conn.destroy_machine(name)
Esempio n. 3
0
    def pause_machine(self, username, name):
        """ Asks VM provider to pause VM with provided name.
        TODO: This is not implemented yet by any VM provider class, also we don't have unpause function.
        """
        if not self.__select_vm_provider():
            return

        if not username:
            return
        user_machines = User.get_user_machines_by_name(username)
        if user_machines != [True]:
            if name not in user_machines:
                return

        return self.conn.pause_machine(name)
Esempio n. 4
0
    def stop_machine(self, username, name):
        """ Asks VM provider to stop VM with provided name.
        This can be ignored by target so UI after calling this method can call
        `destroy_machine` method to force stopping.
        """
        if not self.__select_vm_provider():
            return

        if not username:
            return
        user_machines = User.get_user_machines_by_name(username)
        if user_machines != [True]:
            if name not in user_machines:
                return

        return self.conn.stop_machine(name)
Esempio n. 5
0
    def vnc_connection(self, username, machine_name, local_user=False):
        """ Function to prepare proxy VNC connection for VM provider """
        global localhost

        # Step 1: can user view this machine at all?
        if not username:
            return False, "Not allowed"
        user_machines = User.get_user_machines_by_name(username)
        if user_machines == [True]:
            pass # Admin
        elif machine_name not in user_machines:
            return False, 'Now allowed'

        # Step 2: Finding your IP to bind to it (this allows remote users to connect)
        if not local_user: # This was added because local user does not store cookies for WAN IP connection.
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            sock.connect(('yandex.ru', 0))
            hostname = sock.getsockname()[0]
        else:
            hostname = localhost

        # Step 3: Finding but not binding free port to run
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.bind((hostname, 0)) # Asking OS to bind free port on your WAN IP
        except Exception:
            sock.bind(('', 0)) # If failed - on local IP.
            hostname = localhost
        finally:
            listen_port = sock.getsockname()[1] # << Here it is
        del(sock)

        # Step 4: Processing by VM manager
        answer = self.conn.vnc_connection(user_groups=groupfinder(username, None),
                                            machine_name=machine_name,
                                            listen_port=listen_port,
                                            listen_host=hostname,
                                            target_host=localhost)

        # Step 5: Pack answer to client
        return answer
Esempio n. 6
0
    def get_vms_list(self, username=None, no_cache=False):
        """ Asking VM provider to provide list of all non-presets VMs, then filtering by allowed.

            @param no_cache - clear machines list cache flag
        """
        if not self.__select_vm_provider():
            return
        if not username:
            return

        vms = self.conn.get_machines_list(no_cache)
        user_machines = User.get_user_machines_by_name(username)

        if user_machines == [True]:
            return vms
        elif user_machines is not None:
            vms['offline'] = filter(lambda x: x['name'] in user_machines, vms['offline'])
            vms['online'] = filter(lambda x: x['name'] in user_machines, vms['online'])
            return vms
        else:
            return None