source: src-sh/libsh/functions.sh @ c88b2a4

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

Add a placeholder for where we will add a new pkg rquery call to skip
re-downloading valid cached files

  • Property mode set to 100755
File size: 13.9 KB
Line 
1#!/bin/sh
2# Functions we can source for pc-bsd scripts
3# Author: Kris Moore
4# Copyright: 2012
5# License: BSD
6##############################################################
7
8PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
9
10download_cache_packages()
11{
12  if [ ! -e "/usr/local/etc/pkg.conf" ] ; then
13    exit_err "No /usr/local/etc/pkg.conf!"
14  fi
15
16  # Tickle pkg update first
17  pkg-static update
18  local ARCH="`uname -m`"
19
20  ${1} > /tmp/.pkgUpList.$$
21
22  while read line
23  do
24     lTag=`echo $line | awk '{print $1}'` 
25     case $lTag in
26    Upgrading|Downgrading) pkgList="`echo $line | awk '{print $2}' | sed 's|:||g'`-`echo $line | awk '{print $5}'`.txz $pkgList" ;;
27 Reinstalling) pkgList="`echo $line | awk '{print $2}'`.txz $pkgList" ;;
28   Installing) pkgList="`echo $line | awk '{print $2}' | sed 's|:||g'`-`echo $line | awk '{print $3}'`.txz $pkgList" ;;
29                    *) continue ;;
30     esac
31
32  done < /tmp/.pkgUpList.$$
33  rm /tmp/.pkgUpList.$$
34
35  # Get the PKG_CACHEDIR
36  PKG_CACHEDIR="/var/cache/pkg"
37  cat /usr/local/etc/pkg.conf | grep -q "^PKG_CACHEDIR:"
38  if [ $? -eq 0 ] ; then
39    PKG_CACHEDIR="`grep '^PKG_CACHEDIR:' /usr/local/etc/pkg.conf | awk '{print $2}'`"
40  fi
41  if [ -z "$PKG_CACHEDIR" ] ; then
42     exit_err "Failed getting PKG_CACHEDIR"
43  fi
44  export PKG_CACHEDIR
45
46  PKGREL=`uname -r | cut -d '-' -f 1-2`
47
48  # Where are the packages on our mirrors?
49  pkgUrl="/packages/${PKGREL}/${ARCH}"
50
51  if [ ! -d "$PKG_CACHEDIR/All" ] ; then
52     mkdir -p ${PKG_CACHEDIR}/All
53  fi
54
55  for i in $pkgList
56  do
57    # Does the package already exist?
58    if [ -e "${PKG_CACHEDIR}/All/${i}" ] ; then 
59        # For now just remove the cached file
60        # Once bapt gives us a working rquery string, we can add a check here to skip
61        # re-downloading already valid files
62        #pName=`echo $i | sed 's|.txz$||g'`
63        # Check the sizes
64        #eSize=`pkg rquery "%sb" $pName`
65        #dSize=`ls -al `
66        rm ${PKG_CACHEDIR}/All/${i} ;
67    fi
68    get_file_from_mirrors "${pkgUrl}/All/${i}" "${PKG_CACHEDIR}/All/${i}"
69    if [ $? -ne 0 ] ; then
70      exit_err "Failed downloading: /${pkgUrl}/All/${i}"
71    fi
72  done
73}
74
75get_mirror() {
76
77  # Check if we already looked up a mirror we can keep using
78  if [ -n "$CACHED_PCBSD_MIRROR" ] ; then
79     VAL="$CACHED_PCBSD_MIRROR"
80     export VAL
81     return
82  fi
83
84  # Set the mirror URL
85  VAL="`cat ${PCBSD_ETCCONF} 2>/dev/null | grep 'PCBSD_MIRROR: ' | sed 's|PCBSD_MIRROR: ||g'`"
86  if [ -n "$VAL" ] ; then
87     echo "Using mirror: $VAL"
88     CACHED_PCBSD_MIRROR="$VAL"
89     export VAL CACHED_PCBSD_MIRROR
90     return
91  fi
92
93  echo "Getting regional mirror..."
94  . /etc/profile
95
96  # No URL? Lets get one from the master server
97  local mFile="${HOME}/.mirrorUrl.$$"
98  touch $mFile
99  fetch -o $mFile http://getmirror.pcbsd.org >/dev/null 2>/dev/null
100  VAL="`cat $mFile | grep 'URL: ' | sed 's|URL: ||g'`"
101  rm $mFile
102  if [ -n "$VAL" ] ; then
103     echo "Using mirror: $VAL"
104     CACHED_PCBSD_MIRROR="$VAL"
105     export VAL CACHED_PCBSD_MIRROR
106     return
107  fi
108
109  # Still no mirror? Lets try the PC-BSD FTP server...
110  VAL="ftp://ftp.pcbsd.org/pub/mirror"
111  CACHED_PCBSD_MIRROR="$VAL"
112  export VAL CACHED_PCBSD_MIRROR
113  echo "Using mirror: $VAL"
114  return 
115}
116
117# Function which returns the installed list of PC-BSD mirrors for use
118# with the aria2c command
119# Will return just a single mirror, if the user has manually specified one
120# in /usr/local/etc/pcbsd.conf
121get_aria_mirror_list()
122{
123  if [ -z $1 ] ; then
124     exit_err "Need to supply file to grab from mirrors..."
125  fi
126
127  # Set the mirror URL
128  local VAL="`cat ${PCBSD_ETCCONF} 2>/dev/null | grep 'PCBSD_MIRROR: ' | sed 's|PCBSD_MIRROR: ||g'`"
129  if [ -n "$VAL" ] ; then
130     echo "${VAL}${1}"
131     return
132  fi
133
134  if [ ! -e "/usr/local/share/pcbsd/conf/pcbsd-mirrors" ] ; then
135     exit_err "Missing mirror list: /usr/local/share/pcbsd/conf/pcbsd-mirrors"
136  fi
137
138  # Build the mirror list
139  while read line
140  do
141    VAL="$VAL ${line}${1}"
142  done < /usr/local/share/pcbsd/conf/pcbsd-mirrors
143  echo ${VAL}
144}
145
146# Function to download a file from the pcbsd mirrors
147# Arg1 = Remote File URL
148# Arg2 = Where to save file
149get_file_from_mirrors()
150{
151   _rf="${1}"
152   _lf="${2}"
153
154   # Get any proxy information
155   . /etc/profile
156
157   # Split up the dir / file name
158   local aDir=`dirname $_lf`
159   local aFile=`basename $_lf`
160
161   # Server status flag
162   local aStatFile=${HOME}/.pcbsd-aria-stat
163   if [ -e "$aStatFile" ] ; then
164     local aStat="--server-stat-of=$aStatFile --server-stat-if=$aStatFile --uri-selector=adaptive --server-stat-timeout=864000"
165   else
166     local aStat="--server-stat-of=$aStatFile --uri-selector=adaptive"
167   fi
168   touch $aStatFile
169
170   # Get mirror list
171   local mirrorList="$(get_aria_mirror_list $1)"
172   
173   # Running from a non GUI?
174   if [ "$GUI_FETCH_PARSING" != "YES" -a "$PBI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
175      aria2c -k 5M ${aStat} --check-certificate=false --file-allocation=none -d "${aDir}" -o "${aFile}" ${mirrorList}
176      return $?
177   fi
178
179   echo "FETCH: ${_rf}"
180
181   # Doing a front-end download, parse the output of fetch
182   _eFile="/tmp/.fetch-exit.$$"
183   fetch -s "`echo ${mirrorList} | awk '{print $1}'`" > /tmp/.fetch-size.$$ 2>/dev/null
184   _fSize=`cat /tmp/.fetch-size.$$ 2>/dev/null`
185   _fSize="`expr ${_fSize} / 1024 2>/dev/null`"
186   rm "/tmp/.fetch-size.$$" 2>/dev/null
187   _time=1
188   if [ -z "$_fSize" ] ; then _fSize=0; fi
189
190   ( aria2c -o ${aFile} -d ${aDir} -k 5M ${aStat} --check-certificate=false --file-allocation=none ${mirrorList} >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
191   FETCH_PID=`ps -auwwwx | grep -v grep | grep "aria2c -o ${aFile}" | awk '{print $2}'`
192   while :
193   do
194      if [ -e "${_lf}" ] ; then
195         _dSize=`du -k ${_lf} | tr -d '\t' | cut -d '/' -f 1`
196         if [ $(is_num "$_dSize") ] ; then
197            if [ ${_fSize} -lt ${_dSize} ] ; then _dSize="$_fSize" ; fi
198            _kbs=`expr ${_dSize} \/ $_time`
199            echo "SIZE: ${_fSize} DOWNLOADED: ${_dSize} SPEED: ${_kbs} KB/s"
200         fi
201      fi
202
203      # Make sure download isn't finished
204      ps -p $FETCH_PID >/dev/null 2>/dev/null
205      if [ "$?" != "0" ] ; then break ; fi
206      sleep 2
207      _time=`expr $_time + 2`
208   done
209
210   _err="`cat ${_eFile}`"
211   rm ${_eFile} 2>/dev/null
212   if [ "$_err" = "0" ]; then echo "FETCHDONE" ; fi
213   unset FETCH_PID
214   return $_err
215
216}
217
218# Function to download a file from remote using fetch
219# Arg1 = Remote File URL
220# Arg2 = Where to save file
221# Arg3 = Number of attempts to make before failing
222get_file() {
223
224        _rf="${1}"
225        _lf="${2}"
226        _ftries=${3}
227        if [ -z "$_ftries" ] ; then _ftries=3; fi
228
229        # Get any proxy information
230        . /etc/profile
231
232        if [ -e "${_lf}" ] ; then
233                echo "Resuming download of: ${_lf}"
234        fi
235
236        if [ "$GUI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
237                fetch -r -o "${_lf}" "${_rf}"
238                _err=$?
239        else
240                echo "FETCH: ${_rf}"
241
242                # Doing a front-end download, parse the output of fetch
243                _eFile="/tmp/.fetch-exit.$$"
244                fetch -s "${_rf}" > /tmp/.fetch-size.$$ 2>/dev/null
245                _fSize=`cat /tmp/.fetch-size.$$ 2>/dev/null`
246                _fSize="`expr ${_fSize} / 1024 2>/dev/null`"
247                rm "/tmp/.fetch-size.$$" 2>/dev/null
248                _time=1
249
250                ( fetch -r -o "${_lf}" "${_rf}" >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
251                FETCH_PID=`ps -auwwwx | grep -v grep | grep "fetch -r -o ${_lf}" | awk '{print $2}'`
252                while :
253                do
254                        if [ -e "${_lf}" ] ; then
255                                _dSize=`du -k ${_lf} | tr -d '\t' | cut -d '/' -f 1`
256                                if [ $(is_num "$_dSize") ] ; then
257                                        if [ ${_fSize} -lt ${_dSize} ] ; then _dSize="$_fSize" ; fi
258                                        _kbs=`expr ${_dSize} \/ $_time`
259                                        echo "SIZE: ${_fSize} DOWNLOADED: ${_dSize} SPEED: ${_kbs} KB/s"
260                                fi
261                        fi
262
263                        # Make sure download isn't finished
264                        ps -p $FETCH_PID >/dev/null 2>/dev/null
265                        if [ "$?" != "0" ] ; then break ; fi
266                        sleep 2
267                        _time=`expr $_time + 2`
268                done
269
270                _err="`cat ${_eFile}`"
271                rm ${_eFile} 2>/dev/null
272                if [ "$_err" = "0" ]; then echo "FETCHDONE" ; fi
273                unset FETCH_PID
274        fi
275
276        echo ""
277        if [ $_err -ne 0 -a $_ftries -gt 0 ] ; then
278                sleep 30
279                _ftries=`expr $_ftries - 1`
280
281                # Remove the local file if we failed
282                if [ -e "${_lf}" ]; then rm "${_lf}"; fi
283
284                get_file "${_rf}" "${_lf}" $_ftries     
285                _err=$?
286        fi
287        return $_err
288}
289
290# Check if a value is a number
291is_num()
292{
293        expr $1 + 1 2>/dev/null
294        return $?
295}
296
297# Exit with a error message
298exit_err() {
299        if [ -n "${LOGFILE}" ] ; then
300           echo "ERROR: $*" >> ${LOGFILE}
301        fi
302        echo >&2 "ERROR: $*"
303        exit 1
304}
305
306
307### Print an error on STDERR and bail out
308printerror() {
309  exit_err $*
310}
311
312
313# Check if the target directory is on ZFS
314# Arg1 = The dir to check
315# Arg2 = If set to 1, don't dig down to lower level directory
316isDirZFS() {
317  local _chkDir="$1"
318  while :
319  do
320     # Is this dir a ZFS mount
321     mount | grep -w "on $_chkDir " | grep -qw "(zfs," && return 0
322
323     # If this directory is mounted, but NOT ZFS
324     if [ "$2" != "1" ] ; then
325       mount | grep -qw "on $_chkDir " && return 1
326     fi
327     
328     # Quit if not walking down
329     if [ "$2" = "1" ] ; then return 1 ; fi
330 
331     if [ "$_chkDir" = "/" ] ; then break ; fi
332     _chkDir=`dirname $_chkDir`
333  done
334 
335  return 1
336}
337
338# Gets the mount-point of a particular zpool / dataset
339# Arg1 = zpool to check
340getZFSMount() {
341  local zpool="$1"
342  local mnt=`mount | grep "^${zpool} on" | grep "(zfs," | awk '{print $3}'`
343  if [ -n "$mnt" ] ; then
344     echo "$mnt"
345     return 0
346  fi
347  return 1
348}
349
350# Get the ZFS dataset of a particular directory
351getZFSDataset() {
352  local _chkDir="$1"
353  while :
354  do
355    local zData=`mount | grep " on ${_chkDir} " | grep "(zfs," | awk '{print $1}'`
356    if [ -n "$zData" ] ; then
357       echo "$zData"
358       return 0
359    fi
360    if [ "$2" != "rec" ] ; then return 1 ; fi
361    if [ "$_chkDir" = "/" ] ; then return 1 ; fi
362    _chkDir=`dirname $_chkDir`
363  done
364  return 1
365}
366
367# Get the ZFS tank name for a directory
368# Arg1 = Directory to check
369getZFSTank() {
370  local _chkDir="$1"
371
372  _chkdir=${_chkDir%/}
373  while :
374  do
375     zpath=`zfs list | awk -v path="${_chkDir}" '$5 == path { print $1 }'`
376     if [ -n "${zpath}" ] ; then
377        echo $zpath | cut -f1 -d '/'
378        return 0
379     fi
380
381     if [ "$_chkDir" = "/" ] ; then return 1 ; fi
382     _chkDir=`dirname $_chkDir`
383  done
384
385  return 1
386}
387
388# Get the mountpoint for a ZFS name
389# Arg1 = name
390getZFSMountpoint() {
391   local _chkName="${1}"
392   if [ -z "${_chkName}" ]; then return 1 ; fi
393
394   zfs list "${_chkName}" | tail -1 | awk '{ print $5 }'
395}
396
397# Get the ZFS relative path for a path
398# Arg1 = Path
399getZFSRelativePath() {
400   local _chkDir="${1}"
401   local _tank=`getZFSTank "$_chkDir"`
402   local _mp=`getZFSMountpoint "${_tank}"`
403
404   if [ -z "${_tank}" ] ; then return 1 ; fi
405
406   local _name="${_chkDir#${_mp}}"
407   echo "${_name}"
408   return 0
409}
410
411# Check if an address is IPv6
412isV6() {
413  echo ${1} | grep -q ":"
414  return $?
415}
416   
417# Is a mount point, or any of its parent directories, a symlink?
418is_symlinked_mountpoint()
419{
420        local _dir
421        _dir=$1
422        [ -L "$_dir" ] && return 0
423        [ "$_dir" = "/" ] && return 1
424        is_symlinked_mountpoint `dirname $_dir`
425        return $?
426}
427
428# Function to ask the user to press Return to continue
429rtn()
430{
431  echo -e "Press ENTER to continue\c";
432  read garbage
433};
434
435# Function to check if an IP address passes a basic sanity test
436check_ip()
437{
438  ip="$1"
439 
440  # If this is a V6 address, skip validation for now
441  isV6 "${ip}"
442  if [ $? -eq 0 ] ; then return ; fi
443
444  # Check if we can cut this IP into the right segments
445  SEG="`echo $ip | cut -d '.' -f 1 2>/dev/null`"
446  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
447  if [ "$?" != "0" ]
448  then
449     return 1
450  fi
451  if [ $SEG -gt 255 -o $SEG -lt 0 ]
452  then
453     return 1
454  fi
455 
456  # Second segment
457  SEG="`echo $ip | cut -d '.' -f 2 2>/dev/null`"
458  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
459  if [ "$?" != "0" ]
460  then
461     return 1
462  fi
463  if [ $SEG -gt 255 -o $SEG -lt 0 ]
464  then
465     return 1
466  fi
467
468  # Third segment
469  SEG="`echo $ip | cut -d '.' -f 3 2>/dev/null`"
470  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
471  if [ "$?" != "0" ]
472  then
473     return 1
474  fi
475  if [ $SEG -gt 255 -o $SEG -lt 0 ]
476  then
477     return 1
478  fi
479 
480  # Fourth segment
481  SEG="`echo $ip | cut -d '.' -f 4 2>/dev/null`"
482  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
483  if [ "$?" != "0" ]
484  then
485     return 1
486  fi
487  if [ $SEG -gt 255 -o $SEG -lt 0 ]
488  then
489     return 1
490  fi
491
492  return 0
493};
494
495check_pkg_conflicts()
496{
497  # Lets test if we have any conflicts
498  pkg-static ${1} 2>/tmp/.pkgConflicts.$$ >/tmp/.pkgConflicts.$$
499  if [ $? -eq 0 ] ; then rm /tmp/.pkgConflicts.$$ ; return ; fi
500 
501  # Found conflicts, suprise suprise, yet another reason I hate packages
502  # Lets start building a list of the old packages we can prompt to remove
503
504  # Nice ugly sed line, sure this can be neater
505  cat /tmp/.pkgConflicts.$$ | grep 'WARNING: locally installed' \
506        | sed 's|.*installed ||g' | sed 's| conflicts.*||g' | sort | uniq \
507        > /tmp/.pkgConflicts.$$.2
508  while read line
509  do
510    cList="$line $cList"
511  done < /tmp/.pkgConflicts.$$.2
512  rm /tmp/.pkgConflicts.$$.2
513  rm /tmp/.pkgConflicts.$$
514
515  if [ "$GUI_FETCH_PARSING" != "YES" -a "$PBI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
516        echo "The following packages will conflict with your pkg command:"
517        echo "-------------------------------------"
518        echo "$cList" | more
519        echo "Do you wish to remove them automatically?"
520        echo -e "Default yes: (y/n)\c"
521        read tmp
522        if [ "$tmp" != "y" -a "$tmp" != "Y" ] ; then return 1 ; fi
523  else
524        echo "PKGCONFLICTS: $cList"
525        echo "PKGREPLY: /tmp/pkgans.$$"
526        while :
527        do
528          if [ -e "/tmp/pkgans.$$" ] ; then
529            ans=`cat /tmp/pkgans.$$`
530            if [ "$ans" = "yes" ] ; then
531               break
532            else
533               return 1
534            fi
535          fi
536          sleep 3
537        done
538  fi
539
540  # Lets auto-resolve these bad-boys
541  # Right now the logic is pretty simple, you conflict, you die
542  for bPkg in $cList
543  do
544     # Nuked!
545     echo "Removing conflicting package: $bPkg"
546     pkg delete -q -y -f ${bPkg}
547  done
548
549  # Lets test if we still have any conflicts
550  pkg-static ${1} 2>/dev/null >/dev/null
551  if [ $? -eq 0 ] ; then return 0; fi
552
553  # Crapola, we still have conflicts, lets warn and bail
554  echo "ERROR: pkg ${1} is still reporting conflicts... Resolve these manually and try again"
555  return 1
556}
Note: See TracBrowser for help on using the repository browser.