Skip to content

Instantly share code, notes, and snippets.

@joepvd
Last active December 10, 2023 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepvd/17d215004c5f35d957eda355007542b9 to your computer and use it in GitHub Desktop.
Save joepvd/17d215004c5f35d957eda355007542b9 to your computer and use it in GitHub Desktop.
ocp-urls: Spits out GitHub Dockerfiles from image configurations from ocp-build-data

ocp-urls

Spits out urls from image configurations from ocp-build-data files given as arguments.

Usage

ocp-urls <options> <files>

Options

  • -h, --help This help
  • --raw, raw link to raw file on raw.githubusercontent.com
  • --repo, repo, r Link to a directory to browse
  • --file, f, file File to download. Defaults to Dockerfile. Can be anything, like go.mod or .ci-operator.yml
  • --download, dl, download Spit out curl invocation to download

Examples

ocp-urls $(git grep -l openshift-enterprise-cli) | open-urls

open-urls opens a bunch of links to Dockerfiles in a new browser window: https://gist.github.com/joepvd/ec2c9bd9e4cd8cdf8bdcda3eadf200fc

ocp-urls --download -f .ci-operator.yaml 4.1[345]/images/ose-etcd-operator.yml | sh

...leaves these files: ose-etcd_4.13_.ci-operator.yaml ose-etcd_4.14_.ci-operator.yaml ose-etcd_4.15_.ci-operator.yaml

Installation

git clone https://gist.github.com/joepvd/17d215004c5f35d957eda355007542b9 ocp-urls
ln -sf $(pwd)/ocp-urls/ocp-urls ~/bin/ocp-urls

TODO

  • Error on nonsensical combinations of
  • Add support for distgit urls
