Created
June 7, 2018 11:40
-
-
Save aursu/0f5325923679ab9d48696338f8bbc177 to your computer and use it in GitHub Desktop.
GitLab preinstall scriptlet (using /bin/sh):
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/sh | |
# GitLab pre-install script | |
DEST_DIR=/opt/gitlab | |
NEW_MAJOR_VERSION=10 | |
NEW_MINOR_VERSION=10.8 | |
mkdir -p /var/log/gitlab/reconfigure | |
skip_migrations_file=/etc/gitlab/skip-auto-migrations | |
skip_reconfigure_file=/etc/gitlab/skip-auto-reconfigure | |
config_check() { | |
if [ -e "${DEST_DIR}/embedded/service/omnibus-ctl/check_config.rb" ] ; then | |
${DEST_DIR}/bin/gitlab-ctl check-config --version=${NEW_MINOR_VERSION} | |
if [ $? -ne 0 ]; then | |
exit 1 | |
fi | |
fi | |
} | |
upgrade_check() { | |
if [ -e "${DEST_DIR}/version-manifest.json" ] ; then | |
OLD_VERSION_STRING=$(grep -i "build_version" ${DEST_DIR}/version-manifest.json | awk -F ': ' '{print $2}' | tr -d '",') | |
# Getting the Major and Major.Minor format of existing version string | |
OLD_MAJOR_VERSION=$(echo $OLD_VERSION_STRING | awk -F "." '{print $1}') | |
OLD_MINOR_VERSION=$(echo $OLD_VERSION_STRING | awk -F "." '{print $1"."$2}') | |
# Minimum version from which jumps are permitted to current version. This | |
# points to the last minor version from previous series. | |
MIN_VERSION=9.5 | |
# Checking | |
# (i) if it is a major version jump | |
# (ii) if existing version is less than required minimum version | |
if test ${OLD_MAJOR_VERSION} -lt ${NEW_MAJOR_VERSION}; then | |
if ! $(echo ${OLD_MINOR_VERSION} | awk -v MIN_VERSION="$MIN_VERSION" '$NF+0 < MIN_VERSION {exit 1}'); then | |
notify "It seems you are upgrading from ${OLD_MAJOR_VERSION}.x version series" | |
notify "to ${NEW_MAJOR_VERSION}.x series. It is recommended to upgrade" | |
notify "to the last minor version in a major version series first before" | |
notify "jumping to the next major version." | |
notify "Please follow the upgrade documentation at https://docs.gitlab.com/ee/policy/maintenance.html#upgrade-recommendations" | |
notify "and upgrade to ${MIN_VERSION} first." | |
exit 1 | |
fi | |
fi | |
fi | |
} | |
pg_check() { | |
PG_MIN_VERSION=9.6 | |
# Fetch the currently running version from the database | |
# If this doesn't work, assume this isn't a database node | |
if ! running_version=$(${DEST_DIR}/bin/gitlab-psql -d template1 -c 'SHOW server_version' -qt 2>/dev/null); then | |
notify | |
notify "This node does not appear to be running a database" | |
notify "Skipping version check, if you think this is an error exit now" | |
notify | |
return | |
fi | |
# Check if PostgreSQL version is less than PG_MIN_VERSION and notify user. | |
# In AWK, $NF will return the last column of the version output, which | |
# is the version string. By doing $NF+0, we force awk to convert it to a | |
# float, so that we can do numerical comparison. | |
if ! $(echo ${running_version} | awk -v PG_MIN_VERSION="$PG_MIN_VERSION" '$NF+0 < PG_MIN_VERSION {exit 1}'); then | |
notify | |
notify "Your version of PostgreSQL is no longer supported. Please upgrade your PostgreSQL version." | |
notify "Check https://docs.gitlab.com/omnibus/update/#updating-gitlab-10-0-or-newer for details." | |
notify "" | |
notify "Upgrade failed. Retry the upgrade after upgrading your PostgreSQL version." | |
exit 1 | |
fi | |
} | |
main() { | |
if [ -e "${skip_migrations_file}" ] || [ -e "${skip_reconfigure_file}" ] ; then | |
# The user wants us to do nothing | |
return | |
fi | |
if [ -d ${DEST_DIR}/service/unicorn ] ; then | |
notify "Automatically backing up only the GitLab SQL database (excluding everything else!)" | |
if ! ${DEST_DIR}/bin/gitlab-rake gitlab:backup:create SKIP=repositories,uploads,builds,artifacts,lfs,registry,pages ; then | |
notify | |
notify "Backup failed! If you want to skip this backup, run the following command and" | |
notify "try again:" | |
notify | |
notify " sudo touch ${skip_migrations_file}" | |
notify | |
exit 1 | |
fi | |
fi | |
} | |
notify() { | |
echo "gitlab preinstall: $1" | |
} | |
if [ -n "${GITLAB_DEBUG}" ] ; then | |
notify "debug: arguments: $@" | |
fi | |
case "$1" in | |
2) | |
# Looks like an RPM upgrade | |
upgrade_check | |
config_check | |
pg_check | |
main | |
;; | |
upgrade) | |
# Looks like a DEB upgrade | |
upgrade_check | |
config_check | |
pg_check | |
main | |
;; | |
*) | |
# This is not an upgrade, nothing to do. | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment