Skip to content

Commit 15c5b8d

Browse files
committed
refactor: 重构代码降低重复率
1 parent 98d555c commit 15c5b8d

1 file changed

Lines changed: 61 additions & 115 deletions

File tree

bgesdk/management/commands/model.py

Lines changed: 61 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -469,59 +469,21 @@ def _save_model_config(self, model_id, runtime, memory_size, timeout, home=None)
469469
output('解读模型配置已保存至:{}'.format(config_path))
470470

471471
def _install_sdk(self):
472-
home = get_home()
473472
config_path = self.get_model_config_path()
474473
config = get_config_parser(config_path)
475474
section_name = DEFAULT_MODEL_SECTION
476475
model_id = config_get(config.get, section_name, 'model_id')
477476
runtime = config_get(config.get, section_name, 'runtime')
478-
image_name = RUNTIMES[runtime]
479-
container_name = generate_container_name(model_id)
480-
command = WHEREIS_DOCKER if SYS_STR == 'windows' else WHICH_DOCKER
481-
with os.popen(command) as f:
482-
docker_path = f.read()
483-
if not docker_path:
484-
output(INSTALL_DOCKER_MESSAGE)
485-
sys.exit(1)
486477
client = self._get_docker_client()
487478
command = ('pip install --no-deps bge-python-sdk pimento '
488479
'requests_toolbelt -t /code/lib')
489-
try:
490-
client.images.get(image_name)
491-
except docker.errors.NotFound:
492-
pull_image(image_name)
480+
image_name = RUNTIMES[runtime]
481+
self._get_or_pull_image(client, image_name)
493482
output('开始安装模型依赖包...')
494483
command = ('sh -c "{}"').format(command)
495-
user = get_sys_user()
496-
try:
497-
container = client.containers.get(container_name)
498-
if container.status != 'exited':
499-
try:
500-
container.remove(force=True)
501-
except Exception:
502-
pass
503-
except docker.errors.NotFound:
504-
pass
505-
container = client.containers.run(
506-
image_name,
507-
command=command,
508-
name=container_name,
509-
volumes={home: { 'bind': WORKDIR, 'mode': 'rw' }},
510-
stop_signal='SIGINT',
511-
user=user,
512-
detach=True,
513-
auto_remove=True)
514-
try:
515-
logs = container.logs(stream=True)
516-
for log in logs:
517-
sys.stdout.write(log.decode('utf-8'))
518-
sys.stdout.flush()
519-
finally:
520-
if container.status != 'exited':
521-
try:
522-
container.remove(force=True)
523-
except Exception:
524-
pass
484+
container_name = generate_container_name(model_id)
485+
self._force_remove_container(client, container_name)
486+
self._run_by_container(client, image_name, command, container_name)
525487
output('安装完成')
526488

527489
def upload_model_expfs(self, args):
@@ -857,60 +819,64 @@ def install_deps(self, args):
857819
upgrade = args.upgrade
858820
force_reinstall = args.force_reinstall
859821
pkgs = ' '.join(package_name)
860-
home = get_home()
861822
config_path = self.get_model_config_path()
862823
config = get_config_parser(config_path)
863824
section_name = DEFAULT_MODEL_SECTION
864825
model_id = config_get(config.get, section_name, 'model_id')
865826
runtime = config_get(config.get, section_name, 'runtime')
866-
image_name = RUNTIMES[runtime]
867-
container_name = generate_container_name(model_id)
868-
deps = []
869-
if pkgs:
870-
deps.append(pkgs)
871-
if requirements:
872-
deps.append('-r {}'.format(requirements))
873-
command = WHEREIS_DOCKER if SYS_STR == 'windows' else WHICH_DOCKER
874-
with os.popen(command) as f:
875-
docker_path = f.read()
876-
if not docker_path:
877-
output(INSTALL_DOCKER_MESSAGE)
878-
sys.exit(1)
879827
client = self._get_docker_client()
880828
command = ['pip install']
881829
if upgrade:
882830
command.append('--upgrade')
883831
if force_reinstall:
884832
command.append('--force-reinstall')
833+
deps = []
834+
if pkgs:
835+
deps.append(pkgs)
836+
if requirements:
837+
deps.append('-r {}'.format(requirements))
885838
command.append('-t /code/lib {}'.format(' '.join(deps)))
886839
command = ' '.join(command)
887-
try:
888-
client.images.get(image_name)
889-
except docker.errors.NotFound:
890-
pull_image(image_name)
840+
image_name = RUNTIMES[runtime]
841+
self._get_or_pull_image(client, image_name)
891842
output('开始安装模型依赖包...')
892843
command = ('sh -c "{}"').format(command)
844+
container_name = generate_container_name(model_id)
845+
self._force_remove_container(client, container_name)
846+
self._run_by_container(client, image_name, command, container_name)
847+
output('安装完成')
848+
849+
def start_model(self, args):
850+
output('Model debug server is starting ...')
851+
home = get_home()
852+
config_path = self.get_model_config_path()
853+
config = get_config_parser(config_path)
854+
section_name = DEFAULT_MODEL_SECTION
855+
model_id = config_get(config.get, section_name, 'model_id')
856+
runtime = config_get(config.get, section_name, 'runtime')
857+
client = self._get_docker_client()
858+
image_name = RUNTIMES[runtime]
859+
self._get_or_pull_image(client, image_name)
860+
command = 'python -u /server/app.py'
893861
user = get_sys_user()
894-
try:
895-
container = client.containers.get(container_name)
896-
if container.status != 'exited':
897-
try:
898-
container.remove(force=True)
899-
except Exception:
900-
pass
901-
except docker.errors.NotFound:
902-
pass
862+
container_name = generate_container_name(model_id)
863+
self._force_remove_container(client, container_name)
903864
container = client.containers.run(
904865
image_name,
905866
command=command,
906867
name=container_name,
907868
volumes={home: { 'bind': WORKDIR, 'mode': 'rw' }},
908869
stop_signal='SIGINT',
870+
ports={TEST_SERVER_PORT: TEST_SERVER_PORT},
909871
user=user,
910872
detach=True,
873+
stream=True,
911874
auto_remove=True)
875+
output('Model {} was registered'.format(model_id))
876+
output('\n\tURL: {}/model/{}'.format(TEST_SERVER_ENDPOINT, model_id))
877+
output('\tMethod: GET\n')
912878
try:
913-
logs = container.logs(stream=True)
879+
logs = container.logs(stream=True, follow=True)
914880
for log in logs:
915881
sys.stdout.write(log.decode('utf-8'))
916882
sys.stdout.flush()
@@ -920,31 +886,34 @@ def install_deps(self, args):
920886
container.remove(force=True)
921887
except Exception:
922888
pass
923-
output('安装完成')
924889

