1+ #! /usr/bin/env bash
2+ # Copyright 2025 The kpt and Nephio Authors
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+
16+ # modify-gitea-test-blueprints.sh - Manage kpt packages in test-blueprints.bundle
17+
18+ # Stricter error handling
19+ set -e # Exit on error
20+ set -u # Must predefine variables
21+ set -o pipefail # Check errors in piped commands
22+
23+ SCRIPT_DIR=" $( dirname " $( readlink -f " $0 " ) " ) "
24+ BUNDLE_PATH=" $SCRIPT_DIR /../test/pkgs/test-pkgs/test-blueprints.bundle"
25+
26+ # Initialize bundle workspace
27+ init_bundle () {
28+ TEMP_DIR=$( mktemp -d)
29+ trap " rm -rf $TEMP_DIR " EXIT
30+ git clone " $BUNDLE_PATH " " $TEMP_DIR /repo"
31+ cd " $TEMP_DIR /repo"
32+ git config user.name " e2e Test User"
33+ git config user.email " e2etest@example.com"
34+ }
35+
36+ # Check if package or tag exists
37+ check_exists () {
38+ local package_name=" $1 "
39+ local tag=" $2 "
40+
41+ if [ -d " $package_name " ]; then
42+ echo " Error: Package '$package_name ' already exists"
43+ exit 1
44+ fi
45+
46+ if [ -n " $tag " ] && git tag -l | grep -q " ^$tag $" ; then
47+ echo " Error: Tag '$tag ' already exists"
48+ exit 1
49+ fi
50+ }
51+
52+ # Pull kpt package from repo or local dir
53+ pull_kpt_package () {
54+ local source=" $1 "
55+ local kpt_ref=" $2 "
56+ local kpt_dir=" $3 "
57+ local package_name=" $4 "
58+
59+ cd " $TEMP_DIR /repo"
60+
61+ if [ -d " $source " ]; then
62+ # Local directory
63+ echo " Copying kpt package from local directory $source "
64+ if [ -n " $kpt_dir " ]; then
65+ cp -r " $source /$kpt_dir " " $package_name "
66+ else
67+ cp -r " $source " " $package_name "
68+ fi
69+ else
70+ # Git repository - use kpt pkg get
71+ # Check if kpt is installed
72+ if ! command -v kpt > /dev/null 2>&1 ; then
73+ echo " Error: kpt is not installed or not in PATH"
74+ echo " Please install kpt: https://kpt.dev/installation/"
75+ exit 1
76+ fi
77+
78+ echo " Pulling kpt package from $source @$kpt_ref "
79+
80+ # Build kpt pkg get URL
81+ local kpt_url=" $source "
82+ if [ -n " $kpt_dir " ]; then
83+ kpt_url=" $source /$kpt_dir "
84+ fi
85+ if [ " $kpt_ref " != " main" ]; then
86+ kpt_url=" $kpt_url @$kpt_ref "
87+ fi
88+
89+ kpt pkg get " $kpt_url " " $package_name "
90+ fi
91+
92+ # Update Kptfile name
93+ if [ -f " $package_name /Kptfile" ]; then
94+ sed -i " s/name: .*/name: $package_name /" " $package_name /Kptfile"
95+ fi
96+ }
97+
98+ # Commit and tag package
99+ commit_package () {
100+ local package_name=" $1 "
101+ local source=" $2 "
102+ local kpt_ref=" $3 "
103+ local tag=" $4 "
104+
105+ git add .
106+ if [ -d " $source " ]; then
107+ git commit -m " Add pkg $package_name from local"
108+ else
109+ git commit -m " Add pkg $package_name from $source @$kpt_ref "
110+ fi
111+
112+ if [ -n " $tag " ]; then
113+ git tag " $tag "
114+ fi
115+ }
116+
117+
118+
119+ # Read bundle contents
120+ read_bundle () {
121+ local temp_dir=$( mktemp -d)
122+ trap " rm -rf $temp_dir " EXIT
123+
124+ git clone " $BUNDLE_PATH " " $temp_dir /repo"
125+ cd " $temp_dir /repo"
126+
127+ echo " "
128+ echo " Bundle contents:"
129+ git --no-pager log --oneline --all
130+ echo " "
131+ echo " Packages:"
132+ ls -la
133+ echo " "
134+ echo " Tags:"
135+ git --no-pager tag -l
136+ }
137+
138+ # Revert last commit
139+ revert_bundle () {
140+ local temp_dir=$( mktemp -d)
141+ trap " rm -rf $temp_dir " EXIT
142+
143+ git clone " $BUNDLE_PATH " " $temp_dir /repo"
144+ cd " $temp_dir /repo"
145+
146+ echo " Last commit:"
147+ git --no-pager log -1 --oneline
148+
149+ # Get tags and files before reverting
150+ local tags_to_delete=$( git tag --points-at HEAD)
151+ local files_removed=$( git --no-pager show --name-only --format=" " HEAD)
152+
153+ git reset --hard HEAD~1
154+
155+ # Delete tags that were pointing to the reverted commit
156+ if [ -n " $tags_to_delete " ]; then
157+ echo " $tags_to_delete " | xargs -r git tag -d
158+ fi
159+
160+ git bundle create " $BUNDLE_PATH " --all
161+
162+ echo " Reverted last commit from bundle"
163+ echo " "
164+ echo " Removed files:"
165+ echo " $files_removed "
166+ if [ -n " $tags_to_delete " ]; then
167+ echo " "
168+ echo " Removed tags:"
169+ echo " $tags_to_delete "
170+ fi
171+ }
172+
173+ # Main function
174+ add_kpt_package () {
175+ if [ -z " $PACKAGE_NAME " ] || [ -z " $SOURCE " ]; then
176+ echo " Error: --name and --source are required"
177+ return 1
178+ fi
179+
180+ init_bundle
181+ check_exists " $PACKAGE_NAME " " $TAG "
182+ pull_kpt_package " $SOURCE " " $KPT_REF " " $KPT_DIR " " $PACKAGE_NAME "
183+ commit_package " $PACKAGE_NAME " " $SOURCE " " $KPT_REF " " $TAG "
184+ git bundle create " $BUNDLE_PATH " --all
185+
186+ read_bundle
187+ if [ -d " $SOURCE " ]; then
188+ echo " Added pkg $PACKAGE_NAME to test-blueprints bundle from local directory $SOURCE "
189+ else
190+ echo " Added pkg $PACKAGE_NAME to test-blueprints bundle from $SOURCE "
191+ fi
192+ }
193+
194+ # Reload gitea test-blueprints repo
195+ reload_gitea () {
196+ if ! command -v kubectl > /dev/null 2>&1 ; then
197+ echo " Error: kubectl is not installed or not in PATH"
198+ return 1
199+ fi
200+
201+ if ! kubectl get pods -l app=gitea -n gitea > /dev/null 2>&1 ; then
202+ echo " Error: Gitea is not running"
203+ return 1
204+ fi
205+
206+ echo " Reloading gitea test-blueprints repo..."
207+ " $SCRIPT_DIR /install-dev-gitea-setup.sh" reload
208+ }
209+
210+ # Parse flags
211+ parse_flags () {
212+ PACKAGE_NAME=" "
213+ SOURCE=" "
214+ KPT_REF=" main"
215+ KPT_DIR=" "
216+ TAG=" "
217+
218+ while [[ $# -gt 0 ]]; do
219+ case $1 in
220+ -n|--name)
221+ PACKAGE_NAME=" $2 "
222+ shift 2
223+ ;;
224+ -s|--source)
225+ SOURCE=" $2 "
226+ shift 2
227+ ;;
228+ -r|--ref)
229+ KPT_REF=" $2 "
230+ shift 2
231+ ;;
232+ -d|--dir)
233+ KPT_DIR=" $2 "
234+ shift 2
235+ ;;
236+ -t|--tag)
237+ TAG=" $2 "
238+ shift 2
239+ ;;
240+ * )
241+ echo " Unknown option $1 "
242+ exit 1
243+ ;;
244+ esac
245+ done
246+ }
247+
248+ # Run if called directly
249+ if [ " ${BASH_SOURCE[0]} " == " ${0} " ]; then
250+ if [ " $1 " == " read" ]; then
251+ read_bundle
252+ elif [ " $1 " == " revert" ]; then
253+ revert_bundle
254+ elif [ " $1 " == " reload" ]; then
255+ reload_gitea
256+ elif [ " $1 " == " -h" ] || [ " $1 " == " --help" ] || [ $# -eq 0 ]; then
257+ echo " Usage: $0 -n <name> -s <source> [-r <ref>] [-d <dir>] [-t <tag>]"
258+ echo " $0 read"
259+ echo " $0 revert"
260+ echo " $0 reload"
261+ echo " "
262+ echo " Options:"
263+ echo " -n, --name Package name in bundle (required)"
264+ echo " -s, --source Git repository URL or local directory path (required)"
265+ echo " -r, --ref Git ref (branch/tag, default: main)"
266+ echo " -d, --dir Subdirectory in source (optional)"
267+ echo " -t, --tag Git tag to create (optional)"
268+ echo " "
269+ echo " Commands:"
270+ echo " read Show bundle contents"
271+ echo " revert Undo last commit"
272+ echo " reload Reload gitea test-blueprints repo"
273+ echo " "
274+ echo " Examples:"
275+ echo " # Add bucket package from GCP blueprints"
276+ echo " $0 -n bucket -s https://github.com/GoogleCloudPlatform/blueprints.git -r bucket-blueprint-v0.4.3 -d catalog/bucket -t bucket/bucket-blueprint-v0.4.3"
277+ echo " "
278+ echo " # Add package from local directory"
279+ echo " $0 -n bucket -s ~/Downloads/bucket-copy/ -t bucket/bucket-blueprint-v0.4.3"
280+ echo " "
281+ echo " # Show bundle contents"
282+ echo " $0 read"
283+ echo " "
284+ echo " # Revert last change"
285+ echo " $0 revert"
286+ echo " "
287+ echo " # Reload gitea repo"
288+ echo " $0 reload"
289+ exit 1
290+ else
291+ parse_flags " $@ "
292+ add_kpt_package
293+ fi
294+ fi
0 commit comments