Created
August 23, 2019 14:18
-
-
Save sboardwell/7a9eda0e442656ffdb841287320b6d8b to your computer and use it in GitHub Desktop.
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
add_custom_nexus_repos() { | |
local dir=$1 | |
local volumeNames subPaths repoFiles patchItemString= patchFile separator='' | |
echo "Getting nexus deployment volume names..." | |
volumeNames=$(kubectl get deployment jenkins-x-nexus -o 'jsonpath={ .spec.template.spec.volumes[*].name}') | |
echo "Getting nexus deployment volumeMount subPaths..." | |
subPaths=$(kubectl get deployment jenkins-x-nexus -o 'jsonpath={ .spec.template.spec.containers[0].volumeMounts[*].subPath}') | |
repoFiles=$(cat "${dir}/nexus-repos/nexus-custom-repo-files.yaml" | docker run -i --rm evns/yq -j -r '.data' | docker run -i --rm imega/jq -r 'keys[]' | xargs) | |
echo "Volume Names: $volumeNames" | |
echo "Sub Paths: $subPaths" | |
echo "Found Repo JSONs: $repoFiles" | |
# check the volume | |
if [[ "${volumeNames}" != *nexus-custom-repo-files* ]]; then | |
patchItemString=' { | |
"op": "add", | |
"path": "/spec/template/spec/volumes/-", | |
"value": { | |
"name" : "nexus-custom-repo-files", | |
"configMap" : { | |
"name" : "nexus-custom-repo-files" | |
} | |
} | |
}' | |
fi | |
# check the volume mounts | |
for repoF in $repoFiles; do | |
[ -n "${patchItemString}" ] && separator=',' || separator='' | |
if [[ "${subPaths}" != *${repoF}* ]]; then | |
local mountPath | |
if [[ "${repoF}" == maven-group.json ]]; then | |
mountPath="/opt/sonatype/nexus/${repoF}" | |
else | |
mountPath="/opt/sonatype/nexus/repositories/${repoF}" | |
fi | |
patchItemString="${patchItemString}${separator} | |
{ | |
\"op\": \"add\", | |
\"path\": \"/spec/template/spec/containers/0/volumeMounts/-\", | |
\"value\": { | |
\"mountPath\" : \"${mountPath}\", | |
\"name\" : \"nexus-custom-repo-files\", | |
\"subPath\" : \"${repoF}\" | |
} | |
}" | |
fi | |
done | |
# update config map | |
kubectl apply -f <(sops -d ${dir}/nexus-repos/nexus-custom-repo-files.yaml) | |
# patch if necessary | |
if [ -n "${patchItemString}" ]; then | |
# patch deployment | |
patchFile=$(mktemp) | |
echo "Patch file: ${patchFile}" | |
echo -e "[\n${patchItemString}\n]" > "${patchFile}" | |
kubectl patch deployment jenkins-x-nexus --type='json' --patch "$(cat ${patchFile})" | |
else | |
echo "All custom repositories found. No need to patch." | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment