From dc70eed10d6eb26660d02fe8b18eebb2836ef9de Mon Sep 17 00:00:00 2001 From: Ummon Date: Thu, 23 Apr 2020 22:53:52 +0200 Subject: [PATCH] Updating tools (WIP) --- tools/tools.erl | 272 +++++++++++++++++++--------------------- tools/update_server.erl | 35 +++--- 2 files changed, 149 insertions(+), 158 deletions(-) diff --git a/tools/tools.erl b/tools/tools.erl index d5c24bf..28f36cf 100755 --- a/tools/tools.erl +++ b/tools/tools.erl @@ -1,140 +1,132 @@ -#!/usr/bin/env escript -% coding: utf-8 -% 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 . - -% This file replace the old one 'tools.rb' written in a crapy language ;) -% TODO : -% - création de unit tests (voir eunit) et validation avant la mise en prod - - -main(Args) -> - file:set_cwd(".."), - case Args of - ["prod"] -> in_prod("gburri@euphorik.ch:/var/www/euphorik"); - ["pre"] -> in_preprod("gburri@euphorik.ch:/var/www/euphorik_preprod", "var/www/euphorik"); - ["js"] -> verif_js(); - ["version"] -> update_version(); - _ -> - io:format( - "Usage : ~s (prod | pre | js | version)~n" - " prod : in production~n" - " pre : in preproduction, data are copied from production~n" - " js : check the JavaScript files~n" - " version : update the version into somes files from the VERSION file", - [escript:script_name()] - ) - end. - - -% A simple fonction to log message. -log(Str) -> - io:format("===== ~s =====~n", [Str]). - - -% Execute an OS commande and print the result to stdout. -cmd(Commande_str) -> - cmd(Commande_str, []). -cmd(Commande_str, Params) -> - io:format("~s~n", [os:cmd(lists:flatten(io_lib:format(Commande_str, Params)))]). - - --record(uri, { - host, - path % in absolute -}). - - -% Create an uri record from an uri string. -create_uri(Uri_str) -> - [Host, Path] = string:tokens(Uri_str, ":"), - #uri{host = Host, path = Path}. - - -% Update the version into somes files from the VERSION file. -update_version() -> - log("Update the version tag"), - todo. - - -% Check the JavaScript files. -verif_js() -> - todo. - - -% Start the production processus. -in_prod(Uri) -> - compile_server_part(Uri), - make_var_directory(Uri), - copy_files(Uri), - define_files_rights(Uri), - update_server(). - - -% Start the pre-production processus. -in_preprod(Uri, data_path) -> - compile_server_part(Uri), - make_var_directory(Uri), - copy_files(Uri), - define_files_rights(Uri), - start_server(), - update_server(). - - -% Compile the Erlang modules. -compile_server_part(Uri) -> - todo. - - -% Create the 'var' folder if it doesn't exist. -make_var_directory(Uri) -> - todo. - - -% Copy files from developpement env to production server. -copy_files(Uri) -> - copy_static_part(Uri), - copy_packed_js(Uri). - - -% Copy all static files like modules, styles, pages, etc. -copy_static_part(Uri) -> - %~ creer_rep('modules') - %~ system("rsync -r --exclude 'euphorik_test.beam' modules/ebin #{@uri}:#{@rep}/modules") - %~ system("rsync -r modules/include #{@uri}:#{@rep}/modules") - todo. - - -% Minify and pack JavaScript in one file then copy it to ther server. -copy_packed_js(Uri) -> - todo. - - -% Define the rights for the copied folder et files. -define_files_rights(Uri) -> - todo. - - -% Start the server if it not already started (in preproduction case only). -start_server() -> - todo. - - -% Run a erlang script to -update_server() -> - todo. - +#!/usr/bin/env escript + +% TODO : +% * Create some unit tests with eunit before releasing in production. + +main(Args) -> + file:set_cwd(".."), + case Args of + ["build"] -> build(); + ["run"] -> run_debug(); + ["pre"] -> in_preprod("gburri@euphorik.ch:/var/www/euphorik_preprod", "var/www/euphorik"); + ["prod"] -> in_prod("gburri@euphorik.ch:/var/www/euphorik"); + ["js"] -> verif_js(); + ["version"] -> update_version(); + _ -> + io:format( + "Usage : ~s (build | run | pre | prod | js | version)~n" + " prod : in production~n" + " pre : in preproduction, data are copied from production~n" + " js : check the JavaScript files~n" + " version : update the version into somes files from the VERSION file~n", + [escript:script_name()] + ) + end. + +% A simple fonction to log message. +log(Str) -> + io:format("===== ~s =====~n", [Str]). + +% Execute an OS command and print the result to stdout. +cmd(Command) -> + cmd(Command, []). +cmd(Command, Params) -> + cmd(Command, Params, "."). +cmd(Command, Params, Dir) -> + InitialDir = file:get_cwd(), + file:set_cwd(Dir), + io:format("~s~n", [os:cmd(lists:flatten(io_lib:format(Command, Params)))]), + file:set_cwd(InitialDir). + +-record(uri, { + host, + path % in absolute + }). + +% Create an uri record from an uri string. +create_uri(Uri_str) -> + [Host, Path] = string:tokens(Uri_str, ":"), + #uri{host = Host, path = Path}. + +build() -> + % Create the directory "var/database" if it doesn't exist. + {ok, Files_in_root} = file:list_dir("."), + Var_exists = lists:any(fun(Name) -> Name =:= "var" end, Files_in_root), + if not Var_exists -> + file:make_dir("var"); + true -> ok + end, + {ok, Files_in_var} = file:list_dir("var"), + Database_exists = lists:any(fun(Name) -> Name =:= "database" end, Files_in_var), + if not Database_exists -> + file:make_dir("var/database"); + true -> ok + end, + % Build all the Erlang modules. + cmd("make", [], "modules"). + +% Update the version into somes files from the VERSION file. +update_version() -> + log("Update the version tag"), + todo. + +% Check the JavaScript files. +verif_js() -> + todo. + +run_debug() -> + build(), + todo. + +% Start the production processus. +in_prod(Uri) -> + compile_server_part(Uri), + make_var_directory(Uri), + copy_files(Uri), + define_files_rights(Uri), + update_server(). + +% Start the pre-production processus. +in_preprod(Uri, data_path) -> + compile_server_part(Uri), + make_var_directory(Uri), + copy_files(Uri), + define_files_rights(Uri), + start_server(), + update_server(). + +% Compile the Erlang modules. +compile_server_part(Uri) -> + todo. + +% Create the 'var' folder if it doesn't exist. +make_var_directory(Uri) -> + todo. + +% Copy files from developpement env to production server. +copy_files(Uri) -> + copy_static_part(Uri), + copy_packed_js(Uri). + +% Copy all static files like modules, styles, pages, etc. +copy_static_part(Uri) -> + %~ creer_rep('modules') + %~ system("rsync -r --exclude 'euphorik_test.beam' modules/ebin #{@uri}:#{@rep}/modules") + %~ system("rsync -r modules/include #{@uri}:#{@rep}/modules") + todo. + +% Minify and pack JavaScript in one file then copy it to ther server. +copy_packed_js(Uri) -> + todo. + +% Define the rights for the copied folder et files. +define_files_rights(Uri) -> + todo. + +% Start the server if it not already started (in preproduction case only). +start_server() -> + cmd("yaws --conf ./yaws.conf --sname yaws_dev --mnesiadir \"../var/database/\" -I debian_yaws_dev"). + +% Run a erlang script to +update_server() -> + todo. diff --git a/tools/update_server.erl b/tools/update_server.erl index 72686b1..b375c0e 100755 --- a/tools/update_server.erl +++ b/tools/update_server.erl @@ -1,9 +1,8 @@ #!/usr/bin/env escript -% coding: utf-8 -% Executé sur le serveur après la copie des fichiers lors de la mise en production. -% Recharge les modules de euphorik et met à jour la BD. -% TODO : construire le nom du noeud en fonction du nom de l'host +% Execute remotly after the files has been copied. +% Reload all modules et update the database. +% TODO: build the node name from the host name. hote() -> '@overnux'. @@ -11,27 +10,27 @@ hote() -> % le premier argument est le nom du noeud est peut valoir : % - yaws : noeud de production % - yaws_dev : noeud de pre-production -main([Nom_node]) when Nom_node =:= "yaws"; Nom_node =:= "yaws_dev" -> - Node = list_to_atom(Nom_node ++ atom_to_list(hote())), +main([Node_name]) when Node_name =:= "yaws"; Node_name =:= "yaws_dev" -> + Node = list_to_atom(Node_name ++ atom_to_list(hote())), net_kernel:start([flynux, shortnames]), io:format("rechargement des modules..~n"), rpc:call(Node, euphorik_daemon, reload_euphorik, []), - if Nom_node =:= "yaws_dev" -> copier_bd(Node); + if Node_name =:= "yaws_dev" -> copy_database(Node); true -> true end, io:format("mise à jour de la BD..~n"), rpc:call(Node, euphorik_bd_admin, update, []); main(_) -> - io:format("Usage : mise_en_prod.erl "), + io:format("Usage: update_server.erl "), halt(1). -% Copie la bd du noeud de production -copier_bd(Node) -> - io:format("Copie de la BD de production vers le noeude pre-production~n"), - Fichier = "/tmp/backup_ek_tmp", - Fichier2 = "/tmp/backup_ek_tmp2", - rpc:call(yaws@overnux, mnesia, backup, [Fichier]), - rpc:call(Node, euphorik_bd_admin, change_node_name, [yaws@overnux, yaws_dev@overnux, Fichier, Fichier2]), - rpc:call(Node, mnesia, restore, [Fichier2, [{default_op, recreate_tables}]]), - rpc:call(yaws@overnux, file, delete, [Fichier]), - rpc:call(Node, file, delete, [Fichier2]). +% Copy the datbase from the production to pre production. +copy_database(Node) -> + io:format("Copying production database to pre-production node~n"), + File = "/tmp/backup_ek_tmp", + File2 = "/tmp/backup_ek_tmp2", + rpc:call(yaws@overnux, mnesia, backup, [File]), + rpc:call(Node, euphorik_bd_admin, change_node_name, [yaws@overnux, yaws_dev@overnux, File, File2]), + rpc:call(Node, mnesia, restore, [File2, [{default_op, recreate_tables}]]), + rpc:call(yaws@overnux, file, delete, [File]), + rpc:call(Node, file, delete, [File2]). -- 2.45.2