aboutsummaryrefslogtreecommitdiff
path: root/src/jail-upgrade
blob: 6383cf545e75af384eb71d3fcc798c521b60f613 (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
#!/bin/bash
#
# jail-upgrade v0.5: upgrade packages in jails
# feedback: rhatto@riseup.net | GPL
#
#  Jail-upgrade is free software; you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or any later version.
#
#  Jail-upgrade is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#

COMMON="/usr/libexec/simplepkg/common.sh"

# TODO: merge upgrade functions

function swaret_jail_upgrade {

  echo upgrading jail $1...

  if [ ! -d "$PATCHES_DIR" ]; then
    for file in `find $PATCHES_DIR -name *tgz`; do
      pack=`package_name $file`
      if check_for_upgrade $file $1; then
        ROOT=$1 upgradepkg $file
	      upgraded $pack
      fi
    done
  fi

  if [ "$CONSIDER_ALL_PACKAGES_AS_PATCHES" == "YES" ] ||
     [ "$CONSIDER_ALL_PACKAGES_AS_PATCHES" == "1" ]; then
     # this option does the following:
     # search all packages in the repositories and apply a patch if:
     #       - the package is installed
     #       - it hasnt already applied
     #       - has a different version from the current installed
     #       - has a different build number from the current installed
     if [ -d "$STORAGE" ]; then
       for file in `find $STORAGE -name *tgz`; do
          pack=`package_name $file`
          if check_for_upgrade $file $1; then
            # the package is installed
            if ! echo $UPGRADED | grep -q $pack; then
              # it hasnt already applied
              ROOT=$1 upgradepkg $file
              upgraded $pack
            fi
          fi
       done
     fi
  fi

  # clear the upgraded package list
  unset UPGRADED

}

function simplaret_jail_upgrade {

  echo upgrading jail $1...

  VERSION="`cat $1/etc/slackware-version | awk '{ print $2 }' | sed -e 's/.0$//'`"
  ARCH="`cat $1/etc/slackware-version | awk '{ print $3 }' | sed -e 's/(//' -e 's/)//'`"

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

  if [ -d "$PATCHES_DIR/$ARCH/$VERSION" ]; then
    for file in `find $PATCHES_DIR/$ARCH/$VERSION -name *tgz`; do
      pack=`package_name $file`
      if check_for_upgrade $file $1; then
        ROOT=$1 upgradepkg $file
	      upgraded $pack
      fi
    done
  else
    echo error: cant upgrade for arch $ARCH and version $VERSION on $1: no such patch dir $PATCHES_DIR/$ARCH/$VERSION
  fi

  if [ "$CONSIDER_ALL_PACKAGES_AS_PATCHES" == "YES" ] ||
     [ "$CONSIDER_ALL_PACKAGES_AS_PATCHES" == "1" ]; then
     # this option does the following:
     # search all packages in the repositories and apply a patch if:
     #       - the package is installed
     #       - it hasnt already applied
     #       - has a different version from the current installed
     #       - has a different build number from the current installed
     if [ -d "$STORAGE/$ARCH/$VERSION" ]; then
       for file in `find $STORAGE/$ARCH/$VERSION -name *tgz`; do
          pack=`package_name $file`
          if check_for_upgrade $file $1; then
            # the package is installed
            if ! echo $UPGRADED | grep -q $pack; then
              # it hasnt already applied
              ROOT=$1 upgradepkg $file
              upgraded $pack
            fi
          fi
       done
     fi
  fi

  # clear the upgraded package list
  unset UPGRADED

}

function check_for_upgrade {

  # check if package $1 is installed in the $2 root environment
  # and should be upgraded
  local pack found file

  found="0"
  file="$1"
  pack="`package_name $file`"

  if [ -d "$2/var/log/packages/" ] && [ ! -z "$file" ]; then
    for installed in `ls $2/var/log/packages/$pack* 2> /dev/null`; do
      if [[ "$pack" == "`package_name $installed.tgz`" ]]; then
        if [[ "`basename $installed`" != "`basename $file .tgz`" ]]; then
          found="1"
        fi
      fi
    done
  fi

  # if the package is installed, return true, otherwise false
  if [ "$found" == "0" ]; then
    return 1
  elif [ "$found" == "1" ]; then
    return 0
  fi

}

function upgraded {

  UPGRADED="$UPGRADED $1 "

}

if [ -f "$COMMON" ]; then
  source $COMMON
  eval_config `basename $0`
else
  echo "error: file $COMMON not found, check your `basename $0` installation"
  exit 1
fi

if [ ! -z "$PATCHES" ]; then
  if [ -d "$PATCHES" ]; then
    PATCHES_DIR="$PATCHES" 
  else
    echo "error: folder $PATCHES does not exist"
  fi
fi

if [ "$SIMPLARET" == "simplaret" ]; then
  upgrade_method="simplaret_jail_upgrade"
elif [ "$SIMPLARET" == "swaret" ]; then
  upgrade_method="swaret_jail_upgrade"
else
  echo invalid value $SIMPLARET for SIMPLARET, please check your $CONF
  exit 1
fi

UPGRADED=""

if [ -z "$1" ]; then
  if [ -d "/var/log/packages" ]; then
    $upgrade_method /
  fi
  for vserver in `ls $JAIL_ROOT`; do
    if [ -d "$JAIL_ROOT/$vserver/var/log/packages" ]; then
      $upgrade_method $JAIL_ROOT/$vserver
    fi
  done
elif [ -d "$JAIL_ROOT/$1" ]; then
  if [ -d "$JAIL_ROOT/$1/var/log/packages" ]; then
    $upgrade_method $JAIL_ROOT/$1
  else
    echo error: jail $JAIL_ROOT/$1 dont looks like a slackware system
  fi
 elif [ -f "$1" ]; then
  basename="`basename $1 tgz`"
  if [ "`basename $1 | sed -e "s/^$basename//"`" == "tgz" ]; then
    arch="`package_arch $1`"
    # TODO
    # - atualizar nas jaulas de mesma arquitetura
    true
  fi
else
  echo "error: jail $0 does not exist"
  exit 1
fi