#!/usr/bin/env -S yq -rf
.content.source
| ( .git.branch.target
| sub("{MAJOR}"; $major)
| sub("{MINOR}"; $minor)
) as $branch
| ( if has("path")
then .path + "/"
else "" end
) as $path
| ( if has("dockerfile")
then .dockerfile
else "Dockerfile" end
) as $dockerfile
| ( if ($ARGS.named.file == "Dockerfile") then
@text "\($path)\($dockerfile)"
else
$ARGS.named.file
end
) as $repo_file
| ( if ($ARGS.named.raw == "true" or $ARGS.named.download == "true")
then .git.web | sub("github.com"; "raw.githubusercontent.com")
else .git.web
end
) as $gh_host
| (
$ARGS.named.file
| gsub("/"; "_")
| @text "\($ARGS.named.dg_name)_\($ARGS.named.major).\($ARGS.named.minor)_\(.)"
) as $dl_file
| if ($ARGS.named.download == "true")
then @text "curl -sSL \($gh_host)/\($branch)/\($repo_file) >\($dl_file)"
elif ($ARGS.named.repo == "false")
then @text "\($gh_host)/\(if ($raw == "true") then "" else "blob/" end)\($branch)/\($repo_file)"
elif ($ARGS.named.repo == "true")
then @text "\($gh_host)/tree/\($branch)/"
else {
"major": $ARGS.named.major,
"minor": $ARGS.named.minor,
"github": $ARGS.named.github,
"repo": $ARGS.named.repo,
"raw": $ARGS.named.raw,
"dg_name": $ARGS.named.dg_name,
"download": $ARGS.named.download
} | @text "Ran with args: \(.)"
end
content:
source:
dockerfile: Dockerfile.rhel7
git:
branch:
target: release-{MAJOR}.{MINOR}
url: git@github.com:openshift-priv/cluster-etcd-operator.git
web: https://github.com/openshift/cluster-etcd-operator
ci_alignment:
streams_prs:
ci_build_root:
stream: rhel-9-golang-ci-build-root
pkg_managers:
- gomod
distgit:
branch: rhaos-{MAJOR}.{MINOR}-rhel-9
component: cluster-etcd-operator-container
enabled_repos:
- rhel-9-baseos-rpms
- rhel-9-appstream-rpms
for_payload: true
from:
builder:
- stream: rhel-9-golang
member: openshift-enterprise-base-rhel9
name: openshift/ose-cluster-etcd-rhel9-operator
payload_name: cluster-etcd-operator
owners:
- sbatsche@redhat.com
#!/usr/bin/env bash
#% # ocp-urls
#% Spits out urls from image configurations from ocp-build-data
#% files given as arguments.
#%
#% ## Usage:
#% ocp-urls <options> <files>
#%
#% ## Options:
#% -h --help: This help
#% --raw raw: link to raw file on raw.githubusercontent.com
#% --repo repo r: Link to a directory to browse
#% --file f file: File to download. Defaults to Dockerfile.
#% Can be anything, like go.mod or .ci-operator.yml
#% --download dl download: Spit out curl invocation to download
#%
#% ## Examples:
#% ocp-urls $(git grep -l openshift-enterprise-cli) | open-urls
#% open-urls opens a bunch of links to Dockerfiles in a new browser window:
#% https://gist.github.com/joepvd/ec2c9bd9e4cd8cdf8bdcda3eadf200fc
#%
#% ocp-urls --download -f .ci-operator.yaml 4.1[345]/images/ose-etcd-operator.yml | sh
#% ...leaves thes files: ose-etcd_4.13_.ci-operator.yaml ose-etcd_4.14_.ci-operator.yaml ose-etcd_4.15_.ci-operator.yaml
#%
#% ## Installation
#% git clone https://gist.github.com/joepvd/17d215004c5f35d957eda355007542b9 ocp-urls
#% ln -sf $(pwd)/ocp-urls/ocp-urls ~/bin/ocp-urls
#%
#% TODO:
#% - Error on nonsensical combinations of
#% - Add support for distgit urls
help() {
sed -n 's/^#% //p' "${BASH_SOURCE[0]}"
}
main() {
declare -A cfg # associative array of config
declare -a files # Normal array of files
parse_args "$@"
run
}
parse_args() {
# set defaults
cfg[github]=true
cfg[raw]=false
cfg[file]=Dockerfile
cfg[repo]=false
cfg[download]=false
cfg[dg_name]=unknown
while (( $# > 0 )); do
case "$1" in
-h|help|--help)
help
exit 0
;;
repo|--repo|-r)
cfg[repo]=true
shift
;;
file|--file|-f)
cfg[file]="$2"
shift
shift
;;
raw|--raw)
cfg[raw]=true
shift
;;
download|dl|-d|dl|--download)
cfg[download]=true
cfg[raw]=true
cfg[repo]=false
shift
;;
*)
files+=("$1")
shift
;;
esac
done
}
compile_cfg() {
printf -- '--arg %s %s ' $(
for k in ${!cfg[@]}; do
echo $k ${cfg[$k]}
done
)
}
run() {
previous_group_yml=""
for f in "${files[@]}"; do
group_yml="$(dirname $(realpath $f))/../group.yml"
if [[ $previous_group_yml != "$group_yml" ]]; then
group_cfg="$(yq -r '.vars | @text "--arg major \(.MAJOR) --arg minor \(.MINOR)"' $group_yml)"
fi
previous_group="$group"
dg="${f%.yml}"
cfg[dg_name]="${dg##*/}"
if [[ ${cfg[github]} == true ]]; then
jq_prog="$(dirname "$(readlink "${BASH_SOURCE[0]}")")/gh-url-parser.jq"
fi
# echo yq -r $group_cfg $(compile_cfg) -f "$jq_prog" "$f"
yq -r $group_cfg $(compile_cfg) -f "$jq_prog" "$f"
done
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
#!/usr/bin/env bash
compile_cfg() {
printf -- '--arg %s %s ' $( for k in ${!cfg[@]}; do echo $k ${cfg[$k]}; done)
}
main() {
good=0
bad=0
for fun in $(declare -F | awk '$NF ~ /^test_/ {print $NF}'); do
$fun
done
echo "$((good)) out of $((good + bad)) tests succeeded"
if (( bad > 0 )); then exit 1; fi
}
test_github_dockerfile() {
make_cfg
run image1.yml https://github.com/openshift/cluster-etcd-operator/blob/release-4.16/Dockerfile.rhel7
}
test_github_raw_dockerfile() {
make_cfg
cfg[raw]=true
run image1.yml https://raw.githubusercontent.com/openshift/cluster-etcd-operator/release-4.16/Dockerfile.rhel7
}
test_github_random_file() {
make_cfg
cfg[file]=.ci-operator.yaml
run image1.yml https://github.com/openshift/cluster-etcd-operator/blob/release-4.16/.ci-operator.yaml
}
test_github_raw_random_file() {
make_cfg
cfg[file]=.ci-operator.yaml
cfg[raw]=true
run image1.yml https://raw.githubusercontent.com/openshift/cluster-etcd-operator/release-4.16/.ci-operator.yaml
}
test_github_repo() {
make_cfg
cfg[repo]=true
run image1.yml https://github.com/openshift/cluster-etcd-operator/tree/release-4.16/
}
test_download_file() {
make_cfg
cfg[download]=true
run image1.yml 'curl -sSL https://raw.githubusercontent.com/openshift/cluster-etcd-operator/release-4.16/Dockerfile.rhel7 >ose-etcd-operator_4.16_Dockerfile'
}
make_cfg() {
unset cfg
declare -gA cfg
# default config
cfg[repo]=false
cfg[github]=true
cfg[dg_name]=ose-etcd-operator
cfg[download]=false
cfg[raw]=false
cfg[file]=Dockerfile
}
major_minor="--arg major 4 --arg minor 16"
run() {
file="$1"
url="$2"
# echo "yq -r $major_minor $(compile_cfg) -f ../ocp-yaml-parser.jq $file"
result="$(yq -r $major_minor $(compile_cfg) -f ../gh-url-parser.jq $file)"
if [[ "$result" == "$url" ]]; then
echo "✓ $fun"
: $((good++))
else
echo "✗ $fun: $result != $url"
: $((bad++))
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment