diff --git a/.github/workflows/debug.yml b/.github/workflows/debug.yml deleted file mode 100644 index a678421..0000000 --- a/.github/workflows/debug.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: debug - -on: push - -jobs: - one: - runs-on: ubuntu-latest - steps: - - name: Dump GitHub context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - name: Dump job context - env: - JOB_CONTEXT: ${{ toJson(job) }} - run: echo "$JOB_CONTEXT" - - name: Dump steps context - env: - STEPS_CONTEXT: ${{ toJson(steps) }} - run: echo "$STEPS_CONTEXT" - - name: Dump runner context - env: - RUNNER_CONTEXT: ${{ toJson(runner) }} - run: echo "$RUNNER_CONTEXT" - - name: Dump strategy context - env: - STRATEGY_CONTEXT: ${{ toJson(strategy) }} - run: echo "$STRATEGY_CONTEXT" - - name: Dump matrix context - env: - MATRIX_CONTEXT: ${{ toJson(matrix) }} - run: echo "$MATRIX_CONTEXT" diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3dd00b2..51407ee 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,6 +10,10 @@ on: release: types: [published] +env: + registory: 'jiro4989/websh' + image-tag: 'latest' + jobs: skip: runs-on: ubuntu-latest @@ -27,27 +31,38 @@ jobs: needs: before steps: - uses: actions/checkout@v1 - - name: Pull docker image - run: docker pull jiro4989/websh:latest - - uses: jiro4989/setup-nim-action@v1 - with: - nim-version: devel - - name: Test server + - name: Setup run: | pushd websh_server echo -e '\nswitch("d", "ci")' > tests/config.nims - nimble test -Y - - name: Build server - run: docker-compose -f docker-compose-ci.yml run server - # FIXME: GitHubActionsでDockerAPIを使用可能にする必要がある - # - name: Test API - # run: ./.github/scripts/api_test.sh - - name: Build front - run: docker-compose -f docker-compose-ci.yml run front - - name: Archive release files + + - name: Build image (base) + run: docker build --target base -t base . + + - name: Build (front) + run: docker build --target websh_front_builder . + + - name: Test (server) + run: docker run --rm -v $PWD/websh_server:/work -t base nimble test -Y + + - name: Build image (server runtime) + run: docker build --target websh_server_runtime -t ${{ env.registory }}:server-${{ env.image-tag }} . + + - name: Build image (remover runtime) + run: docker build --target websh_remover_runtime -t ${{ env.registory }}:remover-${{ env.image-tag }} . + + - name: Login docker + run: echo "${{ secrets.DOCKER_PASS }}" | docker login -u "${{ secrets.DOCKER_USER }}" --password-stdin + + - name: Push image to registory + run: | + docker push ${{ env.registory }}:server-${{ env.image-tag }} + docker push ${{ env.registory }}:remover-${{ env.image-tag }} + if: startsWith(github.ref, 'refs/tags/') + + - name: Create artifact run: | mkdir -p dist/websh - cp -r websh_server/bin dist/websh cp -r websh_front/public dist/websh git describe --tags --abbrev=0 > dist/websh/tag.txt pushd dist diff --git a/.gitignore b/.gitignore index e853fe7..3e38394 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,9 @@ -/bin/ +websh_server/bin/* +websh_server/tests/twebsh_server +websh_server/tmp/* +!websh_server/tmp/.gitkeep -websh_server/bin/websh_server websh_front/public/js/index.js websh_front/tests/*.js -websh_server/tests/twebsh_server - -websh_server/bin/websh_remover -websh_server/tools/visor \ No newline at end of file +websh_remover/bin/* diff --git a/Dockerfile b/Dockerfile index 0ac2975..b7aa7fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,48 @@ -FROM ubuntu:19.10 +FROM alpine:3.12 AS base -RUN apt-get update -yqq \ - && apt-get install -y --no-install-recommends \ - ca-certificates \ - curl \ - build-essential \ - git \ - ; +RUN apk add --no-cache \ + alpine-sdk ENV PATH /root/.nimble/bin:$PATH -RUN curl https://nim-lang.org/choosenim/init.sh -sSf > init.sh -RUN sh init.sh -y \ - && choosenim devel + +# install nim (#devel) and nim-tools +RUN mkdir -p /nim && \ + git clone https://github.com/nim-lang/Nim /nim && \ + cd /nim && \ + sh build_all.sh && \ + ln -s `pwd`/bin/nim /bin/nim && \ + nim c koch && \ + ./koch tools && \ + ln -s `pwd`/bin/nimble /bin/nimble && \ + ln -s `pwd`/bin/nimsuggest /bin/nimsuggest + +RUN mkdir /work +WORKDIR /work + +################################################################################ +# builder stages +################################################################################ + +FROM base AS websh_front_builder +COPY websh_front/ /work +RUN nimble build -Y + +FROM base AS websh_server_builder +COPY websh_server/ /work +RUN nimble build -Y -d:release + +FROM base AS websh_remover_builder +COPY websh_remover/ /work +RUN nimble build -Y -d:release + +################################################################################ +# runtime stages +################################################################################ + +FROM alpine:3.12 AS websh_server_runtime +COPY --from=websh_server_builder /work/bin/websh_server /usr/local/bin/ +ENTRYPOINT ["/usr/local/bin/websh_server"] + +FROM alpine:3.12 AS websh_remover_runtime +COPY --from=websh_remover_builder /work/bin/websh_remover /usr/local/bin/ +ENTRYPOINT ["/usr/local/bin/websh_remover"] diff --git a/README.rst b/README.rst index 6b3784b..31a7675 100644 --- a/README.rst +++ b/README.rst @@ -134,30 +134,25 @@ APIサーバはコンテナの破棄のトリガーを生成するのみに留 Path Description ===================== ======================================== docs READMEの画像ファイルなど -proxy ローカル開発用のnginxの設定 -websh_front フロントエンドのソースコード -websh_server バックエンドのソースコード +nginx ローカル開発用のnginxの設定 +websh_front フロントエンドのプログラム +websh_server バックエンドのAPIサーバのプログラム +websh_remover バックエンドの後始末を行うプログラム config.nims タスク定義 -Dockerfile ローカル開発でのみ使用する +Dockerfile アプリのDockerイメージ docker-compose.yml ローカル開発でのみ使用する開発環境設定 -docker-compose-ci.yml GitHub Actionsでのみ使用する ===================== ======================================== 開発環境の起動方法 ------------------ -リポジトリのルート直下の `config.nims` にリポジトリ内で使用するタスクを定義して -いる。 以下のコマンドをリポジトリディレクトリ配下で実行する。 .. code-block:: shell - # 最初の一度、あるいはDockerイメージを更新したいときだけ実行 - nim --hints:off pullShellgeiBotImage - - # 開発環境の起動 - nim --hints:off run + docker-compose build + docker-compose up サーバを起動して待機状態になったら、ブラウザで以下のページにアクセスする。 diff --git a/build/compiler_devel/Dockerfile b/build/compiler_devel/Dockerfile new file mode 100644 index 0000000..82a3abe --- /dev/null +++ b/build/compiler_devel/Dockerfile @@ -0,0 +1,24 @@ +FROM ubuntu:19.10 + +RUN apt-get update -yqq && \ + apt-get install -yqq --no-install-recommends \ + build-essential \ + ca-certificates \ + git \ + && \ + rm -rf /var/lib/apt/lists/* + +RUN git clone --depth 1 https://github.com/nim-lang/Nim && \ + cd Nim && \ + bash build_all.sh + +ENV PATH $PWD/Nim/bin:$PATH + +RUN nimble install -Y \ + jester \ + karax \ + regex \ + ; + +RUN nim --version && \ + nimble --version diff --git a/build/compiler_stable/Dockerfile b/build/compiler_stable/Dockerfile new file mode 100644 index 0000000..5814d94 --- /dev/null +++ b/build/compiler_stable/Dockerfile @@ -0,0 +1,10 @@ +FROM nimlang/nim:1.0.6-ubuntu + +RUN nimble install -Y \ + jester \ + karax \ + regex \ + ; + +RUN nim --version && \ + nimble --version diff --git a/build/executor/Dockerfile b/build/executor/Dockerfile new file mode 100644 index 0000000..5e11bf7 --- /dev/null +++ b/build/executor/Dockerfile @@ -0,0 +1,35 @@ +FROM docker:19.03-dind AS base + +# Let's start with some basic stuff. +RUN apk add --no-cache alpine-sdk +RUN mkdir -p /nim && \ + curl -sL "http://nim-lang.org/download/nim-1.0.6.tar.xz" | tar xJ --strip-components=1 -C /nim && \ + cd /nim && \ + sh build.sh && \ + rm -r c_code tests + +ENV PATH=/nim/bin:/root/.nimble/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +WORKDIR /nim +RUN nim c -d:release koch.nim && \ + ./koch nimble + +RUN nim --version && \ + nimble --version + + +FROM base AS builder + +COPY nimbot_executor /work +RUN cd /work && nimble install -Y + + +FROM docker:19.03-dind AS runtime + +RUN apk add --update --no-cache tzdata && \ + cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \ + echo "Asia/Tokyo" > /etc/timezone && \ + apk del tzdata + +COPY --from=builder /root/.nimble/bin/nimbot_executor /usr/local/bin/nimbot_executor +ENTRYPOINT ["/usr/local/bin/nimbot_executor"] diff --git a/build/fluentd/Dockerfile b/build/fluentd/Dockerfile new file mode 100644 index 0000000..5142a0e --- /dev/null +++ b/build/fluentd/Dockerfile @@ -0,0 +1,5 @@ +FROM fluentd:v1.9-1 AS runtime + +USER root +RUN mkdir /var/log/nimbot +COPY conf/fluentd /fluentd/etc diff --git a/build/server/Dockerfile b/build/server/Dockerfile new file mode 100644 index 0000000..0dfbe1a --- /dev/null +++ b/build/server/Dockerfile @@ -0,0 +1,18 @@ +FROM nimlang/nim:1.0.6-ubuntu AS builder + +COPY nimbot_server /work +RUN cd /work && nimble install -Y + + +FROM nimlang/nim:1.0.6-ubuntu AS runtime + +ENV DEBIAN_FRONTEND=noninteractive +RUN apt-get update -yqq && \ + apt-get install -y tzdata && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* +ENV TZ=Asia/Tokyo + +COPY --from=builder /root/.nimble/bin/nimbot_server /usr/local/bin/nimbot_server +ENTRYPOINT ["/usr/local/bin/nimbot_server"] + diff --git a/docker-compose-ci.yml b/docker-compose-ci.yml deleted file mode 100644 index 06bee30..0000000 --- a/docker-compose-ci.yml +++ /dev/null @@ -1,23 +0,0 @@ -version: '3' - -services: - server: - image: jiro4989/websh:latest - volumes: - - "./websh_server:/work" - working_dir: /work - entrypoint: nimble - command: - - build - - -d:release - - -Y - - front: - image: jiro4989/websh:latest - volumes: - - "./websh_front:/work" - working_dir: /work - entrypoint: nimble - command: - - build - - -Y diff --git a/docker-compose.yml b/docker-compose.yml index 4e24f61..c27a691 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,44 @@ -version: '3' +version: '3.7' services: - proxy: + nginx: image: nginx:1.17 volumes: - - "./proxy/conf.d:/etc/nginx/conf.d:ro" + - "./nginx/conf.d:/etc/nginx/conf.d:ro" - "./websh_front/public:/var/www:ro" ports: - "80:80" network_mode: "host" + websh_front: + build: &app-build + context: . + dockerfile: Dockerfile + target: base + volumes: + - "./websh_front:/work" + entrypoint: nimble + command: + - build + - -Y + - -d:local + + websh_server: + build: *app-build + volumes: + - "./websh_server:/work" + environment: + HOST_PWD: "$PWD/websh_server" + entrypoint: ./entrypoint.sh + ports: + - "5000:5000" + network_mode: "host" + + websh_remover: + build: *app-build + volumes: + - "./websh_remover:/work" + - "./websh_server/tmp:/dirs" + entrypoint: ./entrypoint.sh + environment: + WEBSH_REMOVER_DIR: "/dirs" diff --git a/proxy/conf.d/nginx.conf b/nginx/conf.d/nginx.conf similarity index 97% rename from proxy/conf.d/nginx.conf rename to nginx/conf.d/nginx.conf index 4230f37..056a70e 100644 --- a/proxy/conf.d/nginx.conf +++ b/nginx/conf.d/nginx.conf @@ -27,6 +27,8 @@ server { return 204; } + client_max_body_size 8m; + add_header Access-Control-Allow-Origin '*' always; proxy_set_header X-Forwarded-for $remote_addr; proxy_set_header X-Real-IP $remote_addr; diff --git a/websh_remover/entrypoint.sh b/websh_remover/entrypoint.sh new file mode 100755 index 0000000..7b36d28 --- /dev/null +++ b/websh_remover/entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -eu + +nimble build -Y +./bin/websh_remover diff --git a/websh_server/src/websh_remover.nim b/websh_remover/src/websh_remover.nim similarity index 73% rename from websh_server/src/websh_remover.nim rename to websh_remover/src/websh_remover.nim index d01ed9f..cba3513 100644 --- a/websh_server/src/websh_remover.nim +++ b/websh_remover/src/websh_remover.nim @@ -1,8 +1,10 @@ import os, json, times from strformat import `&` -let - tmpDir = getCurrentDir() / "tmp" +let tmpDir = getEnv("WEBSH_REMOVER_DIR") +if tmpDir == "": + echo %*{"time": $now(), "level": "error", "msg": "'WEBSH_REMOVER_DIR' environment variables was not set", "nimVersion": NimVersion} + quit 1 when isMainModule and not defined modeTest: echo %*{"time": $now(), "level": "info", "msg": "remover begin", "nimVersion": NimVersion} diff --git a/websh_remover/websh_remover.nimble b/websh_remover/websh_remover.nimble new file mode 100644 index 0000000..c602c09 --- /dev/null +++ b/websh_remover/websh_remover.nimble @@ -0,0 +1,14 @@ +# Package + +version = "0.1.0" +author = "jiro4989" +description = "A new awesome nimble package" +license = "MIT" +srcDir = "src" +bin = @["websh_remover"] +binDir = "bin" + + +# Dependencies + +requires "nim >= 1.2.0" diff --git a/websh_server/entrypoint.sh b/websh_server/entrypoint.sh new file mode 100755 index 0000000..dac0735 --- /dev/null +++ b/websh_server/entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -eu + +nimble build -Y +./bin/websh_server diff --git a/websh_server/src/dockerclient.nim b/websh_server/src/dockerclient.nim index 07c5257..8c761ee 100644 --- a/websh_server/src/dockerclient.nim +++ b/websh_server/src/dockerclient.nim @@ -20,7 +20,7 @@ type proc newClient*(): DockerClient = let client = newHttpClient(timeout = 10 * 1000) - url = "http://localhost:2376" + url = "http://127.0.1.1:2376" # ホストのDockerAPI return DockerClient(client: client, url: url) proc createContainer*(self: DockerClient, name: string, image: string, cmds: seq[string], script = "", mediaDir = "", imageDir = ""): Response = diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index 911a36b..1487fbb 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -22,7 +22,6 @@ const proc getTmpDir(): string = getCurrentDir() / "tmp" - proc getImages(dir: string): seq[ImageObj] = ## 画像ディレクトリから画像ファイルを取得。 ## 取得の際はBase64エンコードした文字列として取得する。 @@ -64,6 +63,10 @@ router myrouter: imageDir = contDir / "images" mediaDir = contDir / "media" removeFlag = contDir / "removes" + hostContDir = getEnv("HOST_PWD") / "tmp" / uuid + hostScriptDir = hostContDir / "script" + hostImageDir = hostContDir / "images" + hostMediaDir = hostContDir / "media" createDir(imageDir) @@ -71,6 +74,7 @@ router myrouter: createDir(scriptDir) let shellScriptPath = scriptDir/scriptName writeFile(shellScriptPath, respJson.code) + let hostShellScriptPath = hostScriptDir/scriptName # Mediaの配置 createMediaFiles(mediaDir, respJson.images) @@ -88,9 +92,9 @@ router myrouter: name = uuid, image = image, cmds = cmds, - script = shellScriptPath, - mediaDir = mediaDir, - imageDir = imageDir) + script = hostShellScriptPath, + mediaDir = hostMediaDir, + imageDir = hostImageDir) # TODO: ここ邪魔だなぁ case status diff --git a/websh_server/tmp/.gitkeep b/websh_server/tmp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/websh_server/tools/visor.nim b/websh_server/tools/visor.nim deleted file mode 100644 index 38f6abb..0000000 --- a/websh_server/tools/visor.nim +++ /dev/null @@ -1,8 +0,0 @@ -import os, osproc, strutils, sequtils - -while true: - for i in 1..4: - let s = execProcess("docker-compose", args = ["ps"], options = {poUsePath}) - for name in s.split("\n").filterIt("Exit" in it).mapIt(it.split(" ")[0]): - discard execShellCmd("docker-compose up -d " & name) - sleep 500 diff --git a/websh_server/websh_server.nimble b/websh_server/websh_server.nimble index da1bcad..0043e5e 100644 --- a/websh_server/websh_server.nimble +++ b/websh_server/websh_server.nimble @@ -5,7 +5,7 @@ author = "jiro4989" description = "A new awesome nimble package" license = "MIT" srcDir = "src" -bin = @["websh_server", "websh_remover"] +bin = @["websh_server"] binDir = "bin"