5dd126
#!/bin/bash
5dd126
#set -e
5dd126
rm -f /tmp/pyxis*.json
5dd126
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`
5dd126
if [ "$TOTAL" == "null" ]; then
5dd126
  echo "Error comunicating with Pyxis API."
5dd126
  exit 1
5dd126
fi
5dd126
PAGES=$(($TOTAL/500))
5dd126
for P in `seq 0 $PAGES`; do
5dd126
  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
5dd126
done
5dd126
cat /tmp/pyxis*.json > /tmp/pyx.json
5dd126
rm -f /tmp/rhel-shortnames.conf
5dd126
while read -r LINE; do
5dd126
  if [[ "$LINE" == *\"_id\":* ]] || [[ "$LINE" == *\"total\":* ]]; then
5dd126
    if [ -z $REGISTRY ] ||
5dd126
       [ -z $PUBLISHED ] ||
5dd126
       [ -z $REPOSITORY ] ||
5dd126
       [  $REPOSITORY == \"\" ] ||
5dd126
       [ "$AVAILABLE" != "Generally Available" ] ||
5dd126
       [[ $REPOSITORY == *[@:]* ]] ||
21a30b
       [[ $REPOSITORY == *[* ]] ||
5dd126
       [[ "$REGISTRY" == *non_registry* ]] ||
5dd126
       [[ $REGISTRY != *.* ]]
5dd126
     then
5dd126
      continue
5dd126
    fi
5dd126
    if [[ $REGISTRY == *quay.io* ]] ||
5dd126
       [[ $REGISTRY == *redhat.com* ]]; then
5dd126
      if [ "$REQUIRES_TERMS" == "1" ]; then
5dd126
        REGISTRY=registry.redhat.io
5dd126
      fi
5dd126
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\""
5dd126
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\"" >> /tmp/rhel-shortnames.conf
5dd126
    fi
5dd126
    REGISTRY=""
5dd126
    PUBLISHED=""
5dd126
    AVAILABLE=""
5dd126
    REPOSITORY=""
5dd126
    REQUIRES_TERMS=""
5dd126
    continue
5dd126
  fi
5dd126
  if [[ "$LINE" == *\"published\":\ true,* ]]; then
5dd126
    PUBLISHED=1
5dd126
  fi
5dd126
  if [[ "$LINE" == *\"requires_terms\":\ true,* ]]; then
5dd126
    REQUIRES_TERMS=1
5dd126
  fi
5dd126
  if [[ "$LINE" == *\"repository\":\ * ]]; then
5dd126
    REPOSITORY=`echo $LINE | sed 's,^.* ",,' | sed 's;",$;;'`
5dd126
  fi
5dd126
  if [[ "$LINE" == *\"registry\":\ * ]]; then
5dd126
    REGISTRY=`echo $LINE | sed -e 's,^.*:\ ",,' -e 's,".*,,'`
5dd126
  fi
5dd126
  if [[ "$LINE" == *\"release_categories\":\ * ]]; then
5dd126
    read -r LINE
5dd126
    AVAILABLE=`echo $LINE | sed 's,",,g'`
5dd126
  fi
5dd126
done < /tmp/pyx.json
5dd126
5dd126
cp /tmp/rhel-shortnames.conf /tmp/r.conf
5dd126
for D in `cut -d\  -f1 /tmp/r.conf | sort | uniq -d`; do
5dd126
  echo $D
5dd126
  M=`grep ^$D /tmp/r.conf | grep 'redhat.com' | tail -n1`
5dd126
  [ -z "$M" ] && M=`grep ^$D /tmp/r.conf | tail -n1`
5dd126
  echo $M
5dd126
  if [ ! -z "$M" ]; then
5dd126
    echo "replacing $D with $M"
5dd126
    grep -v "^$D.*" /tmp/r.conf > /tmp/r2.conf
5dd126
    echo "$M" >> /tmp/r2.conf
5dd126
    mv /tmp/r2.conf /tmp/r.conf
5dd126
  fi
5dd126
done
5dd126
5dd126
echo "[aliases]" > 001-rhel-shortnames-pyxis.conf
5dd126
sort /tmp/r.conf >> 001-rhel-shortnames-pyxis.conf