Skip to content

Commit

Permalink
Dockerize and Added tools for dockerizing
Browse files Browse the repository at this point in the history
  • Loading branch information
minhoryang committed Dec 6, 2019
1 parent ed765cc commit b76224b
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/.git
*/
*
!Dockerfile
!konlpy_grpc_gateway/
!tools/
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:latest AS gateway-build
WORKDIR /build
COPY konlpy_grpc_gateway/go.mod konlpy_grpc_gateway/go.sum ./
RUN go mod download
COPY konlpy_grpc_gateway .
RUN go build -o konlpy_grpc_gateway .

FROM python:latest AS swagger-merge
WORKDIR /build
COPY --from=gateway-build /build/_generated/ /build/konlpy_grpc_gateway/_generated/
COPY tools/merge.swagger.py merge.swagger.py
RUN python3 merge.swagger.py

FROM minhoryang/konlpy:v0.5.2 AS konlpy-grpc
LABEL maintainer="Minho Ryang <[email protected]>"
LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.name="minhoryang/konlpy-grpc"
LABEL org.label-schema.description="KoNLPy gRPC/HTTP Server"
LABEL org.label-schema.version="v0.1.0"

WORKDIR /app
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
COPY --from=gateway-build /build/konlpy_grpc_gateway /app/konlpy_grpc_gateway
COPY --from=swagger-merge /build/konlpy_grpc_gateway/_generated/index.swagger.json /app/_generated/index.swagger.json
COPY tools/supervisor.conf /app/supervisor.conf

# RUN python3 -m pip install konlpy-grpc
RUN python3 -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ konlpy-grpc

CMD ["/usr/local/bin/supervisord", "-c", "supervisor.conf"]
ENTRYPOINT []
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ python -m pytest --grpc-real-server=[::]:50051
python -m pytest --konlpy-repo=../konlpy
```

## Release
```bash
rm -rf dist/
poetry publish --build -r test
poetry run twine upload --repository-url https://test.pypi.org/legacy/ dist/*
```

## TODO
- [x] [P0] client.py will be a konlpy-alike module.
- [x] [P0] KoNLPy monkey-patcher
- [x] [P1] Packaging with Poetry `pyproject.toml`.
- [ ] PyPI Register
- [x] PyPI Register
- [ ] Find lowerbound-version of requirements. <!-- poetry debug:resolve -->
- [P1] gRPC Proto Compile
- [P1] In-house tool: `manage.py`
Expand All @@ -54,7 +61,7 @@ python -m pytest --konlpy-repo=../konlpy
- [x] [P1] gRPC reflection
- [P1] gRPC heartbeat
- [x] [P1] gRPC Gateway (gRPC to JSON)
- [P2] Dockerize / Register
- [x] [P2] Dockerize / Register
- k8s and istio?
- [P2] CI
- [P3] Button for deploying this to AWS/GCS/Azure now! (and connect by README.)
Expand Down
13 changes: 12 additions & 1 deletion konlpy_grpc_gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ go get -u github.com/minhoryang/KoNLPy-gRPC/konlpy_grpc_gateway
go install github.com/minhoryang/KoNLPy-gRPC/konlpy_grpc_gateway
$GOPATH/bin/konlpy_grpc_gateway -endpoint "[::]:50051" -swagger_dir _generated
```
It will run at http://localhost:8080
It will run at http://localhost


## Docker Build (gRPC and Gateway)
```bash
DOCKER_BUILDKIT=1 docker build --build-arg BUILDKIT_INLINE_CACHE=1 -t minhoryang/konlpy-grpc:v0.1.0 -f Dockerfile .
```

## Docker Run (gRPC and Gateway)
```bash
docker run -it -p 50051:50051 -p 80:80 minhoryang/konlpy-grpc:v0.1.0
```

## Thanks
https://github.com/grpc-ecosystem/grpc-gateway
2 changes: 1 addition & 1 deletion konlpy_grpc_gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {

ctx := context.Background()
opts := gateway.Options{
Addr: ":8080",
Addr: ":80",
GRPCServer: gateway.Endpoint{
Network: *network,
Addr: *endpoint,
Expand Down
177 changes: 171 additions & 6 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ homepage = "https://github.com/minhoryang/KoNLPy-gRPC"
repository = "https://github.com/minhoryang/KoNLPy-gRPC"
include = [
"konlpy_grpc/_generated/*.py",
"konlpy_grpc_gateway/README.md",
]
exclude = [
"konlpy_grpc/_generated/.git*",
"konlpy_grpc_gateway/*",
]

[tool.poetry.dependencies]
Expand All @@ -22,8 +24,8 @@ grpcio-tools = "*"
grpcio-reflection = "*"
konlpy = [
{ version = "^0.5.2", optional = true },
{ git = "https://github.com/minhoryang/konlpy", branch = "pruned/tags_only", optional = true, develop = true },
{ path = "../konlpy/", optional = true, develop = true },
# { git = "https://github.com/minhoryang/konlpy", branch = "pruned/tags_only", optional = true, develop = true },
# { path = "../konlpy/", optional = true, develop = true },
]
python = "^3.6"

Expand All @@ -37,6 +39,7 @@ poetry = "^0.12"
pytest = "*"
pytest-cov = "*"
pytest-grpc = "*"
twine = "*"

[tool.poetry.extras]
server = ["konlpy"]
Expand Down
24 changes: 24 additions & 0 deletions tools/merge.swagger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import glob
import json
import pathlib

ROOT = pathlib.Path.cwd() / "konlpy_grpc_gateway" / "_generated"
RESULT = {}
RESULT["swagger"] = "2.0"
RESULT["info"] = {}
RESULT["info"]["title"] = "KoNLPy Service"
RESULT["info"]["version"] = "0.1.0"
RESULT["consumes"] = ["application/json"]
RESULT["produces"] = ["application/json"]
RESULT["paths"] = {}
RESULT["definitions"] = {}
for swagger_file in ROOT.glob("*.swagger.json"):
operation_id_prefix = swagger_file.name.split(".")[0]
swagger_json = json.loads(swagger_file.read_bytes())
for path in swagger_json["paths"].values():
for method in path.values():
method["operationId"] = f"{operation_id_prefix}{method['operationId']}"
RESULT["paths"].update(swagger_json["paths"])
RESULT["definitions"].update(swagger_json["definitions"])

(ROOT / "index.swagger.json").write_text(json.dumps(RESULT))
6 changes: 6 additions & 0 deletions tools/supervisor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[program:grpc]
command = python3 -m konlpy_grpc server

[program:http]
command = ./konlpy_grpc_gateway -alsologtostderr -endpoint localhost:50051 -swagger_dir _generated
depends_on = grpc

0 comments on commit b76224b

Please sign in to comment.