ACC SHELL

Path : /usr/share/YaST2/clients/
File Upload :
Current File : //usr/share/YaST2/clients/online_update.ycp

/**
 * Summary:	Main file
 * Authors:	Gabriele Strattner <gs@suse.de>
 *		Stefan Schubert <schubi@suse.de>
 *              Cornelius Schumacher <cschum@suse.de>
 */

{

  textdomain "online-update";

  import "CommandLine";
  import "Confirm";
  import "Directory";
  import "FileUtils";
  import "Label";
  import "Mode";
  import "OnlineUpdate";
  import "OnlineUpdateCallbacks";
  import "PackageLock";
  import "Popup";
  import "Progress";
  import "SourceManager";
  import "URL";
  import "Wizard";
  import "GetInstArgs";

/**
 * Main sequence for Online Update
 */
define symbol OnlineUpdateSequence () {

  Wizard::CreateDialog();
  Wizard::SetDesktopIcon("online_update");
  // help text for online-update initialization
  Wizard::RestoreHelp (_("<p>The system is initializing the installation and update repositories. Software repositories can be altered in the <b>Installation Source</b> module.</p>"));

  list<string> stages =  [
	    // progress stage label
	    _("Initialize the target system"),
	    // progress stage label
	    _("Refresh software repositories"),
	    // progress stage label
	    _("Check for available updates"),
  ];
  list steps = [
	    // progress step label
	    _("Initializing the target system..."),
	    // progress step label
	    _("Refreshing software repositories..."),
	    // progress step label
	    _("Checking for available updates..."),
	    // final progress step label
	    _("Finished")
  ];

  string caption	= OnlineUpdate::cd_update ?
    // dialog caption
    _("Initializing CD Update") :
    // dialog caption
    _("Initializing Online Update");
  Progress::New (caption, " ", 2, stages, steps, "");

  // check whether running as root
  // and having the packager for ourselves
  if (! Confirm::MustBeRoot () || !PackageLock::Connect(false)["connected"]:false )
  {
      Wizard::CloseDialog ();
      return `cancel;
  }

  Progress::NextStage();

  // initialize target to import all trusted keys (#165849)
  Pkg::TargetInit( "/", false );

  Progress::NextStage ();

  OnlineUpdateCallbacks::RefreshAllSources();

  Progress::NextStage ();

  if (!OnlineUpdate::cd_update) // CD for cd update was not initialized yet
  {
    boolean is_available	= false;
    /* FIXME bnc#459527
    current update repositories are still not tagged with is_update_repo
    foreach (map source, Pkg::SourceEditGet (), {
	integer srcid   = source["SrcId"]:-1;
	map data        = Pkg::SourceGeneralData (srcid);
	if (data["is_update_repo"]:false)
	{
	    is_available	= true;
	    break;
	}
    });
    */
    // old way: check if there is any patch for installation
    foreach (map patch, Pkg::ResolvableProperties ("", `patch, ""), {
	if (patch["status"]:`none == `available)
	{
	    is_available        = true;
	    break;
	}
    });

    // yes/no message
    if (!is_available && Popup::YesNo (_("No update repository
configured yet. Run configuration workflow now?")))
    {
	// inst_suse_register is still able to configure update repo,
	// however this will change in future (see bnc#565094)
	any res	= WFM::CallFunction ("inst_suse_register", []);
	if (res != `next && res != `finish)
	{
	    Wizard::CloseDialog();
	    return `abort;
	}
    }
  }

  OnlineUpdateCallbacks::RegisterOnlineUpdateCallbacks();

  Progress::Finish();

  /*
   * Main dialog cycle
   *
   */

  list dialog = [
    [ "online_update_select",	[ GetInstArgs::Buttons(false, true) ] ],
    [ "online_update_install",	[ GetInstArgs::Buttons(false, true) ] ],
    [ "inst_suseconfig",	[ GetInstArgs::Buttons(false,false) ] ]
  ];

  integer id = 0;
  any result = `next;

  while ( ( id >= 0 ) && ( id < size( dialog ) ) ) {

      list page = dialog[ id ]:[];
      string module_name = page[ 0 ]:"";
      list module_args = page[ 1 ]:[];

      y2debug( "ONLINE: Module: %1 Args: %2", module_name, module_args);

      if ( id == (size( dialog ) - 1)) {
          Wizard::SetNextButton(`next, Label::FinishButton() );
      }

      result = WFM::CallFunction( module_name, module_args );

      if ( result == nil ) result = `cancel;

      if (result == `again )
	  continue;
      else if (result == `cancel || result == `abort)
	  break;
      else if (result == `next || result == `auto )// SuSEConfig returns `auto
	  id = id + 1;
      else if (result == `back)
	  id = id - 1;
      else if ( result == `finish )
          if ( !Mode::installation() && !Mode::update() )
	  {
	      id = size(dialog)-1;		// call last module
          }
	  else
	  {
              result = `next;
              break;
          }
  }

  Wizard::CloseDialog();

  // `back is strange, user can go back after installation...
  if (result == `abort || result == `cancel) return `abort;

  if (OnlineUpdate::cd_update && OnlineUpdate::cd_source > -1)
  {
    // map of the new source added on start
    map new_source		= $[];
    list<map> sources		= [];
    foreach (map<string,any> one_source, Pkg::SourceEditGet (), {
	integer srcid	= one_source["SrcId"]:-1;
	map source_data	= union (Pkg::SourceGeneralData (srcid),
	    Pkg::SourceProductData (srcid));
	if (srcid == OnlineUpdate::cd_source)
	    new_source	= source_data;
	else
	    sources	= add (sources, source_data);
    });
    boolean add_new_source	= true;
    foreach (map source, sources, {
	y2debug ("source to check %1", source);
	// checking URL is not enough, typical url is cd:///
	if (source["url"]:"" == new_source["url"]:"" &&
	    source["product_dir"]:"" == new_source["product_dir"]:"" &&
	    source["productname"]:"" == new_source["productname"]:"" &&
	    source["productversion"]:"" == new_source["productversion"]:"")
	{
	    y2milestone ("Patch CD source already present, not adding again");
	    add_new_source	= false;
	    break;
	}
    });
    if (add_new_source)
    {
	// now, ensure there's first CD in the drive (bnc#381594)
	map parsed = URL::Parse (new_source["url"]:"");
	if (parsed["scheme"]:"" == "cd")
	{
	    // SourceProvideFile should use a callback to ask user for 1st media
	    if (Pkg::SourceProvideFile (
		OnlineUpdate::cd_source, 1, "/media.1/media") == nil)
		add_new_source	= false;
	}
    }

    if (add_new_source)
    {
	Pkg::SourceSaveAll ();
    }
  }

  if (OnlineUpdate::restart_yast)
  {
    if (FileUtils::Exists (Directory::vardir + "/selected_patches.ycp"))
    {
	Popup::Message (OnlineUpdate::restart_message);
    }
    OnlineUpdate::restart_yast	= false;
  }
  if (OnlineUpdate::reboot_needed)
  {
    if (size (OnlineUpdate::reboot_packages) > 0)
	Popup::Message (sformat (OnlineUpdate::reboot_message_list, mergestring (OnlineUpdate::reboot_packages, "\n")));
    else
	Popup::Message (OnlineUpdate::reboot_message);
  }
  else if (OnlineUpdate::relogin_needed)
  {
    Popup::Message (OnlineUpdate::relogin_message);
  }

  return `next;
}

  /**
   * command line handler for CD update (=> add CD as installation source)
   */
  define boolean CDUpdateHandler (map options ) {
    OnlineUpdate::cd_update	= true;
    if (options["cd_url"]:"" != "")
    {
	OnlineUpdate::cd_url      = options["cd_url"]:"";
    }
    if (options["cd_directory"]:"" != "")
    {
	OnlineUpdate::cd_directory	= options["cd_directory"]:"";
    }
    OnlineUpdateSequence ();
    return true;
  }

  /**
   * command line handler for "Simple Mode": simple package selector
   */
  define boolean SimpleModeHandler (map options ) {
    OnlineUpdate::simple_mode	= true;
    OnlineUpdateSequence ();
    return true;
  }

  // the command line description map
  map cmdline = $[
    "id"		: "online_update",
    // command line help text
    "help"		: _("Online Update module"),
    "guihandler"	: OnlineUpdateSequence,
    "actions"		: $[
	"cd_update" :$[
	    "handler"	: CDUpdateHandler,
	    // command line help text for cd_update action
	    "help"	: _("Start Patch CD Update")
	],
	"simple_mode"	: $[
	    "handler"	: SimpleModeHandler,
	    // command line help text for simple_mode action
	    "help"	: _("Use simple package selector")
	],
    ],
    "options"		: $[
	"cd_url"	:$[
	    // command line help text for cd_url option
	    "help"	: sformat(_("URL for Patch CD (default value is '%1')"),
	    OnlineUpdate::cd_url),
	    "type"	: "string"
	],
	"cd_directory"	:$[
	    // command line help text for cd_directory option
	    "help"	: sformat(_("Directory for patch data on Patch CD (default value is '%1')"), OnlineUpdate::cd_directory),
	    "type"	: "string"
	],
    ],
    "mappings"		: $[
	"cd_update"	: [ "cd_url", "cd_directory"],
	"simple_mode"	: [],
    ]
  ];

  // first check for obsoleted arguments
  integer arg_n = 0;
  while (arg_n < size (WFM::Args())) {
    any arg = WFM::Args( arg_n );
    if ( arg == .cd_default || arg == ".cd_default" )
    {
	y2warning (".cd_default parameter is OBSOLETE, use cd_update instead.");
	OnlineUpdate::cd_update	= true;
    }
    else if ( arg == .simple_mode || arg == ".simple_mode" )
    {
	OnlineUpdate::simple_mode	= true;
    }
    else if ( arg == .auto.get || arg == ".auto.get" )
    {
	y2warning (".auto.get parameter for online_update is OBSOLETE, use zypper or rug instead.");
    }
    arg_n = arg_n + 1;
  }
  any ret	= `next;
  if (OnlineUpdate::cd_update || OnlineUpdate::simple_mode) // obsolete .cd_default argument was used
    ret		= OnlineUpdateSequence ();
  else
    ret		= CommandLine::Run (cmdline);
  return ret;
}

ACC SHELL 2018