925-
def start_model(self, args):
926-
output('Model debug server is starting ...')
927-
home = get_home()
928-
config_path = self.get_model_config_path()
929-
config = get_config_parser(config_path)
930-
section_name = DEFAULT_MODEL_SECTION
931-
model_id = config_get(config.get, section_name, 'model_id')
932-
runtime = config_get(config.get, section_name, 'runtime')
933-
image_name = RUNTIMES[runtime]
934-
container_name = generate_container_name(model_id)
890+
def _get_docker_client(self):
935891
command = WHEREIS_DOCKER if SYS_STR == 'windows' else WHICH_DOCKER
936892
with os.popen(command) as f:
937893
docker_path = f.read()
938894
if not docker_path:
939895
output(INSTALL_DOCKER_MESSAGE)
940896
sys.exit(1)
941-
client = self._get_docker_client()
897+
try:
898+
client = docker.from_env()
899+
except docker.errors.DockerException:
900+
output('请确认 docker 服务是否已开启。')
901+
output('启动命令:/etc/init.d/docker start')
902+
sys.exit(1)
903+
return client
904+
905+
def _get_or_pull_image(self, client, image_name):
942906
try:
943907
client.images.get(image_name)
944908
except docker.errors.NotFound:
945-
pull_image(image_name)
946-
command = 'python -u /server/app.py'
947-
user = get_sys_user()
909+
output('本地 docker 镜像 {} 不存在,开始拉取...'.format(image_name))
910+
return_code = os.system('docker pull {}'.format(image_name))
911+
if return_code != 0:
912+
output('镜像拉取失败,请重试')
913+
sys.exit(1)
914+
output('镜像拉取成功')
915+
916+
def _force_remove_container(self, client, container_name):
948917
try:
949918
container = client.containers.get(container_name)
950919
if container.status != 'exited':
@@ -954,22 +923,21 @@ def start_model(self, args):
954923
pass
955924
except docker.errors.NotFound:
956925
pass
926+
927+
def _run_by_container(self, client, image_name, command, container_name):
928+
home = get_home()
929+
user = get_sys_user
957930
container = client.containers.run(
958931
image_name,
959932
command=command,
960933
name=container_name,
961934
volumes={home: { 'bind': WORKDIR, 'mode': 'rw' }},
962935
stop_signal='SIGINT',
963-
ports={TEST_SERVER_PORT: TEST_SERVER_PORT},
964936
user=user,
965937
detach=True,
966-
stream=True,
967938
auto_remove=True)
968-
output('Model {} was registered'.format(model_id))
969-
output('\n\tURL: {}/model/{}'.format(TEST_SERVER_ENDPOINT, model_id))
970-
output('\tMethod: GET\n')
971939
try:
972-
logs = container.logs(stream=True, follow=True)
940+
logs = container.logs(stream=True)
973941
for log in logs:
974942
sys.stdout.write(log.decode('utf-8'))
975943
sys.stdout.flush()
@@ -978,26 +946,4 @@ def start_model(self, args):
978946
try:
979947
container.remove(force=True)
980948
except Exception:
981-
pass
982-
983-
def _get_docker_client(self):
984-
try:
985-
client = docker.from_env()
986-
except docker.errors.DockerException:
987-
notice_docker()
988-
sys.exit(1)
989-
return client
990-
991-
992-
def notice_docker():
993-
output('请确认 docker 服务是否已开启。')
994-
output('启动命令:/etc/init.d/docker start')
995-
996-
997-
def pull_image(image_name):
998-
output('本地 docker 镜像 {} 不存在,开始拉取...'.format(image_name))
999-
return_code = os.system('docker pull {}'.format(image_name))
1000-
if return_code != 0:
1001-
output('镜像拉取失败,请重试')
1002-
sys.exit(1)
1003-
output('镜像拉取成功')
949+
pass

0 commit comments

Comments
 (0)