forked from redhat-developer/devspaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterIIB.sh
executable file
·145 lines (129 loc) · 6.56 KB
/
filterIIB.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
#!/bin/bash
#
# Copyright (c) 2022-2023 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Utility script to filter the Dev Spaces, Web Terminal, DevWorkspace (and optionally CodeReady Workspaces) operators
# from an IIB image. Creates a file tree of json files, one folder per filtered package.
# OPM from 4.12 (>v1.26.3 upstream version) is required to run buildCatalog.sh (CRW-4192, OCPBUGS-11841)
#
usage() {
cat <<EOF
Filter an IIB operator catalog to extract bundles, channel, and packages for specific
operator(s). By default files are output to ./olm-catalog, unless the --dir option is
specified.
Requires:
* jq 1.6+, podman 2.0+, glibc 2.28+
* opm v1.26.3+ (see https://docs.openshift.com/container-platform/4.12/cli_reference/opm/cli-opm-install.html#cli-opm-install )
Usage: $0 [OPTIONS]
Options:
-s, --iib <source_index> : Source registry, org, index image and tag from which to pull operators. Required.
--channel-all <channel_name> : Target channel to use when publishing all operators. If unspecified, channels from IIB are used
--packages '<package1> <package2>' : Space separated list of packages to filter and include in target image. If
unspecified, default is 'devworkspace-operator devspaces web-terminal'
--dir <directory> : Output files to <directory>/olm-catalog instead of ./olm-catalog
-v, --verbose : Verbose output: include additional information
-h, --help : Show this help
Examples:
* Filter IIB 226720 for devspaces, devworkspace, and web-terminal operators
$0 -s registry-proxy.engineering.redhat.com/rh-osbs/iib:226720
* Filter IIB 226720 for only devspaces and web-terminal operators
$0 -s registry-proxy.engineering.redhat.com/rh-osbs/iib:226720 --packages 'devspaces web-terminal'
EOF
}
VERBOSE=0
PACKAGES='devworkspace-operator devspaces web-terminal'
WORKING_DIR='./'
while [[ "$#" -gt 0 ]]; do
case $1 in
'-s'|'--iib') sourceIndexImage="$2"; shift 1;;
'--channel-all') targetChannelAll="$2"; shift 1;;
'--packages') PACKAGES="$2"; shift 1;;
'--dir') WORKING_DIR="$2"; shift 1;;
'-v'|'--verbose') VERBOSE=1;;
'-h'|'--help') usage;;
*) echo "Unknown parameter used: $1."; usage; exit 1;;
esac
shift 1
done
if [[ $VERBOSEFLAG == "-v" ]]; then echo "[DEBUG] Working in $WORKING_DIR"; fi
pushd "$WORKING_DIR" > /dev/null || exit
trap 'popd >> /dev/null' EXIT
# install opm if not installed by ansible https://gitlab.cee.redhat.com/codeready-workspaces/ansible-scripts/-/blob/master/roles/users/tasks/profile-hudson/main.yml#L107 to /usr/local/bin/opm
if [[ ! -x /usr/local/bin/opm ]] && [[ ! -x ${HOME}/.local/bin/opm ]]; then
pushd /tmp >/dev/null || exit
echo "[INFO] Installing latest opm from https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/clients/ocp/latest-4.12/opm-linux.tar.gz ..."
# shellcheck disable=SC2046
curl -sSLo- https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/clients/ocp/latest-4.12/opm-linux.tar.gz | tar xz; chmod 755 opm
sudo cp opm /usr/local/bin/ || cp opm "${HOME}"/.local/bin/
sudo chmod 755 /usr/local/bin/opm || chmod 755 "${HOME}"/.local/bin/opm
if [[ ! -x /usr/local/bin/opm ]] && [[ ! -x ${HOME}/.local/bin/opm ]]; then
echo "[ERROR] Could not install opm v1.26.3 or higher (see https://docs.openshift.com/container-platform/4.12/cli_reference/opm/cli-opm-install.html#cli-opm-install )";
exit 1
fi
popd >/dev/null || exit
fi
PODMAN=$(command -v podman)
if [[ ! -x $PODMAN ]]; then echo "[ERROR] podman is not installed. Aborting."; echo; usage; exit 1; fi
command -v jq >/dev/null 2>&1 || which jq >/dev/null 2>&1 || { echo "jq is not installed. Aborting."; exit 1; }
# shellcheck disable=SC2086
if [ -z $sourceIndexImage ]; then echo "IIB image required"; echo; usage; exit 1; fi
if [ -f ./render.json ]; then rm -f ./render.json; fi
# CRW-3463 use --skip-tls-verify as workaround for "certificate signed by unknown authority"
if [[ $VERBOSE -eq 1 ]]; then
echo "[DEBUG] Rendering 'opm --skip-tls-verify render $sourceIndexImage > render.json'"
echo "[DEBUG] This may take several minutes."
time opm --skip-tls-verify render "$sourceIndexImage" > render.json
else
opm --skip-tls-verify render "$sourceIndexImage" > render.json
fi
# ls -la render.json
mkdir -p "./olm-catalog"
for PACKAGE in ${PACKAGES}; do
if [[ $VERBOSE -eq 1 ]]; then echo "Filtering render.json for package $PACKAGE"; fi
if [ -d "./olm-catalog/$PACKAGE" ]; then echo "Package $PACKAGE already in filtered catalog; aborting"; exit 1; fi
mkdir -p "./olm-catalog/$PACKAGE"
jq --arg PACKAGE "$PACKAGE" 'select(.schema == "olm.package") | select(.name == $PACKAGE)' render.json > "./olm-catalog/$PACKAGE/package.json"
if [ ! -s "./olm-catalog/$PACKAGE/package.json" ]; then
echo "Could not find package $PACKAGE in IIB; aborting"
rm -rf "./olm-catalog/$PACKAGE/"
exit 1
fi
jq --arg PACKAGE "$PACKAGE" 'select(.schema == "olm.channel") | select(.package == $PACKAGE)' render.json > "./olm-catalog/$PACKAGE/channel.json"
for BUNDLE in $(jq -r --arg PACKAGE "$PACKAGE" 'select(.package == $PACKAGE) | select(.schema == "olm.bundle") | .name' render.json); do
echo "extracting bundle $BUNDLE"
# shellcheck disable=SC2086
jq --arg BUNDLE $BUNDLE 'select(.name == $BUNDLE) | select(.schema == "olm.bundle")' render.json > "./olm-catalog/$PACKAGE/$BUNDLE.bundle.json"
done
done
replaceField()
{
theFile="$1"
updateName="$2"
updateVal="$3"
# echo " ${0##*/} rF :: * ${updateName}: ${updateVal}"
# shellcheck disable=SC2016 disable=SC2002 disable=SC2086
changed=$(jq --arg updateName "${updateName}" --arg updateVal "${updateVal}" ${updateName}' = $updateVal' "${theFile}")
echo "${changed}" > "${theFile}"
}
replaceChannelName()
{
if [[ -d ${1} ]]; then
replaceField "${1}/channel.json" ".name" "${2}"
replaceField "${1}/package.json" ".defaultChannel" "${2}"
fi
}
# optionally, override the channels from the IIBs with a targetChannel (for all operators or for the devspaces operator only)
# olm-catalog/devspaces/channel.json # "name": "stable"
# olm-catalog/devspaces/package.json # "defaultChannel": "stable"
pushd olm-catalog/ >/dev/null || exit
if [[ -n $targetChannelAll ]]; then
for d in devspaces web-terminal devworkspace-operator codeready-workspaces; do
replaceChannelName "${d}" "$targetChannelAll"
done
fi
popd >/dev/null || exit