aboutsummaryrefslogtreecommitdiff
path: root/lib/common.sh
blob: 07b92b4f731d58813f0aa57570df7c4a6cf56392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
#
# common.sh: common functions for simplepkg
# feedback: rhatto at riseup.net | gpl
#
# Uses some functions from pkgtools, which license is:
#
# Copyright 1999  Patrick Volkerding, Moorhead, Minnesota, USA 
# Copyright 2001, 2002, 2003  Slackware Linux, Inc., Concord, California, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

BASE_CONF="/etc/simplepkg"
CONF="$BASE_CONF/simplepkg.conf"
JAIL_LIST="$BASE_CONF/jailist"

# pkgtool stuff
function package_name {

  STRING=`basename $1 .tgz`
  # Check for old style package name with one segment:
  if [ "`echo $STRING | cut -f 1 -d -`" = "`echo $STRING | cut -f 2 -d -`" ]; then
    echo $STRING
  else # has more than one dash delimited segment
    # Count number of segments:
    INDEX=1
    while [ ! "`echo $STRING | cut -f $INDEX -d -`" = "" ]; do
      INDEX=`expr $INDEX + 1`
    done
    INDEX=`expr $INDEX - 1` # don't include the null value
    # If we don't have four segments, return the old-style (or out of spec) package name:
    if [ "$INDEX" = "2" -o "$INDEX" = "3" ]; then
      echo $STRING
    else # we have four or more segments, so we'll consider this a new-style name:
      NAME=`expr $INDEX - 3`
      NAME="`echo $STRING | cut -f 1-$NAME -d -`"
      echo $NAME
    fi
  fi

}

function package_version {

  # get VERSION from a package name
  local file pack version
  file="`basename $1`"
  pack="`package_name $1`"
  version="`echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 1`"
  echo $version

}

function package_arch {

  # get ARCH from a package name
  local file pack arch
  file="`basename $1`"
  pack="`package_name $1`"
  arch="`echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 2`"
  echo $arch

}

function package_build {

  # get BUILD from a package name
  local file pack build
  file="`basename $1 .tgz`"
  pack="`package_name $1`"
  build="`echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 3`"
  echo $build

}

function install_packages {

  # check if is time to clean the local repository
  if [[ "$SIMPLARET_CLEAN"  == "1" ]]; then
    ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
  elif [[ ! -z "$SIMPLARET_PURGE_WEEKS" ]] && [[ "$SIMPLARET_PURGE_WEEKS" != "0" ]]; then
    ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge -w $SIMPLARET_PURGE_WEEKS
  fi

  # now tries to install each package listed in the template
  for pack in `cat $TEMPLATE | grep -v -e "^#" | cut -d : -f 1`; do

    package_downloaded="0"

    if [ "$SIMPLARET" == "simplaret" ]; then
      extrafolder="$ARCH/$VERSION/"
      extraoptions=""
    else
      unset extrafolder
      extraoptions="-a"
    fi

    # first search the package in the patches folder
    for file in `find $PATCHES_DIR/$extrafolder -name $pack*tgz`; do
      if [[ "`package_name $file`" == "$pack" ]]; then
        package_downloaded="1"
        package_file="$file"
        break
      fi
    done

    # then search the package in the standard packages folder
    if [[ "$package_downloaded" != "1" ]]; then
      for file in `find $STORAGE/$extrafolder -name $pack*tgz`; do
        if [[ "`package_name $file`" == "$pack" ]]; then
          package_downloaded="1"
          package_file="$file"
          break
        fi
      done
    fi

    # if the package wasnt found, try to donwload it
    if [[ "$package_downloaded" != "1" ]]; then
      ARCH=$ARCH VERSION=$VERSION $SIMPLARET --get $pack $extraoptions

      # it can be stored at the patches folder
      for file in `find $PATCHES_DIR/$extrafolder -name $pack*tgz`; do
        if [[ "`package_name $file`" == "$pack" ]]; then
          package_file="$file"
          break
        fi
      done

      # or it can be stored at the standard packages folder
      if [ -z "$package_file" ]; then
        for file in `find $STORAGE/$extrafolder -name $pack*tgz`; do
          if [[ "`package_name $file`" == "$pack" ]]; then
            package_file="$file"
            break
          fi
        done
      fi

    fi

    # now tries to install the package, if available
    if [ -z "$package_file" ]; then
      echo error: could not install package $pack
    else
      installed_packs="`ls /$JAIL_ROOT/$server/var/log/packages/$pack* 2> /dev/null`"
      if [ ! -z "$installed_packs" ]; then
        for installed in $installed_packs; do
          if [[ "$pack" == "`package_name $installed.tgz`" ]]; then
            echo "package $pack already installed in $JAIL_ROOT/$server ($installed)"
            package_installed="1"
            break
          fi
          if [[ "$package_installed" != "1" ]]; then
            installpkg -root /$JAIL_ROOT/$server $package_file
            unset package_installed
            if [ ! -z "$SIMPLARET_DELETE_DURING" ] && [ "$SIMPLARET_DELETE_DURING" != "0" ]; then
              SILENT=1 ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
            fi
            break
          fi
        done
      else
        installpkg -root /$JAIL_ROOT/$server $package_file
        if [ ! -z "$SIMPLARET_DELETE_DURING" ] && [ "$SIMPLARET_DELETE_DURING" != "0" ]; then
          SILENT=1 ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
        fi
      fi
    fi

    unset package_file

  done

  # purge packages, if needed
  if [[ "$SIMPLARET_DELETE_DOWN" == "1" ]]; then
    ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
  fi

}

