Last active
June 30, 2023 10:32
-
-
Save akexorcist/df4d44ae521f4946063b2df7973effb3 to your computer and use it in GitHub Desktop.
Nexus Artifact Migration for macOS with zsh. In case you need to copy artifact from Nexus to another nexus repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Copy artifact from JFrog to another Nexus | |
# | |
# ## Prerequisites: ## | |
# * macOS | |
# * zsh | |
# * jq | |
# * wget | |
# * curl | |
# * JFrog | |
# * Nexus v3 | |
# | |
# ## Usage ## | |
# ./move_artifact_jfrog_to_nexus.sh \ | |
# -h "<source_jfrog_host>" \ | |
# -u "<source_jfrog_username>" \ | |
# -p "<source_jfrog_password>" \ | |
# -r "<source_jfrog_repository>" \ | |
# -g "<group_id>" \ | |
# -a "<artifact_id>" \ | |
# -v "<artifact_version>" \ | |
# -H "<target_nexus_host>" \ | |
# -U "<target_nexus_username>" \ | |
# -P "<target_nexus_password>" \ | |
# -R "<target_nexus_repository>" | |
# | |
# ## Example ## | |
# ./move_artifact_jfrog_to_nexus.sh \ | |
# -h "jfrog.host1.com" \ | |
# -u "host1_username" \ | |
# -p "host1_password" \ | |
# -r "release" \ | |
# -g "com.akexorcist" \ | |
# -a "sleepingforless" \ | |
# -v "1.0.0" \ | |
# -H "nexus.host2.com" \ | |
# -U "host2_username" \ | |
# -P "host2_password" \ | |
# -R "maven-release" | |
# | |
# ## Special Thanks ## | |
# * https://github.com/metallaw/scripts/blob/master/nexus_migration/nexus_copy_artifacts.sh | |
# * ChatGPT | |
# | |
##################################################################################################### | |
# Define opts | |
while getopts h:u:p:r:g:a:v:H:U:P:R: option | |
do | |
case "${option}" | |
in | |
h) HOST=${OPTARG};; | |
u) USER=${OPTARG};; | |
p) PASSWORD=${OPTARG};; | |
r) REPO=${OPTARG};; | |
g) GROUP_ID=${OPTARG};; | |
a) ARTIFACT_ID=${OPTARG};; | |
v) ARTIFACT_VERSION=${OPTARG};; | |
H) TARGET_HOST=${OPTARG};; | |
U) TARGET_HOST_USER=${OPTARG};; | |
P) TARGET_HOST_PASSWORD=${OPTARG};; | |
R) TARGET_REPO=${OPTARG};; | |
esac | |
done | |
# Get all assets for soecified repository and extract download URL | |
# https://repo.navercorp.com/api/storage/maven-release/com/linecorp/planetkit/planetkit/5.0.11 | |
GROUP_PATH=$(echo "$GROUP_ID" | sed 's/\./\//g') | |
SOURCE_PATH="http://${HOST}/api/storage/${REPO}/${GROUP_PATH}/${ARTIFACT_ID}/${ARTIFACT_VERSION}" | |
curl -X GET -u ${USER}:"${PASSWORD}" "${SOURCE_PATH}" | jq -r --arg SOURCE_PATH "$SOURCE_PATH" '.children| .[]| $SOURCE_PATH + .uri' > temp_metadata_${REPO} | |
declare -p metadata_urls | |
metadata_urls=("${(@f)$(< ./temp_metadata_${REPO})}") | |
for i in "${metadata_urls[@]}" | |
do | |
curl -X GET -u ${USER}:"${PASSWORD}" "${i}" | jq -r --arg SOURCE_PATH "$SOURCE_PATH" '.downloadUri' >> temp_file_${REPO} | |
done | |
rm temp_metadata_${REPO} | |
# Read download urls into array | |
declare -p urls | |
urls=("${(@f)$(< ./temp_file_${REPO})}") | |
# Create temp directory for storing the artifacts - be aware that you got enough free disk space! Of course you can also change the location here ;) | |
mkdir -p ./assets | |
# wget all artifacts from array | |
for i in "${urls[@]}" | |
do | |
wget --cut-dirs=1 -nH -x -P ./assets ${i} | |
done | |
rm temp_file_${REPO} | |
# Upload the artifacts to the target instance | |
cd assets/ | |
find ./ -type f | sed "s#^.//##" | xargs -I '{}' curl -v -u "${TARGET_HOST_USER}:${TARGET_HOST_PASSWORD}" -T {} https://${TARGET_HOST}/repository/${TARGET_REPO}/{}; | |
# Clean up temp directory | |
cd .. | |
rm -rf assets | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Copy artifact from Nexus to another Nexus | |
# | |
# ## Prerequisites: ## | |
# * macOS | |
# * zsh | |
# * jq | |
# * wget | |
# * curl | |
# * Nexus v3 | |
# | |
# ## Usage ## | |
# ./move_artifact_nexus_to_nexus.sh \ | |
# -h "<source_nexus_host>" \ | |
# -u "<source_nexus_username>" \ | |
# -p "<source_nexus_password>" \ | |
# -r "<source_nexus_repository>" \ | |
# -g "<group_id>" \ | |
# -a "<artifact_id>" \ | |
# -v "<artifact_version>" \ | |
# -H "<target_nexus_host>" \ | |
# -U "<target_nexus_username>" \ | |
# -P "<target_nexus_password>" \ | |
# -R "<target_nexus_repository>" | |
# | |
# ## Example ## | |
# ./move_artifact_nexus_to_nexus.sh \ | |
# -h "nexus.host1.com" \ | |
# -u "host1_username" \ | |
# -p "host1_password" \ | |
# -r "release" \ | |
# -g "com.akexorcist" \ | |
# -a "sleepingforless" \ | |
# -v "1.0.0" \ | |
# -H "nexus.host2.com" \ | |
# -U "host2_username" \ | |
# -P "host2_password" \ | |
# -R "maven-release" | |
# | |
# ## Special Thanks ## | |
# * https://github.com/metallaw/scripts/blob/master/nexus_migration/nexus_copy_artifacts.sh | |
# * ChatGPT | |
# | |
##################################################################################################### | |
# Define opts | |
while getopts h:u:p:r:g:a:v:H:U:P:R: option | |
do | |
case "${option}" | |
in | |
h) HOST=${OPTARG};; | |
u) USER=${OPTARG};; | |
p) PASSWORD=${OPTARG};; | |
r) REPO=${OPTARG};; | |
g) GROUP_ID=${OPTARG};; | |
a) ARTIFACT_ID=${OPTARG};; | |
v) ARTIFACT_VERSION=${OPTARG};; | |
H) TARGET_HOST=${OPTARG};; | |
U) TARGET_HOST_USER=${OPTARG};; | |
P) TARGET_HOST_PASSWORD=${OPTARG};; | |
R) TARGET_REPO=${OPTARG};; | |
esac | |
done | |
# Get all assets for soecified repository and extract download URL | |
curl -X GET -u ${USER}:"${PASSWORD}" "https://${HOST}/service/rest/v1/search/assets?repository=${REPO}&maven.groupId=${GROUP_ID}&maven.artifactId=${ARTIFACT_ID}&maven.baseVersion=${ARTIFACT_VERSION}" | jq -r '.items| .[]| .downloadUrl' > temp_${REPO} | |
# Read download urls into array | |
declare -p urls | |
urls=("${(@f)$(< ./temp_${REPO})}") | |
# Create temp directory for storing the artifacts - be aware that you got enough free disk space! Of course you can also change the location here ;) | |
mkdir -p ./assets | |
# wget all artifacts from array | |
for i in "${urls[@]}" | |
do | |
wget --cut-dirs=2 -nH -x -P ./assets ${i} | |
done | |
rm temp_${REPO} | |
# Upload the artifacts to the target instance | |
cd assets/ | |
find ./ -type f | sed "s#^.//##" | xargs -I '{}' curl -v -u "${TARGET_HOST_USER}:${TARGET_HOST_PASSWORD}" -T {} https://${TARGET_HOST}/repository/${TARGET_REPO}/{}; | |
# Clean up temp directory | |
cd .. | |
rm -rf assets | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment