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