home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / boot / i386 / root / usr / share / YaST2 / modules / AutoinstFile.ycp < prev    next >
Text File  |  2006-11-29  |  6KB  |  162 lines

  1. /**
  2.  * File:    modules/AutoinstFile.ycp
  3.  * Package:    AutoYaST
  4.  * Authors:    Anas Nashif (nashif@suse.de)
  5.  * Summary:    Handle complete configuration file dumps
  6.  *
  7.  * $Id: AutoinstFile.ycp 27288 2006-01-24 14:29:15Z ug $
  8.  */
  9. {
  10.     module "AutoinstFile";
  11.     textdomain "autoinst";
  12.  
  13.     import "AutoinstConfig";
  14.     import "Summary";
  15.  
  16.  
  17.     /* default value of settings modified */
  18.     global boolean modified = false;
  19.  
  20.  
  21.     /**
  22.      * Function sets internal variable, which indicates, that any
  23.      * settings were modified, to "true"
  24.      */
  25.     global define void SetModified () {
  26.         y2milestone("SetModified");
  27.         modified = true;
  28.     }
  29.  
  30.     /**
  31.      * Functions which returns if the settings were modified
  32.      * @return boolean  settings were modified
  33.      */
  34.     global define boolean GetModified () {
  35.         return modified;
  36.     }
  37.  
  38.     global list<map> Files = [];
  39.  
  40.  
  41.     /**
  42.      * Settings Summary
  43.      */
  44.     global define string Summary() ``{
  45.  
  46.         string summary = "";
  47.         summary = Summary::AddHeader(summary, _("Configured Files:"));
  48.         if (size( Files ) > 0)
  49.         {
  50.             summary = Summary::OpenList(summary);
  51.             foreach(map file, Files, ``{
  52.                 summary = Summary::AddListItem(summary, file["file_path"]:"" );
  53.             });
  54.             summary = Summary::CloseList(summary);
  55.         }
  56.         else
  57.         {
  58.             summary = Summary::AddLine(summary, Summary::NotConfigured());
  59.         }
  60.         return summary;
  61.     }
  62.  
  63.  
  64.     /**
  65.      * Import Settings
  66.      */
  67.     global define boolean Import (list<map> settings) ``{
  68.         Files = settings;
  69.         return true;
  70.     }
  71.  
  72.     /**
  73.      * Export Settings
  74.      */
  75.     global define list<map> Export () ``{
  76.         return Files;
  77.     }
  78.  
  79.     /**
  80.      * Write Settings
  81.      */
  82.     global define boolean Write ()
  83.     {
  84.         import "AutoInstall";
  85.         y2milestone("Writing Files to the system");
  86.         if ( size( Files )==0 )
  87.         {
  88.             return true;;
  89.         }
  90.  
  91.         integer counter = 0;
  92.         boolean success = false;
  93.  
  94.         foreach(map file, Files,
  95.                 ``{
  96.             if (file["file_contents"]:"" != "")
  97.             {
  98.                 string alternate_location = sformat("%1/%2", AutoinstConfig::files_dir, counter);
  99.                 string alter_file = sformat("file_%1",  counter);
  100.                 y2milestone("AutoInstall: Copying file %1",  file["file_path"]:alternate_location );
  101.                 list t = splitstring( file["file_path"]:alternate_location, "/");
  102.                 integer pos = size(t) - 1;
  103.  
  104.                 // SCR::Write (.target.string, AutoInstall::var_dir + "/files" + t[pos]:alter_file, file["file_contents"]:"");
  105.                 SCR::Write (.target.string,  file["file_path"]:alternate_location , file["file_contents"]:"");
  106.  
  107.                 if (file["file_permissions"]:"" != "")
  108.                 {
  109.                     SCR::Execute (.target.bash, sformat("chmod %1 %2", file["file_permissions"]:"",  file["file_path"]:alternate_location ));
  110.                 }
  111.                 if (file["file_owner"]:"" != "")
  112.                 {
  113.                     SCR::Execute (.target.bash, sformat("chown %1 %2", file["file_owner"]:"",  file["file_path"]:alternate_location ));
  114.                 }
  115.  
  116.                 map script = file["file_script"]:$[];
  117.                 if (script != $[])
  118.                 {
  119.                     string current_logdir = AutoinstConfig::logs_dir;
  120.                     list name_tok = splitstring(file["file_path"]:alternate_location, "/");
  121.                     string scriptName = "";
  122.                     if (size(name_tok)>0)
  123.                     {
  124.                         string name = name_tok[size(name_tok) - 1 ]:"";
  125.                         scriptName = "script_" + name;
  126.                     }
  127.                     string scriptPath = sformat("%1/%2", AutoinstConfig::scripts_dir, scriptName);
  128.                     y2milestone("Writing (file)  script into %1", scriptPath);
  129.                     SCR::Write(.target.string,  scriptPath, script["source"]:"echo Empty script!");
  130.                     // string message =  sformat(_("Executing user supplied script: %1"), scriptName);
  131.  
  132.                     string scriptInterpreter = script["interpreter"]:"shell";
  133.                     string executionString = "";
  134.                     if (scriptInterpreter == "shell")
  135.                     {
  136.                         executionString = sformat("/bin/sh -x %1  2&> %2/%3.log", scriptPath, current_logdir, scriptName);
  137.                         SCR::Execute (.target.bash, executionString);
  138.                     }
  139.                     else if (scriptInterpreter == "perl")
  140.                     {
  141.                         executionString = sformat("/usr/bin/perl %1  2&> %2/%3.log", scriptPath, current_logdir, scriptName);
  142.                         SCR::Execute (.target.bash,executionString);
  143.                     }
  144.                     else if (scriptInterpreter == "python")
  145.                     {
  146.                         executionString = sformat("/usr/bin/python %1  2&> %2/%3.log", scriptPath, current_logdir, scriptName);
  147.                         SCR::Execute (.target.bash,executionString);
  148.                     }
  149.                     else
  150.                     {
  151.                         y2error("Unknown interpreter: %1", scriptInterpreter);
  152.                     }
  153.                     y2milestone("Script Execution command: %1", executionString );
  154.                 }
  155.                 success = ( SCR::Execute (.target.bash, sformat("cp %1 %2", file["file_path"]:alternate_location, AutoinstConfig::files_dir)) == 0 );
  156.             }
  157.             counter = counter + 1;
  158.         });
  159.         return success;;
  160.     }
  161. }
  162.