X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=tools%2Ftools.rb;h=9bf9b867741bc927c3a88604ae8369dcec07befe;hp=aff62af543c02e7e45d95c772555514ca5ee1a9b;hb=3b379ad72d9806ffd49deb602de137771a0012e7;hpb=2648c8ab1a531e660e18ac97d2efe08d4d63968d diff --git a/tools/tools.rb b/tools/tools.rb index aff62af..9bf9b86 100644 --- a/tools/tools.rb +++ b/tools/tools.rb @@ -1,195 +1,288 @@ -#!/usr/bin/ruby -# coding: utf-8 -=begin -Copyright 2008 Grégory Burri - -This file is part of Euphorik. - -Euphorik is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Euphorik is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Euphorik. If not, see . -=end - -# TODO : -# - création de unit tests (voir eunit) et validation avant la mise en prod - -# Met à disposition plusieurs outils (classes), tel que : -# - Vérification du code javascript -# - Mise à jour du numéro de version à partir du fichier VERSION -# - Mise en production et en preproduction -# tools.rb peut s'utiliser à la ligne de commande, exemples : -# * Mise en production : -# ./tools.rb --doprod gburri@euphorik.ch:/var/www/euphorik -# * Mise en préproduction, l'emplacement de production peut être indiqué pour copier la base -# ./tools.rb --dopreprod gburri@euphorik.ch:/var/www/euphorik_preprod --prod gburri@euphorik.ch:/var/www/euphorik - -# Classe permettant la vérification du code JS pas jslint. -# Passe en revu chaque fichier js de manière récursive à partir d'un dossier de départ.s -class VerifJS - - def initialize(dossier) - @dossier = dossier - end - - def verifier - verifierRecu(@dossier) - end - - def verifierRecu(dossier) - Dir.foreach(dossier){|fichier| - if fichier != '.' and fichier != '..' and File.directory?(fichier) and fichier != 'dirs' - if not verifierRecu(dossier + '/' + fichier) - return false - end - elsif fichier[-3, 3] == '.js' - puts "== Vérification de #{dossier}/#{fichier} ==" - system("java org.mozilla.javascript.tools.shell.Main jslint.js #{dossier}/#{fichier}") - puts $?.exitstatus - if $?.exitstatus > 0 - return false - end - end - } - return true - end -end - -# Classe de gestion de la version -class Version - # @param dossier la racine du site (par exemple "/var/www/euphorik" - def initialize(dossier) - @dossier = dossier - File.open(@dossier + '/VERSION') {|file| - @version = file.readline() - } - # les fichiers HTML dans lesquels mettre à jour la version - @fichiers = ['/pages/about.html'] - @balise = /().*?(<\/span>)/ - end - - # met à jour la version dans les fichiers @fichiers - def maj - @fichiers.each{|fichier| - fichier = @dossier + fichier - lines = IO.readlines(fichier) - File.open(fichier, 'w') {|io| - lines.each{|l| - io.write(l.sub(@balise){|m| $1 + @version + $2}) - } - } - } - end -end - -# Permet la mise en production et preproduction -class MiseEnProd - # obsolète ! - @@rep_remote = '/var/www/euphorik' - @@host = 'euphorik.ch' - @@opt_rsync = '' - - def initialize(prod_uri, preprod_uri) - @prod = prod_uri - @preprod = preprod_uri - end - - # Effectue la mise en production. - def miseEnProd - end - - # Effectue la mise en préproduction. - def miseEnPreProd - end - - def creer_remote_rep(rep) - begin - `ssh #{@@host} "mkdir #{@@rep_remote}/#{rep}"` - rescue - end - end - - def compiler_partie_serveuse - Dir.chdir(@@rep_remote + '/modules') - puts `make` - if $?.exitstatus != 0 - puts "Echec de compilation de la partie serveuse" - exit 1 - end - Dir.chdir('..') - end - - def creer_repertoire_bd - # création du repertoire BD - creer_remote_rep('BD') - creer_remote_rep('BD/backup') - `ssh #{@@host} "chmod g+w #{@@rep_remote}/BD"` - end - - # css, images, html, etc.. - def copier_partie_statique - print `rsync #{$opt_rsync} index.yaws #{$host}:#{$rep_remote}` - print `rsync #{$opt_rsync} favicon.ico #{$host}:#{$rep_remote}` - print `rsync #{$opt_rsync} -r css #{$host}:#{$rep_remote}` - print `rsync #{$opt_rsync} -r pages #{$host}:#{$rep_remote}` - print `rsync #{$opt_rsync} -r --exclude 'autres' img #{$host}:#{$rep_remote}` - end - - # minification et package des fichiers js dans euphorik.js - def pack_js - # copie des js avec minification - rep_js = 'js' - creer_remote_rep(rep_js) - Dir.entries(rep_js).each{|fichier| - if fichier[0..0] != '.' and fichier != 'debug.js' - puts "Minimisation et copie de #{fichier}" - print `tools/jsmin.rb < #{rep_js}/#{fichier} | ssh #{@@host} "cat > #{@@rep_remote}/#{rep_js}/#{fichier}"` - end - } - end - - def copie_modules_serveurs - # copie des modules erlang - creer_remote_rep('modules') - `rsync #{@@opt_rsync} -r --exclude 'euphorik_test.beam' modules/ebin #{@@host}:#{@@rep_remote}/modules` - `rsync #{@@opt_rsync} -r modules/include #{@@host}:#{@@rep_remote}/modules` - end - - def set_droits_fichiers - # attribution des droits - `ssh #{$host} "chmod -R g+rx #{$rep_remote}"` - end - - def maj - # execution du script de mise à jour - print `cat tools/mise_en_prod.erl | ssh #{$host} "cat > /tmp/mise_en_prod.erl"` - print `ssh #{$host} "chmod u+x /tmp/mise_en_prod.erl; /tmp/mise_en_prod.erl; rm /tmp/mise_en_prod.erl"` - end -end - - -# Traite la ligne de commande lorsque tools.rb est utilisé comme tel -class Commande - def traiter - ARGV - #miseEnProd = MiseEnProd.new("gburri@euphorik.ch:/var/www/euphorik", "gburri@euphorik.ch:/var/www/euphorik_preprod") - #miseEnProd.miseEnPreProd() - - #verifJS = VerifJS.new("../js") - #verifJS.verifier() - - #version = Version.new("..") - #version.maj() - end -end - -cl = Commande.new -cl.traiter() +#!/usr/bin/ruby +# coding: utf-8 +=begin +Copyright 2008 Grégory Burri + +This file is part of Euphorik. + +Euphorik is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Euphorik is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Euphorik. If not, see . +=end + +# TODO : +# - création de unit tests (voir eunit) et validation avant la mise en prod + +# Met à disposition plusieurs outils (classes), tel que : +# - Vérification du code javascript +# - Mise à jour du numéro de version à partir du fichier VERSION +# - Mise en production et en preproduction +# tools.rb peut s'utiliser à la ligne de commande, exemples : +# * Mise en production : +# ./tools.rb prod gburri@euphorik.ch:/var/www/euphorik +# * Mise en préproduction, l'emplacement de production peut être indiqué pour copier la base +# ./tools.rb pre gburri@euphorik.ch:/var/www/euphorik_preprod gburri@euphorik.ch:/var/www/euphorik + +# voir : http://net-ssh.rubyforge.org/ssh/v2/api/index.html +# require 'net/ssh' + +# Classe permettant la vérification du code JS pas jslint. +# Passe en revu chaque fichier js de manière récursive à partir d'un dossier de départ.s +class VerifJS + + def initialize(dossier) + @dossier = dossier + end + + def verifier + verifierRecur(@dossier) + end + + def verifierRecur(dossier) + Dir.foreach(dossier){|fichier| + if fichier != '.' and fichier != '..' and File.directory?(fichier) and fichier != 'dirs' + if not verifierRecur(dossier + '/' + fichier) + return false + end + elsif fichier[-3, 3] == '.js' + puts "== Vérification de #{dossier}/#{fichier} ==" + # TODO : mettre un if pour la version windows si dessous + #system("java org.mozilla.javascript.tools.shell.Main jslint.js #{dossier}/#{fichier}") + system("rhino ./tools/jslint.js #{dossier}/#{fichier}") + # puts $?.exitstatus + if $?.exitstatus > 0 + return false + end + end + } + return true + end +end + +# Classe de gestion de la version +class Version + # @param dossier la racine du site (par exemple "/var/www/euphorik") + def initialize(dossier) + @dossier = dossier + File.open(@dossier + '/VERSION') {|file| + @version = file.readline() + } + # les fichiers HTML dans lesquels mettre à jour la version + @fichiers = ['/pages/about.html'] + @balise = /().*?(<\/span>)/ + end + + # met à jour la version dans les fichiers @fichiers + def maj + @fichiers.each{|fichier| + fichier = @dossier + fichier + lines = IO.readlines(fichier) + File.open(fichier, 'w') {|io| + lines.each{|l| + io.write(l.sub(@balise){|m| $1 + @version + $2}) + } + } + } + end +end + +# Permet la mise en production et preproduction +class MiseEnProd + # obsolète ! + @@rep_remote = '/var/www/euphorik' + @@host = 'euphorik.ch' + + def initialize(racine) + Dir.chdir(racine) + end + + # L'emplacement ou sont copié les fichiers + # A définir avant la mise en prod + def uri=(uri) + plop = uri.split(':') + @uri = plop[0] + @rep = plop[1] + end + + # Effectue la mise en production. + def miseEnProd + copierFichiers() + maj() + end + + # Effectue la mise en préproduction. + def miseEnPreProd + copierFichiers() + copierBD() + lancerYaws() + end + + def copierFichiers + compiler_partie_serveuse() + creer_repertoire_bd() + copier_partie_statique() + pack_js() + copie_modules_serveurs() + set_droits_fichiers() + end + + # TODO + def copierBD + end + + def lancerYaws + creer_rep("tools") + system("rsync tools/yaws.conf #{@uri}:#{@rep}/tools") + system("rsync tools/start_yaws.sh #{@uri}:#{@rep}/tools") + end + + def exec(commande) + system("ssh #{@uri} \"cd #{@rep} && #{commande}\"") + end + + def creer_rep(rep) + begin + exec("test -d #{rep} || mkdir #{rep}") + rescue + end + end + + def compiler_partie_serveuse + Dir.chdir('modules') + system("make") + if $?.exitstatus != 0 + puts "Echec de compilation de la partie serveuse" + exit 1 + end + Dir.chdir('..') + end + + def creer_repertoire_bd + # création du repertoire BD + creer_rep('BD') + creer_rep('BD/backups') + exec("chmod -R g+w BD") + end + + # css, images, html, etc.. + def copier_partie_statique + uri = "#{@uri}:#{@rep}" + system("awk '$0 !~ /prod=\"delete\"/' index.yaws | ssh #{@uri} \" cat > #{@rep}/index.yaws\"") + system("rsync favicon.ico #{uri}") + system("rsync --delete -r styles #{uri}") + system("rsync --delete -r pages #{uri}") + system("rsync --delete -r --exclude 'autres' img #{uri}") + end + + # minification et package des fichiers js dans euphorik.js + def pack_js + rep_js = 'js' + creer_rep(rep_js) + # jquery.js et euphorik.js doivent se trouve en premier + fichiers = ['js/libs/jquery.js', 'js/euphorik.js'].concat(get_fichiers_js(rep_js)) + commande_cat = "cat " + fichiers.each{|f| + commande_cat += f + " " + } + #copie des js concaténés avec minification + system("#{commande_cat} | tools/jsmin.rb | ssh #{@uri} \"cd #{@rep} && cat > #{rep_js}/euphorik.js\"") + end + + #renvoie une liste des fichiers js + def get_fichiers_js(rep) + fichiers = [] + Dir.entries(rep).each{|fichier| + if fichier[0..0] != '.' and fichier != 'euphorik.js' and fichier != 'jquery.js' + fichier = rep + "/" + fichier + if File.directory?(fichier) + fichiers.concat(get_fichiers_js(fichier)) + else + fichiers << fichier + end + end + } + return fichiers + end + + def copie_modules_serveurs + # copie des modules erlang + creer_rep('modules') + system("rsync -r --exclude 'euphorik_test.beam' modules/ebin #{@uri}:#{@rep}/modules") + system("rsync -r modules/include #{@uri}:#{@rep}/modules") + end + + def set_droits_fichiers + # attribution des droits + exec("chmod -R g+rx .") + end + + def maj + # execution du script de mise à jour + system("cat tools/mise_en_prod.erl | ssh #{@uri} \"cat > /tmp/mise_en_prod.erl\"") + system("ssh #{@uri} \"chmod u+x /tmp/mise_en_prod.erl; /tmp/mise_en_prod.erl; rm /tmp/mise_en_prod.erl\"") + end +end + + +# Traite la ligne de commande lorsque tools.rb est utilisé comme tel +class Commande + def initialize + Dir.chdir("..") + @miseEnProd = MiseEnProd.new(".") + @verifJS = VerifJS.new("js") + @version = Version.new(".") + end + + def traiter + if ARGV.size == 0 + afficherUsage + return + end + + case ARGV[0] + when 'prod' + @version.maj() + @miseEnProd.uri = "gburri@euphorik.ch:/var/www/euphorik" + @miseEnProd.miseEnProd() + when 'pre' + @version.maj() + @miseEnProd.uri = "gburri@euphorik.ch:/var/www/euphorik_preprod" + @miseEnProd.miseEnPreProd() + when 'js' + @verifJS.verifier() + when 'version' + @version.maj() + end + +=begin + Net::SSH.start('euphorik.ch', 'gburri') {|ssh| + output = ssh.exec!("hostname") + stdout = "" + ssh.exec!("ls -l /tmp"){|channel, stream, data| + stdout << data if stream == :stdout + } + puts stdout + } +=end + end + + def afficherUsage + puts "Usage : tools.rb (prod | pre | js | version)\n" + + " prod : Mise en production\n" + + " pre : Mise en préproduction, copie les données en production\n" + + " js : vérification des fichiers JavaScript\n" + + " version : met à jour la version à partir du fichier VERSION" + end +end + +cl = Commande.new +cl.traiter()