source: src-sh/pc-metapkgmanager/pc-metapkgmanager @ 98b904e

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 98b904e was 1620346, checked in by Kris Moore <kris@…>, 18 months ago

Initial import of PC-BSD /current/ SVN repo

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[1620346]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        # Running from a GUI?
78        if [ -n "$PCFETCHGUI" ] ; then
79          # Setup EVENT_PIPE
80          EVENT_PIPE="/tmp/pkgpipe.$$"
81          export EVENT_PIPE
82          mkfifo $EVENT_PIPE
83          echo "EVENT_PIPE: $EVENT_PIPE"
84
85          # Wait for QT to open the pipe file
86          sleep 5
87
88        fi
89
90        # Lets pre-download all packages
91        download_cache_packages "pkg-static install -U -n $pkgName"
92
93        # Time to run pkgng and let it install packages!
94        pkg install -yLf "$pkgName"
95
96        # Cleanup EVENT_PIPE
97        if [ -n "$PCFETCHGUI" ] ; then
98          rm $EVENT_PIPE
99        fi
100
101        # Make sure the program appears fully installed now, if not set error
102        stat_metapkg ${_apkg}
103        if [ "$?" != "0" ] ; then _pkgStatus=1 ; fi
104
105        # Apply our PC-BSD specific xdg menu entry files
106        if [ -e "/usr/local/bin/pc-xdgutil" ] ; then
107          /usr/local/bin/pc-xdgutil updatemenu >>${LOGFILE} 2>>${LOGFILE}
108        fi
109
110        # Hack to ensure that we share cursors / icons between linux / FBSD apps
111        if [ ! -h "/compat/linux/usr/share/icons" ] ; then
112           rm -rf /compat/linux/usr/share/icons 2>/dev/null
113           ln -fs /usr/local/lib/X11/icons /compat/linux/usr/share/icons 2>/dev/null
114        fi
115        if [ ! -h "/compat/linux/usr/share/fonts" ] ; then
116           rm -rf /compat/linux/usr/share/fonts 2>/dev/null
117           ln -fs /usr/local/lib/X11/fonts /compat/linux/usr/share/fonts 2>/dev/null
118        fi
119
120        # If a post-install script, run it now
121        if [ -e "${MPDIR}/${_apkg}/post-install.sh" ] ; then
122          sh ${MPDIR}/${_apkg}/post-install.sh >/dev/null 2>/dev/null
123        fi
124
125        # Prune any port files we dont want
126        # Extract custom files
127        pc-extractoverlay ports
128
129        echo "Finished Meta-Package: $_apkg"
130       
131}
132
133# Delete specified meta-pkg, and packages which it installed that are not used elsewhere
134del_metapkgs() {
135        if [ -z "$1" ] ; then exit_err "No meta-pkg specified!" ; fi
136        if [ ! -e "${MPDIR}/${1}/ports-list" ] ; then exit_err "No such meta-pkg: $1" ; fi
137        local rmPkg="`cat ${MPDIR}/$1/ports-list | cut -d ':' -f 1`"
138        if [ -z "$rmPkg" ] ; then exit_err "No FreeBSD package specified for meta-pkg: ${1}" ; fi
139        _dpkg=$1
140        kPkgs=""
141
142        echo "Removing Meta-Package: $_dpkg"
143
144        # kPkgs is our list of packages which are required by other installed meta-pkgs
145        export kPkgs
146
147        # If a pre-remove script, run it now
148        if [ -e "${MPDIR}/${_dpkg}/pre-remove.sh" ] ; then
149          sh ${MPDIR}/${_dpkg}/pre-remove.sh >/dev/null 2>/dev/null
150        fi
151
152        # Make sure this pkg is installed
153        pkg info -e $rmPkg >/dev/null 2>/dev/null
154        if [ $? -ne 0 ] ; then
155                echo "Already uninstalled: ${rmPkg}" 
156                echo "Already uninstalled: ${rmPkg}" >>${LOGFILE}
157                return
158        fi
159
160        # confirm this package isn't required by any others
161        nbrequired=`pkg query "%?r" ${rmPkg}`
162        if [ $nbrequired -ne 0 ] ; then 
163                # Still in use, we will re-scan it at the end
164                echo "The package: ${rmPkg} is required by other packages: $nbrequired" 
165                echo "The package: ${rmPkg} is required by other packages: $nbrequired" >>${LOGFILE}
166                return
167        fi
168
169        # Now we use pkgng and remove it
170        echo "Removing: ${rmPkg}"
171        echo "Removing: ${rmPkg}" >>${LOGFILE}
172        pkg delete -y ${rmPkg} >>${LOGFILE} 2>>${LOGFILE}
173
174        # If the program is still fully installed, set status to error
175        stat_metapkg ${_dpkg}
176        if [ $? -eq 0 ] ; then _pkgStatus=1 ; fi
177
178        # If a post-remove script, run it now
179        if [ -e "${MPDIR}/${_dpkg}/post-remove.sh" ] ; then
180          sh ${MPDIR}/${_dpkg}/post-remove.sh >/dev/null 2>/dev/null
181        fi
182
183}
184
185# Function to list available metapkgs
186list_metapkgs() {
187        if [ ! -d "${MPDIR}" ] ; then exit_err "No available meta-pkg dir" ; fi
188
189        # Start listing each meta-pkg
190        for i in `find ${MPDIR}/* -type d`
191        do
192                # Is this package ignored on this arch?
193                if [ -e "${i}/ignore-arch" ] ; then
194                        if [ `cat ${i}/ignore-arch` = "$ARCH" ] ; then continue; fi
195                fi
196
197                if [ -e "${i}/pkg-desc" ] ; then
198
199                        echo "Meta Package: `basename ${i}`"
200                        echo "-------------------------------------"
201                        echo "Description: `cat ${i}/pkg-desc`"
202                        if [ -e "${i}/pkg-icon.png" ] ; then
203                                echo "Icon: ${i}/pkg-icon.png"
204                        fi
205                        if [ -e "${i}/pkg-parent" ] ; then
206                                echo "Parent: `cat ${i}/pkg-parent`"
207                        fi
208                        if [ -e "${i}/desktop" ] ; then
209                                echo "Desktop: YES"
210                        else
211                                echo "Desktop: NO"
212                        fi
213                        echo " "
214                        if [ -e "${i}/ports-list" ] ; then
215                                echo "Required Packages:"
216                                cat ${i}/ports-list | cut -d ':' -f 1
217                        else
218                                echo "Category Entry"
219                        fi
220                        echo " "
221                fi
222        done
223
224}
225
226# Check on a meta-pkg, see if its installed, partially installed, or not.
227stat_metapkg() {
228        if [ -z "$1" ] ; then exit_err "No meta-pkg specified!" ; fi
229        if [ ! -e "${MPDIR}/${1}/ports-list" ] ; then exit_err "No such meta-pkg: $1" ; fi
230        local myPkg="`cat ${MPDIR}/$1/ports-list | cut -d ':' -f 1`"
231        if [ -z "$myPkg" ] ; then exit_err "No FreeBSD package specified for meta-pkg: ${1}" ; fi
232        _mpkg=$1
233
234        # Parse pkg db to see if pkgs are installed
235        pkg query "%n" | grep -q "^$myPkg"
236        if [ $? -eq 0 ] ; then
237                echo "The meta-pkg $_mpkg is installed"
238                return 0
239        else
240                echo "The meta-pkg $_mpkg is not installed"
241                return 255
242        fi
243
244}
245
246# Read through comma delimited list of meta-pkgs
247parse_metapkgs() {
248
249        if [ "$2" != "add" -a "$2" != "del" ] ; then
250                exit_err "Internal error, must use add/del for parse_metapkgs"
251        fi
252
253        create_logfile
254
255        get_num_pkgs "$1"
256        echo "Pending Meta-Package changes: $VAL"
257        echo "Pending Meta-Package changes: $VAL" >>${LOGFILE}
258
259        # Set the package add exit status to default of 0
260        _pkgStatus=0
261
262        local list
263        list=`echo "$1" | sed 's|,| |g'`       
264        for z in $list
265        do
266                if [ "$2" = "add" ] ; then add_metapkgs "$z" "$3" ; fi
267                if [ "$2" = "del" ] ; then del_metapkgs "$z" ; fi
268        done
269
270        # Update the mime data
271        xdg-update-defaults >/dev/null 2>/dev/null
272
273        echo "Meta-Package changes finished!"
274        exit "$_pkgStatus"
275
276}
277
278
279# Get the number of packages we are working on
280get_num_pkgs() {
281        VAL=`echo $1 | sed 's|,| |g' | wc -w | tr -d ' '`
282}
283
284# Create the logfile
285create_logfile() {
286        if [ -e "${LOGFILE}" ]; then
287                # Rotate the log file if over 5 MB
288                if [ `du -m ${LOGFILE} | awk '{print $1}'` -gt 5 ]; then
289                        rm ${LOGFILE}
290                fi
291        fi
292        touch ${LOGFILE}
293        chmod 666 ${LOGFILE}
294}
295
296intercept_pkg_dl()
297{
298   local count=0;
299   fullopts="$@"
300   op="$1"
301   
302   # Make sure we insert some flags we need
303   while [ $# -gt 0 ]; do
304     if [ $count -gt 0 ] ; then
305        # If we are already using -n, we can skip downloading anything
306        if [ "$1" = "-n" ] ; then
307           pkg $fullopts
308           exit $?
309        fi
310        flags="$flags $1"
311     else
312        flags="-U -n"
313     fi
314     count=`expr $count + 1`
315     shift
316   done
317
318   # Lets pre-download all packages
319   download_cache_packages "pkg $op $flags"
320
321   # Time to run pkgng and let it do its thing
322   pkg $fullopts 
323   result=$?
324
325   # Nobody likes a litter bug
326   if [ $result -eq 0 -a -d "${PKG_CACHEDIR}/All" ] ; then
327      echo -e "Cleaning up cache files..\c"
328      rm -rf "${PKG_CACHEDIR}/All"
329      echo -e "Done"
330   fi
331   
332   exit $result
333}
334
335get_file_dl()
336{
337   if [ "$2" = "-o" ] ; then
338      dlOut="$3"
339      dlPath="$4"
340   else
341      dlOut="`basename $2`"
342      dlPath="$2"
343   fi
344   get_file_from_mirrors "$dlPath" "$dlOut"
345   exit $?
346}
347
348# Parse the flags we are sending over to pkg
349# will intercept the install / upgrade flags
350# and use our internal function to download
351# in a much faster method ;)
352do_pc-pkg()
353{
354   case $1 in
355 install|upgrade) intercept_pkg_dl ${@} ;;
356         getfile) get_file_dl ${@} ;;
357               *) pkg ${@} 
358                  exit $?
359                  ;;
360   esac 
361}
362
363# Are we running in pc-pkg mode?
364if [ `basename $0` = "pc-pkg" ] ; then
365   do_pc-pkg $@
366   exit 0
367fi
368
369# Unset some vars
370_pkgflags=""
371PKGUPDATES=""
372
373# The default PKGSET
374PKGSET="pcbsd"
375
376# Check if we have a different dataset specified in pcbsd.conf
377PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
378_pkgChk="`sed -n 's/PCBSD_METAPKGSET: //p' ${PCBSD_ETCCONF} 2>/dev/null`"
379if [ -n "${_pkgChk}" ] ; then PKGSET="$_pkgChk" ; fi
380MPDIR="${DBDIR}/${PKGSET}"
381TMPDIR="/usr/local/tmp"
382
383if [ $# -eq 0 ]; then display_usage; fi
384
385# Parse ye olde cli flags
386while [ $# -gt 0 ]; do
387  case "$1" in
388      list) list_metapkgs ; exit 0 ;;
389       add) parse_metapkgs "$2" "add" "$3" ; exit 0 ;;
390       del) parse_metapkgs "$2" "del" ; exit 0 ;;
391    status) stat_metapkg "$2" ; exit 0 ;;
392  --chroot) if [ -z "$2" ] ; then display_usage ; fi 
393            _chroot="$2" 
394            if [ ! -e "${_chroot}/bin/sh" ] ; then
395              echo "Invalid chroot dir: ${_chroot}"
396              exit 1
397            fi
398            _pkgflags="-C ${_chroot}"
399            _chrootcmd="chroot ${_chroot}"
400            _chroottag="--chroot ${_chroot}"
401
402            # Check for an alternative default pkgset in this chroot
403            PCBSD_ETCCONF="${_chroot}/usr/local/etc/pcbsd.conf"
404            _pkgChk="`sed -n 's/PCBSD_METAPKGSET: //p' ${PCBSD_ETCCONF} 2>/dev/null`"
405            if [ -n "${_pkgChk}" ] ; then PKGSET="$_pkgChk" ; fi
406            MPDIR="${DBDIR}/${PKGSET}"
407            shift
408            ;;
409  --pkgset) if [ -z "$2" ] ; then display_usage ; fi 
410            PKGSET="$2" 
411            if [ ! -d "${DBDIR}/$PKGSET" ] ; then
412              echo "No such package set $PKGSET"
413              exit 1
414            fi
415            MPDIR="${DBDIR}/${PKGSET}"
416            shift ;;
417         *) if [ -z "$1" ] ; then
418              shift
419            else
420              display_usage
421            fi 
422            ;;
423  esac
424  shift
425done
426
427exit 0
428
Note: See TracBrowser for help on using the repository browser.