aboutsummaryrefslogtreecommitdiff
path: root/trunk/src/templatepkg
blob: e6050119cb53d5edf73a472c5ef6ff0dc4937943 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
#
# templatepkg v0.3: template maintenance script from simplepkg suite
#
# feedback: rhatto at riseup.net | gpl
# 
#  Templatepkg 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.
#
#  Templatepkg 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"
BASENAME="`basename $0`"

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

function usage {

  echo "usage: $BASENAME <option> <template> [arguments]"
  echo "options:"
  echo ""
  echo "  -c | --create: create a template from a jail; arguments are:"
  echo ""
  echo "      $BASENAME -c <template> [jail-root]"
  echo ""
  echo "  -u | --update: update a template from a jail; arguments are:"
  echo ""
  echo "      $BASENAME -u <template> [jail-root]"
  echo ""
  echo "  -a | --add: add files into a template; arguments for -a are:"
  echo ""
  echo "      $BASENAME -a <template> <file-name> [jail-root]"
  echo ""
  echo "      file-name: the file or directory to be added"
  echo "      jail-root: the jail under file-name is located"
  echo ""
  echo "  -d | --delete: delete files or folders from a template; arguments are:"
  echo ""
  echo "      $BASENAME -d <template> <file-name>"
  echo ""
  echo "  -s | --sync: sync $BASE_CONF/templates working copy; arguments are:"
  echo ""
  echo "      $BASENAME -s"
  echo ""   
  echo "  -e | --export: export $BASE_CONF/templates to a svn repository; arguments are:"
  echo ""   
  echo "      $BASENAME -e <svn-repository>"
  echo ""   
  echo "  -i | --import: grab /etc/simplepkg/templates from a svn repository; arguments are:"   
  echo ""   
  echo "      $BASENAME -i <svn-repository>"
  echo ""   
  echo "  in all cases (-c, -u and -a), jail-root defaults to / if ommited"
  echo ""
  exit

}

function check_svn_repo {

  # check a svn repository URL
  # usage: set_svn_repo <repository>

  if [ ! -z "$1" ]; then
    if echo $1 | grep -q -v -e "^svn://"; then
      if echo $1 | grep -q -v -e "^svn+ssh://"; then
        if echo $1 | grep -q -v -e "^file://"; then
          echo $BASENAME: invalid repository URL $1
          return 1
        fi
      fi
    fi
  else
    echo $BASENAME: no repository defined
    return 1
  fi

}

function import_export_templates {

  # import from or export to $BASE_CONF in a svn repository
  # usage: template_export <import|export> <repository>

  local templates_folder basedir repository mode preposition

  if [ "$?" != 0 ]; then
    usage
    exit 1
  elif ! use_svn; then
    echo $BASENAME: simplepkg not configured to use svn
    exit 1
  fi

  templates_folder="$BASE_CONF/templates"
  basedir="$BASE_CONF"
  mode="$1"
  repository="$2"
  preposition="from"

  check_svn_repo $repository

  if [ ! -z "$2" ]; then

    if [ ! -d "$templates_folder/.svn" ]; then

      cd $basedir

      if [ "$mode" == "export" ]; then
        preposition="to"
        svn import templates $repository -m "initial import"
        if [ "$?" != "0" ]; then
          echo $BASENAME: export failed
          exit 1
        fi
      fi

      if [ -d "templates" ]; then
        mv templates templates.old
      fi

      svn checkout $repository templates

      if [ "$?" == "0" ]; then
        rm -rf templates.old
      else
        rm -rf templates
        mv templates.old templates
        echo $BASENAME: $mode failed
        exit 1
      fi

    else
      echo "$BASENAME: $templates_folder seens to be already $mode""ed $preposition $repository"
      exit 1
    fi

  fi

}

function template_create {

  # create a new template

  local template_base info_commit

  if  [ ! -d "`dirname $TEMPLATE_BASE`" ]; then
    if use_svn && [ -d "`dirname $TEMPLATE_BASE`/.svn" ]; then
      cd `dirname $TEMPLATE_BASE`
      svn mkdir `dirname $TEMPLATE_BASE`
    else
      mkdir -p `dirname $TEMPLATE_BASE`
    fi
  else
    echo $BASENAME: template `basename $TEMPLATE_BASE` already exists
    exit 1
  fi

  touch $TEMPLATE_BASE.perms
  touch $TEMPLATE_BASE.template

  if use_svn && [ -d "$TEMPLATE_BASE/.svn" ]; then

    cd $TEMPLATE_BASE

    if ! svn_check $TEMPLATE_BASE.d; then
      svn mkdir $TEMPLATE_BASE.d
      info_commit="yes"
    fi

    if ! svn_check $TEMPLATE_BASE.s; then
      svn mkdir $TEMPLATE.s
      info_commit="yes"
    fi

    if ! svn_check $TEMPLATE_BASE.template; then
      svn add $TEMPLATE_BASE.template
      info_commit="yes"
    fi

    if ! svn_check $TEMPLATE_BASE.perms; then
      svn add $TEMPLATE_BASE.perms
      info_commit="yes"
    fi

    if [ "$info_commit" == "yes" ]; then
      echo $BASENAME: please run jail-commit to add files under $file into the svn repository
    fi

  else
    mkdir $TEMPLATE_BASE.{d,s}
  fi

  template_update

}

function template_update {

  # update the template package list

  components="$TEMPLATE_BASE.template $TEMPLATE_BASE.perms $TEMPLATE_BASE.d $TEMPLATE_BASE.s"
  for component in $components; do
    if [ ! -e "$component" ]; then
      echo $BASENAME: template component not found: $component
      echo $BASENAME: please run $BASENAME -c before update a template
      exit 1
    fi
  done

  if [ ! -d "$ROOT/var/log/packages" ]; then
    echo $ROOT/var/log/packages: directory not found
    exit 1
  fi

  echo Checking package list for template `basename $TEMPLATE_BASE`

  for package in `ls -1 $ROOT/var/log/packages/`; do
    pack=`package_name $package`
    if [ -f $TEMPLATE ]; then
      if ! `grep -v -e "^#" $TEMPLATE | cut -d : -f 1 | awk '{ print $1 }' | grep -q -e "^$pack\$"`; then
        echo $pack >> $TEMPLATE
        echo Added $pack on $TEMPLATE
      fi
    else
      echo $pack >> $TEMPLATE
      echo Added $pack on $TEMPLATE
    fi
  done

  # check if each package from the template is installed
  grep -v -e "^#" $TEMPLATE | cut -d : -f 1 | awk '{ print $1 }' | while read pack; do

    if [ ! -z "$pack" ]; then
      unset found
      for candidate in `ls $ROOT/var/log/packages/$pack* 2> /dev/null`; do
        candidate="`package_name $candidate`"
        if [ "$pack" == "$candidate" ]; then
          found="1"
          break
        fi
      done
      if [ "$found" != "1" ]; then
        # remove a non-installed package from the template
        sed "/^$pack$/d" $TEMPLATE | sed "/^$pack $/d" | sed "/^$pack:*/d" | sed "/^$pack */d" > $TEMPLATE.tmp
        cat $TEMPLATE.tmp > $TEMPLATE
        rm -f $TEMPLATE.tmp
        echo Removed $pack from $TEMPLATE
      fi
    fi

  done

}

