source: src-sh/pc-metapkgmanager/pc-metapkgmanager @ 84406a4

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 84406a4 was d5d5aa1, checked in by Kris Moore <kris@…>, 19 months ago

Remove old EVENT_PIPE stuff

Add conflict checker to package installation

  • Property mode set to 100644
File size: 11.5 KB
Line 
1#!/bin/sh
2#
3# Copyright 2013 Kris Moore (iXsystems)
4# All rights reserved
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted providing that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26
27# Source our functions
28. /usr/local/share/pcbsd/scripts/functions.sh
29
30# Define some defaults
31DBDIR="/usr/local/share/pcbsd/metaset"
32LOGFILE="/tmp/.pc-metapkgmanager.log"
33FTP_PASSIVE_MODE="YES" ; export FTP_PASSIVE_MODE
34PACKAGESUFFIX=".txz" ; export PACKAGESUFFIX
35ARCH="`uname -m`"
36
37# Start by sourcing /etc/profile
38# This grabs any HTTP_ / FTP_ PROXY variables
39. /etc/profile
40
41# Fix some pkgs bugging us with license questions
42PACKAGE_BUILDING=yes
43export PACKAGE_BUILDING
44
45# Trigger File for Tray Application
46TRIGGERFILE="/tmp/.sysupdatetraytrigger"
47
48display_usage() {
49        cat <<EOF
50usage: `basename $0` [options]
51   
52Options:
53  add pkg1,pkg2          -- Add the specified list of meta-packages
54  del pkg1,pkg2          -- Delete the specified list of meta-packages
55  list                   -- List the available meta-packages
56  status <pkg>           -- List the status of the specified meta-packages
57  --pkgset <pkgset>      -- Change default pkgset we are using
58
59EOF
60        exit 1
61}
62
63add_metapkgs() {
64        if [ -z "$1" ] ; then exit_err "No meta-pkg specified!" ; fi
65        if [ ! -e "${MPDIR}/${1}/ports-list" ] ; then exit_err "No such meta-pkg: $1" ; fi
66        local pkgName="`cat ${MPDIR}/$1/ports-list | cut -d ':' -f 1`"
67        if [ -z "$pkgName" ] ; then exit_err "No FreeBSD package specified for meta-pkg: ${1}" ; fi
68
69        _apkg=$1
70        echo "Installing Meta-Package: $_apkg ($pkgName)"
71
72        # If a pre-install script, run it now
73        if [ -e "${MPDIR}/${_apkg}/pre-install.sh" ] ; then
74          sh ${MPDIR}/${_apkg}/pre-install.sh >/dev/null 2>/dev/null
75        fi
76
77        # Lets pre-download all packages
78        download_cache_packages "pkg-static install -U -n $pkgName"
79
80        # Lets check for any conflicts doing this install
81        check_pkg_conflicts "install -U -F -y $pkgName"
82        if [ $? -ne 0 ] ; then
83                exit 1
84        fi
85
86        # Time to run pkgng and let it install packages!
87        pkg install -yUf "$pkgName"
88
89        # Make sure the program appears fully installed now, if not set error
90        stat_metapkg ${_apkg}
91        if [ "$?" != "0" ] ; then _pkgStatus=1 ; fi
92
93        # Apply our PC-BSD specific xdg menu entry files
94        if [ -e "/usr/local/bin/pc-xdgutil" ] ; then
95          /usr/local/bin/pc-xdgutil updatemenu >>${LOGFILE} 2>>${LOGFILE}
96        fi
97
98        # Hack to ensure that we share cursors / icons between linux / FBSD apps
99        if [ ! -h "/compat/linux/usr/share/icons" ] ; then
100           rm -rf /compat/linux/usr/share/icons 2>/dev/null
101           ln -fs /usr/local/lib/X11/icons /compat/linux/usr/share/icons 2>/dev/null
102        fi
103        if [ ! -h "/compat/linux/usr/share/fonts" ] ; then
104           rm -rf /compat/linux/usr/share/fonts 2>/dev/null
105           ln -fs /usr/local/lib/X11/fonts /compat/linux/usr/share/fonts 2>/dev/null
106        fi
107
108        # If a post-install script, run it now
109        if [ -e "${MPDIR}/${_apkg}/post-install.sh" ] ; then
110          sh ${MPDIR}/${_apkg}/post-install.sh >/dev/null 2>/dev/null
111        fi
112
113        # Prune any port files we dont want
114        # Extract custom files
115        pc-extractoverlay ports
116
117        echo "Finished Meta-Package: $_apkg"
118       
119}
120
121# Delete specified meta-pkg, and packages which it installed that are not used elsewhere
122del_metapkgs() {
123        if [ -z "$1" ] ; then exit_err "No meta-pkg specified!" ; fi
124        if [ ! -e "${MPDIR}/${1}/ports-list" ] ; then exit_err "No such meta-pkg: $1" ; fi
125        local rmPkg="`cat ${MPDIR}/$1/ports-list | cut -d ':' -f 1`"
126        if [ -z "$rmPkg" ] ; then exit_err "No FreeBSD package specified for meta-pkg: ${1}" ; fi
127        _dpkg=$1
128        kPkgs=""
129
130        echo "Removing Meta-Package: $_dpkg"
131
132        # kPkgs is our list of packages which are required by other installed meta-pkgs
133        export kPkgs
134
135        # If a pre-remove script, run it now
136        if [ -e "${MPDIR}/${_dpkg}/pre-remove.sh" ] ; then
137          sh ${MPDIR}/${_dpkg}/pre-remove.sh >/dev/null 2>/dev/null
138        fi
139
140        # Make sure this pkg is installed
141        pkg info -e $rmPkg >/dev/null 2>/dev/null
142        if [ $? -ne 0 ] ; then
143                echo "Already uninstalled: ${rmPkg}" 
144                echo "Already uninstalled: ${rmPkg}" >>${LOGFILE}
145                return
146        fi
147
148        # confirm this package isn't required by any others
149        nbrequired=`pkg query "%?r" ${rmPkg}`
150        if [ $nbrequired -ne 0 ] ; then 
151                # Still in use, we will re-scan it at the end
152                echo "The package: ${rmPkg} is required by other packages: $nbrequired" 
153                echo "The package: ${rmPkg} is required by other packages: $nbrequired" >>${LOGFILE}
154                return
155        fi
156
157        # Now we use pkgng and remove it
158        echo "Removing: ${rmPkg}"
159        echo "Removing: ${rmPkg}" >>${LOGFILE}
160        pkg delete -y ${rmPkg} >>${LOGFILE} 2>>${LOGFILE}
161
162        # If the program is still fully installed, set status to error
163        stat_metapkg ${_dpkg}
164        if [ $? -eq 0 ] ; then _pkgStatus=1 ; fi
165
166        # If a post-remove script, run it now
167        if [ -e "${MPDIR}/${_dpkg}/post-remove.sh" ] ; then
168          sh ${MPDIR}/${_dpkg}/post-remove.sh >/dev/null 2>/dev/null
169        fi
170
171}
172
173# Function to list available metapkgs
174list_metapkgs() {
175        if [ ! -d "${MPDIR}" ] ; then exit_err "No available meta-pkg dir" ; fi
176
177        # Start listing each meta-pkg
178        for i in `find ${MPDIR}/* -type d`
179        do
180                # Is this package ignored on this arch?
181                if [ -e "${i}/ignore-arch" ] ; then
182                        if [ `cat ${i}/ignore-arch` = "$ARCH" ] ; then continue; fi
183                fi
184
185                if [ -e "${i}/pkg-desc" ] ; then
186
187                        echo "Meta Package: `basename ${i}`"
188                        echo "-------------------------------------"
189                        echo "Description: `cat ${i}/pkg-desc`"
190                        if [ -e "${i}/pkg-icon.png" ] ; then
191                                echo "Icon: ${i}/pkg-icon.png"
192                        fi
193                        if [ -e "${i}/pkg-parent" ] ; then
194                                echo "Parent: `cat ${i}/pkg-parent`"
195                        fi
196                        if [ -e "${i}/desktop" ] ; then
197                                echo "Desktop: YES"
198                        else
199                                echo "Desktop: NO"
200                        fi
201                        echo " "
202                        if [ -e "${i}/ports-list" ] ; then
203                                echo "Required Packages:"
204                                cat ${i}/ports-list | cut -d ':' -f 1
205                        else
206                                echo "Category Entry"
207                        fi
208                        echo " "
209                fi
210        done
211
212}
213
214# Check on a meta-pkg, see if its installed, partially installed, or not.
215stat_metapkg() {
216        if [ -z "$1" ] ; then exit_err "No meta-pkg specified!" ; fi
217        if [ ! -e "${MPDIR}/${1}/ports-list" ] ; then exit_err "No such meta-pkg: $1" ; fi
218        local myPkg="`cat ${MPDIR}/$1/ports-list | cut -d ':' -f 1`"
219        if [ -z "$myPkg" ] ; then exit_err "No FreeBSD package specified for meta-pkg: ${1}" ; fi
220        _mpkg=$1
221
222        # Parse pkg db to see if pkgs are installed
223        pkg query "%n" | grep -q "^$myPkg"
224        if [ $? -eq 0 ] ; then
225                echo "The meta-pkg $_mpkg is installed"
226                return 0
227        else
228                echo "The meta-pkg $_mpkg is not installed"
229                return 255
230        fi
231
232}
233
234# Read through comma delimited list of meta-pkgs
235parse_metapkgs() {
236
237        if [ "$2" != "add" -a "$2" != "del" ] ; then
238                exit_err "Internal error, must use add/del for parse_metapkgs"
239        fi
240
241        create_logfile
242
243        get_num_pkgs "$1"
244        echo "Pending Meta-Package changes: $VAL"
245        echo "Pending Meta-Package changes: $VAL" >>${LOGFILE}
246
247        # Set the package add exit status to default of 0
248        _pkgStatus=0
249
250        local list
251        list=`echo "$1" | sed 's|,| |g'`       
252        for z in $list
253        do
254                if [ "$2" = "add" ] ; then add_metapkgs "$z" "$3" ; fi
255                if [ "$2" = "del" ] ; then del_metapkgs "$z" ; fi
256        done
257
258        # Update the mime data
259        xdg-update-defaults >/dev/null 2>/dev/null
260
261        echo "Meta-Package changes finished!"
262        exit "$_pkgStatus"
263
264}
265
266
267# Get the number of packages we are working on
268get_num_pkgs() {
269        VAL=`echo $1 | sed 's|,| |g' | wc -w | tr -d ' '`
270}
271
272# Create the logfile
273create_logfile() {
274        if [ -e "${LOGFILE}" ]; then
275                # Rotate the log file if over 5 MB
276                if [ `du -m ${LOGFILE} | awk '{print $1}'` -gt 5 ]; then
277                        rm ${LOGFILE}
278                fi
279        fi
280        touch ${LOGFILE}
281        chmod 666 ${LOGFILE}
282}
283
284intercept_pkg_dl()
285{
286   local count=0;
287   fullopts="$@"
288   op="$1"
289   
290   # Make sure we insert some flags we need
291   while [ $# -gt 0 ]; do
292     if [ $count -gt 0 ] ; then
293        # If we are already using -n, we can skip downloading anything
294        if [ "$1" = "-n" ] ; then
295           pkg $fullopts
296           exit $?
297        fi
298        flags="$flags $1"
299     else
300        flags="-U -n"
301     fi
302     count=`expr $count + 1`
303     shift
304   done
305
306   # Lets pre-download all packages
307   download_cache_packages "pkg $op $flags"
308
309   # Time to run pkgng and let it do its thing
310   pkg $fullopts 
311   result=$?
312
313   # Nobody likes a litter bug
314   if [ $result -eq 0 -a -d "${PKG_CACHEDIR}/All" ] ; then
315      echo -e "Cleaning up cache files..\c"
316      rm -rf "${PKG_CACHEDIR}/All"
317      echo -e "Done"
318   fi
319   
320   exit $result
321}
322
323get_file_dl()
324{
325   if [ "$2" = "-o" ] ; then
326      dlOut="$3"
327      dlPath="$4"
328   else
329      dlOut="`basename $2`"
330      dlPath="$2"
331   fi
332   get_file_from_mirrors "$dlPath" "$dlOut"
333   exit $?
334}
335
336# Parse the flags we are sending over to pkg
337# will intercept the install / upgrade flags
338# and use our internal function to download
339# in a much faster method ;)
340do_pc-pkg()
341{
342   case $1 in
343 install|upgrade) intercept_pkg_dl ${@} ;;
344         getfile) get_file_dl ${@} ;;
345               *) pkg ${@} 
346                  exit $?
347                  ;;
348   esac 
349}
350
351# Are we running in pc-pkg mode?
352if [ `basename $0` = "pc-pkg" ] ; then
353   do_pc-pkg $@
354   exit 0
355fi
356
357# Unset some vars
358_pkgflags=""
359PKGUPDATES=""
360
361# The default PKGSET
362PKGSET="pcbsd"
363
364# Check if we have a different dataset specified in pcbsd.conf
365PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
366_pkgChk="`sed -n 's/PCBSD_METAPKGSET: //p' ${PCBSD_ETCCONF} 2>/dev/null`"
367if [ -n "${_pkgChk}" ] ; then PKGSET="$_pkgChk" ; fi
368MPDIR="${DBDIR}/${PKGSET}"
369TMPDIR="/usr/local/tmp"
370
371if [ $# -eq 0 ]; then display_usage; fi
372
373# Parse ye olde cli flags
374while [ $# -gt 0 ]; do
375  case "$1" in
376      list) list_metapkgs ; exit 0 ;;
377       add) parse_metapkgs "$2" "add" "$3" ; exit 0 ;;
378       del) parse_metapkgs "$2" "del" ; exit 0 ;;
379    status) stat_metapkg "$2" ; exit 0 ;;
380  --chroot) if [ -z "$2" ] ; then display_usage ; fi 
381            _chroot="$2" 
382            if [ ! -e "${_chroot}/bin/sh" ] ; then
383              echo "Invalid chroot dir: ${_chroot}"
384              exit 1
385            fi
386            _pkgflags="-C ${_chroot}"
387            _chrootcmd="chroot ${_chroot}"
388            _chroottag="--chroot ${_chroot}"
389
390            # Check for an alternative default pkgset in this chroot
391            PCBSD_ETCCONF="${_chroot}/usr/local/etc/pcbsd.conf"
392            _pkgChk="`sed -n 's/PCBSD_METAPKGSET: //p' ${PCBSD_ETCCONF} 2>/dev/null`"
393            if [ -n "${_pkgChk}" ] ; then PKGSET="$_pkgChk" ; fi
394            MPDIR="${DBDIR}/${PKGSET}"
395            shift
396            ;;
397  --pkgset) if [ -z "$2" ] ; then display_usage ; fi 
398            PKGSET="$2" 
399            if [ ! -d "${DBDIR}/$PKGSET" ] ; then
400              echo "No such package set $PKGSET"
401              exit 1
402            fi
403            MPDIR="${DBDIR}/${PKGSET}"
404            shift ;;
405         *) if [ -z "$1" ] ; then
406              shift
407            else
408              display_usage
409            fi 
410            ;;
411  esac
412  shift
413done
414
415exit 0
416
Note: See TracBrowser for help on using the repository browser.