fa61e0
#!/bin/bash
fa61e0
#set -e
fa61e0
rm -f /tmp/pyxis*.json
fa61e0
TOTAL=`curl -s --negotiate -u: -H 'Content-Type: application/json' -H 'Accept: application/json' -X GET "https://pyxis.engineering.redhat.com/v1/repositories?page_size=1" | jq .total`
fa61e0
if [ "$TOTAL" == "null" ]; then
fa61e0
  echo "Error comunicating with Pyxis API."
fa61e0
  exit 1
fa61e0
fi
fa61e0
PAGES=$(($TOTAL/500))
fa61e0
for P in `seq 0 $PAGES`; do
fa61e0
  curl -s --negotiate -u: -H 'Content-Type: application/json' -H 'Accept: application/json' -X GET "https://pyxis.engineering.redhat.com/v1/repositories?page_size=500&page=$P" > /tmp/pyxis$P.json
fa61e0
done
fa61e0
cat /tmp/pyxis*.json > /tmp/pyx.json
fa61e0
rm -f /tmp/rhel-shortnames.conf
fa61e0
while read -r LINE; do
fa61e0
  if [[ "$LINE" == *\"_id\":* ]] || [[ "$LINE" == *\"total\":* ]]; then
fa61e0
    if [ -z $REGISTRY ] ||
fa61e0
       [ -z $PUBLISHED ] ||
fa61e0
       [ -z $REPOSITORY ] ||
fa61e0
       [  $REPOSITORY == \"\" ] ||
fa61e0
       [ "$AVAILABLE" != "Generally Available" ] ||
fa61e0
       [[ $REPOSITORY == *[@:]* ]] ||
fa61e0
       [[ "$REGISTRY" == *non_registry* ]] ||
fa61e0
       [[ $REGISTRY != *.* ]]
fa61e0
     then
fa61e0
      continue
fa61e0
    fi
fa61e0
    if [[ $REGISTRY == *quay.io* ]] ||
fa61e0
       [[ $REGISTRY == *redhat.com* ]]; then
fa61e0
      if [ "$REQUIRES_TERMS" == "1" ]; then
fa61e0
        REGISTRY=registry.redhat.io
fa61e0
      fi
fa61e0
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\""
fa61e0
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\"" >> /tmp/rhel-shortnames.conf
fa61e0
    fi
fa61e0
    REGISTRY=""
fa61e0
    PUBLISHED=""
fa61e0
    AVAILABLE=""
fa61e0
    REPOSITORY=""
fa61e0
    REQUIRES_TERMS=""
fa61e0
    continue
fa61e0
  fi
fa61e0
  if [[ "$LINE" == *\"published\":\ true,* ]]; then
fa61e0
    PUBLISHED=1
fa61e0
  fi
fa61e0
  if [[ "$LINE" == *\"requires_terms\":\ true,* ]]; then
fa61e0
    REQUIRES_TERMS=1
fa61e0
  fi
fa61e0
  if [[ "$LINE" == *\"repository\":\ * ]]; then
fa61e0
    REPOSITORY=`echo $LINE | sed 's,^.* ",,' | sed 's;",$;;'`
fa61e0
  fi
fa61e0
  if [[ "$LINE" == *\"registry\":\ * ]]; then
fa61e0
    REGISTRY=`echo $LINE | sed -e 's,^.*:\ ",,' -e 's,".*,,'`
fa61e0
  fi
fa61e0
  if [[ "$LINE" == *\"release_categories\":\ * ]]; then
fa61e0
    read -r LINE
fa61e0
    AVAILABLE=`echo $LINE | sed 's,",,g'`
fa61e0
  fi
fa61e0
done < /tmp/pyx.json
fa61e0
fa61e0
cp /tmp/rhel-shortnames.conf /tmp/r.conf
fa61e0
for D in `cut -d\  -f1 /tmp/r.conf | sort | uniq -d`; do
fa61e0
  echo $D
fa61e0
  M=`grep ^$D /tmp/r.conf | grep 'redhat.com' | tail -n1`
fa61e0
  [ -z "$M" ] && M=`grep ^$D /tmp/r.conf | tail -n1`
fa61e0
  echo $M
fa61e0
  if [ ! -z "$M" ]; then
fa61e0
    echo "replacing $D with $M"
fa61e0
    grep -v "^$D.*" /tmp/r.conf > /tmp/r2.conf
fa61e0
    echo "$M" >> /tmp/r2.conf
fa61e0
    mv /tmp/r2.conf /tmp/r.conf
fa61e0
  fi
fa61e0
done
fa61e0
fa61e0
echo "[aliases]" > 001-rhel-shortnames-pyxis.conf
fa61e0
sort /tmp/r.conf >> 001-rhel-shortnames-pyxis.conf