【问题标题】:Error when creating a pool from a custom image with azure python sdk使用 azure python sdk 从自定义图像创建池时出错
【发布时间】:2018-12-06 11:04:50
【问题描述】:

我正在尝试使用我从带有 azure python sdk 的 VM 创建的自定义映像创建一个池。位置和资源组匹配。

这是我的代码:

import azure.batch as batch
from azure.batch import BatchServiceClient
from azure.batch.batch_auth import SharedKeyCredentials
from azure.batch import models



account = 'mybatch'
key = 'Adgfdj1hhsdfqATc/K2fgxdfg/asYgKRP2pUdfglBce7mgmSBdfgdhC7f3Zdfgrcgkdgh/dfglA=='
batch_url = 'https://mybatch.westeurope.batch.azure.com'

creds = SharedKeyCredentials(account, key)
batch_client = BatchServiceClient(creds, base_url = batch_url)

pool_id_base = 'mypool'
idx = 1

pool_id = pool_id_base + str( idx )

while batch_client.pool.exists( pool_id ):
  idx += 1
  pool_id = pool_id_base + str( idx )

print( 'pool_id ' + pool_id )

sku_to_use =  'batch.node.ubuntu 18.04'

# 
# image_ref_to_use = models.ImageReference(
#     offer = 'UbuntuServer', 
#     publisher = 'Canonical',
#     sku = '18.04-LTS', 
#     version = 'latest'
#   )



image_ref_to_use = models.ImageReference(
    virtual_machine_image_id = '/subscriptions/1834572sd-34sd409a-sdfb-sc345csdfesourceGroups/resource-group-1/providers/Microsoft.Compute/images/my-image-1'
  )


vm_size = 'Standard_D3_v2' 

vmc = models.VirtualMachineConfiguration(
  image_reference = image_ref_to_use,
  node_agent_sku_id = sku_to_use
)

new_pool = models.PoolAddParameter(
  id = pool_id, 
  vm_size = vm_size, 
  virtual_machine_configuration = vmc,
  target_dedicated_nodes = 1
)

batch_client.pool.add(new_pool)

According to the docs 我应该可以使用 virtual_machine_image_id 其他提供的市场图像参数。 我可以创建一个标准市场图像池,但在尝试使用自定义图像的 ID 时出现错误。

Traceback (most recent call last):   File "create_pool.py", line 60, in <module>
    batch_client.pool.add(new_pool)   File "/root/miniconda/lib/python3.6/site-packages/azure/batch/operations/pool_operations.py", line 312, in add
    raise models.BatchErrorException(self._deserialize, response) azure.batch.models.batch_error.BatchErrorException: {'additional_properties': {}, 'lang': 'en-US', 'value': 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:0dfdf9c1-edad-4b72-8e8f-f8dbcfd0abbdf\nTime:2018-12-06T10:51:21.9417222Z'}

我该如何解决这个问题?


更新

我尝试将 ServicePrincipalCredentials 与以下内容一起使用:

CLIENT_ID:我在默认目录中创建了一个新应用程序 -> 添加注册并获得它的应用程序 ID。

SECRET:A 为新应用程序创建了一个密钥并使用了它的值。

TENANT_IDaz account show 在云外壳中。

资源:使用“https://batch.core.windows.net/”。

像这样更新了我的代码:

from azure.common.credentials import ServicePrincipalCredentials

creds = ServicePrincipalCredentials(
  client_id=CLIENT_ID,
  secret=SECRET,
  tenant=TENANT_ID,
  resource=RESOURCE
)

我得到另一个错误:

Keyring cache token has failed: No recommended backend was available. Install the keyrings.alt package if you want to use the non-recommended backends. See README.rst for details.
Traceback (most recent call last):
  File "create_pool.py", line 41, in <module>
    while batch_client.pool.exists( pool_id ):
  File "/root/miniconda/lib/python3.6/site-packages/azure/batch/operations/pool_operations.py", line 624, in exists
    raise models.BatchErrorException(self._deserialize, response)
azure.batch.models.batch_error.BatchErrorException: Operation returned an invalid status code 'Server failed to authorize the request.'

【问题讨论】:

标签: azure azure-batch azure-sdk azure-sdk-python


【解决方案1】:

尝试使用服务主体凭据而不是共享密钥凭据

credentials = ServicePrincipalCredentials(
    client_id=CLIENT_ID,
    secret=SECRET,
    tenant=TENANT_ID,
    resource=RESOURCE
)

共享密钥凭据似乎存在错误。

文档链接:https://docs.microsoft.com/en-us/azure/batch/batch-aad-auth

问题链接:https://github.com/Azure/azure-sdk-for-python/issues/1668

注意:请删除您的帐户详细信息,因为任何人都可以访问它。将帐户名称和密钥替换为 ****。

更新 如果服务主体凭据不起作用,请尝试使用用户凭据并查看是否有效。

from azure.common.credentials import UserPassCredentials
import azure.batch.batch_service_client as batch

credentials = UserPassCredentials(
    azure_user,
    azure_pass
)
batch_client = batch.BatchServiceClient(credentials, base_url = batch_url)

【讨论】:

  • 它们不是真实的帐户详细信息,不用担心 ;-) 我仍然有一个错误,更新了我的问题
  • 好的,我刚刚尝试了您的编辑建议,但出现了错误。我正在使用跟踪订阅。我们相信,这是某种试用版限制。在我们转向付费订阅后,我会回来讨论它。另一件事,在这里使用用户/密码验证似乎很奇怪,我会尽量避免将我的帐户凭据存储在代码中的任何位置。
  • 实际上用户凭证通常不应该使用。但是,由于其他方法不适用于您的情况,我想尝试一下。但是,如果您拥有必要的特权,那么这应该可行。如果这可行,则意味着您创建的 Principal 凭据存在问题。但由于它不起作用,这意味着确实存在一些帐户限制。
  • 我正在使用我刚刚注册的个人帐户测试所有内容(可以访问所有内容,以避免可能的权限问题)。所以我想,我在这里确实拥有所有可能的权限。我刚刚获得了公司帐户的访问权限,我正在那里搬东西。也许我会回到这个问题,但目前,我们认为这是一个线索订阅限制。无论如何,感谢您的帮助。 :)
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 2016-08-22
  • 2016-01-21
  • 1970-01-01
  • 2016-10-31
  • 2017-05-07
  • 1970-01-01
相关资源
最近更新 更多