summaryrefslogtreecommitdiff
path: root/bin/pkgimg.sh
blob: 7d0df70a96f45afc54875a1a55e80d8a6607aac6 (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # Portimg uses Crux port-like system for creating software deployment images.
    4 # Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
    5 #
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 3 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 #
   19 set -u
   20 
   21 export BASEDIR="$(cd $(dirname ${0})/../ && pwd)"
   22 export LIBDIR="${BASEDIR}/lib"
   23 
   24 source ${LIBDIR}/log.sh
   25 source ${LIBDIR}/pkg.sh
   26 source ${LIBDIR}/common.sh
   27 
   28 export PORTSDIR=${PORTSDIR:-${BASEDIR}/ports}
   29 export KEEP_BUILD=${KEEP_BUILD:-0}
   30 
   31 function main {
   32   local manifest=${1:-}
   33 
   34   local _installbase # Path to the temporary image working directory
   35   local _porttmp     # Path to the temporary package working directory
   36 
   37   # Port image variables
   38   local _imgname      # Name of the image/port collection
   39   local _imgdepends   # Dependencies to build the port image
   40   local _imgversion   # Version of the image
   41   local _imgrelease   # Release number of the image
   42   local _imgbuildname # Name of the built image file
   43 
   44   if [[ -z "${manifest}" ]]; then
   45     lerror "Please specify a port manifest."
   46     return 1
   47   fi
   48   if [[ ! -f "${manifest}" ]]; then
   49     lerror "Path '${manifest}' does not exist or is not a manifest"
   50     return 2
   51   fi
   52 
   53   source ${manifest}
   54   # Set all these variables as the same variables will be set later by port
   55   # files
   56   _imgname=${name}              && unset 'name'
   57   _imgdepends=("${depends[@]}") && unset 'depends[@]'
   58   _imgversion=${version}        && unset 'version'
   59   _imgrelease=${release}        && unset 'release'
   60   _imgbuildname="${_imgname}#${_imgversion}_${_imgrelease}"
   61 
   62   # Convert portsdir to absolute path
   63   PORTSDIR="$(cd ${PORTSDIR} && pwd)"
   64 
   65   _installbase=$(mktemp -d /tmp/pkgimg-${_imgbuildname}.XXXX)
   66 
   67   # Loop over each port in the manifest
   68   for port in ${_imgdepends[@]}; do
   69     # Call pkgmk to build the port
   70     pkgmk ${port} '_porttmp'
   71     [ $? -gt 0 ] && return 1
   72 
   73     # Move the package contents to the install base
   74     linfo "Synchronizing ${port} build to install base ${_installbase}"
   75     rsync -a ${_porttmp}/pkg/ ${_installbase}/
   76 
   77     # Cleanup
   78     if [ "${KEEP_BUILD}" = 0 ]; then
   79       rm -rf ${_porttmp}
   80     else
   81       linfo "KEEP_BUILD is set. Skipping cleanup of ${port}."
   82     fi
   83   done
   84 
   85   linfo "Generating footprint file for ${_installbase}"
   86   footprint_dir ${_installbase} > ${_installbase}.footprint
   87 
   88   linfo "Creating deployment tarball from ${_installbase}"
   89   tar -cf ${_installbase}.tar -C ${_installbase} ./*
   90 
   91   linfo "Compressing ${_installbase}.tar"
   92   xz -v ${_installbase}.tar
   93 
   94   # Move the package and footprint to current directory
   95   mv ${_installbase}.tar.xz    ${BASEDIR}/${_imgbuildname}.tar.xz
   96   mv ${_installbase}.footprint ${BASEDIR}/${_imgbuildname}.footprint
   97 
   98   linfo "Build dir:          ${_installbase}"
   99   linfo "Deployment tarball: ${BASEDIR}/${_imgbuildname}.tar.xz"
  100   linfo "Tarball footprint:  ${BASEDIR}/${_imgbuildname}.footprint"
  101 
  102 }
  103 
  104 main ${@}

Generated by cgit