From 201db9ed9917763299cbebe53f17c5ce2bd7e33d Mon Sep 17 00:00:00 2001 From: George Dunlap Date: Fri, 10 Apr 2015 11:57:21 +0000 Subject: [PATCH] First, create a new global variable, PKGTYPE. At the moment support "deb" and "rpm". Define _check-package-$PKGTYPE which returns true if the package is installed, false otherwise, and _install-package-$PKGTYPE which will install a list of packages. Define check-package(), which will take a list of packages, and check to see if they're installed. Any missing packages will be added to an array called "missing". Change _${COMPONENT}_install_dependencies to ${COMPONENT}_check_package. Have these call check-package. Define check-builddeps(). Define an empty "missing" array. Call check-package for "raisin" dependincies (like git and rpmbuild). Then call for_each_component check_package. At this point we have an array with all missing packages. If it's empty, be happy. If it's non-empty, and deps=true, try to install the packages; otherwise print the missing packages and exit. Add install-builddeps(), which is basically check-builddeps() with YES=y. Call check-builddeps from build() to close the loop. Add function keyword to all other exported functions in commands.sh. Remove --no-deps command line option. Signed-off-by: George Dunlap Signed-off-by: Stefano Stabellini --- components/grub | 8 ++--- components/libvirt | 8 ++--- components/xen | 8 ++--- lib/commands.sh | 68 ++++++++++++++++++++++++++------------- lib/common-functions.sh | 71 ++++++++++++++++++++++++++++++++--------- raise | 5 ++- 6 files changed, 112 insertions(+), 56 deletions(-) diff --git a/components/grub b/components/grub index a5aa27d..b538fb1 100644 --- a/components/grub +++ b/components/grub @@ -1,6 +1,6 @@ #!/usr/bin/env bash -function _grub_install_dependencies() { +function grub_check_package() { local DEP_Debian_common="build-essential tar autoconf bison flex" local DEP_Debian_x86_32="$DEP_Debian_common" local DEP_Debian_x86_64="$DEP_Debian_common libc6-dev-i386" @@ -18,14 +18,12 @@ function _grub_install_dependencies() { echo grub is only supported on x86_32 and x86_64 return fi - echo installing Grub dependencies - eval install_dependencies \$DEP_"$DISTRO"_"$ARCH" + echo Checking Grub dependencies + eval check-package \$DEP_"$DISTRO"_"$ARCH" } function grub_build() { - _grub_install_dependencies - cd "$BASEDIR" rm -f memdisk.tar tar cf memdisk.tar -C data grub.cfg diff --git a/components/libvirt b/components/libvirt index 6602dcf..430d651 100644 --- a/components/libvirt +++ b/components/libvirt @@ -1,6 +1,6 @@ #!/usr/bin/env bash -function _libvirt_install_dependencies() { +function libvirt_check_package() { local DEP_Debian_common="build-essential libtool autoconf autopoint \ xsltproc libxml2-utils pkg-config python-dev \ libxml-xpath-perl libyajl-dev libxml2-dev \ @@ -18,13 +18,11 @@ function _libvirt_install_dependencies() { local DEP_Fedora_x86_32="$DEP_Fedora_common" local DEP_Fedora_x86_64="$DEP_Fedora_common" - echo installing Libvirt dependencies - eval install_dependencies \$DEP_"$DISTRO"_"$ARCH" + echo Checking Libvirt dependencies + eval check-package \$DEP_"$DISTRO"_"$ARCH" } function libvirt_build() { - _libvirt_install_dependencies - cd "$BASEDIR" git-checkout $LIBVIRT_UPSTREAM_URL $LIBVIRT_UPSTREAM_REVISION libvirt-dir cd libvirt-dir diff --git a/components/xen b/components/xen index 7a9f22d..2d345a8 100644 --- a/components/xen +++ b/components/xen @@ -1,6 +1,6 @@ #!/usr/bin/env bash -function _xen_install_dependencies() { +function xen_check_package() { local DEP_Debian_common="build-essential python-dev gettext uuid-dev \ libncurses5-dev libyajl-dev libaio-dev pkg-config libglib2.0-dev \ libssl-dev libpixman-1-dev bridge-utils wget" @@ -15,13 +15,11 @@ function _xen_install_dependencies() { local DEP_Fedora_x86_32="$DEP_Fedora_common dev86 acpica-tools texinfo" local DEP_Fedora_x86_64="$DEP_Fedora_x86_32 glibc-devel.i686" - echo installing Xen dependencies - eval install_dependencies \$DEP_"$DISTRO"_"$ARCH" + echo Checking Xen dependencies + eval check-package \$DEP_"$DISTRO"_"$ARCH" } function xen_build() { - _xen_install_dependencies - cd "$BASEDIR" git-checkout $XEN_UPSTREAM_URL $XEN_UPSTREAM_REVISION xen-dir cd xen-dir diff --git a/lib/commands.sh b/lib/commands.sh index 5827565..bf5493b 100755 --- a/lib/commands.sh +++ b/lib/commands.sh @@ -1,30 +1,52 @@ #!/usr/bin/env bash -build() { - if [[ $YES != "y" ]] +function check-builddep() { + local -a missing + + check-package git + + if [[ $DISTRO = "Fedora" ]] then - echo "Do you want Raisin to automatically install build time dependencies for you? (y/n)" - while read answer - do - if [[ "$answer" = "n" ]] - then - NO_DEPS=1 - break - elif [[ "$answer" = "y" ]] - then - break - else - echo "Reply y or n" - fi - done + check-package rpm-build fi - mkdir -p "$INST_DIR" &>/dev/null - install_dependencies git - if [[ $DISTRO = "Fedora" ]] + for_each_component check_package + + if [[ -n "${missing[@]}" ]] then - install_dependencies rpm-build + echo "Missing packages: ${missing[@]}" + if [[ $YES != "y" ]] + then + echo "Do you want Raisin to automatically install them for you? (y/n)" + while read answer + do + if [[ "$answer" = "n" ]] + then + echo "Please install, or run ./raise install-builddep" + exit 1 + elif [[ "$answer" = "y" ]] + then + break + else + echo "Reply y or n" + fi + done + fi + + echo "Installing..." + install-package "${missing[@]}" fi +} + +function install-builddep() { + YES=y check-builddep +} + +function build() { + check-builddep + + mkdir -p "$INST_DIR" &>/dev/null + # build and install under $DESTDIR for_each_component clean for_each_component build @@ -32,7 +54,7 @@ build() { build_package xen-system } -unraise() { +function unraise() { for_each_component clean uninstall_package xen-system @@ -41,7 +63,7 @@ unraise() { rm -rf "$INST_DIR" } -install() { +function install() { # need single braces for filename matching expansion if [ ! -f xen-sytem*rpm ] && [ ! -f xen-system*deb ] then @@ -51,7 +73,7 @@ install() { install_package xen-system } -configure() { +function configure() { if [[ $YES != "y" ]] then echo "Proceeding we'll make changes to the running system," diff --git a/lib/common-functions.sh b/lib/common-functions.sh index cfa0c7f..a523f5d 100644 --- a/lib/common-functions.sh +++ b/lib/common-functions.sh @@ -97,15 +97,23 @@ function get_distro() { case "$os_VENDOR" in "Debian"* | "Ubuntu"* | "LinuxMint"* ) DISTRO="Debian" + PKGTYPE="deb" + ;; + "Fedora" ) + DISTRO="Fedora" + PKGTYPE="rpm" ;; "SUSE"* ) DISTRO="SUSE" + PKGTYPE="rpm" ;; "OpenSUSE"* | "openSUSE"* ) DISTRO="openSUSE" + PKGTYPE="rpm" ;; "Red"* | "CentOS"* ) DISTRO="CentOS" + PKGTYPE="rpm" ;; *) DISTRO=$os_VENDOR @@ -114,6 +122,7 @@ function get_distro() { export os_VENDOR os_RELEASE os_UPDATE os_CODENAME export DISTRO + export PKGTYPE } function get_arch() { @@ -122,24 +131,56 @@ function get_arch() { -e s/aarch64/arm64/` } -function install_dependencies() { - if [[ "$NO_DEPS" && "$NO_DEPS" -eq 1 ]] +function _check-package-deb() { + if [[ $VERBOSE -eq 1 ]] + then + echo "Checking for package ${args[0]}" + fi + + if dpkg -s "$1" 2>/dev/null | grep -q "Status:.*installed" then - echo "Not installing any dependencies, as requested." - echo "Depency list: $*" return 0 + else + return 1 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 _install-package-deb() { + $SUDO apt-get install -y $* +} + +function _check-package-rpm() { + if [[ $VERBOSE -eq 1 ]] + then + echo "Checking for package $1" + fi + + if rpm -q "$1" 2>&1 >/dev/null + then + return 0 + else + return 1 + fi +} + +function _install-package-rpm() { + $SUDO yum install -y $* +} + +# Modifies inherited variable "missing" +function check-package() { + for p in $* + do + if ! _check-package-${PKGTYPE} $p + then + missing+=("$p") + fi + done + +} + +function install-package() { + _install-package-${PKGTYPE} $* } function start_initscripts() { diff --git a/raise b/raise index 45c0c25..b254904 100755 --- a/raise +++ b/raise @@ -5,10 +5,10 @@ set -e _help() { echo "Usage: ./build.sh " echo "where options are:" - echo " -n | --no-deps Do no install build-time dependencies" echo " -v | --verbose Verbose" echo " -y | --yes Do not ask questions and continue" echo "where commands are:" + echo " install-builddep Install build time dependencies (requires sudo)" echo " build Build the components enabled in config" echo " install Install binaries under / (requires sudo)" echo " configure Configure the system (requires sudo)" @@ -42,7 +42,6 @@ common_init # parameters check export VERBOSE=0 export YES="n" -export NO_DEPS=0 while [[ $# -gt 1 ]] do if [[ "$1" = "-v" || "$1" = "--verbose" ]] @@ -60,7 +59,7 @@ do done case "$1" in - "build" | "install" | "configure" | "unraise" ) + "install-builddep" | "build" | "install" | "configure" | "unraise" ) COMMAND=$1 ;; *) -- 2.39.5