Created
June 7, 2018 11:46
-
-
Save aursu/93c1b48cbb4dd5bf546438da64803327 to your computer and use it in GitHub Desktop.
GitLab postinstall 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 | |
# | |
# Perform necessary gitlab setup steps | |
# after package is installed. | |
# | |
DEST_DIR=/opt/gitlab | |
notify() | |
{ | |
echo "gitlab: $1" | |
} | |
create_config_template() | |
{ | |
# Create a minimal gitlab.rb template if /etc/gitlab/gitlab.rb does not exist. | |
if ! [ -e /etc/gitlab/gitlab.rb ] ; then | |
mkdir -p /etc/gitlab | |
cp "${DEST_DIR}/etc/gitlab.rb.template" /etc/gitlab/gitlab.rb | |
sed -i 's!GENERATED_EXTERNAL_URL!'$EXTERNAL_URL'!g' /etc/gitlab/gitlab.rb | |
chmod 600 /etc/gitlab/gitlab.rb | |
else | |
EXTERNAL_URL=$(awk '/^external_url/ { print $2 }' /etc/gitlab/gitlab.rb | tr -d "'\"") | |
fi | |
} | |
fix_directory_permissions() | |
{ | |
if [ -x /usr/bin/dpkg-query ] ; then | |
# We are in the land of .deb packages. We should fix package directory owners | |
# because of the faulty 7.2.0 / 7.2.1 .deb packages. | |
/usr/bin/dpkg-query -L gitlab-ce gitlab-ee 2>/dev/null | while read f ; do | |
if [ -d "$f" ] ; then | |
# This directory may have been created when installing omnibus-gitlab | |
# 7.2.0 / 7.2.1, so it could have the wrong owner. | |
chown root:root "$f" | |
fi | |
done | |
fi | |
} | |
print_upgrade_info() | |
{ | |
# Moving towards GitLab 11 we're deprecating skip-auto-migrations in favour of | |
# skip-auto-reconfigure. In the meantime check for both and populate skip_file. | |
if [ -e "/etc/gitlab/skip-auto-migrations" ]; then | |
skip_file="/etc/gitlab/skip-auto-migrations" | |
elif [ -e "/etc/gitlab/skip-auto-reconfigure" ]; then | |
skip_file="/etc/gitlab/skip-auto-reconfigure" | |
else | |
skip_file="" | |
fi | |
# As of 9.0, we're updating PostgreSQL by default. | |
# But, if /etc/gitlab/skip-auto-migrations exists we won't do anything. | |
# Here we'll warn users with out of date databases. | |
if [ -n "${skip_file}" ] | |
then | |
local version=$(cat /var/opt/gitlab/postgresql/data/PG_VERSION 2>/dev/null || true) | |
local new_version=$(awk '$1 == "postgresql_new" {print $2}' /opt/gitlab/version-manifest.txt) | |
if [ -n "${new_version}" -a "${new_version#*$version}" = "${new_version}" ] | |
then | |
echo | |
notify "WARNING:" | |
notify "GitLab now ships with a newer version of PostgreSQL (${new_version}) by default" | |
notify "We did not upgrade since ${skip_file} exists" | |
notify "To manually upgrade, RUN THE FOLLOWING COMMAND:" | |
echo | |
echo "sudo gitlab-ctl pg-upgrade" | |
echo | |
notify "In the future, we will be removing old versions of PostgreSQL from the omnibus package" | |
notify "Hosts which have not upgraded to the new version will begin to fail then" | |
notify "For more details, please see:" | |
notify "https://docs.gitlab.com/omnibus/settings/database.html#upgrade-packaged-postgresql-server" | |
notify | |
fi | |
fi | |
} | |
if [ -n "${GITLAB_DEBUG}" ] ; then | |
notify "debug: arguments: $@" | |
fi | |
check_if_ec2() | |
{ | |
if [ -f /sys/hypervisor/uuid ] && [ `head -c 3 /sys/hypervisor/uuid` = 'ec2' ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
get_ec2_address() | |
{ | |
url=$1 | |
# Try collecting fqdn if it is set correctly | |
fqdn=$(/opt/gitlab/embedded/bin/curl -s ${url}) | |
if [ -n "${fqdn}" ]; then | |
# Checking if curl returned an XML message | |
word="<?xml" | |
if ! $(test "${fqdn#*$word}" != "$fqdn"); then | |
EXTERNAL_URL="http://${fqdn}" | |
fi | |
fi | |
} | |
get_details_from_ec2() | |
{ | |
get_ec2_address "http://169.254.169.254/latest/meta-data/public-hostname" | |
if [ -z "${EXTERNAL_URL}" ]; then | |
get_ec2_address "http://169.254.169.254/latest/meta-data/public-ipv4" | |
fi | |
} | |
set_protocol() | |
{ | |
# Checking if EXTERNAL_URL starts with http:// or https:// | |
if ! $(echo ${EXTERNAL_URL} | awk '$0 !~ /^http[s]?:\/\// {exit 1}'); then | |
EXTERNAL_URL="http://${EXTERNAL_URL}" | |
fi | |
} | |
if [ -z "${EXTERNAL_URL}" ]; then | |
check_if_ec2 | |
if [ $? -eq 0 ] ; then | |
get_details_from_ec2 | |
fi | |
else | |
set_protocol | |
fi | |
if [ -z "${EXTERNAL_URL}" ]; then | |
EXTERNAL_URL="http://gitlab.example.com" | |
fi | |
${DEST_DIR}/embedded/bin/symlink_ctl_cmds ${DEST_DIR} | |
create_config_template | |
fix_directory_permissions | |
print_upgrade_info | |
case "$1" in | |
configure) | |
# Looks like a DEB install. We don't know if it is a fresh install or an | |
# upgrade. | |
EXTERNAL_URL=${EXTERNAL_URL} ${DEST_DIR}/bin/gitlab-ctl upgrade | |
;; | |
*) | |
# No op. | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment