From 2dbda8a8b465fa93644920fdc53fbab9d16153a1 Mon Sep 17 00:00:00 2001 From: Guillaume Boutry Date: Fri, 19 Apr 2024 11:37:07 +0200 Subject: [PATCH] Add script to automatically create charm tracks The tracks are looked up from zuul.d/zuul.yaml. Changes these tracks before running the script. This script will check if the track exists before creating it. Change-Id: If3fd2910319970f63608350ea43c3c2ab6c77ffc --- create_tracks.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 create_tracks.sh diff --git a/create_tracks.sh b/create_tracks.sh new file mode 100755 index 00000000..33c04df3 --- /dev/null +++ b/create_tracks.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# This script look inside publish_channels for all tracks that should exits. +# If the track does not exist, it creates it. + +if ! command -v charmcraft &> /dev/null +then + echo "charmcraft could not be found" + echo "Install it with: snap install charmcraft --classic" + exit 1 +fi + +if ! command -v yq &> /dev/null +then + echo "yq could not be found" + echo "Install it with: snap install yq" + exit 1 +fi + +if ! command -v jq &> /dev/null +then + echo "jq could not be found" + echo "Install it with: snap install jq" + exit 1 +fi + +if ! command -v curl &> /dev/null +then + echo "curl could not be found" + echo "Install it with: apt install curl" + exit 1 +fi + +opt_charm="" + +publish_channel=$(yq -r '.[0].project.vars.publish_channels | to_entries | .[] | (.key + " " + .value)' zuul.d/zuul.yaml) + +while IFS=' ' read -r charm _; do + opt_charm="$opt_charm --charm=$charm" +done < <(echo "$publish_channel") + + +charmcraft login --export=sunbeam-charms.auth \ + $opt_charm \ + --permission=package-view-metadata \ + --permission=package-manage-metadata \ + --ttl=3600 + +CHARMHUB_MACAROON_HEADER="Authorization: Macaroon $(cat sunbeam-charms.auth | base64 -d | jq -r .v)" + +while IFS=' ' read -r charm channel; do + track=$(echo "$channel" | cut -d'/' -f1) + url=https://api.charmhub.io/v1/charm/$charm + metadata=$(curl -s "$url" -H'Content-type: application/json' -H "$CHARMHUB_MACAROON_HEADER") + tracks=$(echo "$metadata" | jq -r '.metadata.tracks[].name') + if [[ $tracks =~ $track ]]; then + echo "Track $track already exists for charm $charm" + else + echo "Creating track $track for charm $charm" + curl -s "$url/tracks" -X POST -H'Content-type: application/json' -H "$CHARMHUB_MACAROON_HEADER" -d '[{"name": "'"$track"'"}]' + fi +done < <(echo "$publish_channel") + +# Clean the created files +rm sunbeam-charms.*