REPORT de la branche 1.1
[euphorik.git] / tools / tools.rb
1 #!/usr/bin/ruby
2 # coding: utf-8
3 =begin
4 Copyright 2008 Grégory Burri
5
6 This file is part of Euphorik.
7
8 Euphorik is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Euphorik is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
20 =end
21
22 # TODO :
23 # - création de unit tests (voir eunit) et validation avant la mise en prod
24
25 # Met à disposition plusieurs outils (classes), tel que :
26 # - Vérification du code javascript
27 # - Mise à jour du numéro de version à partir du fichier VERSION
28 # - Mise en production et en preproduction
29 # tools.rb peut s'utiliser à la ligne de commande, exemples :
30 # * Mise en production :
31 # ./tools.rb prod gburri@euphorik.ch:/var/www/euphorik
32 # * Mise en préproduction, l'emplacement de production peut être indiqué pour copier la base
33 # ./tools.rb pre gburri@euphorik.ch:/var/www/euphorik_preprod gburri@euphorik.ch:/var/www/euphorik
34
35 # voir : http://net-ssh.rubyforge.org/ssh/v2/api/index.html
36 # require 'net/ssh'
37
38 # Classe permettant la vérification du code JS pas jslint.
39 # Passe en revu chaque fichier js de manière récursive à partir d'un dossier de départ.s
40 class VerifJS
41
42 def initialize(dossier)
43 @dossier = dossier
44 end
45
46 def verifier
47 verifierRecur(@dossier)
48 end
49
50 def verifierRecur(dossier)
51 Dir.foreach(dossier){|fichier|
52 cheminComplet = "#{dossier}/#{fichier}"
53 if fichier[0,1] != '.' and File.directory?(cheminComplet) and fichier != 'libs'
54 if not verifierRecur(cheminComplet)
55 return false
56 end
57 elsif fichier[-3, 3] == '.js'
58 puts "== Vérification de #{cheminComplet} =="
59 # TODO : mettre un if pour la version windows si dessous
60 #system("java org.mozilla.javascript.tools.shell.Main jslint.js #{cheminComplet}")
61 system("rhino ./tools/jslint.js #{cheminComplet}")
62 # puts $?.exitstatus
63 if $?.exitstatus > 0
64 return false
65 end
66 end
67 }
68 return true
69 end
70 end
71
72 # Classe de gestion de la version
73 class Version
74 # @param dossier la racine du site (par exemple "/var/www/euphorik")
75 def initialize(dossier)
76 @dossier = dossier
77 File.open(@dossier + '/VERSION') {|file|
78 @version = file.readline()
79 }
80 # les fichiers HTML dans lesquels mettre à jour la version
81 @fichiers = ['/pages/about.html']
82 @balise = /(<a.+?href=".*?\/tags\/).*?(".+?class.*?=.*?"version".*?>).*?(<\/a>)/
83 end
84
85 # met à jour la version dans les fichiers @fichiers
86 def maj
87 @fichiers.each{|fichier|
88 fichier = @dossier + fichier
89 lines = IO.readlines(fichier)
90 File.open(fichier, 'w') {|io|
91 lines.each{|l|
92 io.write(l.sub(@balise){|m| $1 + @version + $2 + @version + $3})
93 }
94 }
95 }
96 end
97 end
98
99 # Permet la mise en production et preproduction
100 class MiseEnProd
101 # obsolète !
102 @@rep_remote = '/var/www/euphorik'
103 @@host = 'euphorik.ch'
104
105 def initialize(racine)
106 Dir.chdir(racine)
107 end
108
109 # L'emplacement ou sont copié les fichiers
110 # A définir avant la mise en prod
111 def uri=(uri)
112 plop = uri.split(':')
113 @uri = plop[0]
114 @rep = plop[1]
115 end
116
117 # Effectue la mise en production.
118 def miseEnProd
119 copierFichiers()
120 maj('yaws')
121 end
122
123 # Effectue la mise en préproduction.
124 def miseEnPreProd
125 copierFichiers()
126 lancerYaws()
127 maj('yaws_dev')
128 end
129
130 def copierFichiers
131 compiler_partie_serveuse()
132 creer_repertoire_bd()
133 copier_partie_statique()
134 pack_js()
135 copie_modules_serveurs()
136 set_droits_fichiers()
137 end
138
139 def lancerYaws
140 creer_rep("tools")
141 system("rsync tools/yaws.conf #{@uri}:#{@rep}/tools")
142 system("rsync tools/start_yaws.sh #{@uri}:#{@rep}/tools")
143 system("ssh #{@uri} \"cd #{@rep}/tools; screen -d -m -S yaws_dev ./start_yaws.sh\"")
144 end
145
146 def exec(commande)
147 system("ssh #{@uri} \"cd #{@rep} && #{commande}\"")
148 end
149
150 def creer_rep(rep)
151 begin
152 exec("test -d #{rep} || mkdir #{rep}")
153 rescue
154 end
155 end
156
157 def compiler_partie_serveuse
158 log "compilation des modules serveur"
159 Dir.chdir('modules')
160 system("make")
161 if $?.exitstatus != 0
162 puts "Echec de compilation de la partie serveuse"
163 exit 1
164 end
165 Dir.chdir('..')
166 end
167
168 def creer_repertoire_bd
169 log "création du répertoire de la base de données"
170 # création du repertoire BD
171 creer_rep('var')
172 creer_rep('var/images')
173 creer_rep('var/BD/backups')
174 exec("chmod -R g+w var")
175 end
176
177 # css, images, html, etc..
178 def copier_partie_statique
179 log "copie de la partie statique"
180 uri = "#{@uri}:#{@rep}"
181 system("awk '$0 !~ /prod=\"delete\"/' index.yaws | ssh #{@uri} \" cat > #{@rep}/index.yaws\"")
182 system("rsync favicon.ico #{uri}")
183 system("rsync --delete -r styles #{uri}")
184 system("rsync --delete -r pages #{uri}")
185 system("rsync --delete -r --exclude 'autres' img #{uri}")
186 end
187
188 # minification et package des fichiers js dans euphorik.js
189 def pack_js
190 log "minification, assemblage et copie du javascript"
191 rep_js = 'js'
192 creer_rep(rep_js)
193 # jquery.js et euphorik.js doivent se trouve en premier
194 fichiers = ['js/libs/jquery.js', 'js/euphorik.js'].concat(get_fichiers_js(rep_js))
195 commande_cat = "cat "
196 fichiers.each{|f|
197 commande_cat += f + " "
198 }
199 #copie des js concaténés avec minification
200 system("#{commande_cat} | tools/jsmin.rb | ssh #{@uri} \"cd #{@rep} && cat > #{rep_js}/euphorik.js\"")
201 end
202
203 #renvoie une liste des fichiers js
204 def get_fichiers_js(rep)
205 fichiers = []
206 Dir.entries(rep).each{|fichier|
207 if fichier[0..0] != '.' and fichier != 'euphorik.js' and fichier != 'jquery.js'
208 fichier = rep + "/" + fichier
209 if File.directory?(fichier)
210 fichiers.concat(get_fichiers_js(fichier))
211 else
212 fichiers << fichier
213 end
214 end
215 }
216 return fichiers
217 end
218
219 def copie_modules_serveurs
220 log "copie des modules du serveur"
221 # copie des modules erlang
222 creer_rep('modules')
223 system("rsync -r --exclude 'euphorik_test.beam' modules/ebin #{@uri}:#{@rep}/modules")
224 system("rsync -r modules/include #{@uri}:#{@rep}/modules")
225 end
226
227 def set_droits_fichiers
228 log "attribution des droits sur les fichiers"
229 # attribution des droits
230 exec("chmod -R g+rx .")
231 end
232
233 # noeud : le nom du noeud sur lequel le script de mise en prod est exécuté
234 # Execute le script 'mise_en_prod.erl' sur le serveur afin de :
235 # - Recharger les modules
236 # - Mettre à jour la base de données
237 def maj(noeud)
238 log "rechargement des modules serveur et mise à jour de la base de données"
239 # execution du script de mise à jour
240 system("cat tools/mise_en_prod.erl | ssh #{@uri} \"cat > /tmp/mise_en_prod.erl\"")
241 system("ssh #{@uri} \"chmod u+x /tmp/mise_en_prod.erl; /tmp/mise_en_prod.erl #{noeud}; rm /tmp/mise_en_prod.erl\"")
242 end
243
244 def log(message)
245 puts "----- #{message} -----"
246 end
247 end
248
249
250 # Traite la ligne de commande lorsque tools.rb est utilisé comme tel
251 class Commande
252 def initialize
253 Dir.chdir("..")
254 @miseEnProd = MiseEnProd.new(".")
255 @verifJS = VerifJS.new("js")
256 @version = Version.new(".")
257 end
258
259 def traiter
260 if ARGV.size == 0
261 afficherUsage
262 return
263 end
264
265 case ARGV[0]
266 when 'prod'
267 @version.maj()
268 @miseEnProd.uri = "gburri@euphorik.ch:/var/www/euphorik"
269 @miseEnProd.miseEnProd()
270 when 'pre'
271 @version.maj()
272 @miseEnProd.uri = "gburri@euphorik.ch:/var/www/euphorik_preprod"
273 @miseEnProd.miseEnPreProd()
274 when 'js'
275 @verifJS.verifier()
276 when 'version'
277 @version.maj()
278 end
279 end
280
281 def afficherUsage
282 puts "Usage : tools.rb (prod | pre | js | version)\n" +
283 " prod : Mise en production\n" +
284 " pre : Mise en préproduction, copie les données en production\n" +
285 " js : vérification des fichiers JavaScript\n" +
286 " version : met à jour la version à partir du fichier VERSION"
287 end
288 end
289
290 cl = Commande.new
291 cl.traiter()