-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from PatrickRWells/server-settings
Better Server Settings Management
- Loading branch information
Showing
10 changed files
with
282 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,5 @@ | ||
import os | ||
import pickle | ||
import signal | ||
import subprocess | ||
import time | ||
from functools import cache | ||
from pathlib import Path | ||
from urllib import parse | ||
from .cmd import set_server_location, start, stop | ||
from .config import get_config | ||
from .install import install, upgrade | ||
|
||
import appdirs | ||
|
||
from .install import DEFAULT_SERVER_INSTALL_LOCATION, install, upgrade | ||
|
||
|
||
@cache | ||
def get_server_path(): | ||
config_path = Path(appdirs.user_config_dir("godata")) / "server_path" | ||
if not config_path.exists(): | ||
return DEFAULT_SERVER_INSTALL_LOCATION / "godata_server" | ||
|
||
with open(config_path, "rb") as f: | ||
return pickle.load(f) | ||
|
||
|
||
@cache | ||
def get_server_location(): | ||
full_path = get_server_path() | ||
return full_path.parent | ||
|
||
|
||
def set_server_location(path: Path): | ||
server_path = path / "godata_server" | ||
config_path = Path(appdirs.user_config_dir("godata")) | ||
config_path.mkdir(parents=True, exist_ok=True) | ||
path_path = config_path / "server_path" | ||
with open(path_path, "wb") as f: | ||
pickle.dump(server_path, f) | ||
|
||
|
||
def start(port: int = None): | ||
# check if a godata_server process is already running | ||
|
||
try: | ||
server_pid = subprocess.check_output(["pgrep", "godata_server"]) | ||
print( | ||
f"Server is already running with PID {int(server_pid)}. " | ||
"Please stop the server before starting a new one." | ||
) | ||
return | ||
except subprocess.CalledProcessError: | ||
pass | ||
|
||
try: | ||
command = str(get_server_path()) | ||
if port: | ||
command += f" --port={port}" | ||
url = f"http://localhost:{port}" | ||
else: | ||
SERVER_PATH = str(Path.home() / ".godata.sock") | ||
url = f"http+unix://{parse.quote(SERVER_PATH, safe='')}" | ||
FILE_OUTPUT_PATH = Path.home() / ".godata_server" | ||
with open(FILE_OUTPUT_PATH, "w") as f: | ||
f.write(url) | ||
subprocess.Popen(command, close_fds=True, shell=True) | ||
except FileNotFoundError: | ||
raise FileNotFoundError( | ||
"Unable to start godata server: could not find the server binary. " | ||
"Please run `godata server install` first." | ||
) | ||
time.sleep(0.5) | ||
return True | ||
|
||
|
||
def stop(): | ||
try: | ||
server_pid = subprocess.check_output(["pgrep", "godata_server"]) | ||
except subprocess.CalledProcessError: | ||
print("Server is not running.") | ||
return | ||
# kill the server | ||
print(int(server_pid)) | ||
os.kill(int(server_pid), signal.SIGINT) | ||
# remove the file that stores the server url | ||
print("STOPPED") | ||
FILE_OUTPUT_PATH = Path.home() / ".godata_server" | ||
if FILE_OUTPUT_PATH.exists(): | ||
FILE_OUTPUT_PATH.unlink() | ||
|
||
|
||
__all__ = ["start", "stop", "install", "upgrade"] | ||
__all__ = ["start", "stop", "install", "upgrade", "get_config", "set_server_location"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
import os | ||
import signal | ||
import subprocess | ||
import time | ||
from pathlib import Path | ||
from urllib import parse | ||
|
||
import portalocker | ||
|
||
from .config import SERVER_CONFIG_PATH, ServerConfig | ||
|
||
|
||
def set_server_location(path: Path): | ||
if not path.is_dir(): | ||
raise ValueError(f"{path} is not a valid directory.") | ||
binary_path = path / "godata_server" | ||
|
||
with portalocker.Lock(SERVER_CONFIG_PATH, "r+") as f: | ||
config = json.load(f) | ||
config = ServerConfig(**config) | ||
if config.is_running: | ||
raise RuntimeError("Cannot change server path while server is running") | ||
config.server_path = binary_path | ||
# clear the file contents | ||
f.seek(0) | ||
f.truncate() | ||
f.write(config.model_dump_json(indent=2)) | ||
|
||
|
||
def start(port: int = None): | ||
# check if a godata_server process is already running | ||
with portalocker.Lock(SERVER_CONFIG_PATH, "r+") as f: | ||
config = json.load(f) | ||
config = ServerConfig(**config) | ||
already_running = config.is_running | ||
|
||
if not already_running: | ||
config.is_running = True | ||
config.port = port | ||
|
||
try: | ||
command = str(config.server_path) | ||
if port: | ||
command += f" --port={port}" | ||
url = f"http://localhost:{port}" | ||
else: | ||
SERVER_PATH = str(Path.home() / ".godata.sock") | ||
url = f"http+unix://{parse.quote(SERVER_PATH, safe='')}" | ||
|
||
config.server_url = url | ||
subprocess.Popen(command, close_fds=True, shell=True) | ||
except FileNotFoundError: | ||
raise FileNotFoundError( | ||
"Unable to start godata server: could not find the server binary. " | ||
"Please run `godata server install` first." | ||
) | ||
f.seek(0) | ||
f.truncate() | ||
f.write(config.model_dump_json(indent=2)) | ||
time.sleep(0.1) | ||
|
||
return not already_running | ||
|
||
|
||
def stop(): | ||
with portalocker.Lock(SERVER_CONFIG_PATH, "r+") as f: | ||
config = json.load(f) | ||
config = ServerConfig(**config) | ||
if not config.is_running: | ||
raise RuntimeError("Server is not running.") | ||
|
||
# check if the url is a unix socket or localhost | ||
local_urls = ["http://localhost", "http+unix://"] | ||
if not any(config.server_url.startswith(url) for url in local_urls): | ||
raise RuntimeError("Cannot stop server running on a remote host.") | ||
|
||
server_pid = subprocess.check_output(["pgrep", "godata_server"]) | ||
os.kill(int(server_pid), signal.SIGINT) | ||
|
||
config.stop() | ||
f.seek(0) | ||
f.truncate() | ||
f.write(config.model_dump_json(indent=2)) |
Oops, something went wrong.