function template_add {

  # add a file in a template
  # usage: template_add <jail-root> <file>

  local info_commit cwd

  mkdir -p $TEMPLATE_BASE.d

  if [ -z "$1" ] || [ -z "$2" ]; then
    return 1
  fi

  jail="/$1" 
  file="$2"

  if [ -a "$TEMPLATE_BASE.d/$file" ]; then
    if [ -d "$TEMPLATE_BASE.d/$file" ]; then

      echo $BASENAME: folder `slash $file` already on $TEMPLATE_BASE.d, checking for contents

      cd $jail
      for candidate in `find $file`; do
        if [ ! -a "$TEMPLATE_BASE.d/$candidate" ]; then
          mkdir -p $TEMPLATE_BASE.d/`dirname $candidate`
          cp -a $jail/$candidate $TEMPLATE_BASE.d/$candidate
          if use_svn && [ -d "$TEMPLATE_BASE.d/.svn" ]; then
            cwd="`pwd`"
            cd $TEMPLATE_BASE.d
            svn add ./$candidate
            if [ "$?" != "0" ]; then
              echo $BASENAME: error adding `slash $candidate` into the revision system
            fi
            cd $cwd
            info_commit="yes"
          else
            echo Added `slash $jail/$candidate` on $TEMPLATE_BASE.d/$candidate
          fi
        fi
      done

      if [ "$info_commit" == "yes" ]; then
        echo $BASENAME: please run jail-commit to add files under `slash $file` into the svn repository
      fi

    else
      echo $BASENAME: file `slash $file` already on $TEMPLATE_BASE.d
      exit 1
    fi
  else
    if [ -a "$jail/$file" ]; then

      mkdir -p $TEMPLATE_BASE.d/`dirname $file`/
      destination="`echo $TEMPLATE_BASE.d/$file | sed -e 's/\/$//'`"
      cp -a $jail/$file $destination
      if use_svn && [ -d "$TEMPLATE_BASE.d/.svn" ]; then
        cwd="`pwd`"
        cd $TEMPLATE_BASE.d
        svn add ./$file
        if [ "$?" != "0" ]; then
          echo $BASENAME: error adding `slash $candidate` into the revision system
        fi
        cd $cwd
        echo $BASENAME: please run jail-commit to add `slash $file` into the svn repository
        true
      else
        echo Added `slash $jail/$file` on $destination
      fi

    else
      echo $BASENAME: `slash $jail/$file`: file not found
      exit 1
    fi
  fi

}

function setroot {

  # set ROOT variable
  # usage: setroot <value1> <value2>
  
  if [ -z "$1" ]; then
    ROOT="/"
  else
    ROOT="$1"
  fi

}

function slash {

  # remove additional slashes
  echo $1 | sed -e 's/\/\+/\//g'

}

# command line parsing

if [ -z "$2" ]; then
  usage
fi

search_template $2 --new
TEMPLATE="$TEMPLATE_BASE.template"

if [ "$1" == "-u" ] || [ "$1" == "--update" ]; then

  setroot $3
  template_update

elif [ "$1" == "-c" ] || [ "$1" == "--create" ]; then

  setroot $3
  template_create

elif [ "$1" == "-a" ] || [ "$1" == "--add" ]; then

  if [ -z "$3" ]; then
    usage
  else
    setroot $4
  fi

  template_add $ROOT $3

elif [ "$1" == "-d" ] || [ "$1" == "--delete" ]; then

  if [ -z "$3" ]; then
    usage
  else

    if [ -a "$TEMPLATE_BASE.d/$3" ]; then
      if use_svn && [ -d "$TEMPLATE_BASE.d/.svn" ]; then
        cd $TEMPLATE_BASE.d
        svn del --force ./$3 || rm -rf ./$3
        echo $BASENAME: please run jail-commit to del $3 in the svn repository
      else
        rm -rf $TEMPLATE_BASE.d/$3
        echo Removed $3 from $TEMPLATE_BASE.d
      fi
    fi

  fi

elif [ "$1" == "-s" ] || [ "$1" == "--sync" ]; then

  if use_svn && [ -d "$BASE_CONF/templates/.svn" ]; then
    ( cd $BASE_CONF/templates && svn update )
    true
  fi

elif [ "$1" == "-e" ] || [ "$1" == "--export" ]; then
  
  import_export_templates export $2

elif [ "$1" == "-i" ] || [ "$1" == "--import" ]; then

  import_export_templates import $2

else
  usage
fi