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