예제 #1
0
def create_dummy_machine(uuid, box):
    try:
        log_string(uuid, "Creating {}".format(box["vm"]), "Green")
        machine = VirtualBox().create_machine(settings_file="",
                                              name=box["vm"],
                                              os_type_id="",
                                              groups=['/'],
                                              flags="")
        VirtualBox().register_machine(machine)
        virtual_machine = VirtualBox().find_machine(box["vm"])
        with virtual_machine.create_session() as session:
            process, x = session.machine.take_snapshot(
                box["snapshot"],
                datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"), True)
            process.wait_for_completion(timeout=-1)
            return True
    except Exception as e:
        log_string(uuid, "create_dummy_machine Failed {}".format(e), "Red")
예제 #2
0
def restore_machine(uuid, box):
    restoring_machine = False
    ret = False
    try:
        virtual_machine = VirtualBox().find_machine(box["vm"])
        snap = virtual_machine.find_snapshot(box["snapshot"])
        with virtual_machine.create_session() as session:
            try:
                restoring = session.machine.restore_snapshot(snap)
                restoring.wait_for_completion(-1)
                if restoring.completed == 1:
                    log_string(uuid,
                               "Restoring completed {}".format(box["vm"]),
                               "Green")
                    restoring_machine = True
            except Exception as e:
                log_string(uuid, "start_task Failed {}".format(e), "Red")
            if restoring_machine:
                ret = True
    except Exception as e:
        log_string(uuid, "start_task Failed {}".format(e), "Red")
    return ret
예제 #3
0
def vbox_remote_control(uuid, box):
    ret = False
    try:
        queue = Redis.from_url(redis_settings_docker)
        virtual_machine = VirtualBox().find_machine(box["vm"])
        vm_name_lock = "{}_lock".format(box["vm"])
        vm_name_frame = "{}_frame".format(box["vm"])
        vm_name_action = "{}_action".format(box["vm"])
        with virtual_machine.create_session() as session:
            session.unlock_machine()
            proc = virtual_machine.launch_vm_process(session, "headless", "")
            proc.wait_for_completion(timeout=-1)
            with session.console.guest.create_session(box["user"],
                                                      box["pass"]) as gs:
                h, w, _, _, _, _ = session.console.display.get_screen_resolution(
                    0)
                update_item(mongo_settings_docker["worker_db"],
                            mongo_settings_docker["worker_col_logs"], uuid, {
                                "status": "live",
                                "started_time": datetime.now()
                            })
                queue.set(vm_name_lock, "False")
                while queue.get(vm_name_lock) == b"False":
                    x, y, dz, dw, button_state, key = "false", "false", "false", "false", "false", "false"
                    try:
                        t = queue.get(vm_name_action)
                        if t and t != "None":
                            x, y, dz, dw, button_state, key = loads(t)
                            #log_string(uuid,">>>>>>>>>> {} {} {} {} {} {}".format(x,y,dz,dw,button_state,key),"Red")
                    except e:
                        pass
                    try:
                        if key != "false":
                            session.console.keyboard.put_scancodes(
                                list(scan_code_table[key]))
                        if "false" not in (x, y, dz, dw, button_state):
                            session.console.mouse.put_mouse_event_absolute(
                                x, y, dz, dw, 0)
                            if button_state == "leftclick":
                                session.console.mouse.put_mouse_event_absolute(
                                    x, y, dz, dw, 1)
                                session.console.mouse.put_mouse_event_absolute(
                                    x, y, dz, dw, 0)
                            elif button_state == "leftdoubleclick":
                                session.console.mouse.put_mouse_event_absolute(
                                    x, y, dz, dw, 1)
                                session.console.mouse.put_mouse_event_absolute(
                                    x, y, dz, dw, 0)
                            elif button_state == "rightclick":
                                session.console.mouse.put_mouse_event_absolute(
                                    x, y, dz, dw, 2)
                                session.console.mouse.put_mouse_event_absolute(
                                    x, y, dz, dw, 0)
                        queue.set(vm_name_action, "None")
                        png = session.console.display.take_screen_shot_to_array(
                            0, h, w, BitmapFormat.png)
                        queue.set(vm_name_frame, png)
                    except:
                        pass
                    sleep(.2)
                ret = True
            session.console.power_down()
    except Exception as e:
        log_string(uuid, "custom_task Failed {}".format(e), "Red")
    return ret