0

Updates

I am going to propose a Capabilities Provider here as an update to my post. If you need more details please let me know.

We currently have a bunch of shipped Capabilities Providers in the agent source code:

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

  • Agent
  • Environment
  • Nix
  • PowerShell

What is being proposed is one additional Provider named ExecutableCapabilitiesProvider.

This new ExecutableCapabilitiesProvider will probably have a config file which can be edited on the agent machine. The format of this file could probably be:

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

As the maintainer of the self-hosted pool, I would configure this file with entries suiting me and have the agent run it as it starts. This way I am not hard-coding any values for my capabilities but rather those be determined at the start up.

And I would go one step further and add a new API call to add capabilities which is more flexible than the current one asking for name/values. An example, would be to change the parameters to Name, Provider, Params:

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

Original Post

I'd like to make my agents report on new capabilities which are not static but rather result of a command or something similar? How can I do that? Our agents run on linux boxes. To be specific, I'd like to have a new capability called pip-packages and the value for that is the result of the command pip freeze executed on the shell.

Community
  • 1
  • 1
Jeff Saremi
  • 2,674
  • 3
  • 33
  • 57

1 Answers1

0

If you mean to add User-defined capabilities, then you can write a script to call the REST API to update the agent capabilities.

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

Request body:
{"pip-packages": "xxxx"}

For example, you can set a variable and run command pip freeze and export the response as the value of that variable, then update the agent capability by calling the REST API:

Below PowerShell sample for your reference :

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 "==========================================================" 
Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • What you are saying in a lot of words is that I could add a capability in terms of name/value. I know this already. That's not what i'm asking. In fact what I'm asking is more like a Capabilities Provider which could execute commands to determine the value for a given capability name – Jeff Saremi Oct 16 '19 at 18:05
  • “Capabilities Provider which could execute commands to determine the value for a given capability name” Could you please provide a sample for this scenario? How do you want to determine the value for a given capability name? What's the specific logic? – Andy Li-MSFT Oct 17 '19 at 02:45
  • I updated the post to have a proposal on what I was thinking. I did this only because you asked. I am not telling anyone to do anything. thanks – Jeff Saremi Oct 17 '19 at 04:43
  • It appears that the mainly part of the requirement is developing a new project/extension. We can call API to add user capabilities, but no any idea for others of your requirement. – Andy Li-MSFT Oct 22 '19 at 06:40