Last active
October 15, 2019 13:40
-
-
Save matheusneder/fd363255eb0d35686538e23cfa0db1fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Migrate VSTS release pipelines agent | |
function get-vsts-creedentials | |
{ | |
echo -n "_:$VSTS_TOKEN" | base64 -w 0 | |
} | |
function any-release-pipeline | |
{ | |
local project_name="$1" | |
local qt=$(curl -s -X GET \ | |
"https://vsrm.dev.azure.com/$ORGANIZATION/$project_name/_apis/release/definitions?api-version=5.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Host: vsrm.dev.azure.com' \ | |
-H 'cache-control: no-cache' | jq -r '.count') | |
if [ -z "$qt" ] || [ "$qt" = "null" ] | |
then | |
qt=0 | |
fi | |
if [ "$qt" -gt 0 ] | |
then | |
return 0 | |
fi | |
return 1 | |
} | |
function configure-project-agentpool | |
{ | |
local project_name="$1" | |
local project_id="$2" | |
local queue_id=$(curl -s -X GET \ | |
"https://dev.azure.com/$ORGANIZATION/$project_name/_apis/distributedtask/queues?api-version=5.1-preview.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Cache-Control: no-cache' \ | |
-H 'Host: dev.azure.com' \ | |
-H 'cache-control: no-cache' | jq '.value[] | select(.name == "Kubernetes") | .id') | |
if [ -z "$queue_id" ] | |
then | |
queue_id=$(curl -s -X GET \ | |
"https://dev.azure.com/$ORGANIZATION/$project_name/_apis/distributedtask/queues?api-version=5.1-preview.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Cache-Control: no-cache' \ | |
-H 'Connection: keep-alive' \ | |
-H 'Host: dev.azure.com' \ | |
-H 'cache-control: no-cache' | jq '.value[] | select(.name == "kubernetes") | .id') | |
if ! [ -z "$queue_id" ] | |
then | |
curl -s -X DELETE \ | |
"https://dev.azure.com/$ORGANIZATION/$project_name/_apis/distributedtask/queues/$queue_id?api-version=5.1-preview.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Host: dev.azure.com' \ | |
-H 'cache-control: no-cache' | |
fi | |
queue_id=$(curl -s -X POST \ | |
"https://dev.azure.com/$ORGANIZATION/$project_name/_apis/distributedtask/queues?api-version=5.1-preview.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Cache-Control: no-cache' \ | |
-H 'Content-Type: application/json' \ | |
-H 'Host: dev.azure.com' \ | |
-d '{ "name": "Kubernetes", "projectId": "'$project_id'", "pool": { "id": 12 } }' | jq -r '.id' ) | |
fi | |
echo "$queue_id" | |
} | |
function do-migration | |
{ | |
local project_name="$1" | |
local queue_id="$2" | |
local release_id="$3" | |
echo "- Migrating $project_name, release_id: $release_id" | |
curl -s -X GET \ | |
"https://vsrm.dev.azure.com/$ORGANIZATION/$project_name/_apis/release/definitions/$release_id?api-version=5.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Host: vsrm.dev.azure.com' \ | |
-H 'cache-control: no-cache' -o /tmp/vsts-cd | |
sed -E -i 's/"queueId": *[1-9][0-9]*/"queueId": '$queue_id'/g' /tmp/vsts-cd | |
sed -E -i 's/"dockerHostEndpoint": *"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"/"dockerHostEndpoint": null/g' /tmp/vsts-cd | |
curl -s -X PUT \ | |
"https://vsrm.dev.azure.com/$ORGANIZATION/$project_name/_apis/release/definitions?api-version=5.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Content-Type: application/json' \ | |
-H 'Host: vsrm.dev.azure.com' \ | |
-H 'cache-control: no-cache' \ | |
-d @/tmp/vsts-cd -o /dev/null | |
} | |
function iterate-through-project-release-pipelines | |
{ | |
local project_name="$1" | |
local queue_id="$2" | |
for release_id in $(curl -s -X GET \ | |
"https://vsrm.dev.azure.com/$ORGANIZATION/$project_name/_apis/release/definitions?api-version=5.1" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Host: vsrm.dev.azure.com' \ | |
-H 'cache-control: no-cache' | jq -r '.value[].id') | |
do | |
release_id="$(echo "$release_id" | tr -d '[:space:]')" | |
do-migration "$project_name" "$queue_id" "$release_id" | |
done | |
echo "> '$project_name' has migrated." | |
echo "$project_name" >> /tmp/agent_migration_migrated_projects | |
} | |
function main | |
{ | |
if [ -z "$VSTS_TOKEN" ] | |
then | |
echo "Missing VSTS_TOKEN (env var)" >&2 | |
exit 1 | |
fi | |
if [ -z "$ORGANIZATION" ] | |
then | |
echo "Missing ORGANIZATION (env var)" >&2 | |
exit 2 | |
fi | |
for i in $(\ | |
curl -s -X GET \ | |
"https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=5.1&\$top=1000&stateFilter=wellFormed" \ | |
-H 'Accept: */*' \ | |
-H "Authorization: Basic $(get-vsts-creedentials)" \ | |
-H 'Host: dev.azure.com' \ | |
-H 'cache-control: no-cache' | jq -r '.value[] | "\(.id),\(.name)"') | |
do | |
i="$(echo "$i" | tr -d '[:space:]')" | |
if echo "$i" | egrep '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12},[a-zA-Z0-9-]+$' > /dev/null | |
then | |
local oifs="$IFS" | |
local p | |
IFS=',' read -ra p <<< "$i" | |
IFS="$oifs" | |
local project_id="${p[0]}" | |
local project_name="${p[1]}" | |
if grep "^${project_name}$" /tmp/agent_migration_verified_projects > /dev/null 2>&1 | |
then | |
echo "Skiping $project_name (already verified)" | |
continue | |
fi | |
echo "+ Looking for release pipelines on '$project_name' ..." | |
if any-release-pipeline "$project_name" | |
then | |
local queue_id=$(configure-project-agentpool "$project_name" "$project_id") | |
if [ -z "$queue_id" ] || ! echo $queue_id | egrep '^[1-9][0-9]*$' > /dev/null | |
then | |
echo "Bad queue_id: '$queue_id'" >&2 | |
exit 10 | |
fi | |
iterate-through-project-release-pipelines "$project_name" "$queue_id" | |
fi | |
echo "$project_name" >> /tmp/agent_migration_verified_projects | |
fi | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment