Skip to content

Instantly share code, notes, and snippets.

@socram8888
Last active November 4, 2021 10:54
Show Gist options
  • Save socram8888/a555aede0791e02ae2e67723b474c30a to your computer and use it in GitHub Desktop.
Save socram8888/a555aede0791e02ae2e67723b474c30a to your computer and use it in GitHub Desktop.
Semi-automated Nexus update script
#!/bin/bash -e
cd $(dirname "$0")
log() {
echo -e "\e[92m---\e[39m $1"
}
err() {
echo -e "\e[91m!!!\e[39m $1" >&2
}
log "Fetching current hash"
if [ -f current.sha1 ]; then
oldhash=$(cat current.sha1)
else
oldhash="???"
fi
newhash=$(wget -O- https://download.sonatype.com/nexus/3/latest-unix.tar.gz.sha1)
if [ "$newhash" == "$oldhash" ]; then
log "No new version detected"
exit 0
fi
log "Downloading new version (old: $oldhash, new: $newhash)"
wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz -O nexus-latest-unix.tar.gz
log "Checking hash"
calchash=$(sha1sum nexus-latest-unix.tar.gz | cut -d ' ' -f1)
if [ "$calchash" != "$newhash" ]; then
err "Hash mismatch, calculated $calchash"
exit 1
fi
log "Hash matches"
log "Getting version number"
newversion=$(tar -tf nexus-latest-unix.tar.gz | sed -nE "s/^nexus-([^/]+).*/\1/; T; p" | head -n1)
if [ -z "$newversion" ]; then
err "Failed to detect version"
exit 1
fi
log "New version: $newversion"
if [ -f /etc/init.d/nexus ]; then
if service nexus status | grep -q running; then
log "Stopping old service"
service nexus stop
else
log "Service not running - nothing to do"
fi
rm /etc/init.d/nexus
fi
log "Extracting"
tar -xf nexus-latest-unix.tar.gz
# Find new version
newfolder="$(realpath "nexus-$newversion")"
log "New version at $newfolder"
log "Preparing system"
# Configure Java VM
sed -ri "s%^# (INSTALL4J_JAVA_HOME_OVERRIDE)=%\1=/opt/jdk8u302-b08-jre%" "$newfolder/bin/nexus"
# Create symbolic link
ln -s "$newfolder/bin/nexus" /etc/init.d/nexus
cat <<"EOF" >"$newfolder/bin/nexus.rc"
run_as_user="nexus"
EOF
echo "$calchash">current.sha1
log "Starting"
service nexus start
log "Removing really old versions"
find -maxdepth 1 -type d -name "nexus-*" | sed -nE "s/^.\/nexus-([^/]+).*/\1/; T; p" | sort -nr | tail -n +3 | while read oldver; do
log "Removing $oldver"
rm -r nexus-$oldver
done
log "Done. Have a good day"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment