11570dd407d346dee69e1cffe80b85f8c9b768f2
[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 if fichier != '.' and fichier != '..' and File.directory?(fichier) and fichier != 'dirs'
53 if not verifierRecur(dossier + '/' + fichier)
54 return false
55 end
56 elsif fichier[-3, 3] == '.js'
57 puts "== Vérification de #{dossier}/#{fichier} =="
58 # TODO : mettre un if pour la version windows si dessous
59 #system("java org.mozilla.javascript.tools.shell.Main jslint.js #{dossier}/#{fichier}")
60 system("rhino ./tools/jslint.js #{dossier}/#{fichier}")
61 # puts $?.exitstatus
62 if $?.exitstatus > 0
63 return false
64 end
65 end
66 }
67 return true
68 end
69 end
70
71 # Classe de gestion de la version
72 class Version
73 # @param dossier la racine du site (par exemple "/var/www/euphorik")
74 def initialize(dossier)
75 @dossier = dossier
76 File.open(@dossier + '/VERSION') {|file|
77 @version = file.readline()
78 }
79 # les fichiers HTML dans lesquels mettre à jour la version
80 @fichiers = ['/pages/about.html']
81 @balise = /(<span.+?class.*?=.*?"version".*?>).*?(<\/span>)/
82 end
83
84 # met à jour la version dans les fichiers @fichiers
85 def maj
86 @fichiers.each{|fichier|
87 fichier = @dossier + fichier
88 lines = IO.readlines(fichier)
89 File.open(fichier, 'w') {|io|
90 lines.each{|l|
91 io.write(l.sub(@balise){|m| $1 + @version + $2})
92 }
93 }
94 }
95 end
96 end
97
98 # Permet la mise en production et preproduction
99 class MiseEnProd
100 # obsolète !
101 @@rep_remote = '/var/www/euphorik'
102 @@host = 'euphorik.ch'
103
104 def initialize(racine)
105 Dir.chdir(racine)
106 end
107
108 # L'emplacement ou sont copié les fichiers
109 # A définir avant la mise en prod
110 def uri=(uri)
111 plop = uri.split(':')
112 @uri = plop[0]
113 @rep = plop[1]
114 end
115
116 # Effectue la mise en production.
117 def miseEnProd
118 copierFichiers()
119 maj()
120 end
121
122 # Effectue la mise en préproduction.
123 def miseEnPreProd
124 copierFichiers()
125 copierVAR()
126 lancerYaws()
127 end
128
129 def copierFichiers
130 compiler_partie_serveuse()
131 creer_repertoire_bd()
132 copier_partie_statique()
133 pack_js()
134 copie_modules_serveurs()
135 set_droits_fichiers()
136 end
137
138 # TODO
139 def copierVar
140 #TODO
141 end
142
143 def lancerYaws
144 creer_rep("tools")
145 system("rsync tools/yaws.conf #{@uri}:#{@rep}/tools")
146 system("rsync tools/start_yaws.sh #{@uri}:#{@rep}/tools")
147 # TODO
148 end
149
150 def exec(commande)
151 system("ssh #{@uri} \"cd #{@rep} && #{commande}\"")
152 end
153
154 def creer_rep(rep)
155 begin
156 exec("test -d #{rep} || mkdir #{rep}")
157 rescue
158 end
159 end
160
161 def compiler_partie_serveuse
162 Dir.chdir('modules')
163 system("make")
164 if $?.exitstatus != 0
165 puts "Echec de compilation de la partie serveuse"
166 exit 1
167 end
168 Dir.chdir('..')
169 end
170
171 def creer_repertoire_var
172 # création du repertoire BD
173 creer_rep('var')
174 creer_rep('var/images')
175 creer_rep('var/BD/backups')
176 exec("chmod -R g+w var")
177 end
178
179 # css, images, html, etc..
180 def copier_partie_statique
181 uri = "#{@uri}:#{@rep}"
182 system("awk '$0 !~ /prod=\"delete\"/' index.yaws | ssh #{@uri} \" cat > #{@rep}/index.yaws\"")
183 system("rsync favicon.ico #{uri}")
184 system("rsync --delete -r styles #{uri}")
185 system("rsync --delete -r pages #{uri}")
186 system("rsync --delete -r --exclude 'autres' img #{uri}")
187 end
188
189 # minification et package des fichiers js dans euphorik.js
190 def pack_js
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 # copie des modules erlang
221 creer_rep('modules')
222 system("rsync -r --exclude 'euphorik_test.beam' modules/ebin #{@uri}:#{@rep}/modules")
223 system("rsync -r modules/include #{@uri}:#{@rep}/modules")
224 end
225
226 def set_droits_fichiers
227 # attribution des droits
228 exec("chmod -R g+rx .")
229 end
230
231 def maj
232 # execution du script de mise à jour
233 system("cat tools/mise_en_prod.erl | ssh #{@uri} \"cat > /tmp/mise_en_prod.erl\"")
234 system("ssh #{@uri} \"chmod u+x /tmp/mise_en_prod.erl; /tmp/mise_en_prod.erl; rm /tmp/mise_en_prod.erl\"")
235 end
236 end
237
238
239 # Traite la ligne de commande lorsque tools.rb est utilisé comme tel
240 class Commande
241 def initialize
242 Dir.chdir("..")
243 @miseEnProd = MiseEnProd.new(".")
244 @verifJS = VerifJS.new("js")
245 @version = Version.new(".")
246 end
247
248 def traiter
249 if ARGV.size == 0
250 afficherUsage
251 return
252 end
253
254 case ARGV[0]
255 when 'prod'
256 @version.maj()
257 @miseEnProd.uri = "gburri@euphorik.ch:/var/www/euphorik"
258 @miseEnProd.miseEnProd()
259 when 'pre'
260 @version.maj()
261 @miseEnProd.uri = "gburri@euphorik.ch:/var/www/euphorik_preprod"
262 @miseEnProd.miseEnPreProd()
263 when 'js'
264 @verifJS.verifier()
265 when 'version'
266 @version.maj()
267 end
268
269 =begin
270 Net::SSH.start('euphorik.ch', 'gburri') {|ssh|
271 output = ssh.exec!("hostname")
272 stdout = ""
273 ssh.exec!("ls -l /tmp"){|channel, stream, data|
274 stdout << data if stream == :stdout
275 }
276 puts stdout
277 }
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()