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 / AutoinstScripts.ycp < prev    next >
Text File  |  2006-11-29  |  18KB  |  558 lines

  1. /**
  2.  * File:    modules/AutoinstScripts.ycp
  3.  * Module:    Auto-Installation
  4.  * Summary:    Custom scripts
  5.  * Authors:    Anas Nashif <nashif@suse.de>
  6.  *
  7.  * $Id: AutoinstScripts.ycp 32858 2006-09-13 12:52:17Z ug $
  8.  */
  9.  
  10. {
  11.     module "AutoinstScripts";
  12.     textdomain "autoinst";
  13.  
  14.     import "Mode";
  15.     import "AutoinstConfig";
  16.     import "Summary";
  17.     import "URL";
  18.     import "Service";
  19.     import "Popup";
  20.  
  21.     include "autoinstall/io.ycp";
  22.  
  23.     /* Pre scripts */
  24.     global list<map> pre = [];
  25.  
  26.     /* Post scripts */
  27.     global list<map> post = [];
  28.  
  29.     /* Chroot scripts */
  30.     global list<map> chroot = [];
  31.  
  32.     /* Init scripts */
  33.     global list<map> init = [];
  34.  
  35.     /* Merged scripts */
  36.     global list<map> merged = [];
  37.  
  38.     /* default value of settings modified */
  39.     global boolean modified = false;
  40.  
  41.     /**
  42.      * Function sets internal variable, which indicates, that any
  43.      * settings were modified, to "true"
  44.      */
  45.     global define void SetModified ()
  46.     {
  47.         modified = true;
  48.     }
  49.  
  50.     /**
  51.      * Functions which returns if the settings were modified
  52.      * @return boolean  settings were modified
  53.      */
  54.     global define boolean GetModified ()
  55.     {
  56.         return modified;
  57.     }
  58.  
  59.  
  60.     /**
  61.      * merge all types of scripts into one single list
  62.      * @param -
  63.      * @return merged list
  64.      */
  65.     list<map> mergeScripts ()
  66.     {
  67.  
  68.         list<map> result = maplist (map p, pre,
  69.                                  ``{
  70.                                      p = add(p,"type","pre-scripts");
  71.                                      return p;
  72.                                  });
  73.         result = (list<map>)union(result,    maplist (map p, post,
  74.                                          ``{
  75.                                              p = add(p,"type","post-scripts");
  76.                                              return p;
  77.                                          })
  78.                        );
  79.         result = (list<map>)union(result,    maplist (map p, chroot,
  80.                                          ``{
  81.                                              p = add(p,"type","chroot-scripts");
  82.                                              return p;
  83.                                          })
  84.                        );
  85.         result = (list<map>)union(result,    maplist (map p, init,
  86.                                          ``{
  87.                                              p = add(p,"type","init-scripts");
  88.                                              return p;
  89.                                          })
  90.                        );
  91.         return result;
  92.     }
  93.  
  94.  
  95.     /**
  96.      * Constructor
  97.      */
  98.     define void AutoinstScripts()
  99.     {
  100.         if ( !Mode::autoinst () )
  101.         {
  102.             merged = mergeScripts();
  103.         }
  104.     }
  105.  
  106.  
  107.     /**
  108.      * Dump the settings to a map, for autoinstallation use.
  109.      * @return map
  110.      */
  111.     global define map<string, list> Export()
  112.     {
  113.         pre = [];
  114.         post = [];
  115.         chroot = [];
  116.         init = [];
  117.         y2milestone("Merged %1", merged);
  118.  
  119.         // split
  120.         foreach(map s, merged, ``{
  121.             if (s["type"]:"" == "pre-scripts")
  122.                 pre = add(pre,s);
  123.             else if (s["type"]:"" == "post-scripts")
  124.                 post = add(post,s);
  125.             else if (s["type"]:"" == "init-scripts")
  126.                 init = add(init,s);
  127.             else if (s["type"]:"" == "chroot-scripts")
  128.                 chroot = add(chroot,s);
  129.         });
  130.  
  131.  
  132.         // clean
  133.         list<map> expre =  maplist (map p, pre,  ``{
  134.             return ($["filename":p["filename"]:"",
  135.                       "interpreter": p["interpreter"]:"",
  136.                       "source":p["source"]:"",
  137.                       "location":p["location"]:"",
  138.                       "feedback":p["feedback"]:false,
  139.                       "debug":p["debug"]:true
  140.                     ]);
  141.         });
  142.         list<map> expost =  maplist (map p, post, ``{
  143.             return ($["filename":p["filename"]:"",
  144.                       "interpreter": p["interpreter"]:"",
  145.                       "source":p["source"]:"",
  146.                       "location":p["location"]:"",
  147.                       "feedback":p["feedback"]:false,
  148.                       "debug":p["debug"]:true,
  149.                       "network_needed":p["network_needed"]:false
  150.                       ]
  151.                     );
  152.         });
  153.         list<map> exchroot =  maplist (map p, chroot, ``{
  154.             return ($["filename":p["filename"]:"",
  155.                       "interpreter": p["interpreter"]:"",
  156.                       "source":p["source"]:"",
  157.                       "chrooted":p["chrooted"]:false,
  158.                       "location":p["location"]:"",
  159.                       "feedback":p["feedback"]:false,
  160.                       "debug":p["debug"]:true
  161.                     ]);
  162.         });
  163.         list<map> exinit =  maplist (map p, init, ``{
  164.             return ($["filename":p["filename"]:"",
  165.                       "source":p["source"]:"",
  166.                       "location":p["location"]:"",
  167.                       "debug":p["debug"]:true
  168.                     ]);
  169.         });
  170.         map<string, list> result = $[];
  171.         if (size(expre) > 0 )
  172.             result["pre-scripts"] = expre;
  173.         if (size(expost) > 0 )
  174.             result["post-scripts"] = expost;
  175.         if (size(exchroot) > 0 )
  176.             result["chroot-scripts"] = exchroot;
  177.         if (size(exinit) > 0 )
  178.             result["init-scripts"] = exinit;
  179.  
  180.         return result;
  181.  
  182.     }
  183.  
  184.  
  185.     /**
  186.      * Get all the configuration from a map.
  187.      * When called by autoinst_<module name> (preparing autoinstallation data)
  188.      * the map may be empty.
  189.      * @param settings    $[...]
  190.      * @return    success
  191.      */
  192.     global define boolean Import(map s)
  193.     {
  194.         y2debug("Calling AutoinstScripts::Import()");
  195.         pre = s["pre-scripts"]:[];
  196.         init = s["init-scripts"]:[];
  197.         post = s["post-scripts"]:[];
  198.         chroot = s["chroot-scripts"]:[];
  199.         merged = mergeScripts();
  200.         y2debug("merged: %1", merged);
  201.         return true;
  202.     }
  203.  
  204.     /**
  205.      * Return Summary
  206.      * @return string summary
  207.      */
  208.     global define string Summary()
  209.     {
  210.         string summary = "";
  211.         summary = Summary::AddHeader(summary, _("Preinstallation Scripts"));
  212.         if (size( pre) > 0 )
  213.         {
  214.             summary = Summary::OpenList(summary);
  215.             foreach(map script, pre, ``{
  216.                 summary = Summary::AddListItem(summary, script["filename"]:"" );
  217.             });
  218.             summary = Summary::CloseList(summary);
  219.         }
  220.         else
  221.         {
  222.             summary = Summary::AddLine(summary, Summary::NotConfigured());
  223.         }
  224.         summary = Summary::AddHeader(summary, _("Postinstallation Scripts"));
  225.         if (size( post) > 0)
  226.         {
  227.             summary = Summary::OpenList(summary);
  228.             foreach(map script, post, ``{
  229.                 summary = Summary::AddListItem(summary, script["filename"]:"" );
  230.             });
  231.             summary = Summary::CloseList(summary);
  232.         }
  233.         else
  234.         {
  235.             summary = Summary::AddLine(summary, Summary::NotConfigured());
  236.         }
  237.         summary = Summary::AddHeader(summary, _("Chroot Scripts"));
  238.         if (size( chroot) > 0 )
  239.         {
  240.             summary = Summary::OpenList(summary);
  241.             foreach(map script, chroot, ``{
  242.                 summary = Summary::AddListItem(summary, script["filename"]:"" );
  243.             });
  244.             summary = Summary::CloseList(summary);
  245.         }
  246.         else
  247.         {
  248.             summary = Summary::AddLine(summary, Summary::NotConfigured());
  249.         }
  250.         summary = Summary::AddHeader(summary, _("Init Scripts"));
  251.         if (size( init) > 0 )
  252.         {
  253.             summary = Summary::OpenList(summary);
  254.             foreach(map script, init, ``{
  255.                 summary = Summary::AddListItem(summary, script["filename"]:"" );
  256.             });
  257.             summary = Summary::CloseList(summary);
  258.         }
  259.         else
  260.         {
  261.             summary = Summary::AddLine(summary, Summary::NotConfigured());
  262.         }
  263.         return summary;
  264.     }
  265.  
  266.     /**
  267.      * delete a script from a list
  268.      * @param script name
  269.      * @return void
  270.      */
  271.     global define void  deleteScript(string scriptName)
  272.     {
  273.         list<map> clean = filter(map s, merged, ``(s["filename"]:"" != scriptName));
  274.         merged = clean;
  275.         return;
  276.     }
  277.  
  278.     /**
  279.      * Add or edit a script
  280.      * @param scriptName script name
  281.      * @param source source of script
  282.      * @param interpreter interpreter to be used with script
  283.      * @param type type of script
  284.      * @return void
  285.      */
  286.     global define void AddEditScript(string scriptName,
  287.             string source,
  288.             string interpreter,
  289.             string type,
  290.             boolean chrooted,
  291.             boolean debug,
  292.             boolean feedback,
  293.             boolean network
  294.             )
  295.     {
  296.         boolean mod = false;
  297.         merged = maplist (map script , merged, ``{
  298.             // Edit
  299.             if (script["filename"]:"" == scriptName)
  300.             {
  301.                 map oldScript = $[];
  302.                 oldScript=add(oldScript,"filename", scriptName);
  303.                 oldScript=add(oldScript,"source", source);
  304.                 oldScript=add(oldScript,"interpreter", interpreter);
  305.                 oldScript=add(oldScript,"type", type);
  306.                 oldScript=add(oldScript,"chrooted", chrooted);
  307.                 oldScript=add(oldScript,"debug",debug);
  308.                 oldScript=add(oldScript,"feedback",feedback);
  309.                 oldScript=add(oldScript,"network_needed",network);
  310.  
  311.                 mod = true;
  312.                 return oldScript;
  313.             }
  314.             else {
  315.                 return script;
  316.             }
  317.         });
  318.  
  319.         if (!mod)
  320.         {
  321.             map script = $[];
  322.             script=add(script,"filename", scriptName);
  323.             script=add(script,"source", source);
  324.             script=add(script,"interpreter", interpreter);
  325.             script=add(script,"type", type);
  326.             script=add(script,"chrooted", chrooted);
  327.             script=add(script,"debug",debug);
  328.             script=add(script,"feedback",feedback);
  329.             script=add(script,"network_needed",network);
  330.  
  331.             merged=add(merged,script);
  332.         }
  333.         y2debug("Merged scripts: %1", merged);
  334.         return;
  335.     }
  336.  
  337.  
  338.     /**
  339.      * return type of script as formatted string
  340.      * @param script type
  341.      * @return string type as translated string
  342.      */
  343.     global define string typeString(string type)
  344.     {
  345.         if (type == "pre-scripts")
  346.         {
  347.             return _("Pre");
  348.         }
  349.         else if (type == "post-scripts")
  350.         {
  351.             return _("Post");
  352.         }
  353.         else if (type == "init-scripts")
  354.         {
  355.             return _("Init");
  356.         }
  357.         else if (type == "chroot-scripts")
  358.         {
  359.             return _("Chroot");
  360.         }
  361.         return _("Unknown");
  362.     }
  363.  
  364.  
  365.  
  366.     /**
  367.      * Execute pre scripts
  368.      * @param string type of script
  369.      * @param boolean if script should be executed in chroot env.
  370.      * @return boolean true on success
  371.      */
  372.     global define boolean Write( string type , boolean special)
  373.     {
  374.         if (!Mode::autoinst ())
  375.             return true;
  376.  
  377.         list<map> scripts = [];
  378.         if (type == "pre-scripts")
  379.         {
  380.             scripts = pre;
  381.         }
  382.         else if (type == "init-scripts")
  383.         {
  384.             scripts = init;
  385.         }
  386.         else if ( type == "chroot-scripts" && !special)
  387.         {
  388.             scripts = filter(map s, chroot, ``(!s["chrooted"]:false));
  389.         }
  390.         else if ( type == "chroot-scripts" && special)
  391.         {
  392.             scripts = filter(map s, chroot, ``(s["chrooted"]:false));
  393.         }
  394.         else if (type == "post-scripts" && !special)
  395.         {
  396.             scripts = filter(map s, post, ``(!s["network_needed"]:false));
  397.         }
  398.         else if (type == "post-scripts" && special)
  399.         {
  400.             scripts = filter(map s, post, ``(s["network_needed"]:false));
  401.         }
  402.         else
  403.         {
  404.             y2error("Unsupported script type");
  405.             return false;
  406.         }
  407.  
  408.  
  409.         string tmpdirString = "";
  410.         string current_logdir = "";
  411.  
  412.         if (type == "pre-scripts")
  413.         {
  414.             tmpdirString = sformat("%1/%2", AutoinstConfig::tmpDir, type);
  415.             SCR::Execute (.target.mkdir, tmpdirString);
  416.  
  417.             current_logdir = sformat("%1/logs", tmpdirString);
  418.             SCR::Execute (.target.mkdir,  current_logdir);
  419.         }
  420.         else if (type == "chroot-scripts")
  421.         {
  422.             tmpdirString = sformat("%1%2", AutoinstConfig::destdir,  AutoinstConfig::scripts_dir);
  423.             SCR::Execute (.target.mkdir, tmpdirString);
  424.  
  425.             current_logdir = sformat("%1%2", AutoinstConfig::destdir, AutoinstConfig::logs_dir);
  426.             SCR::Execute (.target.mkdir, current_logdir);
  427.         }
  428.         else
  429.         {
  430.             current_logdir = AutoinstConfig::logs_dir;
  431.         }
  432.  
  433.  
  434.         foreach( map s, scripts,
  435.                  ``{
  436.             string scriptInterpreter    = s["interpreter"]:"shell";
  437.             string scriptName = s["filename"]:type;
  438.             if (scriptName=="")
  439.             {
  440.                 map t = URL::Parse(s["location"]:"");
  441.                 scriptName=basename(t["path"]:"");
  442.             }
  443.             string scriptPath = "";
  444.  
  445.             if (type == "pre-scripts")
  446.             {
  447. //        y2milestone("doing /sbin/udevcontrol stop_exec_queue now to prevent trouble if one is doing partitioning in a pre-script");
  448. //        SCR::Execute( .target.bash, "/sbin/udevcontrol stop_exec_queue" );
  449.                 scriptPath = sformat("%1/%2/%3", AutoinstConfig::tmpDir, type, scriptName);
  450.                 y2milestone("Writing pre script into %1", scriptPath);
  451.                 if (s["location"]:"" != "")
  452.                 {
  453.                     y2debug("getting script: %1", s["location"]:"" );
  454.                     if (!GetURL(s["location"]:"", scriptPath ) )
  455.                     {
  456.                        y2error("script %1 could not be retrieved", s["location"]:"");
  457.                     }
  458.                 } else {
  459.                     SCR::Write(.target.string,  scriptPath, s["source"]:"echo Empty script!");
  460.                 }
  461.             }
  462.             else if (type == "init-scripts")
  463.             {
  464.                 scriptPath = sformat("%1/%2",  AutoinstConfig::initscripts_dir,  scriptName);
  465.                 y2milestone("Writing init script into %1", scriptPath);
  466.                 if (s["location"]:""!="")
  467.                 {
  468.                     y2debug("getting script: %1", s["location"]:"" );
  469.                     if (!GetURL(s["location"]:"", scriptPath ) )
  470.                     {
  471.                        y2error("script %1 could not be retrieved", s["location"]:"");
  472.                     }
  473.                 } else {
  474.                     SCR::Write(.target.string,  scriptPath, s["source"]:"echo Empty script!");
  475.                 }
  476.                 Service::Enable("autoyast");
  477.             }
  478.             else if (type == "chroot-scripts")
  479.             {
  480.                 //scriptPath = sformat("%1%2/%3", (special) ? "" : AutoinstConfig::destdir,  AutoinstConfig::scripts_dir,  scriptName);
  481.  
  482.                 // write always to /mnt/....
  483.                 scriptPath = sformat("%1%2/%3",AutoinstConfig::destdir,  AutoinstConfig::scripts_dir,  scriptName);
  484.                 y2milestone("Writing chroot script into %1", scriptPath);
  485.                 if (s["location"]:""!="")
  486.                 {
  487.                     if (!GetURL(s["location"]:"", scriptPath ) )
  488.                     {
  489.                        y2error("script %1 could not be retrieved", s["location"]:"");
  490.                     }
  491.                 } else {
  492.                     SCR::Write(.target.string,  scriptPath, s["source"]:"echo Empty script!");
  493.                 }
  494.             }
  495.             else
  496.             {
  497.                 // disable all sources and finish target
  498.                 Pkg::SourceFinishAll();
  499.                 Pkg::TargetFinish();
  500.  
  501.                 scriptPath = sformat("%1/%2", AutoinstConfig::scripts_dir, scriptName);
  502.                 y2milestone("Writing  script into %1", scriptPath);
  503.                 if (s["location"]:""!="")
  504.                 {
  505.                     if (!GetURL(s["location"]:"", scriptPath ) )
  506.                     {
  507.                        y2error("script %1 could not be retrieved", s["location"]:"");
  508.                     }
  509.                 } else {
  510.                     SCR::Write(.target.string,  scriptPath, s["source"]:"echo Empty script!");
  511.                 }
  512.             }
  513.  
  514.             if (type != "init-scripts")
  515.             {
  516.                 // string message =  sformat(_("Executing user supplied script: %1"), scriptName);
  517.                 string executionString = "";
  518.                 boolean showFeedback = s["feedback"]:false;
  519.  
  520.                 if (scriptInterpreter == "shell")
  521.                 {
  522.                     string debug = ( s["debug"]:true ? "-x" : "" );
  523.                     executionString = sformat("/bin/sh %1 %2 2&> %3/%4.log ", debug, scriptPath, current_logdir, scriptName);
  524.                     SCR::Execute (.target.bash, executionString);
  525.                 }
  526.                 else if (scriptInterpreter == "perl")
  527.                 {
  528.                     string debug = ( s["debug"]:true ? "-w" : "" );
  529.                     executionString = sformat("/usr/bin/perl %1 %2 2&> %3/%4.log ", debug, scriptPath, current_logdir, scriptName);
  530.                     SCR::Execute (.target.bash, executionString);
  531.                 }
  532.                 else if (scriptInterpreter == "python")
  533.                 {
  534.                     executionString = sformat("/usr/bin/python %1 2&> %2/%3.log ", scriptPath, current_logdir, scriptName );
  535.                     SCR::Execute (.target.bash, executionString);
  536.                 }
  537.                 else
  538.                 {
  539.                     y2error("Unknown interpreter: %1", scriptInterpreter);
  540.                 }
  541.                 string feedback = "";
  542.                 if( showFeedback ) {
  543.                     feedback = (string)SCR::Read(.target.string, current_logdir+"/"+scriptName+".log" );
  544.                 }
  545.                 if( size(feedback) > 0 ) {
  546.                     Popup::LongText(type, `RichText(`opt(`plainText), feedback), 50, 20 );
  547.                 }
  548.                 y2milestone("Script Execution command: %1", executionString );
  549.             }
  550.         });
  551.  
  552.         return true;
  553.     }
  554.  
  555.  
  556. // EOF
  557. }
  558.