From: George Dunlap Date: Thu, 9 Apr 2015 14:40:18 +0000 (+0100) Subject: Move common-functions.sh and git-checkout.sh into lib X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=172c7565a6a3ea31821f84fbd0f2b6aaea35deab;p=raisin.git Move common-functions.sh and git-checkout.sh into lib "script" implies something which is designed to be run standalone. "lib" implies that this is going to be sourced from another bash script. Also change "git-checkout" to be a function rather than a script Signed-off-by: George Dunlap Acked-by: Stefano Stabellini --- diff --git a/components/grub b/components/grub index 5a42000..a5aa27d 100644 --- a/components/grub +++ b/components/grub @@ -29,7 +29,7 @@ function grub_build() { cd "$BASEDIR" rm -f memdisk.tar tar cf memdisk.tar -C data grub.cfg - ./scripts/git-checkout.sh $GRUB_UPSTREAM_URL $GRUB_UPSTREAM_REVISION grub-dir + git-checkout $GRUB_UPSTREAM_URL $GRUB_UPSTREAM_REVISION grub-dir cd grub-dir ./autogen.sh ## GRUB32 diff --git a/components/libvirt b/components/libvirt index e22996e..6602dcf 100644 --- a/components/libvirt +++ b/components/libvirt @@ -26,7 +26,7 @@ function libvirt_build() { _libvirt_install_dependencies cd "$BASEDIR" - ./scripts/git-checkout.sh $LIBVIRT_UPSTREAM_URL $LIBVIRT_UPSTREAM_REVISION libvirt-dir + git-checkout $LIBVIRT_UPSTREAM_URL $LIBVIRT_UPSTREAM_REVISION libvirt-dir cd libvirt-dir CFLAGS="-I$INST_DIR/$PREFIX/include" \ LDFLAGS="-L$INST_DIR/$PREFIX/lib -Wl,-rpath-link=$INST_DIR/$PREFIX/lib" \ diff --git a/components/xen b/components/xen index a49a1d1..70b72b0 100644 --- a/components/xen +++ b/components/xen @@ -23,7 +23,7 @@ function xen_build() { _xen_install_dependencies cd "$BASEDIR" - ./scripts/git-checkout.sh $XEN_UPSTREAM_URL $XEN_UPSTREAM_REVISION xen-dir + git-checkout $XEN_UPSTREAM_URL $XEN_UPSTREAM_REVISION xen-dir cd xen-dir ./configure --prefix=$PREFIX $MAKE diff --git a/lib/common-functions.sh b/lib/common-functions.sh new file mode 100644 index 0000000..cfa0c7f --- /dev/null +++ b/lib/common-functions.sh @@ -0,0 +1,228 @@ +#!/usr/bin/env bash + +# Executed once at the beginning of the script +function common_init() { + export BASEDIR=`pwd` + export GIT=${GIT-git} + export SUDO=${SUDO-sudo} + export MAKE=${MAKE-make} + export PREFIX=${PREFIX-/usr} + export INST_DIR=${DESTDIR-dist} + + INST_DIR=`readlink -f $INST_DIR` + + # execution + if [[ $EUID -eq 0 ]] + then + export SUDO="" + elif [[ ! -f `which sudo 2>/dev/null` ]] + then + echo "Raisin requires sudo to install build dependencies for you." + echo "Please install sudo, then run this script again." + exit 1 + fi + + if [[ -z "$BASH_VERSINFO" || ${BASH_VERSINFO[0]} -lt 3 || + (${BASH_VERSINFO[0]} -eq 3 && ${BASH_VERSINFO[1]} -lt 2) ]] + then + echo "Raisin requires BASH 3.2 or newer." + exit 1 + fi + + get_distro + get_arch + + for f in `cat "$BASEDIR"/components/series` + do + source "$BASEDIR"/components/"$f" + done +} + +function get_distro() { + if [[ -x "`which lsb_release 2>/dev/null`" ]] + then + os_VENDOR=`lsb_release -i -s` + os_RELEASE=`lsb_release -r -s` + os_CODENAME=`lsb_release -c -s` + os_UPDATE="" + elif [[ -r /etc/redhat-release ]] + then + # Red Hat Enterprise Linux Server release 5.5 (Tikanga) + # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo) + # CentOS release 5.5 (Final) + # CentOS Linux release 6.0 (Final) + # Fedora release 16 (Verne) + # XenServer release 6.2.0-70446c (xenenterprise) + os_CODENAME="" + for r in "Red Hat" "CentOS" "Fedora" "XenServer"; do + os_VENDOR="$r" + if [[ -n "`grep -i \"$r\" /etc/redhat-release`" ]] + then + ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` + os_CODENAME=${ver#*|} + os_RELEASE=${ver%|*} + os_UPDATE=${os_RELEASE##*.} + os_RELEASE=${os_RELEASE%.*} + break + fi + done + elif [[ -r /etc/SuSE-release ]] + then + for r in "openSUSE" "SUSE Linux" + do + os_VENDOR="$r" + + if [[ -n "`grep -i \"$r\" /etc/SuSE-release`" ]] + then + os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | \ + sed 's:.* = ::g'` + os_RELEASE=`grep "VERSION = " /etc/SuSE-release | \ + sed 's:.* = ::g'` + os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | \ + sed 's:.* = ::g'` + break + fi + done + # If lsb_release is not installed, we should be able to detect Debian OS + elif [[ -f /etc/debian_version && `cat /proc/version` =~ "Debian" ]] + then + os_VENDOR="Debian" + os_CODENAME=`awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | \ + sed -r 's/\"|\(|\)//g' | awk '{print $2}'` + os_RELEASE=`awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' \ + | sed 's/\"//g'` + fi + + # Simply distro version string + case "$os_VENDOR" in + "Debian"* | "Ubuntu"* | "LinuxMint"* ) + DISTRO="Debian" + ;; + "SUSE"* ) + DISTRO="SUSE" + ;; + "OpenSUSE"* | "openSUSE"* ) + DISTRO="openSUSE" + ;; + "Red"* | "CentOS"* ) + DISTRO="CentOS" + ;; + *) + DISTRO=$os_VENDOR + ;; + esac + + export os_VENDOR os_RELEASE os_UPDATE os_CODENAME + export DISTRO +} + +function get_arch() { + export ARCH=`uname -m | sed -e s/i.86/x86_32/ -e s/i86pc/x86_32/ -e \ + s/amd64/x86_64/ -e s/armv7.*/arm32/ -e s/armv8.*/arm64/ \ + -e s/aarch64/arm64/` +} + +function install_dependencies() { + if [[ "$NO_DEPS" && "$NO_DEPS" -eq 1 ]] + then + echo "Not installing any dependencies, as requested." + echo "Depency list: $*" + return 0 + fi + case $DISTRO in + "Debian" ) + $SUDO apt-get install -y $* + ;; + "Fedora" ) + $SUDO yum install -y $* + ;; + * ) + echo "I don't know how to install dependencies on $DISTRO" + ;; + esac +} + +function start_initscripts() { + while [[ $# -ge 1 ]] + do + case $DISTRO in + "Debian" ) + $SUDO update-rc.d $1 defaults || echo "Couldn't set $1 to start" + ;; + "Fedora" ) + $SUDO chkconfig --add $1 || echo "Couldn't set $1 to start" + ;; + * ) + echo "I don't know how to start initscripts on $DISTRO" + return 1 + ;; + esac + shift 1 + done +} + +function stop_initscripts() { + while [[ $# -ge 1 ]] + do + case $DISTRO in + "Debian" ) + $SUDO update-rc.d $1 remove || echo "Couldn't remove $1 from init" + ;; + "Fedora" ) + $SUDO chkconfig --del $1 || echo "Couldn't remove $1 from init" + ;; + * ) + echo "I don't know how to start initscripts on $DISTRO" + return 1 + ;; + esac + shift 1 + done +} + +function for_each_component () { + for component in `cat "$BASEDIR"/components/series` + do + capital=`echo $component | tr '[:lower:]' '[:upper:]'` + if eval [[ ! -z \$"$capital"_UPSTREAM_REVISION ]] + then + "$component"_"$1" + fi + done +} + +function build_package() { + if [[ $DISTRO = "Debian" ]] + then + ./scripts/mkdeb "$1" + elif [[ $DISTRO = "Fedora" ]] + then + ./scripts/mkrpm "$1" + else + echo "Don't know how to create packages for $DISTRO" + fi +} + +function install_package() { + if [[ $DISTRO = "Debian" ]] + then + $SUDO dpkg -i "$1".deb + elif [[ $DISTRO = "Fedora" ]] + then + $SUDO rpm -i --force "$1"-`git show --oneline | head -1 | cut -d " " -f 1`-0.$ARCH.rpm + else + echo "Don't know how to install packages on $DISTRO" + fi +} + +function uninstall_package() { + if [[ $DISTRO = "Debian" ]] + then + $SUDO dpkg -r "$1" + elif [[ $DISTRO = "Fedora" ]] + then + $SUDO rpm -e "$1" + else + echo "Don't know how to uninstall packages on $DISTRO" + fi +} diff --git a/lib/git-checkout.sh b/lib/git-checkout.sh new file mode 100755 index 0000000..2ca8f25 --- /dev/null +++ b/lib/git-checkout.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +function git-checkout() { + if [[ $# -lt 3 ]] + then + echo "Usage: $0 " + exit 1 + fi + + TREE=$1 + TAG=$2 + DIR=$3 + + set -e + + if [[ ! -d $DIR-remote ]] + then + rm -rf $DIR-remote $DIR-remote.tmp + mkdir -p $DIR-remote.tmp; rmdir $DIR-remote.tmp + $GIT clone $TREE $DIR-remote.tmp + if [[ "$TAG" ]] + then + cd $DIR-remote.tmp + $GIT branch -D dummy >/dev/null 2>&1 ||: + $GIT checkout -b dummy $TAG + cd .. + fi + mv $DIR-remote.tmp $DIR-remote + fi + rm -f $DIR + ln -sf $DIR-remote $DIR +} diff --git a/raise.sh b/raise.sh index 3c8281e..422fbe4 100755 --- a/raise.sh +++ b/raise.sh @@ -3,7 +3,8 @@ set -e source config -source scripts/common-functions.sh +source lib/common-functions.sh +source lib/git-checkout.sh _help() { echo "Usage: ./build.sh " diff --git a/scripts/common-functions.sh b/scripts/common-functions.sh deleted file mode 100644 index cfa0c7f..0000000 --- a/scripts/common-functions.sh +++ /dev/null @@ -1,228 +0,0 @@ -#!/usr/bin/env bash - -# Executed once at the beginning of the script -function common_init() { - export BASEDIR=`pwd` - export GIT=${GIT-git} - export SUDO=${SUDO-sudo} - export MAKE=${MAKE-make} - export PREFIX=${PREFIX-/usr} - export INST_DIR=${DESTDIR-dist} - - INST_DIR=`readlink -f $INST_DIR` - - # execution - if [[ $EUID -eq 0 ]] - then - export SUDO="" - elif [[ ! -f `which sudo 2>/dev/null` ]] - then - echo "Raisin requires sudo to install build dependencies for you." - echo "Please install sudo, then run this script again." - exit 1 - fi - - if [[ -z "$BASH_VERSINFO" || ${BASH_VERSINFO[0]} -lt 3 || - (${BASH_VERSINFO[0]} -eq 3 && ${BASH_VERSINFO[1]} -lt 2) ]] - then - echo "Raisin requires BASH 3.2 or newer." - exit 1 - fi - - get_distro - get_arch - - for f in `cat "$BASEDIR"/components/series` - do - source "$BASEDIR"/components/"$f" - done -} - -function get_distro() { - if [[ -x "`which lsb_release 2>/dev/null`" ]] - then - os_VENDOR=`lsb_release -i -s` - os_RELEASE=`lsb_release -r -s` - os_CODENAME=`lsb_release -c -s` - os_UPDATE="" - elif [[ -r /etc/redhat-release ]] - then - # Red Hat Enterprise Linux Server release 5.5 (Tikanga) - # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo) - # CentOS release 5.5 (Final) - # CentOS Linux release 6.0 (Final) - # Fedora release 16 (Verne) - # XenServer release 6.2.0-70446c (xenenterprise) - os_CODENAME="" - for r in "Red Hat" "CentOS" "Fedora" "XenServer"; do - os_VENDOR="$r" - if [[ -n "`grep -i \"$r\" /etc/redhat-release`" ]] - then - ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` - os_CODENAME=${ver#*|} - os_RELEASE=${ver%|*} - os_UPDATE=${os_RELEASE##*.} - os_RELEASE=${os_RELEASE%.*} - break - fi - done - elif [[ -r /etc/SuSE-release ]] - then - for r in "openSUSE" "SUSE Linux" - do - os_VENDOR="$r" - - if [[ -n "`grep -i \"$r\" /etc/SuSE-release`" ]] - then - os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | \ - sed 's:.* = ::g'` - os_RELEASE=`grep "VERSION = " /etc/SuSE-release | \ - sed 's:.* = ::g'` - os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | \ - sed 's:.* = ::g'` - break - fi - done - # If lsb_release is not installed, we should be able to detect Debian OS - elif [[ -f /etc/debian_version && `cat /proc/version` =~ "Debian" ]] - then - os_VENDOR="Debian" - os_CODENAME=`awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | \ - sed -r 's/\"|\(|\)//g' | awk '{print $2}'` - os_RELEASE=`awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' \ - | sed 's/\"//g'` - fi - - # Simply distro version string - case "$os_VENDOR" in - "Debian"* | "Ubuntu"* | "LinuxMint"* ) - DISTRO="Debian" - ;; - "SUSE"* ) - DISTRO="SUSE" - ;; - "OpenSUSE"* | "openSUSE"* ) - DISTRO="openSUSE" - ;; - "Red"* | "CentOS"* ) - DISTRO="CentOS" - ;; - *) - DISTRO=$os_VENDOR - ;; - esac - - export os_VENDOR os_RELEASE os_UPDATE os_CODENAME - export DISTRO -} - -function get_arch() { - export ARCH=`uname -m | sed -e s/i.86/x86_32/ -e s/i86pc/x86_32/ -e \ - s/amd64/x86_64/ -e s/armv7.*/arm32/ -e s/armv8.*/arm64/ \ - -e s/aarch64/arm64/` -} - -function install_dependencies() { - if [[ "$NO_DEPS" && "$NO_DEPS" -eq 1 ]] - then - echo "Not installing any dependencies, as requested." - echo "Depency list: $*" - return 0 - fi - case $DISTRO in - "Debian" ) - $SUDO apt-get install -y $* - ;; - "Fedora" ) - $SUDO yum install -y $* - ;; - * ) - echo "I don't know how to install dependencies on $DISTRO" - ;; - esac -} - -function start_initscripts() { - while [[ $# -ge 1 ]] - do - case $DISTRO in - "Debian" ) - $SUDO update-rc.d $1 defaults || echo "Couldn't set $1 to start" - ;; - "Fedora" ) - $SUDO chkconfig --add $1 || echo "Couldn't set $1 to start" - ;; - * ) - echo "I don't know how to start initscripts on $DISTRO" - return 1 - ;; - esac - shift 1 - done -} - -function stop_initscripts() { - while [[ $# -ge 1 ]] - do - case $DISTRO in - "Debian" ) - $SUDO update-rc.d $1 remove || echo "Couldn't remove $1 from init" - ;; - "Fedora" ) - $SUDO chkconfig --del $1 || echo "Couldn't remove $1 from init" - ;; - * ) - echo "I don't know how to start initscripts on $DISTRO" - return 1 - ;; - esac - shift 1 - done -} - -function for_each_component () { - for component in `cat "$BASEDIR"/components/series` - do - capital=`echo $component | tr '[:lower:]' '[:upper:]'` - if eval [[ ! -z \$"$capital"_UPSTREAM_REVISION ]] - then - "$component"_"$1" - fi - done -} - -function build_package() { - if [[ $DISTRO = "Debian" ]] - then - ./scripts/mkdeb "$1" - elif [[ $DISTRO = "Fedora" ]] - then - ./scripts/mkrpm "$1" - else - echo "Don't know how to create packages for $DISTRO" - fi -} - -function install_package() { - if [[ $DISTRO = "Debian" ]] - then - $SUDO dpkg -i "$1".deb - elif [[ $DISTRO = "Fedora" ]] - then - $SUDO rpm -i --force "$1"-`git show --oneline | head -1 | cut -d " " -f 1`-0.$ARCH.rpm - else - echo "Don't know how to install packages on $DISTRO" - fi -} - -function uninstall_package() { - if [[ $DISTRO = "Debian" ]] - then - $SUDO dpkg -r "$1" - elif [[ $DISTRO = "Fedora" ]] - then - $SUDO rpm -e "$1" - else - echo "Don't know how to uninstall packages on $DISTRO" - fi -} diff --git a/scripts/git-checkout.sh b/scripts/git-checkout.sh deleted file mode 100755 index 912bfae..0000000 --- a/scripts/git-checkout.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -if [[ $# -lt 3 ]] -then - echo "Usage: $0 " - exit 1 -fi - -TREE=$1 -TAG=$2 -DIR=$3 - -set -e - -if [[ ! -d $DIR-remote ]] -then - rm -rf $DIR-remote $DIR-remote.tmp - mkdir -p $DIR-remote.tmp; rmdir $DIR-remote.tmp - $GIT clone $TREE $DIR-remote.tmp - if [[ "$TAG" ]] - then - cd $DIR-remote.tmp - $GIT branch -D dummy >/dev/null 2>&1 ||: - $GIT checkout -b dummy $TAG - cd .. - fi - mv $DIR-remote.tmp $DIR-remote -fi -rm -f $DIR -ln -sf $DIR-remote $DIR diff --git a/unraise.sh b/unraise.sh index 2f08901..50ce310 100755 --- a/unraise.sh +++ b/unraise.sh @@ -3,7 +3,7 @@ set -e source config -source scripts/common-functions.sh +source lib/common-functions.sh # start execution