简体   繁体   English

python subprocess.check_output() ubuntu容器中json参数解析

[英]python subprocess.check_output() json parameter parsing in ubuntu container

Trying to execute code with subprocess.check_output() inside an ubuntu 20.04 container:尝试在 ubuntu 20.04 容器中使用 subprocess.check_output() 执行代码:

try:
    out = subprocess.check_output(exe)
except subprocess.CalledProcessError as e:
    log.error(f"unable to execute:{' '.join(exe)} -> {e} -> {out}")
    return None
0211022170027|INFO|command used: ./bin/elkq.py --env=test --vertical=trk --index='bps-trace-*'
 --testenv=test12 --fields=@timestamp,message,bps.application --aggs=bps.application --start=now-4h
 --end=now --json --fany='("error")' --fmust='[{"fields.buypass.com/name": "id-pro-_VERT_-*"},
 {"bps.application": "*-_TESTENV_"}]'
20211022170028|ERROR|unable to execute:./bin/elkq.py --env=test --vertical=trk --index='bps-trace-*'
 --testenv=test12 --fields=@timestamp,message,bps.application --aggs=bps.application --start=now-4h
 --end=now --json --fany='("error")' --fmust='[{"fields.buypass.com/name": "id-pro-_VERT_-*"},
 {"bps.application": "*-_TESTENV_"}]' ->
 Command '['./bin/elkq.py', '--env=test', '--vertical=trk', "--index='bps-trace-*'",
 '--testenv=test12', '--fields=@timestamp,message,bps.application', '--aggs=bps.application',
 '--start=now-4h', '--end=now', '--json', '--fany=\'("error")\'',
 '--fmust=\'[{"fields.buypass.com/name": "id-pro-_VERT_-*"}, {"bps.application": "*-_TESTENV_"}]\'']'
 returned non-zero exit status 255. -> None

But if I cut and paste the command directly from the command use line above in the same container it works fine, so I assume the error is within the subprocess environment:但是如果我直接从上面的命令 use 行剪切并粘贴命令在同一个容器中它工作正常,所以我假设错误在子进程环境中:

(base) root@deabf77921e8:/ # ./bin/elkq.py --env=test --vertical=trk --index='bps-trace-*' \
 --testenv=test12 --fields=@timestamp,message,bps.application --aggs=bps.application --start=now-4h \
 --end=now --json --fany='("error")' \
 --fmust='[{"fields.buypass.com/name": "id-pro-_VERT_-*"}, {"bps.application": "*-_TESTENV_"}]'
{
  "aggregations": {
    "bps.application": {
      "buckets": [
        {
          "doc_count": 15,
          "key": "id-pro-trk-trk-scim-api-test12"
        },
        {
          "doc_count": 1,
          "key": "id-pro-trk-trk-freg-gateway-test12"
        },
        {
          "doc_count": 1,
          "key": "id-pro-trk-trk-id-proofing-web-test12"
        }
      ],
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0
    }
  },
  "count": "max count found:17 rows in 0.05 sec"
}
(base) root@deabf77921e8:/ # echo $?
0

I know it has something to do with the --fmust parameter, because if I leave it out it works, and I earlier solved it by using a default parameter via argparse:我知道它与 --fmust 参数有关,因为如果我忽略它,它会起作用,而且我之前通过 argparse 使用默认参数解决了它:

r'[{"fields.buypass.com/name": "id-pro-_VERT_-*"}, {"bps.application": "*-_TESTENV_"}]'

In powershell it works simply by double-quoting the " to "" but the CI runs linux containers.在 powershell 中,它只需双引号"""但 CI 运行 linux 容器。

Any help on debugging this very appreciated, thanks非常感谢有关调试的任何帮助,谢谢

I changed the subprocess.check_output() to use shell=True on linux, might be a dirty workaround but now it works in both powershell and bash with json parameters:我将 subprocess.check_output() 更改为在 linux 上使用shell=True ,这可能是一个肮脏的解决方法,但现在它可以在带有 json 参数的 powershell 和 bash 中使用:

shell = True
if sys.platform != 'win32':
   exe = ' '.join(exe)
   shell = False
try:
    out = subprocess.check_output(exe, shell=shell)
except subprocess.CalledProcessError as e:
    log.error(f"unable to execute:{exe} -> {e} -> {out}")
    return None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM