-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmanage-pkg.sh
executable file
·251 lines (223 loc) · 7.56 KB
/
manage-pkg.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env bash
# Load environment variables from .env file
if [ -f .env ]; then
source .env
fi
# Help function to show usage
usage() {
echo "Usage: $0 [-R] [-r <repo> [-l] [-d [<version>]] [-v <version>] [-o <output_dir>] [-u]]"
echo " -R Make cabot release zip"
echo " -r <repo> Specify a GitHub repository (e.g., user/repo)"
echo " -c Check what version if used"
echo " -l List all releases and attachments"
echo " -v <version> Check if the specified version is available and list its assets"
echo " -d Download all assets for the specified version or latest if no version is given"
echo " -o <output_dir> Specify an output directory for downloaded files (default: CABOT_SITE_PKG_DIR or ./cabot-navigation/cabot_site_pkg)"
echo " -u Unzip downloaded zip files"
echo " -p <tag> Pull docker images"
exit 1
}
# Variables
RELEASE=false
REPO=""
CHECK=false
LIST=false
DOWNLOAD=false
VERSION=""
OUTPUT_DIR=${CABOT_SITE_PKG_DIR:-./cabot-navigation/cabot_site_pkg}
UNZIP=false
AUTH_HEADER=()
PULL=
# Check if GITHUB_TOKEN is set
if [ -n "$GITHUB_TOKEN" ]; then
AUTH_HEADER=(-H "Authorization: token $GITHUB_TOKEN")
fi
# Parse options
while getopts "Rr:cldv:o:up:" opt; do
case ${opt} in
R )
RELEASE=true
;;
r )
REPO=${OPTARG}
;;
c )
CHECK=true
;;
l )
LIST=true
;;
d )
DOWNLOAD=true
;;
v )
VERSION=${OPTARG}
;;
o )
OUTPUT_DIR=${OPTARG}
;;
u )
UNZIP=true
;;
p )
PULL=${OPTARG}
;;
* )
usage
;;
esac
done
if [ -n "$PULL" ]; then
export CABOT_LAUNCH_IMAGE_TAG=$PULL
docker compose --profile build pull
exit 0
fi
# Creates a zip file containing only the minimal files required to run cabot with built docker images
# It is intended to be called from GitHub Actions like `./manage-pkg.sh -R -v {{ github.ref_name }}`.
if [ "$RELEASE" = true ]; then
echo "Making cabot release zip file"
if [ -z "VERSION" ]; then
echo "Please specify a version string"
exit 1
fi
tmpdir=$(mktemp -d)
echo "Temporary directory created: $tmpdir"
cabotdir="cabot-${VERSION}"
releasedir="$tmpdir/$cabotdir"
mkdir -p $releasedir
set -f
IGNORE_FILE=".releaseignore"
# Create exclusion list and exception list
patterns=()
if [[ -f "$IGNORE_FILE" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
line=$(echo "$line" | sed -E 's/[[:space:]]*#.*$//')
# echo "$line"
# Ignore empty lines and comments
[[ -z "$line" ]] && continue
patterns+=("$line")
done < "$IGNORE_FILE"
fi
# Remove files that match the exclusion list
filtered_files=()
while read file; do
exclude=false
# Check if it matches the exclusion list
for pattern in "${patterns[@]}"; do
if [[ "$pattern" =~ ^! ]]; then
pattern=${pattern:1}
if [[ "$file" == $pattern ]] || [[ "$file" =~ ^$pattern ]]; then
exclude=false
fi
else
if [[ "$file" == $pattern ]] || [[ "$file" =~ ^$pattern ]]; then
exclude=true
fi
fi
done
# Add if not an exclusion target or included in the exception list
if [[ "$exclude" == false ]]; then
filtered_files+=("$file")
# echo include $file
else
# echo exclude $file
:
fi
done < <(find . -type l -o -type f | sed 's|^\./||' | sort)
# Find and copy files excluding patterns from .releaseignore
for file in ${filtered_files[@]}; do
mkdir -p "$releasedir/$(dirname "$file")"
if [[ "$OSTYPE" == "darwin"* ]]; then
cp -RP "$file" "$releasedir/$file"
else
cp -d "$file" "$releasedir/$file"
fi
done
pushd $tmpdir
zip -r -y $cabotdir.zip $cabotdir
popd
cp $tmpdir/$cabotdir.zip ./
exit 0
fi
# Check if OUTPUT_DIR exists
if [ ! -d "$OUTPUT_DIR" ]; then
echo "Error: Output directory does not exist: $OUTPUT_DIR"
exit 1
fi
# Check if repository is specified
if [ -z "$REPO" ]; then
echo "Error: Repository is required."
usage
fi
if [ "$CHECK" = true ]; then
# check what version is used
echo "Downloading latest release attachments for $REPO..."
ASSETS=$(curl -s "${AUTH_HEADER[@]}" "https://api.github.com/repos/$REPO/releases/latest" | jq -cr '.assets[] | {url: .url, name: .name}')
echo "$ASSETS" | while read -r ASSET; do
NAME=$(echo "$ASSET" | jq -r '.name')
# get version in <version> tag in package.xml
DIR=$(echo "$NAME" | cut -d'-' -f1)
grep "<version>" $OUTPUT_DIR/$DIR/share/$DIR/package.xml | sed -E 's/.*<version>([^<]+)<\/version>.*/\1/'
done | uniq
exit 0
fi
# List releases
if [ "$LIST" = true ]; then
echo "Fetching releases for $REPO..."
curl -s "${AUTH_HEADER[@]}" "https://api.github.com/repos/$REPO/releases" | jq '.[] | {tag_name, assets: .assets[].name}'
exit 0
fi
# Download specified version or latest release
if [ "$DOWNLOAD" = true ]; then
if [ "$VERSION" = "" ]; then
echo "Downloading latest release attachments for $REPO..."
ASSETS=$(curl -s "${AUTH_HEADER[@]}" "https://api.github.com/repos/$REPO/releases/latest" | jq -r '.assets[] | {url: .url, name: .name}')
else
echo "Downloading release $VERSION attachments for $REPO..."
RELEASE=$(curl -s "${AUTH_HEADER[@]}" "https://api.github.com/repos/$REPO/releases/tags/$VERSION")
if echo "$RELEASE" | jq -e 'has("message")' > /dev/null; then
echo "Error: Version $VERSION not found."
exit 1
fi
ASSETS=$(echo "$RELEASE" | jq -c '.assets[] | {url: .url, name: .name}')
fi
echo "$ASSETS" | while read -r ASSET; do
URL=$(echo "$ASSET" | jq -r '.url')
NAME=$(echo "$ASSET" | jq -r '.name')
FILE_PATH="$OUTPUT_DIR/$NAME"
echo "Downloading $NAME to $OUTPUT_DIR..."
curl -L -o "$FILE_PATH" "${AUTH_HEADER[@]}" -H 'Accept: application/octet-stream' "$URL"
# Unzip if the -u option was specified and the file is a zip
if [ "$UNZIP" = true ] && [[ "$FILE_PATH" == *.zip ]]; then
echo "Unzipping $FILE_PATH..."
unzip -o "$FILE_PATH" -d "$OUTPUT_DIR"
fi
done
exit 0
fi
if [ -n "$VERSION" ]; then
echo "Checking if version $VERSION exists for $REPO..."
RELEASE=$(curl -s "${AUTH_HEADER[@]}" "https://api.github.com/repos/$REPO/releases/tags/$VERSION")
if echo "$RELEASE" | jq -e 'has("message")' > /dev/null; then
echo "Error: Version $VERSION not found."
exit 1
else
echo "Version $VERSION is available. Listing assets:"
echo "$RELEASE" | jq '.assets[].name'
fi
ASSETS=$(echo "$RELEASE" | jq -c '.assets[] | {url: .url, name: .name}')
echo "$ASSETS" | while read -r ASSET; do
NAME=$(echo "$ASSET" | jq -r '.name')
FILE_PATH="$OUTPUT_DIR/$NAME"
# Unzip if the -u option was specified and the file is a zip
if [ "$UNZIP" = true ] && [[ "$FILE_PATH" == *.zip ]] && [[ -e "$FILE_PATH" ]]; then
echo "Unzipping $FILE_PATH..."
unzip -o "$FILE_PATH" -d "$OUTPUT_DIR"
else
echo "Asset $NAME is available for download. Use -d to download."
fi
done
exit 0
fi
usage
exit 1