Skip to content

Added script to mirror containers to OCIR #1794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app-dev/devops-and-containers/ocir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Mirror images to OCIR

These scripts are supposed to be launched from the OCI Cloud Console.

The user must have the permissions to create a Container Registry repository and to push images on it.

To mirror all the images in a Helm chart repository, just type:
```bash
./mirror-helm-ocir.sh -c <helm-repo-name>/<chart-name>
```
The script will create all the required repository. But be sure to configure REMOTE_REGISTRY and COMPARTMENT_ID first directly in the script!

The same script is also available if you want to mirror explicitly some images, by running:
```bash
./mirror-ocir-sh -i image1,image2
```
59 changes: 59 additions & 0 deletions app-dev/devops-and-containers/ocir/mirror-helm-ocir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# RUN IN CLOUD SHELL!!!!

REMOTE_REGISTRY="lhr.ocir.io" # Replace it with your own regional registry
CREATE_OCIR_PUBLIC_REGISTRY=true
COMPARTMENT_ID="ocid1.compartment.oc1..." # Compartment where images will be created

unset -v HELM_CHART;

while getopts c: flag
do
case "${flag}" in
c) HELM_CHART=${OPTARG};;
*) echo 'Error in command line parsing' >&2
exit 1
esac
done

if [ -z "$HELM_CHART" ]; then
echo 'Missing -c (helm chart)' >&2
exit 1
fi

# Extract image names and tags
images=$(helm template $HELM_CHART | grep -oE 'image:[[:space:]]*[^[:space:]]+' | sed 's/image:[[:space:]]*//')
# Remove duplicates
images=$(echo "$images" | tr ' ' '\n' | sort -u | tr '\n' ' ')

create_repo(){
#Checking if registry creation necessary
repo_id=$(oci artifacts container repository list --compartment-id "$COMPARTMENT_ID" --display-name "$1" --limit 1 --query data.items[0].id --raw-output);
# Create repo if it does not exist!
[ -z "$repo_id" ] && repo_id=$(oci artifacts container repository create --display-name "$1" --compartment-id "$COMPARTMENT_ID" --is-public $CREATE_OCIR_PUBLIC_REGISTRY --query data.id --raw-output);
# If repo_id is still empty, an error has occurred
[ -z "$repo_id" ] && exit 1
}

for image in $images; do

# Remove quotes and double quotes
image=${image//[\"\']/}

echo "Processing image: $image"
base_image="${image#*/}"
image_name="${base_image%%:*}"
image_tag="${base_image#*:}"

REPO_NAMESPACE=$(oci artifacts container configuration get --compartment-id "$COMPARTMENT_ID" --query data.namespace --raw-output);

create_repo "$image_name"

TOKEN=$(oci raw-request --http-method GET --target-uri "https://${REMOTE_REGISTRY}/20180419/docker/token" | jq -r .data.token);

skopeo copy "docker://$image" "docker://$REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag" --multi-arch system --override-os "linux" --dest-creds BEARER_TOKEN:$TOKEN

echo "Mirrored $image to $REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag"

done
59 changes: 59 additions & 0 deletions app-dev/devops-and-containers/ocir/mirror-ocir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# RUN IN CLOUD SHELL!!!!

REMOTE_REGISTRY="lhr.ocir.io" # Replace it with your own regional registry
CREATE_OCIR_PUBLIC_REGISTRY=true
COMPARTMENT_ID="ocid1.compartment.oc1..." # Compartment where images will be created

unset -v IMAGES;

while getopts i: flag
do
case "${flag}" in
i) IMAGES=${OPTARG};;
*) echo 'Error in command line parsing' >&2
exit 1
esac
done

if [ -z "$IMAGES" ]; then
echo 'Missing -i (images array with comma separator)' >&2
exit 1
fi

# Extract image names and tags
IMAGES=(${IMAGES//,/ })
# Remove duplicates
IMAGES=$(echo "$IMAGES" | tr ' ' '\n' | sort -u | tr '\n' ' ')

create_repo(){
#Checking if registry creation necessary
repo_id=$(oci artifacts container repository list --compartment-id "$COMPARTMENT_ID" --display-name "$1" --limit 1 --query data.items[0].id --raw-output);
# Create repo if it does not exist!
[ -z "$repo_id" ] && repo_id=$(oci artifacts container repository create --display-name "$1" --compartment-id "$COMPARTMENT_ID" --is-public $CREATE_OCIR_PUBLIC_REGISTRY --query data.id --raw-output);
# If repo_id is still empty, an error has occurred
[ -z "$repo_id" ] && exit 1
}

for image in $IMAGES; do

# Remove quotes and double quotes
image=${image//[\"\']/}

echo "Processing image: $image"
base_image="${image#*/}"
image_name="${base_image%%:*}"
image_tag="${base_image#*:}"

REPO_NAMESPACE=$(oci artifacts container configuration get --compartment-id "$COMPARTMENT_ID" --query data.namespace --raw-output);

create_repo "$image_name"

TOKEN=$(oci raw-request --http-method GET --target-uri "https://${REMOTE_REGISTRY}/20180419/docker/token" | jq -r .data.token);

skopeo copy "docker://$image" "docker://$REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag" --multi-arch system --override-os "linux" --dest-creds BEARER_TOKEN:$TOKEN

echo "Mirrored $image to $REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag"

done