Update docker manager & provider
This commit is contained in:
@@ -4,6 +4,7 @@ import platform
|
||||
import subprocess
|
||||
import time
|
||||
import docker
|
||||
import psutil
|
||||
|
||||
from desktop_env.providers.base import Provider
|
||||
|
||||
@@ -15,42 +16,35 @@ WAIT_TIME = 3
|
||||
class DockerProvider(Provider):
|
||||
def __init__(self, region: str):
|
||||
self.client = docker.from_env()
|
||||
self.vnc_port = self._get_available_port(8006)
|
||||
self.server_port = self._get_available_port(5000)
|
||||
# self.remote_debugging_port = self._get_available_port(1337)
|
||||
self.chromium_port = self._get_available_port(9222)
|
||||
self.environment = {"DISK_SIZE": "64G", "RAM_SIZE": "4G", "CPU_CORES": "2"} # Modify if needed
|
||||
|
||||
@staticmethod
|
||||
def _execute_command(command: list, return_output=False):
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
encoding="utf-8"
|
||||
)
|
||||
|
||||
if return_output:
|
||||
output = process.communicate()[0].strip()
|
||||
return output
|
||||
else:
|
||||
return None
|
||||
def _get_available_port(port: int):
|
||||
while port < 65354:
|
||||
if port not in [conn.laddr.port for conn in psutil.net_connections()]:
|
||||
return port
|
||||
port += 1
|
||||
|
||||
def start_emulator(self, path_to_vm: str, headless: bool, os_type: str):
|
||||
self.container = self.client.containers.run('qemux/qemu-docker', environment={"DISK_SIZE": "64G", "RAM_SIZE": "6G", "CPU_CORES": "8"}, volumes={"/Users/happysix/Programs/HKUNLP/Qemu/Ubuntu.qcow2": {"bind": "/Ubuntu.qcow2", "mode": "ro"}, "/Users/happysix/Programs/HKUNLP/Qemu/snapshot.qcow2": {"bind": "/boot.qcow2", "mode": "rw"}}, cap_add=["NET_ADMIN"], ports={8006: 8006, 5000: 5001}, detach=True)
|
||||
# self.container = self.client.containers.run('qemux/qemu-docker', environment={"DISK_SIZE": "64G", "RAM_SIZE": "6G", "CPU_CORES": "8"}, volumes={"/Users/happysix/Programs/HKUNLP/Qemu/Ubuntu.qcow2": {"bind": "/Ubuntu.qcow2", "mode": "ro"}, "/Users/happysix/Programs/HKUNLP/Qemu/snapshot.qcow2": {"bind": "/boot.qcow2", "mode": "rw"}}, cap_add=["NET_ADMIN"], ports={8006: self.vnc_port, 5000: self.server_port}, detach=True)
|
||||
self.container = self.client.containers.run(path_to_vm, environment=self.environment, cap_add=["NET_ADMIN"], volumes={"/Users/happysix/Programs/HKUNLP/Qemu/Ubuntu.qcow2": {"bind": "/Ubuntu.qcow2", "mode": "ro"}}, ports={8006: self.vnc_port, 5000: self.server_port}, detach=True)
|
||||
|
||||
def get_ip_address(self, path_to_vm: str) -> str:
|
||||
pass
|
||||
return f"localhost:{self.server_port}:{self.chromium_port}:{self.vnc_port}"
|
||||
|
||||
def save_state(self, path_to_vm: str, snapshot_name: str):
|
||||
logger.info("Saving VM state...")
|
||||
DockerProvider._execute_command(
|
||||
["qemu-img", "convert", "-O", "qcow2", snapshot_name, "temp.qcow2"])
|
||||
DockerProvider._execute_command(
|
||||
["mv", "temp.qcow2", path_to_vm]
|
||||
)
|
||||
time.sleep(WAIT_TIME) # Wait for the VM to save
|
||||
raise NotImplementedError("Not available for Docker.")
|
||||
|
||||
def revert_to_snapshot(self, path_to_vm: str, snapshot_name: str):
|
||||
pass
|
||||
|
||||
def stop_emulator(self, path_to_vm: str):
|
||||
logger.info("Stopping VMware VM...")
|
||||
logger.info("Stopping VM...")
|
||||
self.container.stop(WAIT_TIME)
|
||||
self.container.remove()
|
||||
self.container.remove()
|
||||
|
||||
# docker run -it --rm -e "DISK_SIZE=64G" -e "RAM_SIZE=8G" -e "CPU_CORES=8" --volume C:\Users\admin\Documents\Ubuntu.qcow2:/boot.qcow2 --cap-add NET_ADMIN --device /dev/kvm -p 8006:8006 -p 5000:5000 qemux/qemu-docker
|
||||
Reference in New Issue
Block a user