alternc.config 9.58 KB
Newer Older
Alan Garcia's avatar
Alan Garcia committed
1
#! /bin/bash 
2
3

set -e
4

5
6
7
# Source debconf library.
. /usr/share/debconf/confmodule

8
db_capb backup
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
#Return  if everything is good, exit error number otherwise
function valid_ip()
{
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    return $stat
}


#checking mysql connectivity and updating local.sh variables accordingly
check_mysql(){

  STATE=0
    while [ "$STATE" -eq 0 ]; do
      db_input high alternc/mysql/host || true
        db_go
        db_get alternc/mysql/host || true
        MYSQL_HOST="$RET"
        db_input high alternc/mysql/remote_user || true
        db_go
        db_get alternc/mysql/remote_user || true
        MYSQL_USER="$RET"
        db_input high alternc/mysql/remote_password || true
        db_go
        db_get alternc/mysql/remote_password || true
        MYSQL_PASSWORD="$RET"
        db_input high alternc/mysql/client || true
        db_go
        db_get alternc/mysql/client || true
        MYSQL_CLIENT="$RET"
        if [ "`mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" -h"$MYSQL_HOST" -Bse 'SELECT "OK";' 2>/dev/null`" = "OK" ]; then
          STATE=`expr $STATE + 1`
        else
          db_input high alternc/remote_mysql_error || true
            db_go
            db_get alternc/remote_mysql_error || true
            if [ "$RET" = "false" ]; then
              exit 4
            fi
        fi    
    done

}
64
65
66
67
68
69

# default values for local.sh
MYSQL_HOST=127.0.0.1
MYSQL_DATABASE=alternc
MYSQL_USER=sysusr
MYSQL_PASS="`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..10)' `"
70
MYSQL_MAIL_USER=alternc_user
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
71
MYSQL_MAIL_PASS="`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..10)' `"
72
MYSQL_CLIENT=localhost
Alan Garcia's avatar
Alan Garcia committed
73
FQDN="`(cat /etc/mailname 2>/dev/null || hostname -f)|tr '[:upper:]' '[:lower:]'`"
74
INTERNAL_IP="`env LANG=C /sbin/ifconfig|grep inet | grep -v 127.0.0.1| head -1 | cut -d: -f2 | sed -e 's/\([0-9\.]*\).*$/\1/' 2>/dev/null || hostname -f`"
75
76
77
78
79
80
PUBLIC_IP="$INTERNAL_IP"
DEFAULT_MX="`cat /etc/mailname 2>/dev/null || hostname -f`"
ALTERNC_LOC=/var/alternc
NS1_HOSTNAME="$FQDN"
NS2_HOSTNAME="$FQDN"
HOSTING="AlternC"
81
82
SQLBACKUP_TYPE="rotate"
SQLBACKUP_OVERWRITE="no"
83
84
QUEST_STATE=1

85
while [ "$QUEST_STATE" != 0 -a "$QUEST_STATE" != 11 ]; do
86
87
88
89
90
91
  case "$QUEST_STATE" in
  1)
    if [ -r /etc/alternc/local.sh ]; then
        # source the current config
        . /etc/alternc/local.sh
    fi
Alan Garcia's avatar
Alan Garcia committed
92

93
94
95
96
97
    #We ask for the hosting name and the FQDN
    db_set alternc/hostingname "$HOSTING"
    db_set alternc/desktopname "$FQDN"
    db_input high alternc/hostingname || true
  ;;
98
  2)
99
100
101
102
103
104
105
    # Get the FQDN
    db_input high alternc/desktopname || true
    # Be sure that the FQDN is lowercase (Bug #1405)
    db_get alternc/desktopname
    db_set alternc/desktopname "`echo $RET | tr '[:upper:]' '[:lower:]'`"
    # End bug #1405
  ;;
106
  3)
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    #we ask for the public and private ip
    db_set alternc/public_ip "$PUBLIC_IP"
    STATE=0
    while [ $STATE -eq 0 ]; do
      db_input high alternc/public_ip || true
      db_go
      db_get alternc/public_ip 
      ip="$RET"
      if  valid_ip $ip ; then 
        if [ $(echo $ip |egrep '(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1)$') ]; then
          db_input high alternc/use_private_ip || true
          db_go
          db_get alternc/use_private_ip
            if [ "$RET" = "true" ]; then
              STATE=1
