freedombone/src/freedombone-utils-go

241 lines
8.3 KiB
Plaintext
Raw Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-07-03 17:13:34 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-07-03 17:13:34 +02:00
#
# Go functions
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-08-14 23:04:34 +02:00
# For reasons unknown we initially have to upgrade to an intermediate version
# before getting to the version we want
GO_INTERMEDIATE_VERSION=1.4.2
2018-02-11 13:14:25 +01:00
GO_VERSION=1.9.4
2016-07-03 17:13:34 +02:00
GO_REPO="https://go.googlesource.com/go"
2016-07-03 17:13:34 +02:00
GO_PACKAGE_MANAGER_REPO="https://github.com/gpmgo/gopm"
GVM_HOME=/home/git/gvm
GVM_REPO="https://github.com/moovweb/gvm"
GVM_COMMIT='25ea8ae158e2861c92e2b22c458e60840157832f'
function select_go_version {
2016-08-14 23:04:34 +02:00
if [ ! -d $GVM_HOME/bin ]; then
echo $'GVM was not installed'
exit 629532
fi
export GVM_ROOT=$GVM_HOME
if ! grep -q "GVM_ROOT=" ~/.bashrc; then
echo "export GVM_ROOT=$GVM_ROOT" >> ~/.bashrc
else
sed -i "s|export GVM_ROOT=.*|export GVM_ROOT=$GVM_ROOT|g" ~/.bashrc
fi
2018-02-26 14:50:40 +01:00
cd "$GVM_ROOT/bin" || exit 3873658
2016-08-14 23:04:34 +02:00
[[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
gvm use go${GO_VERSION} --default
if [ ${#GOPATH} -lt 2 ]; then
echo $'GOPATH was not set'
exit 629825
fi
2018-02-26 14:50:40 +01:00
systemctl set-environment GOPATH="$GOPATH"
2016-08-14 23:04:34 +02:00
}
function mesh_upgrade_golang_from_source {
chroot "$rootdir" adduser --disabled-login --gecos 'go' ipfs
2018-02-26 14:50:40 +01:00
git clone "$GO_REPO" "$rootdir/home/go/go${GO_VERSION}"
cd "$rootdir/home/go/go${GO_VERSION}" || exit 3463635
git checkout "go${GO_VERSION}" -b "go${GO_VERSION}"
2018-02-26 14:50:40 +01:00
git clone "$GO_REPO" "$rootdir/home/go/go${GO_INTERMEDIATE_VERSION}"
cd "$rootdir/home/go/go${GO_INTERMEDIATE_VERSION}" || exit 672845624
git checkout go${GO_INTERMEDIATE_VERSION} -b go${GO_INTERMEDIATE_VERSION}
2018-02-26 14:50:40 +01:00
cat <<EOF > "${rootdir}/root/upgrade_golang.sh"
2016-08-14 23:04:34 +02:00
#!/bin/bash
2016-10-23 20:38:14 +02:00
apt-get -yq install build-essential libc6-dev
apt-get -yq install gcc-multilib g++-multilib make
apt-get -yq install curl git mercurial binutils bison
2016-08-14 23:04:34 +02:00
if [ -d /home/go/Maildir ]; then
rm -rf /home/go/Maildir
2016-08-14 23:04:34 +02:00
fi
export GOROOT=/home/go
export GOPATH=/home/go/go${GO_INTERMEDIATE_VERSION}
2016-08-14 23:04:34 +02:00
cd /home/go/go${GO_INTERMEDIATE_VERSION}/src
./all.bash
if [ ! -f /home/go/go${GO_INTERMEDIATE_VERSION}/bin/go ]; then
exit 63722
2016-08-14 23:04:34 +02:00
fi
export GOROOT_BOOTSTRAP=/home/go/go${GO_INTERMEDIATE_VERSION}
2016-08-14 23:04:34 +02:00
cd /home/go/go${GO_VERSION}/src
./all.bash
if [ ! -f /home/go/go${GO_VERSION}/bin/go ]; then
exit 528352
2016-08-14 23:04:34 +02:00
fi
chown -R go:go /home/go
export GOPATH=/home/go/go${GO_VERSION}
export GOROOT=/home/go
echo "export GOPATH=/home/go/go${GO_VERSION}" >> /home/go/.bashrc
echo "export GOPATH=/home/go/go${GO_VERSION}" >> ~/.bashrc
echo "export GOROOT=/home/go" >> /home/go/.bashrc
echo "export GOROOT=/home/go" >> ~/.bashrc
echo "systemctl set-environment GOPATH=\$GOPATH" >> ~/.bashrc
echo "systemctl set-environment GOROOT=\$GOROOT" >> ~/.bashrc
echo "systemctl set-environment GOPATH=\$GOPATH" >> /home/go/.bashrc
echo "systemctl set-environment GOROOT=\$GOROOT" >> /home/go/.bashrc
2016-08-14 23:04:34 +02:00
systemctl set-environment GOPATH=\$GOPATH
systemctl set-environment GOROOT=\$GOROOT
2016-08-14 23:04:34 +02:00
exit 0
EOF
2018-02-26 14:50:40 +01:00
chroot "${rootdir}" chmod +x /root/upgrade_golang.sh
chroot "${rootdir}" /root/upgrade_golang.sh
if [ ! -f "${rootdir}/home/go/go${GO_VERSION}/bin/go" ]; then
2016-08-14 23:04:34 +02:00
echo $'Failed to upgrade golang'
2018-02-26 14:50:40 +01:00
cat "${rootdir}/root/upgrade_golang.sh"
rm -f "${rootdir}/root/upgrade_golang.sh"
2016-08-14 23:04:34 +02:00
exit 836535
fi
2018-02-26 14:50:40 +01:00
rm -f "${rootdir}/root/upgrade_golang.sh"
2016-07-03 17:13:34 +02:00
}
function mesh_upgrade_golang {
2016-09-24 17:48:26 +02:00
prefix=
2018-02-26 14:50:40 +01:00
if [ "$rootdir" ]; then
2016-09-24 17:48:26 +02:00
prefix="chroot $rootdir"
2016-09-24 18:22:51 +02:00
fi
2016-09-24 17:48:26 +02:00
$prefix adduser --disabled-login --gecos 'go' go
GOARCH=
2016-09-06 14:51:56 +02:00
if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
GOARCH=386
fi
2016-09-29 11:16:52 +02:00
if [[ $ARCHITECTURE == *"amd64" || $ARCHITECTURE == "x86_64" ]]; then
GOARCH=amd64
fi
if [[ $ARCHITECTURE == *"arm"* ]]; then
GOARCH=armv6l
fi
if [[ $ARCHITECTURE == *"aarch"* || $ARCHITECTURE == *"arm64"* ]]; then
GOARCH=arm64
fi
if [ ! $GOARCH ]; then
2016-09-29 11:16:52 +02:00
echo $'No architecture specified'
ARCHITECTURE=$(uname -m)
if [[ $ARCHITECTURE == "arm"* ]]; then
2016-09-29 11:16:52 +02:00
GOARCH=armv6l
echo $"Using $GOARCH"
2016-09-29 11:16:52 +02:00
fi
if [[ $ARCHITECTURE == *"aarch"* || $ARCHITECTURE == *"arm64"* ]]; then
GOARCH=arm64
echo $"Using $GOARCH"
fi
2016-09-29 11:16:52 +02:00
if [[ $ARCHITECTURE == "amd"* || $ARCHITECTURE == "x86_64" ]]; then
GOARCH=amd64
echo $"Using $GOARCH"
2016-09-29 11:16:52 +02:00
fi
if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
GOARCH=386
echo $"Using $GOARCH"
2016-09-29 11:16:52 +02:00
fi
fi
if [ ! $GOARCH ]; then
echo $'System architecture was not detected when installing Go'
echo "uname reports: $(uname -m)"
exit 63945284
fi
GO_SOURCE=https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GOARCH}.tar.gz
2018-02-26 14:50:40 +01:00
if [ ! -d "${rootdir}${INSTALL_DIR}" ]; then
chroot "$rootdir" mkdir -p "${INSTALL_DIR}"
fi
2018-02-26 14:50:40 +01:00
cd "${rootdir}${INSTALL_DIR}" || exit 236487365
if [ ! -f "${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz" ]; then
wget ${GO_SOURCE}
fi
2018-02-26 14:50:40 +01:00
if [ ! -f "${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz" ]; then
exit 26524
fi
2018-02-26 14:50:40 +01:00
$prefix tar -C /home/go -xzf "${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz"
if [ ! -d "${rootdir}/home/go/go/bin" ]; then
echo 'Go binary not installed'
exit 763562
fi
2018-03-03 13:09:59 +01:00
# shellcheck disable=SC2086
mv ${rootdir}/home/go/go ${rootdir}/home/go/go${GO_VERSION}
2018-02-26 14:50:40 +01:00
echo "export GOROOT=/home/go" >> "${rootdir}/root/.bashrc"
echo "export GOROOT=/home/go" >> "${rootdir}/etc/skel/.bashrc"
echo "export GOROOT=/home/go" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
echo "export GOROOT=/home/go" >> "${rootdir}/home/go/.bashrc"
echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/root/.bashrc"
echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/etc/skel/.bashrc"
echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/home/go/.bashrc"
echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/root/.bashrc"
echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/etc/skel/.bashrc"
echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/home/go/.bashrc"
$prefix chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME"
2016-09-24 17:48:26 +02:00
$prefix chown -R go:go /home/go
2018-03-03 12:10:44 +01:00
if [ ! -d "${rootdir}/home/go/go${GO_VERSION}/bin" ]; then
echo $"golang binaries directory not found ${rootdir}/home/go/go${GO_VERSION}/bin"
exit 356356785
fi
2018-03-03 13:20:31 +01:00
if ! cp "${rootdir}/home/go/go${GO_VERSION}/bin/"* "${rootdir}/usr/bin"; then
2018-03-03 12:25:03 +01:00
ls -la "${rootdir}/home/go/go${GO_VERSION}"
echo $'Copying golang binaries failed'
exit 246824628
fi
}
2016-07-03 17:13:34 +02:00
function upgrade_golang {
2018-02-26 14:50:40 +01:00
if grep -Fxq "upgrade_golang:$GO_VERSION" "$COMPLETION_FILE"; then
2016-09-29 11:25:50 +02:00
return
fi
2016-09-24 17:48:26 +02:00
rootdir=
mesh_upgrade_golang
2016-08-14 23:04:34 +02:00
2016-09-29 18:22:16 +02:00
# alter the go version used by the gogs daemon
if [ -f /etc/systemd/system/gogs.service ]; then
sed -i "s|Environment=.*|Environment=\"USER=gogs\" \"HOME=/home/gogs\" \"GOPATH=/home/go/go${GO_VERSION}\"|g" /etc/systemd/system/gogs.service
systemctl daemon-reload
systemctl restart gogs
fi
2018-02-25 13:50:46 +01:00
set_completion_param "${FUNCNAME[0]}" "$GO_VERSION"
2016-07-03 17:13:34 +02:00
}
# NOTE: deliberately there is no "exit 0"