【问题标题】:az cli run-command invoke with multiple command line parameters fails使用多个命令行参数的 az cli run-command 调用失败
【发布时间】:2018-12-03 06:52:54
【问题描述】:

我正在尝试使用命令行参数执行远程 python 脚本。 python 脚本是使用 argparse 开发的。正如我们所知,argparse 以-/-- 作为参数名称的输入。在 az cli 的 --parameters 中传递它会引发奇怪的错误。

问题 如何使用az run-command invoke为python脚本传递类似python argparse的参数


一些样本尝试和输出

试试 - 1

az vm run-command invoke     --resource-group MDC_VME_DEV_EUS  \
   --name ubuntumemsql002     --command-id RunShellScript  \
   --scripts /mnt/workspace/scripts/test.py   \
  --parameters "-i /mnt/workspace/data/indata" "-o /mnt/workspace/data/outdata" "--input_compressed_data" "--output_compressed_data" "-a 600" "-p 0.1" "-b 600 1200 1800"  "-s 1543275000"

az vm run-command invoke: ' /mnt/workspace/data/outdata' is not a valid value for '--output'. See 'az vm run-command invoke --help'.

试试 - 2

 bash-4.4# az vm run-command invoke    \
 --resource-group MDC_VME_DEV_EUS \
    --name ubuntumemsql002     \ 
--command-id RunShellScript    \
 --scripts /mnt/workspace/scripts/test.py  \
   --parameters "-i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000"

Deployment failed. Correlation ID: 36317037-4cfb-429e-b113-f8995d3419fb. VM has reported a failure when processing extension 'RunCommandLinux'. 
Error message: "Enable failed: failed to execute command: command terminated with exit status=2
    [stdout]

    [stderr]
    /var/lib/waagent/run-command/download/7/script.sh: 1: export: /mnt/workspace/data/indata: bad variable name

".

试试 - 3

bash-4.4# az vm run-command invoke    \
--resource-group MDC_VME_DEV_EUS \
--name ubuntumemsql002  \
--command-id RunShellScript \
--scripts /mnt/workspace/scripts/test.py \
--parameters "-i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000" --verbose
    Deployment failed. Correlation ID: 9efaf01c-3d7b-473a-90c8-802d63b6ccf0. VM has reported a failure when processing extension 'RunCommandLinux'. 
Error message: "Enable failed: failed to execute command: command terminated with exit status=2
    [stdout]

    [stderr]
    /var/lib/waagent/run-command/download/8/script.sh: 1: export: /mnt/workspace/data/indata: bad variable name
    ".

试试 - 4

bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/test.py \
>     --parameters -i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000
az vm run-command invoke: error: argument --parameters: expected at least one argument
usage: az vm run-command invoke [-h] [--verbose] [--debug]
                                [--output {json,jsonc,table,tsv,yaml}]
                                [--query JMESPATH]
                                [--resource-group RESOURCE_GROUP_NAME]
                                [--name NAME] --command-id COMMAND_ID
                                [--scripts SCRIPTS [SCRIPTS ...]]
                                [--parameters PARAMETERS [PARAMETERS ...]]
                                [--subscription _SUBSCRIPTION]
                                [--ids ID [ID ...]]

试试 - 5

 bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/test.py \
>     --parameters \-i /mnt/workspace/data/indata \-o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data \-a 600 \-p 0.1 \-b 600 1200 1800  \-s 1543275000
az vm run-command invoke: error: argument --parameters: expected at least one argument
usage: az vm run-command invoke [-h] [--verbose] [--debug]
                                [--output {json,jsonc,table,tsv,yaml}]
                                [--query JMESPATH]
                                [--resource-group RESOURCE_GROUP_NAME]
                                [--name NAME] --command-id COMMAND_ID
                                [--scripts SCRIPTS [SCRIPTS ...]]
                                [--parameters PARAMETERS [PARAMETERS ...]]
                                [--subscription _SUBSCRIPTION]
                                [--ids ID [ID ...]]
