Projet

Général

Profil

boot.php

Eric Seigne, 30/10/2014 12:18

Télécharger (7,78 ko)

 
1
<?php
2
/** **************************************************************************
3
 * Copyright (C) 2008-2014 Eric Seigne <eric.seigne@ryxeo.com>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 * ***************************************************************************
18
 * File  : boot.php
19
 * Author  : Eric SEIGNE
20
 *           mailto:eric.seigne@ryxeo.com
21
 * Licence : GNU/GPL Version 2
22
 *
23
 * Description:
24
 * ------------
25
 *
26
 * Gestion du script a lancer automatiquement au boot par les clients lourds
27
 * recupere le script boot qui se trouve dans
28
 * /home/machines/$hostname/scripts/horizon-apt
29
 * - Dans l'ordre: on prends le fichier par defaut si il existe
30
 * - Ensuite on enchaine sur les fichiers de groupes
31
 * - Et enfin sur le fichier script spécifique
32
 *
33
 * ************************************************************************* */
34

    
35
  //Totaly quiet
36
@error_reporting(0);
37

    
38
require_once("AbE_Config_Set_Variables.php");
39

    
40
$ip_client   = $_SERVER["REMOTE_ADDR"];
41
$hostname    = gethostbyaddr($ip_client);
42
$hostnametab = explode(".",$hostname);
43
$shortname   = $hostnametab[0];
44
$metamode    = trim(`horizon-getent config metamode -s`);
45
$epackfile   = "/etc/" . $metamode . "/epack";
46

    
47
if($metamode == "abuledu")
48
  $serveur = "servecole";
49
else
50
  $serveur = "serveur";
51

    
52

    
53
//debug print "IP: $ip_client // $hostname // $shortname";
54

    
55
  /*
56
  header('Content-Description: File Transfer');
57
  header('Content-Type: application/octet-stream');
58
  header('Content-Disposition: attachment; filename='.basename($bootfile));
59
  header('Content-Transfer-Encoding: binary');
60
  header('Expires: 0');
61
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
62
  header('Pragma: public');
63
  header('Content-Length: ' . filesize($file));
64
  ob_clean();
65
  flush();*/
66

    
67
//Parcours un repertoire et remplis un tableau
68
function browse_dir_and_push_array($dir,&$tab) {
69
  if(is_dir($dir)) {
70
    $handle=opendir($dir);
71
    while ($file = readdir($handle)){
72
      if($file != "." && $file != "..") {
73
        $tab[] = $file;
74
      }
75
    }
76
    closedir($handle);
77
  }
78
}
79

    
80
//On recupere la version de mise a jour dispo sur ce serveur ...
81
$serverupgradeversion = "0";
82
if(file_exists("/etc/" . $metamode . "/upgradeversion")) {
83
  $fileserver = file("/etc/" . $metamode . "/upgradeversion");
84
  $data = array();
85
  for($i = 0; $i < count($fileserver); $i++) {
86
    $line = explode("=", $fileserver[$i]);
87
    $data[$line[0]] = trim($line[1]);
88
  }
89
  $serverupgradeversion = $data["MYUPGRADE"];
90
}
91

    
92
$bootscript = "#!/bin/bash
93

94
. /lib/lsb/init-functions
95

96
MYUPGRADE=\"\"
97

98
#si le fichier n'existe pas ... on le cree vide
99
if [ ! -f /etc/$metamode/upgradeversion ]; then
100
   touch /etc/$metamode/upgradeversion
101
fi
102

103
. /etc/$metamode/upgradeversion
104

