Skip to content

Commit 7568d0e

Browse files
committed
Added script to mirror containers to OCIR
1 parent 2aafd63 commit 7568d0e

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Mirror images to OCIR
2+
3+
These scripts are supposed to be launched from the OCI Cloud Console.
4+
5+
The user must have the permissions to create a Container Registry repository and to push images on it.
6+
7+
To mirror all the images in a Helm chart repository, just type:
8+
```bash
9+
./mirror-helm-ocir.sh -c <helm-repo-name>/<chart-name>
10+
```
11+
The script will create all the required repository. But be sure to configure REMOTE_REGISTRY and COMPARTMENT_ID first directly in the script!
12+
13+
The same script is also available if you want to mirror explicitly some images, by running:
14+
```bash
15+
./mirror-ocir-sh -i image1,image2
16+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# RUN IN CLOUD SHELL!!!!
4+
5+
REMOTE_REGISTRY="lhr.ocir.io" # Replace it with your own regional registry
6+
CREATE_OCIR_PUBLIC_REGISTRY=true
7+
COMPARTMENT_ID="ocid1.compartment.oc1..." # Compartment where images will be created
8+
9+
unset -v HELM_CHART;
10+
11+
while getopts c: flag
12+
do
13+
case "${flag}" in
14+
c) HELM_CHART=${OPTARG};;
15+
*) echo 'Error in command line parsing' >&2
16+
exit 1
17+
esac
18+
done
19+
20+
if [ -z "$HELM_CHART" ]; then
21+
echo 'Missing -c (helm chart)' >&2
22+
exit 1
23+
fi
24+
25+
# Extract image names and tags
26+
images=$(helm template $HELM_CHART | grep -oE 'image:[[:space:]]*[^[:space:]]+' | sed 's/image:[[:space:]]*//')
27+
# Remove duplicates
28+
images=$(echo "$images" | tr ' ' '\n' | sort -u | tr '\n' ' ')
29+
30+
create_repo(){
31+
#Checking if registry creation necessary
32+
repo_id=$(oci artifacts container repository list --compartment-id "$COMPARTMENT_ID" --display-name "$1" --limit 1 --query data.items[0].id --raw-output);
33+
# Create repo if it does not exist!
34+
[ -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);
35+
# If repo_id is still empty, an error has occurred
36+
[ -z "$repo_id" ] && exit 1
37+
}
38+
39+
for image in $images; do
40+
41+
# Remove quotes and double quotes
42+
image=${image//[\"\']/}
43+
44+
echo "Processing image: $image"
45+
base_image="${image#*/}"
46+
image_name="${base_image%%:*}"
47+
image_tag="${base_image#*:}"
48+
49+
REPO_NAMESPACE=$(oci artifacts container configuration get --compartment-id "$COMPARTMENT_ID" --query data.namespace --raw-output);
50+
51+
create_repo "$image_name"
52+
53+
TOKEN=$(oci raw-request --http-method GET --target-uri "https://${REMOTE_REGISTRY}/20180419/docker/token" | jq -r .data.token);
54+
55+
skopeo copy "docker://$image" "docker://$REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag" --multi-arch system --override-os "linux" --dest-creds BEARER_TOKEN:$TOKEN
56+
57+
echo "Mirrored $image to $REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag"
58+
59+
done
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# RUN IN CLOUD SHELL!!!!
4+
5+
REMOTE_REGISTRY="lhr.ocir.io" # Replace it with your own regional registry
6+
CREATE_OCIR_PUBLIC_REGISTRY=true
7+
COMPARTMENT_ID="ocid1.compartment.oc1..." # Compartment where images will be created
8+
9+
unset -v IMAGES;
10+
11+
while getopts i: flag
12+
do
13+
case "${flag}" in
14+
i) IMAGES=${OPTARG};;
15+
*) echo 'Error in command line parsing' >&2
16+
exit 1
17+
esac
18+
done
19+
20+
if [ -z "$IMAGES" ]; then
21+
echo 'Missing -i (images array with comma separator)' >&2
22+
exit 1
23+
fi
24+
25+
# Extract image names and tags
26+
IMAGES=(${IMAGES//,/ })
27+
# Remove duplicates
28+
IMAGES=$(echo "$IMAGES" | tr ' ' '\n' | sort -u | tr '\n' ' ')
29+
30+
create_repo(){
31+
#Checking if registry creation necessary
32+
repo_id=$(oci artifacts container repository list --compartment-id "$COMPARTMENT_ID" --display-name "$1" --limit 1 --query data.items[0].id --raw-output);
33+
# Create repo if it does not exist!
34+
[ -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);
35+
# If repo_id is still empty, an error has occurred
36+
[ -z "$repo_id" ] && exit 1
37+
}
38+
39+
for image in $IMAGES; do
40+
41+
# Remove quotes and double quotes
42+
image=${image//[\"\']/}
43+
44+
echo "Processing image: $image"
45+
base_image="${image#*/}"
46+
image_name="${base_image%%:*}"
47+
image_tag="${base_image#*:}"
48+
49+
REPO_NAMESPACE=$(oci artifacts container configuration get --compartment-id "$COMPARTMENT_ID" --query data.namespace --raw-output);
50+
51+
create_repo "$image_name"
52+
53+
TOKEN=$(oci raw-request --http-method GET --target-uri "https://${REMOTE_REGISTRY}/20180419/docker/token" | jq -r .data.token);
54+
55+
skopeo copy "docker://$image" "docker://$REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag" --multi-arch system --override-os "linux" --dest-creds BEARER_TOKEN:$TOKEN
56+
57+
echo "Mirrored $image to $REMOTE_REGISTRY/$REPO_NAMESPACE/$image_name:$image_tag"
58+
59+
done

0 commit comments

Comments
 (0)