122
123
124
	    else
              db_reset alternc/use_private_ip || true
              db_fset alternc/use_private_ip "seen" "false" || true
125
126
127
128
129
            fi
        else
          STATE=1
        fi
      else
130
131
        STATE=1
      fi
132
133
    done
  ;;
134
  4)
135
    #private IP
136

137
    db_set alternc/internal_ip "$INTERNAL_IP"
138

139
140
141
142
143
144
145
146
147
148
149
    STATE=0
    while [ $STATE -eq 0 ]; do
      db_input high alternc/internal_ip || true
      db_go
      db_get alternc/internal_ip 
      ip="$RET"
      if  valid_ip $ip ; then 
        STATE=1
      fi 
    done
  ;;
150
  5)
151
152
153
154
155
156
    #We ask for the DNS server for the ip
    #
    db_set alternc/ns1 "$NS1_HOSTNAME"

    db_input high alternc/ns1 || true
  ;;
157
  6)
158
159
160
    db_set alternc/ns1 "$NS2_HOSTNAME"
    db_input high alternc/ns2 || true
  ;;
161
  7)
162
163
164
    db_set alternc/default_mx "$DEFAULT_MX"
    db_input high alternc/default_mx || true
  ;;
165
  8)
166
167
168
169
170
171
    if [ "`mysql --defaults-file=/etc/mysql/debian.cnf -Bse 'SELECT "OK";' 2>/dev/null`" = "OK" ]; then
      db_input critical alternc/use_local_mysql || true
    else
      db_input critical alternc/use_remote_mysql || true
    fi
  ;;
172
  9)
173
    db_get alternc/use_local_mysql || true
174
    if [ -z "$RET" -o "$RET" == "false" ]
175
176
177
      then
      #user want to use a remote server
      check_mysql
178
179
    fi
  ;;
180
  10)
181
    # We need to set the alternc_location here because we have to test for quotas and acls 
182
    db_set alternc/alternc_location "$ALTERNC_LOC"
183

