From 2073b7277d9e6d1a5ea6deebda54fc82dedd23f1 Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 02:53:43 +0900 Subject: [PATCH 1/7] =?UTF-8?q?:bug:=20Docker=E3=82=B3=E3=83=B3=E3=83=86?= =?UTF-8?q?=E3=83=8A=E3=81=8C=E6=B6=88=E3=81=95=E3=82=8C=E3=81=9A=E3=81=AB?= =?UTF-8?q?=E6=AE=8B=E3=82=8A=E7=B6=9A=E3=81=91=E3=82=8B=E3=81=AE=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20#116?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- websh_server/src/websh_server.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index 3c637fd..66a1a3b 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -93,10 +93,14 @@ router myrouter: let img = "images" let imageDir = getCurrentDir() / img / uuid defer: + info &"removes {shellScriptPath} script ..." removeFile(shellScriptPath) - info &"{shellScriptPath} was removed" + + info &"removes {imageDir} directory ..." removeDir(imageDir) - info &"{imageDir} was removed" + + info &"kills {uuid} docker container ..." + discard execCmd(&"docker kill {uuid}") # コマンドを実行するDockerイメージ名 createDir(imageDir) From 7ad5160847e2507e41837f27ec815c88f82a70cd Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 02:56:53 +0900 Subject: [PATCH 2/7] =?UTF-8?q?:fire:=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- websh_server/src/websh_server.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index 66a1a3b..2a05daa 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -75,9 +75,6 @@ proc runCommand(command: string, args: openArray[string], timeout: int = 3): (st router myrouter: post "/shellgei": try: - # TODO: - # uuidを使ってるけれど、どうせならシェル芸botと同じアルゴリズムでファ - # イルを生成したい var respJson = request.body().parseJson().to(ReqShellgeiJSON) info respJson # シェバンを付けないとshとして評価されるため一部の機能がつかえない模様(プロ From 30328a30839c2883f1583da7596c1db55915b978 Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 02:57:19 +0900 Subject: [PATCH 3/7] =?UTF-8?q?:recycle:=20=E4=BD=BF=E3=81=A3=E3=81=A6?= =?UTF-8?q?=E3=81=AA=E3=81=84=E5=A4=89=E6=95=B0=E3=82=92=E7=84=A1=E5=8A=B9?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- websh_server/src/websh_server.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index 2a05daa..e3ccfac 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -128,7 +128,7 @@ router myrouter: for path in walkFiles(imageDir / "*"): if not path.existsFile: continue - let (dir, name, ext) = splitFile(path) + let (_, _, ext) = splitFile(path) if ext.toLowerAscii notin [".png", ".jpg", ".jpeg", ".gif"]: continue let content = readFile(path) From 1da32f92924c4ceb11addf3ab1e6a98aaaeb4f32 Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 03:08:13 +0900 Subject: [PATCH 4/7] =?UTF-8?q?:recycle:=20=E3=83=AD=E3=82=B0=E3=81=AE?= =?UTF-8?q?=E6=9B=B8=E5=BC=8F=E3=82=92=E5=A4=89=E6=9B=B4=20#114?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- websh_server/src/websh_server.nim | 43 ++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index e3ccfac..b8665c9 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -17,13 +17,21 @@ const proc logging(level: string, msgs: varargs[string, `$`]) = ## **Note:** マルチスレッドだとloggingモジュールがうまく機能しないので仮で実装 - var s: string - for msg in msgs: - s.add(msg) - let now = now() - let dt = now.format("yyyy-MM-dd") - let ti = now.format("HH:mm:ss") - echo &"{dt}T{ti}+0900 {level} {s}" + var kvs: seq[string] + for i in 0..<(msgs.len div 2): + let i = i * 2 + let k = msgs[i] + var v = msgs[i+1] + if " " in v: + v = &"'{v}'" + kvs.add(&"{k}={v}") + + let + now = now() + dt = now.format("yyyy-MM-dd") + ti = now.format("HH:mm:ss") + msg = kvs.join(" ") + echo &"{dt}T{ti}+0900 {level} {msg}" proc info(msgs: varargs[string, `$`]) = logging "INFO", msgs @@ -56,11 +64,9 @@ proc runCommand(command: string, args: openArray[string], timeout: int = 3): (st elif exitCode == 137: status = statusTimeout msg = &"timeout: {timeout} second" - info &"exitCode={exitCode}, msg={msg}" else: status = statusSystemError msg = &"failed to run command: command={command}, args={args}" - error &"exitCode={exitCode}, msg={msg}" # 出力の取得 block: @@ -75,14 +81,14 @@ proc runCommand(command: string, args: openArray[string], timeout: int = 3): (st router myrouter: post "/shellgei": try: + let uuid = $genUUID() var respJson = request.body().parseJson().to(ReqShellgeiJSON) - info respJson + info "uuid", uuid, "json", respJson # シェバンを付けないとshとして評価されるため一部の機能がつかえない模様(プロ # セス置換とか) (#7) if not respJson.code.startsWith("#!"): # シェバンがついてないときだけデフォルトbash respJson.code = "#!/bin/bash\n" & respJson.code - let uuid = $genUUID() let scriptName = &"{uuid}.sh" let shellScriptPath = getTempDir() / scriptName writeFile(shellScriptPath, respJson.code) @@ -90,13 +96,13 @@ router myrouter: let img = "images" let imageDir = getCurrentDir() / img / uuid defer: - info &"removes {shellScriptPath} script ..." + info "uuid", uuid, "msg", &"removes {shellScriptPath} script ..." removeFile(shellScriptPath) - info &"removes {imageDir} directory ..." + info "uuid", uuid, "msg", &"removes {imageDir} directory ..." removeDir(imageDir) - info &"kills {uuid} docker container ..." + info "uuid", uuid, "msg", &"kills {uuid} docker container ..." discard execCmd(&"docker kill {uuid}") # コマンドを実行するDockerイメージ名 @@ -123,6 +129,13 @@ router myrouter: let timeout = getEnv("WEBSH_REQUEST_TIMEOUT", "3").parseInt let (stdoutStr, stderrStr, status, systemMsg) = runCommand("docker", args, timeout) + case status + of statusOk: discard + of statusTimeout: + info "uuid", uuid, "msg", systemMsg + else: + error "uuid", uuid, "msg", systemMsg + # 画像ファイルをbase64に変換 var images: seq[ImageObj] for path in walkFiles(imageDir / "*"): @@ -138,7 +151,7 @@ router myrouter: resp %*{"status":status, "system_message":systemMsg, "stdout":stdoutStr, "stderr":stderrStr, "images":images} except: let msg = getCurrentExceptionMsg() - error msg + error "msg", msg resp %*{"status":statusSystemError, "system_message":"System error occured.", "stdout":"", "stderr":"", "images":[]} get "/ping": resp %*{"status":"ok"} From 5675875ecdf3072c0c16c4e8e76d12bb073a6b0e Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 18:44:36 +0900 Subject: [PATCH 5/7] =?UTF-8?q?:bug:=20=E3=83=9E=E3=82=A6=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=81=97=E3=81=A6=E3=81=84=E3=82=8B=E3=82=B9=E3=82=AF?= =?UTF-8?q?=E3=83=AA=E3=83=97=E3=83=88=E3=82=92=E8=AA=AD=E3=81=BF=E8=BE=BC?= =?UTF-8?q?=E3=81=BF=E5=B0=82=E7=94=A8=E3=81=AB=E5=A4=89=E6=9B=B4=20#118?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- websh_server/src/websh_server.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index b8665c9..02809de 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -107,7 +107,7 @@ router myrouter: # コマンドを実行するDockerイメージ名 createDir(imageDir) - let containerShellScriptPath = &"/tmp/{scriptName}" + let vScript = &"/tmp/{scriptName}" let imageName = getEnv("WEBSH_DOCKER_IMAGE", "theoldmoon0602/shellgeibot") let args = [ "run", @@ -120,11 +120,11 @@ router myrouter: "--log-driver=json-file", "--log-opt", "max-size=100m", "--log-opt", "max-file=3", - "-v", &"{shellScriptPath}:{containerShellScriptPath}", + "-v", &"{shellScriptPath}:{vScript}:ro", "-v", &"{imageDir}:/{img}", # "-v", "./media:/media:ro", imageName, - "bash", "-c", &"chmod +x {containerShellScriptPath} && sync && {containerShellScriptPath} | stdbuf -o0 head -c 100K", + "bash", "-c", &"sync && cp {vScript} {vScript}.1 && chmod +x {vScript}.1 && {vScript}.1 | stdbuf -o0 head -c 100K", ] let timeout = getEnv("WEBSH_REQUEST_TIMEOUT", "3").parseInt let (stdoutStr, stderrStr, status, systemMsg) = runCommand("docker", args, timeout) From 41336c36fc43e1300b03c0a1b75d9d81ad88304e Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 23:47:12 +0900 Subject: [PATCH 6/7] =?UTF-8?q?:bug:=20=E7=94=BB=E5=83=8F=E3=83=87?= =?UTF-8?q?=E3=82=A3=E3=83=AC=E3=82=AF=E3=83=88=E3=83=AA=E3=81=AB=E6=A8=A9?= =?UTF-8?q?=E9=99=90=E3=82=92=E6=93=8D=E4=BD=9C=E3=81=95=E3=82=8C=E3=81=9F?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=8C=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=81=A8=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=83=AA=E3=81=8C=E5=89=8A=E9=99=A4=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=9A=E3=81=AB=E6=AE=8B=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3=20#113?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- websh_server/src/websh_server.nim | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/websh_server/src/websh_server.nim b/websh_server/src/websh_server.nim index 02809de..92d929b 100644 --- a/websh_server/src/websh_server.nim +++ b/websh_server/src/websh_server.nim @@ -94,6 +94,7 @@ router myrouter: writeFile(shellScriptPath, respJson.code) let img = "images" + let imageVolume = &"{img}_{uuid}" let imageDir = getCurrentDir() / img / uuid defer: info "uuid", uuid, "msg", &"removes {shellScriptPath} script ..." @@ -105,8 +106,10 @@ router myrouter: info "uuid", uuid, "msg", &"kills {uuid} docker container ..." discard execCmd(&"docker kill {uuid}") + info "uuid", uuid, "msg", &"Remove {imageVolume} docker volume ..." + discard execCmd(&"docker volume rm -f {imageVolume}") + # コマンドを実行するDockerイメージ名 - createDir(imageDir) let vScript = &"/tmp/{scriptName}" let imageName = getEnv("WEBSH_DOCKER_IMAGE", "theoldmoon0602/shellgeibot") let args = [ @@ -121,7 +124,7 @@ router myrouter: "--log-opt", "max-size=100m", "--log-opt", "max-file=3", "-v", &"{shellScriptPath}:{vScript}:ro", - "-v", &"{imageDir}:/{img}", + "-v", &"{imageVolume}:/{img}", # "-v", "./media:/media:ro", imageName, "bash", "-c", &"sync && cp {vScript} {vScript}.1 && chmod +x {vScript}.1 && {vScript}.1 | stdbuf -o0 head -c 100K", @@ -136,6 +139,20 @@ router myrouter: else: error "uuid", uuid, "msg", systemMsg + # 画像ディレクトリにファイルだけ移動 + # 移動前に権限を操作しておく + createDir(imageDir) + let s = execProcess("docker", args=[ + "run", + "--rm", + "-v", &"{imageVolume}:/src", + "-v", &"{imageDir}:/dst", + "bash", + "-c", + """chmod -R 0777 /src/ && ls -1d /src/* | while read -r f; do [[ -f "$f" ]] && mv "$f" /dst/; done """, + ], options={poUsePath}) + info "uuid", uuid, "msg", s + # 画像ファイルをbase64に変換 var images: seq[ImageObj] for path in walkFiles(imageDir / "*"): From b24581ed697fc7f40b7a53bb3231e8ed29ac38fa Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Fri, 31 Jan 2020 23:53:23 +0900 Subject: [PATCH 7/7] =?UTF-8?q?:fire:=20nimlint=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/lint.yml | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index bc484c5..0000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: lint - -on: [pull_request] - -jobs: - nimlint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - name: Nim lint - uses: jiro4989/nimlint-action@v1.0.0 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - reporter: github-pr-review - src: 'websh_*/src/*.nim'