Ce script permet de créer un site complet sur un serveur (dossiers, logs, stats, base MySQL, etc...). Il nécessite une certaine structure dans la gestion des espaces web. Il nécessite aussi : a2ensite, pwgen et un fichier /root/.my.cnf pour ne pas avoir à rentrer les mot de passe MySQL de l'utilisateur root.
1 #!/bin/bash
2 ###############################################################################
3 #
4 # new_site : Permet de créer un site web sur un serveur
5 #
6 # Requiert :
7 # - a2ensite
8 # - pwgen
9 # - Fichier /root/.my.cnf bien configuré ;-)
10 #
11 # by Guillaume Kulakowski a.k.a LLaumgui <guillaume at llaumgui dot com>
12 # Version 1.0
13 #
14 ###############################################################################
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not,
27 # - write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 # - See http://www.gnu.org/licenses/gpl.html
30 ###############################################################################
31
32
33 # Varibles
34 VHOST_MODEL='/etc/httpd/vhost.model.conf' # Chemin vers le modèle pour les vhost
35 VHOST_PATH='/etc/httpd/sites-available' # Chemin vers les fichiers vhost
36
37 AWSTATS_MODEL='/etc/httpd/awstats.model.conf' # Chemin vers le modèle awstats
38 AWSTATS_PATH='/etc/awstats' # Chemin vers les fichiers de conf awstats
39 AWSTATS=1 # Activer Awstats
40 MAIL_ADMIN=root # Mail de l'administrateur
41
42 # Valeurs par défaut
43 DEF_WWW_PATH="/home/llaumgui/public_html" # Chemin par défaut pour les sites
44 DEF_SERVER_ADMIN="spb-box@llaumgui.com" # Valeur par défaut pour ServerAdmin
45
46
47
48
49
50 ##
51 ## Affichage du récapitulatif
52 ##
53 function recap(){
54
55 echo "Récapitulatif"
56 echo "================================"
57 echo -e "\nConfiguration du site"
58 echo "-----------------------------"
59 echo " Adresse du site : http://$WWW_URL"
60 echo " Chemin vers le site : $WWW_PATH"
61 if [ "$PERM" != "" ]; then
62 echo " Permissions données à : $PERM"
63 fi
64
65 echo -e "\nConfiguration d'apache"
66 echo "-----------------------------"
67 echo " ServerAdmin : $SERVER_ADMIN"
68
69 echo -e "\nConfiguration de MySQL"
70 echo "-----------------------------"
71 if [ "$DB_NAME" != "" ]; then
72 echo " Base de données : $DB_NAME"
73 echo " Nom de l'utilisateur : $DB_LOGIN"
74 if [ "$1" = 0 ]; then
75 echo " Mot de passe MySQL : $DB_PASSWD"
76 else
77 echo " Mot de passe MySQL : ********"
78 fi
79 else
80 echo " *** Pas de base de données ***"
81 fi
82
83 echo -e "\nConfiguration d'Awstats"
84 echo "-----------------------------"
85 if [ $AWSTATS = 0 ]; then
86 echo " Stats Awstats : Activé"
87 else
88 echo " Stats Awstats : Désactivé"
89 fi
90 }
91
92
93
94 ##
95 ## Valider la récapitulation
96 ##
97 function recap_valid() {
98
99 echo -n "Est-ce correct ? (o/[n]) "; read CORRECT
100 if [ "$CORRECT" = "" ]; then
101 recap_valid
102 fi
103 if [ "$CORRECT" != "o" -a "$CORRECT" != "O" -a "$CORRECT" != "y" -a "$CORRECT" != "Y" ]; then
104 exit 1
105 fi
106
107 return 0
108 }
109
110
111
112
113
114 ###################################
115 ##
116 ## Définition des paramètres
117 ##
118 ###################################
119
120 ##
121 ## Défini l'url du site :
122 ##
123 function set_url() {
124
125 echo -n "URL principale du site (ex: www.google.fr) ? "; read WWW_URL
126
127 if [ "$WWW_URL" = "" ]; then
128 echo -e "\n\nErreur ! Veuillez renseigner une URL pour le site !";
129 set_url
130 fi
131
132 if [ -f "$VHOST_PATH/$WWW_URL" ]; then
133 echo -e "\n\nErreur ! Le site existe déjà !";
134 set_url
135 fi
136
137 return 0
138 }
139
140
141
142 ##
143 ## Défini le chemin vers le site
144 ## @TODO : Mettre en place la vérification du chemin.
145 function set_path() {
146
147 echo -n "Répertoire du site [$DEF_WWW_PATH/$WWW_URL] ? "; read WWW_PATH
148
149 if [ "$WWW_PATH" = "" ]; then
150 WWW_PATH="$DEF_WWW_PATH/$WWW_URL";
151 fi
152
153 if [ -d "$WWW_PATH" ]; then
154 echo -e "\n\nErreur ! Le site existe déjà !";
155 set_path
156 fi
157
158 echo -n "Donner les permissions sur $WWW_PATH/$WWW_URL/www à l'utilisateur UNIX [] ? "; read PERM
159
160 return 0
161 }
162
163
164
165 ##
166 ## Défini le paramètre ServerAdmin du vhost
167 ##
168 function set_serveradmin() {
169
170 echo -n "ServerAdmin [$DEF_SERVER_ADMIN] ? "; read SERVER_ADMIN
171
172 if [ "$SERVER_ADMIN" = "" ]; then
173 SERVER_ADMIN="$DEF_SERVER_ADMIN";
174 fi
175
176 return 0
177 }
178
179
180
181 ##
182 ## Défini le nom de la base
183 ##
184 function set_db_name() {
185
186 echo -n "Nom de la base de données [] ? "; read DB_NAME
187
188 if [ "$DB_NAME" = "" ]; then
189 return 1
190 fi
191
192 mysqlshow -u root "$DB_NAME" &> /dev/null
193 if [ $? -eq 0 ] ; then
194 echo -e "\n\nLa base de données $DB_NAME existe déjà !";
195 set_db_name
196 fi
197
198 return 0
199 }
200
201
202
203 ##
204 ## Défini du login pour la connection à la base
205 ## @TODO : Mettre en place la vérification de l'user.
206 ##
207 function set_db_login() {
208
209 echo -n "Login pour la connection à la base de données ? "; read DB_LOGIN
210
211 return 0
212 }
213
214
215
216
217 ##
218 ## Définition du mot de passe BD
219 ##function set_db_password() {
220
221 stty -echo;
222 echo -n "Mot de passe pour la base de données [] ? "; read DB_PASSWD1;
223
224 if [ "$DB_PASSWD1" = "" ]; then
225 stty echo;
226 echo -e "\n\nGénération d'un mot de passe avec pwgen..."
227 DB_PASSWD=`pwgen 16 1`
228 echo "$DB_PASSWD"
229 return 0
230 fi
231 echo -ne "\nVeuillez retaper le mot de passe [] ? "; read DB_PASSWD2;
232 if [ $DB_PASSWD1 != $DB_PASSWD2 ]; then
233 echo -e "\n\nLes 2 mots de passe saisis sont dissérents !";
234 set_db_password
235 fi
236 stty echo;
237 DB_PASSWD=$DB_PASSWD1
238
239 return 0
240 }
241
242
243
244 ##
245 ## Définition des stats
246 ##
247 function set_awstats() {
248
249 echo -n "Activer les stats Awstats ? (o/[n]) "; read TMP_AWSTATS
250 if [ "$TMP_AWSTATS" = "o" -o "$TMP_AWSTATS" = "O" -o "$TMP_AWSTATS" = "y" -o "$TMP_AWSTATS" = "Y" ]; then
251 AWSTATS=0
252 else
253 AWSTATS=1
254 fi
255
256 return 0
257 }
258
259
260
261 ##
262 ## Définition du login htaccess pour awstats
263 ##
264 function set_awstats_login() {
265
266 echo -n "Login htaccess pour Awstats ? "; read AW_LOGIN
267
268 if [ "$AW_LOGIN" = "" ]; then
269 set_awstats_login
270 fi
271
272 return 0
273 }
274
275
276
277 ##
278 ## Définition du mot de passe htaccess pour awstats
279 ##
280 function set_awstats_password() {
281
282 stty -echo;
283 echo -n "Mot de passe htaccess pour Awstats [] ? "; read AW_PASSWD1;
284
285 if [ "$AW_PASSWD1" = "" ]; then
286 stty echo;
287 echo -e "\n\nGénération d'un mot de passe avec pwgen..."
288 AW_PASSWD=`pwgen 16 1`
289 echo "$AW_PASSWD"
290 return 0
291 fi
292 echo -ne "\nVeuillez retaper le mot de passe [] ? "; read AW_PASSWD2;
293 if [ $AW_PASSWD1 != $AW_PASSWD2 ]; then
294 echo -e "\n\nLes 2 mots de passe saisis sont dissérents !";
295 set_awstats_password
296 fi
297 stty echo;
298 AW_PASSWD=$AW_PASSWD1
299
300 return 0
301 }
302
303
304
305
306
307 ###################################
308 ##
309 ## Action de création du domaine
310 ##
311 ###################################
312
313 ##
314 ## Création des répertoires web
315 ##
316 function new_www() {
317
318 mkdir "$WWW_PATH"; echo "Création du répertoire $WWW_PATH"
319 mkdir "$WWW_PATH/www"; echo "Création du répertoire $WWW_PATH/www"
320 mkdir "$WWW_PATH/logs"; echo "Création du répertoire $WWW_PATH/logs"
321 mkdir "$WWW_PATH/awstats"; echo "Création du répertoire $WWW_PATH/awstats"
322
323 if [ "$PERM" != "" ]; then
324 chown "$PERM":"$PERM" "$WWW_PATH/www"; echo "Attribution des droits sur le répertoire www"
325 fi
326
327 return 0
328 }
329
330
331
332 ##
333 ## Configuration d'apache
334 ##
335 function new_vhost() {
336
337 new_vhost=`cat $VHOST_MODEL | \
338 sed "s,@@ServerAdmin@@,$SERVER_ADMIN,g" | \
339 sed -e "s,@@DocumentRoot@@,$WWW_PATH,g" | \
340 sed "s,@@ServerName@@,$WWW_URL,g"`
341
342 if [ $AWSTATS = 0 ]; then
343 new_vhost=`echo "$new_vhost"|sed "s,#@@,,g"`
344 fi
345
346 echo "$new_vhost" > "$VHOST_PATH/$WWW_URL"; echo "Configuration du VirtualHost"
347
348 vi "$VHOST_PATH/$WWW_URL"
349 return 0
350 }
351
352
353
354 ##
355 ## Configuration d'Awstats
356 ##function new_awstats() {
357 cat $AWSTATS_MODEL | \
358 sed -e "s,@@DocumentRoot@@,$WWW_PATH,g" | \
359 sed "s,@@ServerName@@,$WWW_URL,g" \
360 > "$AWSTATS_PATH/awstats.$WWW_URL.conf"; echo "Configuration des stats"
361
362 vi "$AWSTATS_PATH/awstats.$WWW_URL.conf"
363
364 htpasswd -cb $WWW_PATH/.htpasswdstats $AW_LOGIN $AW_PASSWD
365 return 0
366 }
367
368
369
370 ##
371 ## Création de la base de données
372 ##
373 function new_db() {
374 mysqladmin -u root create "$DB_NAME"
375 if [ $? -eq 0 ] ; then
376 echo "Base de données $DB_NAME créée avec succès"
377 else
378 echo "Erreur lors de la création de la base de données $DB_NAME"
379 return 1
380 fi
381
382 mysql -u root mysql -e "GRANT ALL ON \`$DB_NAME\`.* TO \"$DB_LOGIN\"@\"localhost\" IDENTIFIED BY \"$DB_PASSWD\";"
383 if [ $? -eq 0 ] ; then
384 echo "Utilisateur $DB_LOGIN créé avec succès"
385 else
386 echo "Erreur lors de la création de l'utilisateur $DB_LOGIN"
387 return 1
388 fi
389 return 0
390 }
391
392
393
394
395
396 ###################################
397 ##
398 ## Code brut
399 ##
400 ###################################
401
402 # Questions
403 echo -e "\nConfiguration du site"
404 echo -e "================================\n"
405 set_url
406 set_path
407
408 echo -e "\n\nConfiguration d'apache"
409 echo -e "================================\n"
410 set_serveradmin
411
412 echo -e "\n\nConfiguration de MySQL"
413 echo -e "================================\n"
414 set_db_name
415 if [ "$DB_NAME" != "" ]; then
416 set_db_login
417 set_db_password
418 fi
419
420 echo -e "\n\nConfiguration d'Awstats"
421 echo -e "================================\n"
422 set_awstats
423 if [ "$AWSTATS" = 0 ]; then
424 set_awstats_login
425 set_awstats_password
426 fi
427 echo -e "\n\n\n\n"
428
429
430 # Récapitulatif
431 recap
432 recap_valid 1
433
434
435 # All is good !
436 echo -e "\n\n"
437 new_www
438 new_vhost
439 if [ "$DB_NAME" != "" ]; then new_db; fi
440 if [ "$AWSTATS" = 0 ]; then new_awstats; fi
441
442 if [ "$MAIL_ADMIN" != "" ]; then
443 recap 0 | mail -s "Création d'un site sur le serveur `hostname`" to-addr "$MAIL_ADMIN"
444 fi
445
446 /root/bin/a2ensite $WWW_URL
Ouverture et Enregistrement XML en Python
1 import xml.dom
2 import xml.dom.ext.reader.Sax2
3 import xml.dom.ext
4
5 # Ouverture du fichier XML
6 f = open('fichier.xml'), 'r')
7 reader = xml.dom.ext.reader.Sax2.Reader()
8 dom = reader.fromStream(f)
9
10 # Manipulation du DOM
11 # ...
12
13 # Enregistrement
14 xml.dom.ext.Print(dom, open('fichier.xml', 'w'))
15 # Enregistrement indenté
16 xml.dom.ext.PrettyPrint(dom, open('fichier.xml', 'w'))
Pages : 1