blob: d093c45a26c765710d999c64f60afa4f9da7bbb6 (
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}/config.sh
27 source ${LIBDIR}/common.sh
28
29 export PORTSDIR=${PORTSDIR:-${BASEDIR}/ports}
30 export KEEP_BUILD=${KEEP_BUILD:-0}
31
32 function main {
33 local manifest=${1:-}
34
35 local _installbase # Path to the temporary image working directory
36 local _porttmp # Path to the temporary package working directory
37
38 # Port image variables
39 local _imgname # Name of the image/port collection
40 local _imgdepends # Dependencies to build the port image
41 local _imgversion # Version of the image
42 local _imgrelease # Release number of the image
43 local _imgbuildname # Name of the built image file
44
45 if [[ -z "${manifest}" ]]; then
46 lerror "Please specify a port manifest."
47 return 1
48 fi
49 if [[ ! -f "${manifest}" ]]; then
50 lerror "Path '${manifest}' does not exist or is not a manifest"
51 return 2
52 fi
53
54 source ${manifest}
55 # Set all these variables as the same variables will be set later by port
56 # files
57 _imgname=${name} && unset 'name'
58 _imgdepends=("${depends[@]}") && unset 'depends[@]'
59 _imgversion=${version} && unset 'version'
60 _imgrelease=${release} && unset 'release'
61 _imgbuildname="${_imgname}#${_imgversion}_${_imgrelease}"
62
63 # Convert portsdir to absolute path
64 PORTSDIR="$(cd ${PORTSDIR} && pwd)"
65
66 _installbase=$(mktemp -d /tmp/pkgimg-${_imgbuildname}.XXXX)
67
68 # Loop over each port in the manifest
69 for port in ${_imgdepends[@]}; do
70 # Call pkgmk to build the port
71 pkgmk ${port} '_porttmp'
72 [ $? -gt 0 ] && return 1
73
74 # Move the package contents to the install base
75 linfo "Synchronizing ${port} build to install base ${_installbase}"
76 rsync -a ${_porttmp}/pkg/ ${_installbase}/
77
78 # Cleanup
79 if [ "${KEEP_BUILD}" = 0 ]; then
80 rm -rf ${_porttmp}
81 else
82 linfo "KEEP_BUILD is set. Skipping cleanup of ${port}."
83 fi
84 done
85
86 linfo "Generating footprint file for ${_installbase}"
87 footprint_dir ${_installbase} > ${_installbase}.footprint
88
89 linfo "Creating deployment tarball from ${_installbase}"
90 tar -cf ${_installbase}.tar -C ${_installbase} ./*
91
92 linfo "Compressing ${_installbase}.tar"
93 xz -v ${_installbase}.tar
94
95 # Move the package and footprint to current directory
96 mv ${_installbase}.tar.xz ${BASEDIR}/${_imgbuildname}.tar.xz
97 mv ${_installbase}.footprint ${BASEDIR}/${_imgbuildname}.footprint
98
99 linfo "Build dir: ${_installbase}"
100 linfo "Deployment tarball: ${BASEDIR}/${_imgbuildname}.tar.xz"
101 linfo "Tarball footprint: ${BASEDIR}/${_imgbuildname}.footprint"
102
103 }
104
105 main ${@}
|