Skip to content

Instantly share code, notes, and snippets.

@tsuyo
Last active May 11, 2021 09:55
Show Gist options
  • Save tsuyo/988cc28d1e77c2d95822ee460c940ab6 to your computer and use it in GitHub Desktop.
Save tsuyo/988cc28d1e77c2d95822ee460c940ab6 to your computer and use it in GitHub Desktop.
Rename a JFrog Artifactory repository
#!/bin/bash
# Rename a JFrog Artifactory repository
# See https://jfrog.com/knowledge-base/is-there-a-way-to-rename-a-repository/ for details
usageAndExit() {
echo "Usage: "
echo " $ export ARTIFACTORY_USER=..."
echo " $ export ARTIFACTORY_PASS=..."
echo " $ $0 -u <ARTIFACTORY_URL> [-d] <OLD_REPO> <NEW_REPO>"
echo " ARTIFACTORY_URL format is like \"https://xxx.jfrog.io/artifactory\""
echo " If you want to delete the old_repo, add -d flag"
echo " Otherwise the old_repo will be kept and not deleted"
exit 1
}
cleanup() {
\rm old-repo-config.json new-repo-config.json log-update.json log-move.json
}
OPTIND=1 # Reset in case getopts has been used previously in the shell
while getopts "u:d" opt; do
case $opt in
u)
ARTIFACTORY_URL=$OPTARG
;;
d)
DELETE_OLD=true
;;
\?)
usageAndExit
;;
:)
usageAndExit
;;
esac
done
shift $((OPTIND-1))
[ -z "${ARTIFACTORY_URL}" ] && usageAndExit
OLD_REPO=$1
NEW_REPO=$2
[ -z "${OLD_REPO}" ] && usageAndExit
[ -z "${NEW_REPO}" ] && usageAndExit
# echo $ARTIFACTORY_URL $OLD_REPO $NEW_REPO $DELETE_OLD
echo "1. get ${OLD_REPO} config"
curl -s -X GET -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASS} \
${ARTIFACTORY_URL}/api/repositories/${OLD_REPO} > old-repo-config.json
err=$(jq ".errors[].status" old-repo-config.json 2>/dev/null)
[ -n "${err}" ] && echo "Error: check if user/password, ${OLD_REPO} exists etc." && exit 1
echo "2. create or update ${NEW_REPO}"
sed -E "s/\"key\" : \"${OLD_REPO}\"/\"key\" : \"${NEW_REPO}\"/" old-repo-config.json > new-repo-config.json
# create a new repo. if it exists, just ignore the error
curl -s -X PUT -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASS} -d@new-repo-config.json \
-H "Content-Type: application/json" \
${ARTIFACTORY_URL}/api/repositories/${NEW_REPO} > /dev/null
curl -s -X POST -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASS} -d@new-repo-config.json \
-H "Content-Type: application/json" \
${ARTIFACTORY_URL}/api/repositories/${NEW_REPO} > log-update.json
msg_update=$(cat log-update.json | grep "successfully")
[ -z "${msg_update}" ] && echo "Error" && exit 1
echo "3. move contents from ${OLD_REPO} to ${NEW_REPO}"
curl -s -X POST -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASS} \
${ARTIFACTORY_URL}/api/move/${OLD_REPO}?to=/${NEW_REPO} > log-move.json
msg_move=$(jq ".messages[].message" log-move.json 2>/dev/null | grep "successfully")
[ -z "${msg_move}" ] && echo "Error" && exit 1
if [ "$DELETE_OLD" ]; then
echo "4. delete ${OLD_REPO}"
curl -X DELETE -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASS} \
${ARTIFACTORY_URL}/api/repositories/${OLD_REPO}
fi
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment