summaryrefslogtreecommitdiffstats
path: root/data/fetch-config
blob: 556f0f41055d0d1fb4ac6c88225722f89f757220 (plain) (blame)
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
#!/bin/sh
# Copyright (c) 2013 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your feedback to feedback@openslx.org
#
# General information about OpenSLX can be found under http://openslx.org
#
# Fetch configuration from server ...

#############################################################################

# Depends on configured networking, provides configuration


# Todo: rewrite for next-gen system - principle would be the same ...

# Function for retrieving configuration file (machine-setup) via tftp from a
# predefined server or given source (file=tftp-server:/path via kernel
# command line)
unpack () {
# $1 is  config file name to get, $2 IP of server to get file from
local dst=$1
if [ -s $dst ] ; then
  # fixme: handle different types of packaging (gzip/bzip2)??
  if ! tar -xpzf $dst 2> /tmp/ConfTGZ-tar-error ; then
     cat /tmp/ConfTGZ-tar-error
     error "$unpack_ConfTGZ" nonfatal
     rm /tmp/ConfTGZ-tar-error
  fi
  [ "$DEBUGLEVEL" -le 2 -o "$DEBUGLEVEL" -eq 8  ] && rm $dst
  return 0
else
  return 1
fi
}

# tftp wrapper
# usage tftp_get <path> <tftpdserver> [count]
# count is optional - default is 3 - use -1 for indefinit 
tftp_get () {
  local file="$1"
  local file_server="$2"
  local download_successful=0
  local countdown="$3"

  if [ -z "$1" -o -z "$2" ]; then
    [ $DEBUGLEVEL -ge 1 ] && \
      echo "[tftp_get] Usage: tftp_get <path> <server> [count]" 
    return 1;
  fi

  [ "$countdown" = "" ] && countdown=3

  until [ $download_successful -eq 1 ]
  do
    if [ "$countdown" = "0" ]; then
      [ $DEBUGLEVEL -ge 1 ] && \
        echo "[tftp_get] download of \"$file\" from \"$file_server\" ... failed"
      return 0;
    fi
    tftp -g -r "/$file" -l /tmp/$(basename $file) $file_server
    [ -s /tmp/$(basename $file) ] && download_successful=1
    countdown=$(expr $countdown - 1)
    usleep 200000
  done
  [ $DEBUGLEVEL -ge 1 ] && \
    echo "[tftp_get] download of \"$file\" from \"$file_server\" ... successful"
  return 0;
}


# wget wrapper
# usage wget_get <path> <ftp/httpserver> [count]
# count is optional - default is 3 - use -1 for indefinit 
wget_get () {
  local file="$1"
  local file_server="$2"
  local download_successful=0
  local countdown="$3"

  if [ -z "$1" -o -z "$2" ]; then
    [ $DEBUGLEVEL -ge 1 ] && \
      echo "[wget_get] Usage: wget_get <path> <server> [count]" 
    return 1;
  fi

  [ "$countdown" = "" ] && countdown=3

  until [ $download_successful -eq 1 ]
  do
    if [ "$countdown" = "0" ]; then
      [ $DEBUGLEVEL -ge 1 ] && \
        echo "[wget_get] download of \"$file\" from \"$file_server\" ... failed"
      return 0;
    fi
    wget -q $file_server$file -O /tmp/$(basename $file)
    [ -s /tmp/$(basename $file) ] && download_successful=1
    countdown=$(expr $countdown - 1)
    usleep 200000
  done
  [ $DEBUGLEVEL -ge 1 ] && \
    echo "[wget_get] download of \"$file\" from \"$file_server\" ... successful"
  return 0;
}


fileget () {
# normally tftp would be used, alternatively use wget for ftp or http
# if local device file is specified - mount and unmount after copying
local cfgfile
[ "x$fileprot" = "x" ] && fileprot=tftp
if [ "x$filepath" != "x" ] ; then
  cfgfile=${filepath}
  [ "x$fileserv" = "x" ] && fileserv=$(checkip ${serverip})
  # wait for dns if "fileserv" is a name and not lbd device
  [ "$fileprot" != "lbd" ] && \
    echo ${fileserv} | grep -qi [a-z]
  [ $DEBUGLEVEL -ge 1 ] && echo "fileget -  fileprot:$fileprot, filepath:\
$filepath, fileserv:$fileserv" >>$LOGFILE
  case "$fileprot" in
   ftp|http)
     wget_get $cfgfile $fileprot://$fileserv \
      && { unpack /tmp/$(basename $cfgfile) && break; } 2>>$LOGFILE 
   ;;
   lbd)
     local ldev=$fileserv
     echo "Waiting for configuration file ${cfgfile} ...."
     [ $DEBUGLEVEL -ge 1 ] && echo "fileget -  fileprot:$fileprot, filepath:\
$filepath, fileserv:$fileserv" >>$LOGFILE
     waitfor /mnt/${cfgfile} 10000
     if [ -f /mnt/${cfgfile} ]; then
       unpack /mnt/$cfgfile
     else
       error "$init_errlfg"
     fi
   ;;
   *)
     tftp_get $cfgfile $fileserv  \
       && unpack /tmp/$(basename $cfgfile) 2>>$LOGFILE
   ;;
   esac
else
  # predefined value for OpenSLX environment; it is expected that this
  # directory is just below the tftpboot (path to which the daemon is
  # restricted to)
  filepath="client-config"
  [ "x$fileserv" = "x" ] && fileserv=$(checkip ${serverip})
  [ $DEBUGLEVEL -ge 1 ] && echo "fileget - fileprot:$fileprot, filepath:\
$filepath, fileserv:$fileserv" >>$LOGFILE
  # try to get configuration files successively; start with distro client
  # and try last distro default ...
  mac=$(echo $macaddr|sed "s/:/-/g")
  for cfgfile in ${filepath}/${SYSTEM_NAME}/01-$mac.tgz \
                 ${filepath}/${SYSTEM_NAME}/default.tgz ; do
    case "$fileprot" in
      ftp|http)
        wget $fileprot://$fileserv/$cfgfile -O /tmp/$(basename $cfgfile) \
          2>>$LOGFILE && { unpack /tmp/$(basename $cfgfile) && break; }
      ;;
      tftp)
        tftp_get $cfgfile $fileserv \
          2>>$LOGFILE && { unpack /tmp/$(basename $cfgfile) && break; }
      ;;
    esac
  done
  echo -e "\n## Configuration via fileget from ${fileprot}://${fileserv}/\
${cfgfile}\n# Hierarchy is distro client and as last distro/default" \
    >>/tmp/confviafile
fi
cat /initramfs/machine-setup >>/tmp/confviafile 2>/dev/null || \
  error "$nomachsetup"
echo "fileget via $fileprot from $fileserv/$cfgfile finished" >/tmp/file-done
[ $DEBUGLEVEL -ge 1 ] && echo "fileget from $cfgfile finished" >>$LOGFILE
}