184
185
186
187
188
189
190
191
192
193
194
195
    db_get alternc/alternc_location
    LOCATION="$RET"
    #checking acl and quota activation, does't work with NFS yet. should now work with lxc
    MOUNT_POINT=$(df -P ${LOCATION%/*} | tail -n 1 | awk '{print $6}')

    # Get the mount options for the path: (warning, / may have multiple declarations, take the last one only :
    MOUNT_OPTS=$(awk '$2=="'$MOUNT_POINT'" { print $4 }' /proc/mounts | tail -1)

    # Check to see if acl is one of the mount points:
    ACL="$(echo "$MOUNT_OPTS" | tr "," "\\n" | egrep '^acl|attr2|user_xattr$')" || true
    if [ -z "$ACL" ]; then
      db_input critical alternc/acluninstalled || true
196
      db_go
197
198
199
200
201
      db_reset alternc/acluninstalled || true
      db_fset alternc/acluninstalled "seen" "false" || true
      exit 2
    fi
    quota_activation=$(quotaon -pa 2>/dev/null |grep '^group.*on$' | /usr/bin/awk '$4 ~ /^\'$MOUNT_POINT'$/ {print $4}') 
202
    
203
    if [ -z "$quota_activation" -o "$quota_activation"  != "$MOUNT_POINT" ]; then
204
205
206
207
208
209
210
        db_get alternc/quotauninstalled
        if [ -z "$RET" ]; then
          db_input critical alternc/quotauninstalled || true
          db_go
          #db_reset alternc/quotauninstalled || true
          db_set alternc/quotauninstalled "false" || true
        fi
211
212
213
214
215
216
217
218
219
    fi
  ;;
  esac
  if db_go; then
    QUEST_STATE=$(($QUEST_STATE + 1))
  else 
    QUEST_STATE=$(($QUEST_STATE - 1))
  fi
done
220

221
222
223
224
225
226
227
228
if [ -r /etc/alternc/my.cnf ]; then
    # make mysql configuration available as shell variables
    # to convert from .cnf to shell syntax, we:
    # * match only lines with "equal" in them (/=/)
    # * remove whitespace around the = and add a left quote operator ' (;s)
    # * add a right quote operator at the end of line (;s)
    # * convert mysql variables into our MYSQL_ naming convention (;s)
    # * print the result (;p)
229
    eval `sed -n -e "/=/{s/ *= *\"\?/='/;s/\"\?\$/'/;s/host/MYSQL_HOST/;s/user/MYSQL_USER/;s/password/MYSQL_PASS/;s/database/MYSQL_DATABASE/;p}" /etc/alternc/my.cnf`
230
fi
231

Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
232
233
234
235
236
237
238
239
240
241
if [ -r /etc/alternc/my_mail.cnf ]; then
    # make mysql configuration available as shell variables
    # to convert from .cnf to shell syntax, we:
    # * match only lines with "equal" in them (/=/)
    # * remove whitespace around the = and add a left quote operator ' (;s)
    # * add a right quote operator at the end of line (;s)
    # * convert mysql variables into our MYSQL_ naming convention (;s)
    # * print the result (;p)
    eval `sed -n -e "/=/{s/ *= *\"\?/='/;s/\"\?\$/'/;s/host/MYSQL_HOST/;s/user/MYSQL_MAIL_USER/;s/password/MYSQL_MAIL_PASS/;s/database/MYSQL_DATABASE/;p}" /etc/alternc/my_mail.cnf`
fi
242

243

244
245
246
247
248
249
250
251
252
db_get alternc/hostingname
if [ -z "$RET" ]
    then
    db_set alternc/hostingname "$HOSTING"
fi

db_get alternc/desktopname
if [ -z "$RET" ]
    then
Alan Garcia's avatar
Alan Garcia committed
253
    db_set alternc/desktopname "$FQDN"
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
fi


db_get alternc/monitor_ip
if [ -z "$RET" ]
    then
db_set alternc/monitor_ip "$MONITOR_IP"
fi

db_get alternc/ns2
if [ -z "$RET" ]
    then
db_set alternc/ns2 "$NS2_HOSTNAME"
fi

269
270
271
272
273
274
db_get alternc/default_mx2
if [ -z "$RET" ]
    then
db_set alternc/default_mx2 "$DEFAULT_SECONDARY_MX"
fi

275
276
277
278
279
280
db_get alternc/mysql/host
if [ -z "$RET" ]
    then
db_set alternc/mysql/host "$MYSQL_HOST"
fi

281
282
#Even if we asked the question concerning the database earlier in the process
#those calls are needed to pass the variable of remote sql server to AlternC
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
db_get alternc/mysql/db
if [ -z "$RET" ]
    then
db_set alternc/mysql/db "$MYSQL_DATABASE"
fi

db_get alternc/mysql/user
if [ -z "$RET" ]
    then
db_set alternc/mysql/user "$MYSQL_USER"
fi

db_get alternc/mysql/password
if [ -z "$RET" ]
    then
db_set alternc/mysql/password "$MYSQL_PASS"
fi

db_get alternc/mysql/client
if [ -z "$RET" ]
    then
db_set alternc/mysql/client "$MYSQL_CLIENT"
fi

307
308
309
310
311
312
313
314
315
316
317
318
db_get alternc/sql/backup_type
if [ -z "$RET" ]
    then
db_set alternc/sql/backup_type "$SQLBACKUP_TYPE"
fi

db_get alternc/sql/backup_overwrite
if [ -z "$RET" ]
    then
db_set alternc/sql/backup_overwrite "$SQLBACKUP_OVERWRITE"
fi

319
db_get alternc/mysql/alternc_mail_user
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
320
321
322
323
324
if [ -z "$RET" ]
    then
db_set alternc/mysql/alternc_mail_user "$MYSQL_MAIL_USER"
fi

325
db_get alternc/mysql/alternc_mail_password
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
326
327
328
329
if [ -z "$RET" ]
    then
db_set alternc/mysql/alternc_mail_password "$MYSQL_MAIL_PASS"
fi
330

331
db_input low alternc/default_mx2 || true
332
333
334
335
db_input low alternc/mysql/host || true
db_input low alternc/mysql/db || true
db_input low alternc/mysql/user || true
db_input low alternc/mysql/password || true
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
336
337
db_input low alternc/mysql/alternc_mail_password || true
db_input low alternc/mysql/alternc_mail_user || true
338
db_input low alternc/mysql/client || true
339
340
db_input low alternc/sql/backup_type || true
db_input low alternc/sql/overwrite || true
341
db_input low alternc/monitor_ip || true
342
db_input low alternc/slaves || true
343
344
345
db_go

# vim: et sw=4