bash-4.4# 

直接 VM CLI 尝试

而且,为了确认,当我在 CLI 中运行相同的命令时,它们可以完美执行

/mnt/workspace/scripts/test.py -i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X13.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X13-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X11.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X11-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X12.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X12-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X15.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X15-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X14.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X14-data-1543275000.gz'result='Success' )

一些例子(在 cmets 中建议)

运行 - 1

bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/test.py \
>     --parameters "\-i" "/mnt/workspace/data/indata" "\-o" "/mnt/workspace/data/outdata" "\--input_compressed_data" "\--output_compressed_data" "\-a" "600" "\-p" "0.1" "\-b" "600 1200 1800"  "\-s" "1543275000"
Deployment failed. Correlation ID: 70f7646e-fa94-4104-8067-e42b92470803. VM has reported a failure when processing extension 'RunCommandLinux'. Error message: "Enable failed: failed to execute command: command terminated with exit status=2
[stdout]

[stderr]
/var/lib/waagent/run-command/download/9/script.sh: 1: export: 1200: bad variable name

运行 - 2

bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/prepare_data_for_memsql_ingest.py \
>     --parameters "\-i" "/mnt/workspace/data/indata" "\-o" "/mnt/workspace/data/outdata" "\--input_compressed_data" "\--output_compressed_data" "\-a" "600" "\-p" "0.1" "\-b" "600" "1200" "1800"  "\-s" "1543275000"
Deployment failed. Correlation ID: e460bfa4-286e-43c3-8a60-6f74b78c7e85. VM has reported a failure when processing extension 'RunCommandLinux'. Error message: "Enable failed: failed to execute command: command terminated with exit status=2
[stdout]

[stderr]
usage: PROG [-h] -i INPUT_DIR -o OUT_DIR
            (--input_compressed_data | --input_uncompressed_data)
            (--output_compressed_data | --output_uncompressed_data) -a
            AGGREGATE_INTERVAL -p {<__main__.Range object at 0x7fd86f8c49d0>}
            -b BACKDATE_INTERVAL_LIST [BACKDATE_INTERVAL_LIST ...] -s
            START_EPOCH
PROG: error: argument -i/--input_dir is required
".

【问题讨论】:

  • 试试--parameters "\-i" "/mnt/workspace/data/indata" "\-o" "/mnt/workspace/data/outdata" ...
  • @Sraw :它不起作用。同样的问题!!!
  • 和哪一个一样?
  • @Sraw :我在主要问题中添加了建议的尝试输出。 HTH!
  • 如果答案有帮助,您可以接受。或者需要更多帮助请给我留言。

标签: python azure argparse azure-cli


【解决方案1】:

@kumar_m_kiran - 请尝试一下,如果可行,请告诉我:

az vm run-command invoke     --resource-group MDC_VME_DEV_EUS  \
   --name ubuntumemsql002     --command-id RunShellScript  \
   --scripts "python /mnt/workspace/scripts/test.py -i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800 -s 1543275000"

您可能需要在开始时使用“python”关键字运行脚本。您还可以在脚本参数本身中提供脚本的所有参数。

【讨论】:

    【解决方案2】:

    通过测试和文档,CLI 命令只需在 Linux VM 中使用 --parameters 的参数运行 shell 脚本。您可以从Run Shell ScriptAvailable Commands 获取详细信息。

    Run Command 使用 VM 代理在 Azure 中运行 shell 脚本 Linux 虚拟机。

    然后用--debug 测试,结果在这里:

    Shell 脚本 Python 脚本

    过程相同,但 shell 脚本有效,python 脚本无效。因此,您似乎可以将 python 脚本作为 shell 脚本运行,参数如下:

    az vm run-command invoke     --resource-group groupName  \
       --name vmName             --command-id RunShellScript  \
       --scripts "python /path/test.py arg1 arg2 arg3"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多