function remove_packages {

  for pack in `cat $TEMPLATE | grep -v -e "^#" | cut -d : -f 1`; do
    ROOT=/$JAIL_ROOT/$server removepkg $pack
  done

}

function eval_parameter {

  # usage: eval $1 parameter from $CONF
  # return the evaluated parameter if available or $2 $3 ... $n

  if grep -qe "^$1=" $CONF; then
    grep -e "^$1=" $CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | awk '{ print $1 }'
  else
    shift
    echo $*
  fi

}

function eval_boolean_parameter {

  # get a boolean parameter from the configuration

  local value

  # get the value
  value="`eval_parameter $1 $2`"

  # force case insensitiveness
  value="`echo $value | tr '[:upper:]' '[:lower:]'`"

  # convert it to wheter 0 or 1
  if [ "$value" == "yes" ] || [ "$value" == "1" ]; then
    echo 1
  else
    echo 0
  fi

}

function eval_config {

  # simplepkg config file evaluation
  # usage: eval_config <program-name> [-u]

  if [ -f "$CONF" ]; then

    SIMPLARET="`eval_parameter SIMPLARET simplaret`"
    STORAGE="`eval_parameter STORAGE /var/simplaret/packages`"
    JAIL_ROOT="`eval_parameter JAIL_ROOT /vservers`"
    PATCHES_DIR="`eval_parameter PATCHES_DIR /var/simplaret/patches`"
    ROOT_PRIORITY="`eval_parameter ROOT_PRIORITY patches slackware extra testing pasture`"
    SIMPLARET_PURGE_WEEKS="`eval_parameter SIMPLARET_PURGE_WEEKS 0`"

    SIMPLARET_CLEAN="`eval_boolean_parameter SIMPLARET_CLEAN 1`"
    SIMPLARET_DELETE_DOWN="`eval_boolean_parameter SIMPLARET_DELETE_DOWN 1`"
    SIMPLARET_UPDATE="`eval_boolean_parameter SIMPLARET_UPDATE 0`"
    SIMPLARET_DELETE_DURING="`eval_boolean_parameter SIMPLARET_DELETE_DURING 0`"
    SIMPLARET_PURGE_PATCHES="`eval_boolean_parameter SIMPLARET_PURGE_PATCHES 0`"
    CONSIDER_ALL_PACKAGES_AS_PATCHES="`eval_boolean_parameter CONSIDER_ALL_PACKAGES_AS_PATCHES 0`"
    DOWNLOAD_EVEN_APPLIED_PATCHES="`eval_boolean_parameter DOWNLOAD_EVEN_APPLIED_PATCHES 0`"
    PASSIVE_FTP="`eval_boolean_parameter PASSIVE_FTP 0`"
    WARNING="`eval_boolean_parameter WARNING 0`"
    
    DEFAULT_ARCH="`eval_parameter DEFAULT_ARCH $(default_arch)`"
    DEFAULT_VERSION="`eval_parameter DEFAULT_VERSION $(default_version)`"

    # Enabling this option, jail-upgrade will look at your
    # standard repositories for new packages; if it find a package
    # with different version of your current installed package and
    # also this package isnt in the packages folder, then the new
    # package is apllied; if in doubt, just say no or leave blank.
    CONSIDER_ALL_PACKAGES_AS_PATCHES="`eval_boolean_parameter CONSIDER_ALL_PACKAGES_AS_PATCHES 0`"

    # now we place "patches" on the top of ROOT_PRIORITY
    ROOT_PRIORITY="patches `echo $ROOT_PRIORITY | sed -e 's/patches//'`"

  else
    echo $1 error: config file $CONFIG not found
    exit 1
  fi

  if [ ! -d "$STORAGE" ]; then
    mkdir -p $STORAGE
  fi

  if [ -z "$ARCH" ]; then
    ARCH="$DEFAULT_ARCH" 
  fi

  if [ -z "$VERSION" ]; then
    VERSION="$DEFAULT_VERSION"
  fi

  if which $SIMPLARET &> /dev/null; then
    if [ "$SIMPLARET_UPDATE" == "1" ]; then
      if [ "$2" == "-u" ]; then
        echo "updating package database..."
        ARCH=$ARCH VERSION=$VERSION $SIMPLARET --update
      fi
    fi
  else
    echo "$SIMPLARET not found, please install it before run $0"
  fi

}

function default_version {

  # get version from /etc/slackware-version
  cat $1/etc/slackware-version | awk '{ print $2 }' | sed -e 's/.0$//'

}

function default_arch {

  # get arch from /etc/slackware-version

  local arch
  arch="`cat $1/etc/slackware-version | awk '{ print $3 }' | sed -e 's/(//' -e 's/)//'`"

  if [ -z "$arch" ]; then
    echo i386
  else
    echo $arch
  fi

}