blob: 3c290e35227c8b8550010933ebaa67c4133e6c1e (
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 -eu
20
21 export BASEDIR="$(cd $(dirname ${0})/../ && pwd)"
22 export LIBDIR="${BASEDIR}/lib"
23 export BINDIR="${BASEDIR}/bin"
24
25 source ${LIBDIR}/log.sh
26 source ${LIBDIR}/pkg.sh
27 source ${LIBDIR}/common.sh
28
29 export PORTSDIR=${PORTSDIR:-${BASEDIR}/ports}
30 export KEEP_BUILD=${KEEP_BUILD:-0}
31
32
33 usage() {
34 printf "
35 Pkgimg is a wrapper script for a simple ports system build process. The ports
36 system provides build instructions for individual packages/ports. The pkgimg
37 script will build multiple ports into the same working directory, creating a
38 composite port package image.
39
40 Usage:
41 pkgimg.sh [-c addtional_configs] <ports.manifest>
42
43 Arguments:
44 -h,--help Print this help text
45 -c,--config Import additional configs file (shell syntax)
46 "
47 }
48
49 img_mkpkg() {
50 local _imgname=${1:-}
51 local _imgversion=${2:-}
52 local _imgrelease=${3:-}
53 local _imgsrc=${4:-}
54
55 local _imgbuildname
56
57 _imgbuildname="${_imgname}#${_imgversion}_${_imgrelease}"
58
59 linfo "Generating footprint file for ${_imgname}"
60 footprint_dir ${_imgsrc} > ${BASEDIR}/${_imgbuildname}.footprint
61
62 linfo "Creating deployment tarball from ${_imgsrc}"
63 tar -cf ${BASEDIR}/${_imgbuildname}.tar -C ${_imgsrc} .
64
65 linfo "Compressing ${_imgname}.tar"
66 xz -v ${BASEDIR}/${_imgbuildname}.tar
67
68 # linfo "Build dir: ${_dest}"
69 # linfo "Deployment tarball: ${BASEDIR}/${_imgbuildname}.tar.xz"
70 # linfo "Tarball footprint: ${BASEDIR}/${_imgbuildname}.footprint"
71 }
72
73
74 build_manifest() {
75 local _manifest=${1:-}
76 local _installbase=${2:-}
77
78 local _porttmp # Path to the temporary package working directory
79
80 # Port image variables
81 local _imgname # Name of the image/port collection
82 local _imgdepends # Dependencies to build the port image
83 local _imgversion # Version of the image
84 local _imgrelease # Release number of the image
85 local _imgbuildname # Name of the built image file
86
87 if [[ -z "${_manifest}" ]]; then
88 lerror "Please specify a port manifest."
89 return 1
90 fi
91 if [[ ! -f "${_manifest}" ]]; then
92 lerror "Path '${manifest}' does not exist or is not a manifest"
93 return 2
94 fi
95
96 source ${_manifest}
97 # Set all these variables as the same variables will be set later by port
98 # files
99 _imgname=${name} && unset 'name'
100 _imgdepends=("${depends[@]}") && unset 'depends[@]'
101 _imgversion=${version} && unset 'version'
102 _imgrelease=${release} && unset 'release'
103 _imgbuildname="${_imgname}#${_imgversion}_${_imgrelease}"
104 _installbase="$(mktemp -d /tmp/pkgimg-${_imgbuildname}.XXXX)"
105
106
107 # Loop over each port in the manifest
108 for port in ${_imgdepends[@]}; do
109 # Build the package
110 ${BINDIR}/pkgmk.sh ${port}
111 [ $? -gt 0 ] && return 1
112
113 # Install the package to the image install base
114 ${BINDIR}/pkgadd.sh "${port}" "${_installbase}"
115 [ $? -gt 0 ] && return 1
116 done
117
118 # Assemble the package composite
119 img_mkpkg "${_imgname}" "${_imgversion}" "${_imgrelease}" "${_installbase}"
120 }
121
122 function main {
123 local argv=(${@})
124
125 local i=0
126 local _configs=() # List of config files to load
127 local _config # Single config file to include
128 local _installbase # Path to the temporary image working directory
129 local _manifest # Manifest file to compile
130
131 for (( i=0; i < ${#argv[@]}; i++ )); do
132 if [ ${argv[$i]} == '-c' ] || [ ${argv[$i]} == '--config' ]; then
133 i=$(( i + 1 ))
134 _configs+=(${argv[$i]})
135 elif [ -f ${argv[$i]} ]; then
136 # If no switch was detected at this argument index, test to see if a
137 # manifest file exists at argument path. If so, assume it is a manifest
138 # file
139 _manifest="${argv[$i]}"
140 elif [ ${argv[$i]} == '-h' ] || [ ${argv[$i]} == '--help' ]; then
141 usage
142 return 0
143 else
144 printf "No arguments specified (for help, run with -h,--help).\n"
145 return 1
146 fi
147 done
148
149 # Ensure a manifest file was specified
150 if [ -z "${_manifest:-}" ]; then
151 printf "No manifest file specified.\n"
152 return 1
153 fi
154
155 for _config in ${_configs[@]}; do
156 if [ -f ${_config} ]; then
157 source ${_config}
158 continue
159 fi
160 printf "Could not find or access config '%s'\n" "${_config}"
161 return 2
162 done
163
164 # Convert portsdir to absolute path
165 PORTSDIR="$(cd ${PORTSDIR} && pwd)"
166
167 build_manifest "${_manifest}"
168 }
169
170 main ${@}
|