Azure函数-触发包含Azure CLI命令的Python脚本 [英] Azure Function - trigger Python script containing Azure CLI commands

查看:120
本文介绍了Azure函数-触发包含Azure CLI命令的Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于在Azure-IaC中配置基础结构的python脚本.该脚本主要使用Python SDK,但也运行多个Azure CLI命令-有时在Python SDK中找不到等效命令时需要使用该脚本.

I have a python script for provisioning infrastructure in Azure - IaC. This script is using mostly the Python SDK, but is also running multiple Azure CLI commands - it is needed at times when I didn't find an equivalent command in the Python SDK.

我的目标是使用Azure Functions按需触发此脚本.在本地测试Azure功能时,一切正常,因为我在计算机上安装了Azure CLI,但是,当我将其发布到Azure功能时,我将遇到以下错误:/bin/sh:1:az:找不到

My goal is to trigger this script on-demand using Azure Functions. When testing the Azure Function locally, everything works fine as I have Azure CLI installed on my machine, however, when I publish it to Azure functions, I will run into an error that: /bin/sh: 1: az: not found

下面是我在Azure函数中触发的示例python函数(请注意,其余脚本可以正常工作,因此我可以创建RG,SQL Server等,问题只是 az 命令).我想知道,是否以及如何在Azure Function上安装Azure CLI以便能够运行CLI命令?

Below is the sample python function I trigger in the Azure Function (Please note that the rest of script works fine, so I can create RG, SQL server etc, the problem is just the az commands). I wonder, if and how could I install Azure CLI on the Azure Function in order to be able to run the CLI commands?

这是导致错误的python函数:

Here is the python function causing the error:

    # Loging to AZ
    call("az login --service-principal -u '%s' -p '%s' --tenant '%s'" % (client_id, client_secret, tenant_id), shell=True)
    b2c_id = check_output("az resource show -g '<rg_name>' -n '<b2c_name>' --resource-type 'Microsoft.AzureActiveDirectory/b2cDirectories' --query id --output tsv", shell=True)
    print("The B2C ID is: %s" % b2c_id)```

推荐答案

我尝试使用HttpTrigger创建一个简单的Azure函数,以通过不同方式调用Azure CLI工具,但是在发布后再也无法在云上运行.

I tried to create a simple Azure Function with HttpTrigger to invoke Azure CLI tools by different ways, but never works on cloud after published.

似乎唯一的解决方案是使用-build-native-deps 选项的命令 func azure functionapp publish<您的功能应用程序名称> ,然后将所需的软件包 azure-cli 添加到 requirements.txt 文件中,如下图所示,带有错误,

It seems the only solution is to publish the function as docker image with --build-native-deps option for the command func azure functionapp publish <your function app name> after add the required package azure-cli into the requirements.txt file, as the figure with error below said,

还原依赖关系时出错.错误:无法安装antlr4-python3-runtime-4.7.2依赖关系:不支持不带轮子的二进制依赖关系.使用--build-native-deps选项可使用Docker容器自动构建和配置依赖项.有关更多信息,请参见

There was an error restoring dependencies.ERROR: cannot install antlr4-python3-runtime-4.7.2 dependency: binary dependencies without wheels are not supported. Use the --build-native-deps option to automatically build and configure the dependencies using a Docker container. More information at https://aka.ms/func-python-publish

由于我的本地没有docker工具,所以我没有成功运行 func azure functionapp publish<您的功能应用程序名称>--build-native-deps .

Due to there is not docker tools in my local, I did not successfully run func azure functionapp publish <your function app name> --build-native-deps.

同时,运行Azure CLI命令不是使用Azure CLI功能的唯一方法. az 命令只是一个可运行的脚本文件,而不是二进制执行文件.在查看了 az azure-cli 软件包的一些源代码之后,我认为您可以直接通过从azure.cli.core import get_default_cli 导入软件包.code>并使用它执行与下面的代码相同的操作.

Meanwhile, running Azure CLI commands is not the only one ways to use the functions of Azure CLI. The az command is just a runnable script file, not a binary execute file. After I reviewd az and some source codes of azure-cli package, I think you can directly import the package via from azure.cli.core import get_default_cli and to use it to do the same operations like the code below.

from azure.cli.core import get_default_cli

az_cli = get_default_cli()
exit_code = az_cli.invoke(args)
sys.exit(exit_code)

该代码是通过参考 azure-cli 包的 azure/cli/__ main __.py 的源代码编写的,您可以从虚拟环境的lib/python3.x/site-packages 路径.

The code is written by referring to the source code of azure/cli/__main__.py of azure-cli package, you can see it from the lib/python3.x/site-packages path of your virtual environment.

希望有帮助.

这篇关于Azure函数-触发包含Azure CLI命令的Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