From 4766a7b12b8fa1261bfc85e6e81567c42b553104 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Sun, 29 Sep 2024 15:31:45 -0800 Subject: [PATCH] Bundle crane cli (#12) Signed-off-by: Tamal Saha --- pkg/cmds/scripts.go | 52 +++++++++++++++++++++++++------ testdata/README.md | 17 ++++++++++ testdata/catalog/copy-images.sh | 24 ++++++++++++++ testdata/catalog/export-images.sh | 22 +++++++++++++ testdata/catalog/imagelist.yaml | 1 + testdata/catalog/import-images.sh | 15 +++++++++ 6 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 testdata/README.md create mode 100755 testdata/catalog/copy-images.sh create mode 100755 testdata/catalog/export-images.sh create mode 100644 testdata/catalog/imagelist.yaml create mode 100755 testdata/catalog/import-images.sh diff --git a/pkg/cmds/scripts.go b/pkg/cmds/scripts.go index b9ae99db..6a859954 100644 --- a/pkg/cmds/scripts.go +++ b/pkg/cmds/scripts.go @@ -111,11 +111,24 @@ func GenerateScripts(files []string, outdir string, nondistro, insecure bool) er var buf bytes.Buffer buf.WriteString(`#!/bin/bash - set -x mkdir -p images +OS=$(uname -o) +if [ "${OS}" = "GNU/Linix" ]; then + OS=Linux +fi +ARCH=$(uname -m) +if [ "${ARCH}" = "aarch64" ]; then + ARCH=arm64 +fi +curl -sL "https://github.com/google/go-containerregistry/releases/latest/download/go-containerregistry_${OS}_${ARCH}.tar.gz" > /tmp/go-containerregistry.tar.gz +tar -zxvf /tmp/go-containerregistry.tar.gz -C /tmp/ +mv /tmp/crane images + +CMD="./images/crane" + `) for _, img := range images { // crane pull appscode/cluster-ui:0.4.16 images/cluster-ui.tar @@ -127,7 +140,7 @@ mkdir -p images return fmt.Errorf("image %s has no tag", img) } - buf.WriteString("crane pull") + buf.WriteString("$CMD pull") if nondistro { buf.WriteString(" --allow-nondistributable-artifacts") } @@ -154,11 +167,16 @@ mkdir -p images set -x -TARBALL=${1:-} -REGISTRY=${2:-} +if [ -z "${IMAGE_REGISTRY}" ]; then + echo "IMAGE_REGISTRY is not set" + exit 1 +fi +TARBALL=${1:-} tar -zxvf $TARBALL +CMD="./crane" + `) for _, img := range images { // crane push images/cluster-ui.tar $IMAGE_REGISTRY/cluster-ui:0.4.16 @@ -170,7 +188,7 @@ tar -zxvf $TARBALL return fmt.Errorf("image %s has no tag", img) } - buf.WriteString("crane push") + buf.WriteString("$CMD push") if nondistro { buf.WriteString(" --allow-nondistributable-artifacts") } @@ -193,10 +211,24 @@ tar -zxvf $TARBALL set -x -TARBALL=${1:-} -REGISTRY=${2:-} - -tar -zxvf $TARBALL +if [ -z "${IMAGE_REGISTRY}" ]; then + echo "IMAGE_REGISTRY is not set" + exit 1 +fi + +OS=$(uname -o) +if [ "${OS}" = "GNU/Linix" ]; then + OS=Linux +fi +ARCH=$(uname -m) +if [ "${ARCH}" = "aarch64" ]; then + ARCH=arm64 +fi +curl -sL "https://github.com/google/go-containerregistry/releases/latest/download/go-containerregistry_${OS}_${ARCH}.tar.gz" > /tmp/go-containerregistry.tar.gz +tar -zxvf /tmp/go-containerregistry.tar.gz -C /tmp/ +mv /tmp/crane . + +CMD="./crane" `) for _, img := range images { @@ -209,7 +241,7 @@ tar -zxvf $TARBALL return fmt.Errorf("image %s has no tag", img) } - buf.WriteString("crane cp") + buf.WriteString("$CMD cp") if nondistro { buf.WriteString(" --allow-nondistributable-artifacts") } diff --git a/testdata/README.md b/testdata/README.md new file mode 100644 index 00000000..8e0bb7c5 --- /dev/null +++ b/testdata/README.md @@ -0,0 +1,17 @@ +# Test scripts + +```bash +docker network create registry +docker run -d --restart=unless-stopped --name registry --net registry \ + -e "REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/var/lib/registry" \ + -e "REGISTRY_STORAGE_DELETE_ENABLED=true" \ + -e "REGISTRY_VALIDATION_DISABLED=true" \ + -v "registry-data:/var/lib/registry" \ + -p "127.0.0.1:5000:5000" \ + registry:2 +``` + +```bash +image-packer generate-scripts --output-dir=catalog \ + --src=catalog/imagelist.yaml +``` diff --git a/testdata/catalog/copy-images.sh b/testdata/catalog/copy-images.sh new file mode 100755 index 00000000..1dbc0e76 --- /dev/null +++ b/testdata/catalog/copy-images.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -x + +if [ -z "${IMAGE_REGISTRY}" ]; then + echo "IMAGE_REGISTRY is not set" + exit 1 +fi + +OS=$(uname -o) +if [ "${OS}" = "GNU/Linix" ]; then + OS=Linux +fi +ARCH=$(uname -m) +if [ "${ARCH}" = "aarch64" ]; then + ARCH=arm64 +fi +curl -sL "https://github.com/google/go-containerregistry/releases/latest/download/go-containerregistry_${OS}_${ARCH}.tar.gz" > /tmp/go-containerregistry.tar.gz +tar -zxvf /tmp/go-containerregistry.tar.gz -C /tmp/ +mv /tmp/crane . + +CMD="./crane" + +$CMD cp ghcr.io/appscode/cluster-ui:0.9.7 $IMAGE_REGISTRY/appscode/cluster-ui:0.9.7 diff --git a/testdata/catalog/export-images.sh b/testdata/catalog/export-images.sh new file mode 100755 index 00000000..627bfc75 --- /dev/null +++ b/testdata/catalog/export-images.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -x + +mkdir -p images + +OS=$(uname -o) +if [ "${OS}" = "GNU/Linix" ]; then + OS=Linux +fi +ARCH=$(uname -m) +if [ "${ARCH}" = "aarch64" ]; then + ARCH=arm64 +fi +curl -sL "https://github.com/google/go-containerregistry/releases/latest/download/go-containerregistry_${OS}_${ARCH}.tar.gz" > /tmp/go-containerregistry.tar.gz +tar -zxvf /tmp/go-containerregistry.tar.gz -C /tmp/ +mv /tmp/crane images + +CMD="./images/crane" + +$CMD pull ghcr.io/appscode/cluster-ui:0.9.7 images/appscode-cluster-ui-0.9.7.tar + +tar -czvf images.tar.gz images diff --git a/testdata/catalog/imagelist.yaml b/testdata/catalog/imagelist.yaml new file mode 100644 index 00000000..51dda162 --- /dev/null +++ b/testdata/catalog/imagelist.yaml @@ -0,0 +1 @@ +- ghcr.io/appscode/cluster-ui:0.9.7 diff --git a/testdata/catalog/import-images.sh b/testdata/catalog/import-images.sh new file mode 100755 index 00000000..ec931b23 --- /dev/null +++ b/testdata/catalog/import-images.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -x + +if [ -z "${IMAGE_REGISTRY}" ]; then + echo "IMAGE_REGISTRY is not set" + exit 1 +fi + +TARBALL=${1:-} +tar -zxvf $TARBALL + +CMD="./crane" + +$CMD push images/appscode-cluster-ui-0.9.7.tar $IMAGE_REGISTRY/appscode/cluster-ui:0.9.7