105
#si le poste a deja eu cette mise a jour on ne va pas plus loin
106
if [ \"\${MYUPGRADE}\" -ge \"$serverupgradeversion\" ]; then
107
  exit
108
fi
109

110
service kdm stop
111

112
clear
113

114
echo \"\"
115
echo \"\"
116
echo \"Lancement des scripts systemes ... soyez patients !\"
117
echo \"Vous êtes sur la console qui affichera la progression dans quelques secondes\"
118
echo \"\"
119
echo \"\"
120
echo \"\"
121
echo \"\" > /dev/tty1
122
echo \"Lancement des scripts systemes ... soyez patients !\" > /dev/tty1
123
echo \"Pour voir ce qui se passe passez sur la console 7 ou 8 avec Alt+F7 ou Alt+F8\" > /dev/tty1
124

125
function remove_application() {
126
  log_daemon_msg \" - suppression de \$1\"
127
  apt-get remove -qq -y --force-yes --purge \$1
128
  if [ \"\${?}\" == \"0\" ]; then
129
    log_end_msg 0
130
  else
131
    log_end_msg 1
132
  fi
133
}
134

135
function add_application() {
136
  log_daemon_msg \" - installation de \$1\"
137
  apt-get install -qq -y --force-yes \$1
138
  if [ \"\${?}\" == \"0\" ]; then
139
    log_end_msg 0
140
  else
141
    log_end_msg 1
142
  fi
143
}
144

145
";
146

    
147
//Pave numerique pour ce poste ?
148
//bug #737
149
$cmdnumpad   = "/opt/ltsp/i386/bin/getltscfg -c /opt/ltsp/i386/etc/lts.conf -n " . $shortname . "XKBNUMPAD";
150
$numpad      = trim(`$cmdnumpad`);
151
if($numpad != "") {
152
  $bootscript .= "sed -i -e s/^NumLock=.*/NumLock=On/ /etc/kde4/kdm/kdmrc\n";
153
}
154
else {
155
  $bootscript .= "sed -i -e s/^NumLock=.*/NumLock=Off/ /etc/kde4/kdm/kdmrc\n";
156
}
157

    
158

    
159
//l'inventaire ... que si le serveur est enregistre sur anet
160
if(file_exists($epackfile)) {
161
  $epack       = explode(":",file_get_contents($epackfile));
162
  //fix #582
163
  $bootscript .= "TESTINVENTORY=`grep KEYVALUE /var/lib/ocsinventory-agent/*ocs.ryxeo.com*/ocsinv.adm | cut -d '>' -f2 | cut -d '.' -f1`
164
if [ \"\${TESTINVENTORY}\" != \"" . $epack[0] . "\" ]; then
165
   rm -rf /var/lib/ocsinventory-agent/*ocs.ryxeo.com*
166
fi
167
nohup ocsinventory-agent --tag=" . $epack[0] . "." . str_replace(":","",$conf_ip_mac_address) . ".clientlinux --server ocs.ryxeo.com:8286 --nosoftware&\n";
168
}
169

    
170
$bootscript .= "echo \"\" > /dev/tty1
171
echo \"\" > /dev/tty1
172
echo \"\" > /dev/tty1
173
echo \"Lancement des scripts systemes ... soyez patients !\" > /dev/tty1
174
echo \"Pour voir ce qui se passe passez sur la console 7 ou 8 avec Alt+F7 ou Alt+F8\" > /dev/tty1
175
echo \"\" > /dev/tty1
176
echo \"\" > /dev/tty1";
177

    
178
// en priorite
179
// #955: propagation de nos cles ssh sur les clients lourds
180
$bootscript .= " 
181
mkdir -p /root/.ssh
182
wget http://$serveur/horizon-apt/authorized_keys -O /root/.ssh/authorized_keys
183
chown root:root /root/.ssh
184
chmod 700 /root/.ssh
185
chmod 600 /root/.ssh/authorized_keys
186
";
187

    
188
// urgent #3830 : stopper nscd pour contourner le bug ubuntu security
189
// penser a le relancer a la fin ...
190
$bootscript .= " 
191
service nscd stop
192
";
193

    
194
//Le fichier par defaut pour tout le parc
195
$bootfile = "/home/machines/groups/default/horizon-apt/boot";
196
if(file_exists($bootfile)) {
197
  $bootscript .= file_get_contents($bootfile);
198
  $bootscript .= "\n";
199
}
200

    
201
$addappstab    = array();
202
$removeappstab = array();
203
//L'ajout/suppr des applications
204
$addappsdir    = "/home/machines/groups/default/horizon-apt/applications.add/";
205
$removeappsdir = "/home/machines/groups/default/horizon-apt/applications.remove/";
206
browse_dir_and_push_array($addappsdir,    $addappstab);
207
browse_dir_and_push_array($removeappsdir, $removeappstab);
208
/* pour les groupes -> a implementer 
209
$bootfile = "/home/machines/groups/$groupname/horizon-apt/boot";
210
if(file_exists($bootfile)) {
211
  $bootscript .= file_get_contents($bootfile);
212
  $bootscript .= "\n";
213
}
214
*/
215

    
216
//Et enfin, le fichier spécifique
217
$bootfile = "/home/machines/" . $shortname . "/horizon-apt/boot";
218
if(file_exists($bootfile)) {
219
  $bootscript .= file_get_contents($bootfile);
220
  $bootscript .= "\n";
221
}
222
$addappsdir    = "/home/machines/" . $shortname . "/horizon-apt/applications.add/";
223
$removeappsdir = "/home/machines/" . $shortname . "/horizon-apt/applications.remove/";
224
browse_dir_and_push_array($addappsdir,    $addappstab);
225
browse_dir_and_push_array($removeappsdir, $removeappstab);
226

    
227
$tabadd = array_unique($addappstab);
228
$tabremove = array_unique($removeappstab);
229

    
230
foreach($tabremove as $remove) {
231
  $bootscript .= "remove_application $remove \n";
232
}
233

    
234
foreach($tabadd as $add) {
235
  $bootscript .= "add_application $add \n";
236
}
237

    
238
// urgent #3830 : penser a le relancer a la fin ...
239
$bootscript .= " 
240
service nscd start
241
";
242

    
243

    
244
$bootscript .= "
245
echo \"Mise à jour terminée\" > /dev/tty1
246
echo \"\" > /dev/tty1
247
service kdm start \n";
248

    
249
$bootscript .= "echo \"\" > /dev/tty1
250
echo \"\" > /dev/tty1
251
echo \"\" > /dev/tty1
252
echo \"Mise à jour terminée\" > /dev/tty1
253
echo \"\" > /dev/tty1
254
echo \"\" > /dev/tty1
255

256
echo MYUPGRADE=$serverupgradeversion >| /etc/$metamode/upgradeversion
257
";
258

    
259
print $bootscript;
260
exit;
261
?>
Redmine Appliance - Powered by TurnKey Linux