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