Skip to content

Commit

Permalink
VSCode devcontainerの設定を追加 (#307)
Browse files Browse the repository at this point in the history
* update pyproject.toml

* annofab v0.111.0

* add devcontainer

* add devcontainer

* add devcontainer

* add vscode setting

* update make

* ignore pattern
  • Loading branch information
yuji38kwmt committed May 19, 2021
1 parent 1a8c25e commit 0623194
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .devcontainer/.extensions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
132 changes: 132 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
FROM ubuntu:18.04 as devcontainer

RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/*

RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
tzdata \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* \
&& ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
&& echo 'Asia/Tokyo' >/etc/timezone

# create vscode user
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
sudo \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* \
&& echo "vscode ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers.d/ALL \
&& groupadd \
--gid 5000 \
vscode \
&& useradd \
--uid 5000 \
--gid 5000 \
--home-dir /home/vscode \
--create-home \
--shell /bin/bash \
vscode

# vscode extensions cache
# https://code.visualstudio.com/docs/remote/containers-advanced#_avoiding-extension-reinstalls-on-container-rebuild
RUN set -x \
&& mkdir -p /home/vscode/.vscode-server/extensions /home/vscode/.vscode-server-insiders \
&& chown -R vscode:vscode /home/vscode/.vscode-server /home/vscode/.vscode-server-insiders \
&& ln -s /home/vscode/.vscode-server/extensions /home/vscode/.vscode-server-insiders/extensions

# common tools
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
bash-completion \
curl \
git \
gnupg2 \
iputils-ping \
jq \
less \
net-tools \
openssh-client \
tar \
time \
unzip \
xz-utils \
zip \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/*

# docker
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common \
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - \
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
docker-ce-cli \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* \
&& groupadd --gid 999 docker \
&& usermod -aG docker vscode

# docker compose
ARG COMPOSE_VERSION=1.27.4
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* \
&& curl -fsSL "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose \
&& chmod +x /usr/local/bin/docker-compose \
&& curl -fsSL -o /etc/bash_completion.d/docker-compose "https://raw.githubusercontent.com/docker/compose/${COMPOSE_VERSION}/contrib/completion/bash/docker-compose"

# docker / docker-compose in devcontainer
RUN set -x \
&& mkdir -p /usr/local/devcontainer-tool/bin \
&& curl -fsSL -o /usr/local/devcontainer-tool/bin/docker https://raw.githubusercontent.com/thamaji/devcontainer-docker/main/docker \
&& chmod +x /usr/local/devcontainer-tool/bin/docker \
&& curl -fsSL -o /usr/local/devcontainer-tool/bin/docker-compose https://github.com/thamaji/devcontainer-compose/releases/download/v1.0.0/docker-compose \
&& chmod +x /usr/local/devcontainer-tool/bin/docker-compose
ENV PATH=/usr/local/devcontainer-tool/bin:${PATH}

# python3.8, make
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
python3.8 \
python3-distutils \
make \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* \
&& ln -s $(which python3.8) /usr/local/bin/python

# pip
RUN set -x \
&& curl -sSL https://bootstrap.pypa.io/get-pip.py | sudo -u vscode -i python -

# poetry
RUN set -x \
&& curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | sudo -u vscode -i python - \
&& sudo -u vscode -i /home/vscode/.local/bin/poetry config virtualenvs.in-project true
ENV PATH=$PATH:/home/vscode/.local/bin

# fix poetry issue (see: https://github.com/python-poetry/poetry/issues/221)
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
language-pack-en \
&& apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/*
ENV LC_ALL=C.UTF-8


USER vscode

ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT [ "bash", "-eux", "/entrypoint.sh" ]
31 changes: 31 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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": "Existing Dockerfile",
"build": {
"dockerfile": "Dockerfile",
"context": ".",
"args": {},
"target": "devcontainer"
},
"containerUser": "vscode",
"remoteUser": "vscode",
"updateRemoteUserUID": true,
"overrideCommand": false,
"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"
],
"forwardPorts": [],
"runArgs": [
"--init",
"--net=host"
],
"postStartCommand": "poetry install",
"extensions": [
"mosapride.zenkaku",
"ms-python.python",
"ms-python.vscode-pylance",
"streetsidesoftware.code-spell-checker"
]
}
9 changes: 9 additions & 0 deletions .devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -eu

# docker.sock から gid を取得して、docker グループの gid を変更
docker_group_id=$(ls -n /var/run/docker.sock | cut -d ' ' -f 4)
sudo groupmod --gid ${docker_group_id} docker

# 無限待ち(vscode デフォルトの entrypoint と同じ方法で)
while sleep 1000; do :; done
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ venv.bak/
.idea/

# Visual Studio Code
.vscode
# .vscode


# temporary directory
Expand Down
49 changes: 49 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"python.linting.pylintEnabled": true,
"python.autoComplete.extraPaths": [
".venv/lib/python3.8/site-packages/"
],

"python.formatting.blackPath": ".venv/bin/black",
"python.formatting.provider": "black",
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
"python.linting.pylintPath": ".venv/bin/pylint",
"python.linting.pylintArgs": [
"--rcfile setup.cfg",
],
"python.linting.flake8Enabled": true,
"python.linting.flake8Path": ".venv/bin/flake8",
"python.linting.flake8Args": [],
"python.linting.mypyEnabled": true,
"python.linting.mypyPath": ".venv/bin/mypy",
"python.linting.mypyArgs": [],
"python.linting.banditEnabled": false,
"python.linting.pylamaEnabled": false,
"python.linting.prospectorEnabled": false,
"python.linting.pydocstyleEnabled": false,
"python.linting.pycodestyleEnabled": false,
"python.linting.ignorePatterns": [
".vscode/*.py",
"**/site-packages/**/*.py"
],
"python.analysis.extraPaths": [
".venv/lib/python3.8/site-packages/"
],
"python.analysis.completeFunctionParens": true,
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
},
"cSpell.ignorePaths": [
"**/.git/objects/**",
".devcontainer/",
".vscode/"
],
"cSpell.words": [
"pylint",
"asyncio"
]
}
8 changes: 4 additions & 4 deletions annofabapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def fatal_code(e):
e: exception
Returns:
True: giveup(リトライしない), False: リトライする
True: give up(リトライしない), False: リトライする
"""
if isinstance(e, requests.exceptions.HTTPError):
Expand Down Expand Up @@ -164,7 +164,7 @@ def _create_kwargs(
@staticmethod
def _response_to_content(response: requests.Response) -> Any:
"""
Reponseのcontentを、Content-Typeに対応した型に変換する。
Responseのcontentを、Content-Typeに対応した型に変換する。
Args:
response:
Expand Down Expand Up @@ -200,7 +200,7 @@ def _request_wrapper(
request_body: Optional[Any] = None,
) -> Tuple[Any, requests.Response]:
"""
HTTP Requestを投げて、Reponseを返す
HTTP Requestを投げて、Responseを返す
Args:
http_method:
Expand Down Expand Up @@ -313,7 +313,7 @@ def login(self) -> Tuple[Dict[str, Any], requests.Response]:
json_obj = response.json()
self.token_dict = json_obj["token"]

logger.debug("Logined successfully. user_id = %s", self.login_user_id)
logger.debug("Logged in successfully. user_id = %s", self.login_user_id)
return json_obj, response

def logout(self) -> Optional[Tuple[Dict[str, Any], requests.Response]]:
Expand Down

0 comments on commit 0623194

Please sign in to comment.