Skip to content

Commit

Permalink
feat: support running inside a container
Browse files Browse the repository at this point in the history
cloase: #10
  • Loading branch information
wey-gu committed Jan 12, 2024
1 parent 2b5d53c commit dd76808
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ With udocker, the opinionated subset docker running in user space, we could run

Thus we support running inside docker container, WSL2, Google Colab.

### Can NebulaGraph-Lite run inside a container?

Yes! Say we are in a container that runs Ubuntu, we could run:

```bash
docker run -it --rm ubuntu:latest bash
# inside the container
apt update && apt install python3-pip curl -y
pip3 install nebulagraph-lite
python3
```

In python3:

```python
from nebulagraph_lite import nebulagraph_let as ng_let
n = ng_let(in_container=True)
n.start()
```

### Does it support Windows?

Yes, it supports Windows with WSL2 or other Linux VMs with a Hypervisor.
Expand Down
25 changes: 23 additions & 2 deletions src/nebulagraph_lite/nebulagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(
base_path: str = BASE_PATH,
debug=False,
clean_up=False,
in_container=False,
):
if clean_up:
self.clean_up()
Expand Down Expand Up @@ -52,13 +53,29 @@ def __init__(
self._python_bin_path = os.path.dirname(_path[0])
else:
self._python_bin_path = os.path.dirname(os.sys.executable)
result = subprocess.run(
f"{self._python_bin_path}/udocker --help",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if result.returncode != 0:
_path = subprocess.getoutput("which udocker")
if _path:
self._python_bin_path = os.path.dirname(_path)
else:
raise Exception(
"udocker not found. Please install or link it manually to your PATH."
)

self._debug = debug

self.on_colab = self._is_running_on_colab()
if self.on_colab:
self.base_path = COLAB_BASE_PATH

self.in_container = in_container

self.create_nebulagraph_lite_folders()

def _is_running_on_colab(self):
Expand Down Expand Up @@ -137,6 +154,8 @@ def _run_udocker(self, command: str):
if self.on_colab:
return self._run_udocker_on_colab(command)
udocker_command_prefix = os.path.join(self._python_bin_path, "udocker")
if self.in_container:
udocker_command_prefix = udocker_command_prefix + " --allow-root"
udocker_command = f"{udocker_command_prefix} {command}"
result = subprocess.run(
udocker_command,
Expand Down Expand Up @@ -179,6 +198,8 @@ def _run_udocker_background(self, command: str):
return

udocker_command_prefix = os.path.join(self._python_bin_path, "udocker")
if self.in_container:
udocker_command_prefix = udocker_command_prefix + " --allow-root"
udocker_command = f"{udocker_command_prefix} {command} &"
subprocess.Popen(
udocker_command,
Expand All @@ -199,7 +220,7 @@ def udocker_pull_backgroud(self, image: str):
def _try_shoot_service(self, service: str):
try:
self._run_udocker(
f"ps | grep {service} | awk '{{print $1}}' | xargs -I {{}} udocker rm {{}}"
f"ps | grep {service} | awk '{{print $1}}' | xargs -I {{}} udocker rm -f {{}}"
)
os.system(f"killall nebula-{service} > /dev/null 2>&1")
except Exception as e:
Expand Down Expand Up @@ -347,7 +368,7 @@ def docker_ps(self):
def stop(self):
if self.on_colab:
self._run_udocker(
"ps | grep nebula | awk '{print $1}' | xargs -I {} udocker rm {}"
"ps | grep nebula | awk '{print $1}' | xargs -I {} udocker rm -f {}"
)
self._try_shoot_all_services()
return
Expand Down

0 comments on commit dd76808

Please sign in to comment.