Skip to content

Commit 3fb78f1

Browse files
author
Zhao Wang
committed
detect user's country and using corresponding docker mirror
1 parent b3346bf commit 3fb78f1

6 files changed

Lines changed: 78 additions & 17 deletions

File tree

brainframe/cli/commands/install.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from argparse import ArgumentParser
22
from pathlib import Path
3+
import json
4+
from urllib.request import urlopen
35

46
import i18n
57

@@ -103,6 +105,13 @@ def install():
103105
docker_compose.download(
104106
install_path / "docker-compose.yml", version=args.version
105107
)
108+
location = check_location()
109+
try:
110+
docker_config = get_docker_config()
111+
except Exception:
112+
print_utils.warning_translate("install.fail-to-load-docker-config")
113+
else:
114+
update_docker_mirror(location=location, docker_config=docker_config)
106115

107116
print_utils.translate("install.downloading-images")
108117
docker_compose.run(install_path, ["pull"])
@@ -120,8 +129,8 @@ def install():
120129
# Recommend to the user to add their custom paths to environment variables
121130
# so that future invocations of the program will know where to look.
122131
if (
123-
install_path != env_vars.install_path.default
124-
or data_path != env_vars.data_path.default
132+
install_path != env_vars.install_path.default
133+
or data_path != env_vars.data_path.default
125134
):
126135
print()
127136
print_utils.translate("install.set-custom-directory-env-vars")
@@ -188,3 +197,42 @@ def _parse_args():
188197
)
189198

190199
return subcommand_parse_args(parser)
200+
201+
202+
def check_location() -> str:
203+
url = 'http://ipinfo.io/json'
204+
try:
205+
response = urlopen(url, timeout=5)
206+
except Exception:
207+
return "US"
208+
data = json.load(response)
209+
return data["country"]
210+
211+
212+
def get_docker_config() -> dict:
213+
with open("/etc/docker/daemon.json") as docker_config_file:
214+
docker_config = json.load(docker_config_file)
215+
return docker_config
216+
217+
218+
def update_docker_mirror(docker_config: dict, location: str):
219+
docker_mirror_host = {"CN": "USTC", "US": "Docker Hub"}
220+
if location == "CN" and \
221+
("registry-mirrors" not in docker_config.keys() or
222+
docker_config["registry-mirrors"] !=
223+
["https://docker.mirrors.ustc.edu.cn/"]):
224+
docker_config["registry-mirrors"] = \
225+
["https://docker.mirrors.ustc.edu.cn/"]
226+
elif location != "CN" and "registry-mirrors" in docker_config.keys():
227+
del docker_config["registry-mirrors"]
228+
else:
229+
return
230+
if print_utils.ask_yes_no(
231+
"install.change-docker-mirror",
232+
location=location,
233+
mirror_host=docker_mirror_host[location]
234+
):
235+
with open("/etc/docker/daemon.json", "w") as docker_config_file:
236+
json.dump(docker_config, docker_config_file, indent=4,
237+
sort_keys=True)
238+
os_utils.restart_docker()

brainframe/cli/os_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def give_brainframe_group_rw_access(paths: List[Path]):
7777
run(["chmod", "-R", "g+rw"] + paths_str)
7878

7979

80+
def restart_docker():
81+
print_utils.translate("general.restarting_docker")
82+
run(["systemctl", "restart", "docker"])
83+
84+
8085
def _current_user():
8186
# If the SUDO_USER environment variable allows us to get the username of
8287
# the user running sudo instead of root. If they're not using sudo, we can

brainframe/cli/translations/general.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ en:
1616
staging-missing-credentials: "The %{username_env_var} and %{password_env_var}
1717
variables must be set to access staging."
1818
user-not-root: "This command must be run as root!"
19+
restarting_docker: "Restarting Docker..."

brainframe/cli/translations/install.en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ en:
8181
group? Doing so will allow the BrainFrame CLI to interact with Docker without
8282
root permissions."
8383
install-version: "BrainFrame:%{version} will be installed."
84+
change-docker-mirror: "We have detected you are located in %{location}, do
85+
you want to change docker mirror hosted by %{mirror_host} to speed up the
86+
download process? Please be aware that this will overwrite you docker
87+
daemon configuration and restart Docker service."
88+
fail-to-load-docker-config: "Fail to load the Docker configuration file,
89+
it's may caused by improper installation of Docker. Will keep using the
90+
original docker mirror."

poetry.lock

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "brainframe-cli"
3-
version = "0.1.5"
3+
version = "0.1.6.dev1"
44
license = "BSD-3-Clause"
55
authors = ["Aotu <info@aotu.ai>"]
66

0 commit comments

Comments
 (0)