56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
import logging
|
|
import os
|
|
import platform
|
|
import subprocess
|
|
import time
|
|
import docker
|
|
|
|
from desktop_env.providers.base import Provider
|
|
|
|
logger = logging.getLogger("desktopenv.providers.vmware.VMwareProvider")
|
|
logger.setLevel(logging.INFO)
|
|
|
|
WAIT_TIME = 3
|
|
|
|
class DockerProvider(Provider):
|
|
def __init__(self, region: str):
|
|
self.client = docker.from_env()
|
|
|
|
@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 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)
|
|
|
|
def get_ip_address(self, path_to_vm: str) -> str:
|
|
pass
|
|
|
|
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
|
|
|
|
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...")
|
|
self.container.stop(WAIT_TIME)
|
|
self.container.remove() |