123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #!/bin/bash
- #
- # .---. . .
- # | | |
- # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
- # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
- # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
- #
- # Freedom in the Cloud
- #
- # Go functions
- #
- # License
- # =======
- #
- # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
- #
- # 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/>.
-
- # 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
-
- GO_VERSION=1.7
-
- GO_REPO="https://go.googlesource.com/go"
- 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 {
- 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
- cd $GVM_ROOT/bin
- [[ -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
-
- systemctl set-environment GOPATH=$GOPATH
- }
-
- function mesh_upgrade_golang_from_source {
- chroot "$rootdir" adduser --disabled-login --gecos 'go' ipfs
-
- git clone $GO_REPO $rootdir/home/go/go${GO_VERSION}
- cd $rootdir/home/go/go${GO_VERSION}
- git checkout go${GO_VERSION} -b go${GO_VERSION}
-
- git clone $GO_REPO $rootdir/home/go/go${GO_INTERMEDIATE_VERSION}
- cd $rootdir/home/go/go${GO_INTERMEDIATE_VERSION}
- git checkout go${GO_INTERMEDIATE_VERSION} -b go${GO_INTERMEDIATE_VERSION}
-
- cat <<EOF > ${rootdir}/root/upgrade_golang.sh
- #!/bin/bash
-
- apt-get -y install build-essential libc6-dev
- apt-get -y install gcc-multilib g++-multilib make
- apt-get -y install curl git mercurial binutils bison
-
- if [ -d /home/go/Maildir ]; then
- rm -rf /home/go/Maildir
- fi
-
- export GOROOT=/home/go
- export GOPATH=/home/go/go${GO_INTERMEDIATE_VERSION}
-
- cd /home/go/go${GO_INTERMEDIATE_VERSION}/src
- ./all.bash
- if [ ! -f /home/go/go${GO_INTERMEDIATE_VERSION}/bin/go ]; then
- exit 63722
- fi
-
- export GOROOT_BOOTSTRAP=/home/go/go${GO_INTERMEDIATE_VERSION}
-
- cd /home/go/go${GO_VERSION}/src
- ./all.bash
- if [ ! -f /home/go/go${GO_VERSION}/bin/go ]; then
- exit 528352
- 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
- systemctl set-environment GOPATH=\$GOPATH
- systemctl set-environment GOROOT=\$GOROOT
-
- exit 0
- EOF
- 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
- echo $'Failed to upgrade golang'
- cat ${rootdir}/root/upgrade_golang.sh
- rm -f ${rootdir}/root/upgrade_golang.sh
- exit 836535
- fi
- rm -f ${rootdir}/root/upgrade_golang.sh
- }
-
- function mesh_upgrade_golang {
- prefix=
- if [ $rootdir ]; then
- prefix="chroot $rootdir"
- fi
- $prefix adduser --disabled-login --gecos 'go' go
-
- GOARCH=
- if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
- GOARCH=386
- fi
- if [[ $ARCHITECTURE == *"amd64" || $ARCHITECTURE == "x86_64" ]]; then
- GOARCH=amd64
- fi
- if [[ $ARCHITECTURE == *"arm"* ]]; then
- GOARCH=armv6l
- fi
- if [ ! $GOARCH ]; then
- echo $'No architecture specified'
- ARCHITECTURE=$(uname -m)
- if [[ $ARCHITECTURE == "arm"* ]]; then
- GOARCH=armv6l
- fi
- if [[ $ARCHITECTURE == "amd"* || $ARCHITECTURE == "x86_64" ]]; then
- GOARCH=amd64
- fi
- if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
- GOARCH=386
- fi
- fi
- GO_SOURCE=https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GOARCH}.tar.gz
-
- if [ ! -d ${rootdir}${INSTALL_DIR} ]; then
- chroot "$rootdir" mkdir -p ${INSTALL_DIR}
- fi
- cd ${rootdir}${INSTALL_DIR}
- wget ${GO_SOURCE}
- if [ ! -f ${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz ]; then
- exit 26524
- fi
- $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
- mv ${rootdir}/home/go/go ${rootdir}/home/go/go${GO_VERSION}
- echo "export GOROOT=/home/go" >> ${rootdir}/root/.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}/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}/home/$MY_USERNAME/.bashrc
- echo 'export PATH=$PATH:$GOPATH' >> ${rootdir}/home/go/.bashrc
- $prefix chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME
- $prefix chown -R go:go /home/go
- cp ${rootdir}/home/go/go${GO_VERSION}/bin/* ${rootdir}/usr/bin
- }
-
- function upgrade_golang {
- if grep -Fxq "upgrade_golang:$GO_VERSION" $COMPLETION_FILE; then
- return
- fi
-
- rootdir=
- mesh_upgrade_golang
-
- # 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
-
- set_completion_param "$FUNCNAME" "$GO_VERSION"
- }
-
- # NOTE: deliberately there is no "exit 0"
|