Skip to content

Commit

Permalink
Annofabの認証情報が設定されていない場合は、標準入力からAnnofabの認証情報を入力できるようにする (#186)
Browse files Browse the repository at this point in the history
* update devcontainer

* poetry update

* annofabの認証情報を標準入力から入力できるようにする
  • Loading branch information
yuji38kwmt authored Mar 1, 2024
1 parent 46c7072 commit 5392181
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
9 changes: 1 addition & 8 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04 as devcontainer
FROM ubuntu:22.04

# Install prerequisites
RUN set -x \
Expand All @@ -23,10 +23,6 @@ ENV LANG ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
ENV LC_CTYPE ja_JP.UTF-8

# ミラーは遅いのが混じっているので好きなリポジトリに変更すべき
RUN set -x \
&& sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list

# Install Ubuntu packages
RUN set -x \
# Add repositories
Expand Down Expand Up @@ -86,8 +82,6 @@ RUN useradd -m -s /bin/bash -u 5000 -U vscode \
&& groupadd --gid 999 docker \
&& usermod -aG docker vscode

ADD entrypoint.sh /entrypoint.sh

USER vscode

RUN poetry config virtualenvs.in-project true
Expand All @@ -98,4 +92,3 @@ RUN set -x \
&& mkdir -p /home/vscode/.vscode-server/extensions /home/vscode/.vscode-server-insiders \
&& ln -s /home/vscode/.vscode-server/extensions /home/vscode/.vscode-server-insiders/extensions

ENTRYPOINT [ "bash", "/entrypoint.sh" ]
18 changes: 8 additions & 10 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.154.0/containers/docker-existing-dockerfile
{
"name": "KCI Python",
"build": {
"dockerfile": "Dockerfile",
"context": ".",
"args": {
"COMPOSE_VERSION": "1.27.4",
},
"target": "devcontainer"
"args": {}
},
"containerUser": "vscode",
"remoteUser": "vscode",
"updateRemoteUserUID": true,
"overrideCommand": false,
// マウント対象のディレクトリを事前に作成する
"initializeCommand": "mkdir -p ${localWorkspaceFolder}/.devcontainer/venv",
"mounts": [
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind",
"source=${localWorkspaceFolder}/.devcontainer/.extensions,target=/home/vscode/.vscode-server/extensions,type=bind",
// ホスト側のvenvとコンテン側のvenvを分類して、ホスト側でpoetryコマンドを利用できるようにする
"source=${localWorkspaceFolder}/.devcontainer/venv,target=${containerWorkspaceFolder}/.venv,type=bind"
],
Expand All @@ -32,9 +25,15 @@
"--env=ANNOFAB_USER_ID",
"--env=ANNOFAB_PASSWORD"
],
"containerEnv": {
"CONTAINER_WORKSPACE": "${containerWorkspaceFolder}",
"LOCAL_WORKSPACE": "${localWorkspaceFolder}"
},
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces,type=bind,consistency=cached",
"workspaceFolder": "/workspaces",
"postStartCommand": "poetry install",
"customizations": {
"vscode": {
"vscode": {
"extensions": [
"mosapride.zenkaku",
"ms-python.python",
Expand All @@ -47,5 +46,4 @@
]
}
}

}
9 changes: 0 additions & 9 deletions .devcontainer/entrypoint.sh

This file was deleted.

27 changes: 25 additions & 2 deletions annoworkcli/annofab/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import getpass
from typing import Optional

import annofabapi
from annofabapi import build as build_annofabapi
from annofabapi.exceptions import CredentialsNotFoundError
from annofabapi.exceptions import MfaEnabledUserExecutionError as AnnofabApiMfaEnabledUserExecutionError


def _get_annofab_user_id_from_stdin() -> str:
"""標準入力からAnnofabにログインする際のユーザーIDを取得します。"""
login_user_id = ""
while login_user_id == "":
login_user_id = input("Enter Annofab User ID: ")
return login_user_id


def _get_annofab_password_from_stdin() -> str:
"""標準入力からAnnofabにログインする際のパスワードを取得します。"""
login_password = ""
while login_password == "":
login_password = getpass.getpass("Enter Annofab Password: ")
return login_password


def build_annofabapi_resource_and_login(
*,
annofab_login_user_id: Optional[str] = None,
Expand All @@ -21,8 +39,13 @@ def build_annofabapi_resource_and_login(
annofabapi.Resourceインスタンス
"""

service = build_annofabapi(annofab_login_user_id, annofab_login_password)
try:
service = build_annofabapi(annofab_login_user_id, annofab_login_password)
except CredentialsNotFoundError:
# 環境変数, netrcフィアルに認証情報が設定されていなかったので、標準入力から認証情報を入力させる。
stdin_login_user_id = _get_annofab_user_id_from_stdin()
stdin_login_password = _get_annofab_password_from_stdin()
service = build_annofabapi(stdin_login_user_id, stdin_login_password)

try:
if mfa_code is not None:
Expand Down
11 changes: 6 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.9"
pyyaml = "*"
annoworkapi = ">=3.1"
annoworkapi = ">=3.2.1"
annofabapi = ">=0.72.0"
more-itertools = "*"
pandas = "*"
Expand Down

0 comments on commit 5392181

Please sign in to comment.