aboutsummaryrefslogtreecommitdiff
path: root/trunk/src/jail-commit
blob: 45aea1eb6269dece85361537ed5dc54ca202f3d8 (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
#!/bin/bash
#
# jail-commit: update config files from a jail to a template
# feedback: rhatto@riseup.net | gpl
#
#  Jail-commit 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-commit 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"

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

function usage {

  echo $BASENAME: commit a jail configuration into a template
  echo usage: $BASENAME [<jail-path> [template]]

}

function template_merge {

  # copy differences between the jail
  # and the template in the template folder

  # usage: template_merge <jail-path>

  if [ -z "$1" ] || [ ! -d "$TEMPLATE_BASE.d" ]; then
    return 1
  fi

  echo "" > $TEMPLATE_BASE.perms
  cd $TEMPLATE_BASE.d

  for file in `find`; do
    if [[ -f "$file" && -f "$1/$file" ]]; then
      if ! diff $file $1/$file; then
        echo updating $file
        cp -af $1/$file $file
      fi
      perms="`numeric_perm $1/$file`"
      owner="`get_owner $1/$file`"
      group="`get_group $1/$file`"
      echo "$file;$owner;$group;$perms" >> $TEMPLATE_BASE.perms
    else
      if [ ! -f "$file" ]; then
        echo $BASENAME: warning: missing file $file 
      else
        echo $BASENAME: warning: missing template file $1/$file 
      fi
    fi
  done

}

function template_svn_commit {

  if use_svn; then
    cd `basedir $TEMPLATE_BASE`
    svn commit -m "changes for `date`"
  fi

}

function do_commit {

  # commit jail changes to a repository
  # usage: do_commit <jailpath> [template]

  local jailpath template

  jailpath="$1"
  jail="`basename $jailpath`"

  if [ ! -z "$2" ]; then
    template="$2"
  else
    template="$jail"
  fi

  search_template $template --update
  if [ "$?" == "0" ]; then
    if [ "$jailpath" == "/" ]; then
      echo updating main installation...
    else
      echo updating $jailpath...
    fi
    if [ -d "$TEMPLATE_BASE.d" ] || [ -a "$TEMPLATE_BASE.template" ]; then
      templatepkg -u $jail $jailpath
      template_merge $jailpath
      template_svn_commit $TEMPLATE_BASE
    fi
  fi

}

if [ ! -z "$1" ]; then
  do_commit $1 $2
else
  if [ -f $JAIL_LIST ]; then
    for jailpath in `cat $JAIL_LIST`; do
      do_commit $jailpath
    done
  fi
  # main jail
  do_commit / main
fi