|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Path of stacks directory in the registry |
| 4 | +STACKS_DIR=${STACKS_DIR:-/registry/stacks} |
| 5 | + |
| 6 | +# Downloads the parent devfile to be used as an offline resource |
| 7 | +download_parent_devfile() { |
| 8 | + local stack_root=$1 |
| 9 | + local name=$2 |
| 10 | + local parent_devfile_uri=$3 |
| 11 | + parent_devfile=${name}-parent.devfile.yaml |
| 12 | + |
| 13 | + if [ ! -f $stack_root/$parent_devfile ]; then |
| 14 | + curl -L $parent_devfile_uri -o $stack_root/$parent_devfile || return 1 |
| 15 | + fi |
| 16 | +} |
| 17 | + |
| 18 | +# Updates the uri to the downloaded offline parent devfile |
| 19 | +replace_parent_devfile() { |
| 20 | + local stack_root=$1 |
| 21 | + local name=$2 |
| 22 | + local parent_devfile_uri=$3 |
| 23 | + stack_devfile=$stack_root/devfile.yaml |
| 24 | + parent_devfile=../${name}-parent.devfile.yaml |
| 25 | + |
| 26 | + if [ -f $stack_root/$parent_devfile ]; then |
| 27 | + export PARENT_DEVFILE=$parent_devfile |
| 28 | + yq e -i ".parent.uri=env(PARENT_DEVFILE)" $stack_devfile |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +download_and_replace() { |
| 33 | + for stack in ${stacks[@]} |
| 34 | + do |
| 35 | + if [ $stack == "OWNERS" ]; then |
| 36 | + continue |
| 37 | + fi |
| 38 | + stack_root=$STACKS_DIR/$stack |
| 39 | + stack_devfile=$stack_root/devfile.yaml |
| 40 | + # Read version list for stack |
| 41 | + versions=($([ -f ${STACKS_DIR}/${stack}/stack.yaml ] && yq e '.versions.[].version' ${STACKS_DIR}/${stack}/stack.yaml)) |
| 42 | + # Multi version stack |
| 43 | + if [[ ${#versions[@]} -gt 0 ]] |
| 44 | + then |
| 45 | + for version in ${versions[@]} |
| 46 | + do |
| 47 | + stack_root=$STACKS_DIR/$stack/$version |
| 48 | + stack_devfile=$stack_root/devfile.yaml |
| 49 | + name="$(yq e ".metadata.name" $stack_devfile)" |
| 50 | + parent_devfile_uri="$(yq e ".parent.uri" $stack_devfile)" |
| 51 | + |
| 52 | + if [ "$parent_devfile_uri" != "null" ] |
| 53 | + then |
| 54 | + echo "Downloading parent devfile in stack ${stack} version ${version}.." |
| 55 | + download_parent_devfile $stack_root $name $parent_devfile_uri |
| 56 | + if [ $? -eq 0 ]; then |
| 57 | + replace_parent_devfile $stack_root $name $parent_devfile_uri |
| 58 | + fi |
| 59 | + echo "Downloading parent devfile in stack ${stack} version ${version}..done!" |
| 60 | + fi |
| 61 | + done |
| 62 | + # Not a multi version stack |
| 63 | + else |
| 64 | + name="$(yq e ".metadata.name" $stack_devfile)" |
| 65 | + parent_devfile_uri="$(yq e ".parent.uri" $stack_devfile)" |
| 66 | + |
| 67 | + if [ "$parent_devfile_uri" != "null" ] |
| 68 | + then |
| 69 | + echo "Downloading parent devfile in stack ${stack}.." |
| 70 | + download_parent_devfile $stack_root $name $parent_devfile_uri |
| 71 | + if [ $? -eq 0 ]; then |
| 72 | + replace_parent_devfile $stack_root $name $parent_devfile_uri |
| 73 | + fi |
| 74 | + echo "Downloading parent devfile in stack ${stack}..done!" |
| 75 | + fi |
| 76 | + fi |
| 77 | + done |
| 78 | +} |
| 79 | + |
| 80 | +# Read stacks list |
| 81 | +read -r -a stacks <<< "$(ls ${STACKS_DIR} | tr '\n' ' ')" |
| 82 | + |
| 83 | +echo "Downloading parent devfiles.." |
| 84 | +download_and_replace |
| 85 | +echo "Downloading parent devfiles..done!" |
0 commit comments