|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eu |
| 4 | +set -o pipefail |
| 5 | + |
| 6 | +readonly PROG_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | +readonly ROOT_DIR="$(cd "${PROG_DIR}/.." && pwd)" |
| 8 | +readonly BIN_DIR="${ROOT_DIR}/.bin" |
| 9 | + |
| 10 | +# shellcheck source=SCRIPTDIR/.util/tools.sh |
| 11 | +source "${PROG_DIR}/.util/tools.sh" |
| 12 | + |
| 13 | +# shellcheck source=SCRIPTDIR/.util/print.sh |
| 14 | +source "${PROG_DIR}/.util/print.sh" |
| 15 | + |
| 16 | +if [[ $BASH_VERSINFO -lt 4 ]]; then |
| 17 | + util::print::error "Before running this script please update Bash to v4 or higher (e.g. on OSX: \$ brew install bash)" |
| 18 | +fi |
| 19 | + |
| 20 | +function main() { |
| 21 | + local build_ref=() |
| 22 | + local run_ref=() |
| 23 | + local image_ref=() |
| 24 | + local build_archive="" |
| 25 | + local run_archive="" |
| 26 | + local image_archive="" |
| 27 | + |
| 28 | + while [[ "${#}" != 0 ]]; do |
| 29 | + case "${1}" in |
| 30 | + --help|-h) |
| 31 | + shift 1 |
| 32 | + usage |
| 33 | + exit 0 |
| 34 | + ;; |
| 35 | + |
| 36 | + --build-ref) |
| 37 | + build_ref+=("${2}") |
| 38 | + shift 2 |
| 39 | + ;; |
| 40 | + |
| 41 | + --run-ref) |
| 42 | + run_ref+=("${2}") |
| 43 | + shift 2 |
| 44 | + ;; |
| 45 | + |
| 46 | + --build-archive) |
| 47 | + build_archive=${2} |
| 48 | + shift 2 |
| 49 | + ;; |
| 50 | + |
| 51 | + --run-archive) |
| 52 | + run_archive=${2} |
| 53 | + shift 2 |
| 54 | + ;; |
| 55 | + |
| 56 | + --image-ref) |
| 57 | + image_ref+=("${2}") |
| 58 | + shift 2 |
| 59 | + ;; |
| 60 | + |
| 61 | + --image-archive) |
| 62 | + image_archive=${2} |
| 63 | + shift 2 |
| 64 | + ;; |
| 65 | + |
| 66 | + "") |
| 67 | + # skip if the argument is empty |
| 68 | + shift 1 |
| 69 | + ;; |
| 70 | + |
| 71 | + *) |
| 72 | + util::print::error "unknown argument \"${1}\"" |
| 73 | + esac |
| 74 | + done |
| 75 | + |
| 76 | + if [[ ${#image_ref[@]} != 0 || -n "$image_archive" ]]; then |
| 77 | + if ((${#image_ref[@]} == 0)); then |
| 78 | + util::print::error "--image-ref is required [Example: docker.io/paketobuildpacks/foo:latest]" |
| 79 | + fi |
| 80 | + |
| 81 | + if [ -z "$image_archive" ]; then |
| 82 | + util::print::error "--image-archive is required [Example: ./path/to/image.oci]" |
| 83 | + fi |
| 84 | + else |
| 85 | + if ((${#build_ref[@]} == 0)); then |
| 86 | + util::print::error "--build-ref is required [Example: docker.io/paketobuildpacks/foo:latest]" |
| 87 | + fi |
| 88 | + |
| 89 | + if ((${#run_ref[@]} == 0)); then |
| 90 | + util::print::error "--run-ref is required [Example: docker.io/paketobuildpacks/foo:1.0.0]" |
| 91 | + fi |
| 92 | + |
| 93 | + if ((${#run_ref[@]} != ${#build_ref[@]})); then |
| 94 | + util::print::error "must have the same number of --build-ref and --run-ref arguments" |
| 95 | + fi |
| 96 | + |
| 97 | + if [ -z "$build_archive" ]; then |
| 98 | + util::print::error "--build-archive is required [Example: ./path/to/build.oci]" |
| 99 | + fi |
| 100 | + |
| 101 | + if [ -z "$run_archive" ]; then |
| 102 | + util::print::error "--run-archive is required [Example: ./path/to/run.oci]" |
| 103 | + fi |
| 104 | + fi |
| 105 | + |
| 106 | + tools::install |
| 107 | + |
| 108 | + if [[ ${#image_ref[@]} != 0 || -n "$image_archive" ]]; then |
| 109 | + stack::publish::image \ |
| 110 | + "$image_archive" \ |
| 111 | + "${#image_ref[@]}" \ |
| 112 | + "${image_ref[@]}" |
| 113 | + else |
| 114 | + stack::publish \ |
| 115 | + "$build_archive" \ |
| 116 | + "$run_archive" \ |
| 117 | + "${#build_ref[@]}" \ |
| 118 | + "${build_ref[@]}" \ |
| 119 | + "${#run_ref[@]}" \ |
| 120 | + "${run_ref[@]}" |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +function usage() { |
| 125 | + cat <<-USAGE |
| 126 | +publish.sh [OPTIONS] |
| 127 | +
|
| 128 | +Publishes the stack using the existing OCI image archives. |
| 129 | +
|
| 130 | +OPTIONS |
| 131 | + --build-ref list of build references to publish to [Required if --image-ref is not provided] |
| 132 | + --run-ref list of run references to publish to [Required if --image-ref is not provided] |
| 133 | + --build-archive path to the build OCI archive file [Required if --image-ref is not provided] |
| 134 | + --run-archive path to the run OCI archive file [Required if --image-ref is not provided] |
| 135 | + --image-ref list of image references to publish to [Required if --build-ref and --run-ref are not provided] |
| 136 | + --image-archive path to the image OCI archive file [Required if --build-ref and --run-ref are not provided] |
| 137 | + --help -h prints the command usage |
| 138 | +USAGE |
| 139 | +} |
| 140 | + |
| 141 | +function tools::install() { |
| 142 | + util::tools::jam::install \ |
| 143 | + --directory "${BIN_DIR}" |
| 144 | +} |
| 145 | + |
| 146 | +function stack::publish() { |
| 147 | + local build_archive="$1" |
| 148 | + local run_archive="$2" |
| 149 | + |
| 150 | + # bash can't easily pass arrays, they all get merged into one list of arguments |
| 151 | + # so we pass the lengths & extract the arrays from the single argument list |
| 152 | + local build_ref_len="$3" # length of build ref array |
| 153 | + local build_ref=("${@:4:$build_ref_len}") # pull out build_ref array |
| 154 | + local run_len_slot=$(( 4 + build_ref_len)) # location of run_ref length |
| 155 | + local run_ref_len="${*:$run_len_slot:1}" # length of run ref array |
| 156 | + local run_ref_slot=$(( 1 + run_len_slot)) # location of run_ref array |
| 157 | + local run_ref=("${@:$run_ref_slot:$run_ref_len}") # pull out run_ref array |
| 158 | + |
| 159 | + # iterate over build_ref & run_ref, they will be the same length |
| 160 | + local len=${#build_ref[@]} |
| 161 | + for (( i=0; i<len; i++ )); do |
| 162 | + local br="${build_ref[$i]}" |
| 163 | + local rr="${run_ref[$i]}" |
| 164 | + args=( |
| 165 | + "--build-ref" "$br" |
| 166 | + "--run-ref" "$rr" |
| 167 | + "--build-archive" "$build_archive" |
| 168 | + "--run-archive" "$run_archive" |
| 169 | + ) |
| 170 | + jam publish-stack "${args[@]}" |
| 171 | + done |
| 172 | +} |
| 173 | + |
| 174 | +function stack::publish::image() { |
| 175 | + local image_archive="$1" |
| 176 | + |
| 177 | + # bash can't easily pass arrays, they all get merged into one list of arguments |
| 178 | + # so we pass the lengths & extract the arrays from the single argument list |
| 179 | + local image_ref_len="$2" # length of image ref array |
| 180 | + local image_ref=("${@:3:$image_ref_len}") # pull out image_ref array |
| 181 | + |
| 182 | + # iterate over image_ref |
| 183 | + local len=${#image_ref[@]} |
| 184 | + for (( i=0; i<len; i++ )); do |
| 185 | + local ir="${image_ref[$i]}" |
| 186 | + args=( |
| 187 | + "--image-ref" "$ir" |
| 188 | + "--image-archive" "$image_archive" |
| 189 | + ) |
| 190 | + jam publish-image "${args[@]}" |
| 191 | + done |
| 192 | +} |
| 193 | + |
| 194 | +main "${@:-}" |
0 commit comments