【问题标题】:How to add a new dynamically discoverable capability to an agent?如何向代理添加新的动态可发现功能?
【发布时间】:2019-10-16 03:24:10
【问题描述】:

更新

我将在这里提出一个功能提供者作为我帖子的更新。 如果您需要更多详细信息,请告诉我。

我们目前在代理源代码中有一堆交付的 Capabilities Providers:

https://github.com/microsoft/azure-pipelines-agent/tree/master/src/Microsoft.VisualStudio.Services.Agent/Capabilities

  • 代理
  • 环境
  • 尼克斯
  • PowerShell

提议的是另外一个名为ExecutableCapabilitiesProvider的提供者。

这个新的 ExecutableCapabilitiesProvider 可能会有一个可以在代理机器上编辑的配置文件。 这个文件的格式可能是:

#name,executable
pip,pip3 freeze
xyz,/usr/bin/xyz-runner
abc,sh -C "ls -l /blah/blah"

作为自托管池的维护者,我会使用适合我的条目配置此文件,并让代理在启动时运行它。这样我就不会为我的能力硬编码任何值,而是在启动时确定这些值。

我会更进一步,添加一个新的 API 调用来添加比当前要求名称/值更灵活的功能。例如,将参数更改为Name, Provider, Params

efg, NixProvider, /path/to/file/efg
klm, ExecutableCapabilitiesProvider, /usr/bin/klm -a -b -c

原帖

我想让我的代理报告新功能,这些功能不是静态的,而是命令或类似的结果?我怎样才能做到这一点? 我们的代理在 linux 机器上运行。 具体来说,我想要一个名为pip-packages 的新功能,它的值是在shell 上执行的命令pip freeze 的结果。

【问题讨论】:

    标签: azure-devops azure-devops-self-hosted-agent


    【解决方案1】:

    如果您的意思是添加用户定义的功能,那么您可以编写一个脚本来调用 REST API 来更新代理功能。

    PUT https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolid}/agents/{agentid}/usercapabilities?api-version=5.0
    
    Request body:
    {"pip-packages": "xxxx"}
    

    例如,您可以设置一个变量并运行命令 pip freeze 并将响应导出为该变量的值,然后通过调用 REST API 更新代理功能:

    以下 PowerShell 示例供您参考:

    Param(
       [string]$collectionurl = "https://dev.azure.com/{organization}",
       [string]$poolid = "14",
       [string]$agentid = "16",
       [string]$user = "user",
       [string]$token = "PAT/Password"
    )
    
    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    
    # Run pip freeze command and get response as the value of the variable $pipfreeze  (Just for your reference here, you need to extract the value with running the commands)
    $pipfreeze = "response of pip freeze" 
    
    # Create json body with that value 
    $baseUri = "$collectionurl/_apis/distributedtask/pools/$poolid/agents/$agentid/usercapabilities?api-version=5.0"
    
    function CreateJsonBody
    {
        $value = @"
        {"pip-packages":"$pipfreeze"}
    "@
     return $value
    }
    $json = CreateJsonBody
    
    
    # Update the Agent user capability
    $agentcapability = Invoke-RestMethod -Uri $baseUri -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    write-host "==========================================================" 
    
    Write-host "userCapabilities :" $agentcapability.userCapabilities.'pip-packages'
    
    write-host "==========================================================" 
    

    【讨论】:

    • 你说的很多话是我可以在名称/值方面添加一个功能。我已经知道了。这不是我要问的。事实上,我要问的更像是一个功能提供者,它可以执行命令来确定给定功能名称的值
    • “可以执行命令以确定给定功能名称的值的功能提供程序”您能否提供此方案的示例?您想如何确定给定功能名称的值?具体逻辑是什么?
    • 我更新了帖子以对我的想法提出建议。我这样做只是因为你问了。我没有告诉任何人做任何事情。谢谢
    • 看来需求的主要部分是开发一个新的项目/扩展。我们可以调用 API 来添加用户功能,但不知道您的其他需求。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多