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

查看:27
本文介绍了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 服务器等,问题只是 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 Function 以通过不同方式调用 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 命令选项将函数发布为 docker 映像 func azure functionapp publish <your function app name> 将需要的包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 容器自动构建和配置依赖项. 更多信息请访问 https://aka.ms/func-python-publish

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 <your function app name>--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 命令只是一个可运行的脚本文件,而不是二进制执行文件.在我查看了 azazure-cli 包的一些源代码后,我认为您可以直接通过 from azure.cli.core import get_default_cli 并使用它来执行与下面的代码相同的操作.

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天全站免登陆