ACC SHELL

Path : /usr/share/YaST2/modules/
File Upload :
Current File : //usr/share/YaST2/modules/ProfileLocation.ycp

/**
 * File:	modules/ProfileLocation.ycp
 * Package:	Auto-installation
 * Summary:	Process Auto-Installation Location
 * Author:	Anas Nashif <nashif@suse.de>
 *
 * $Id: ProfileLocation.ycp 60367 2010-01-14 14:25:09Z ug $
 */
{
    module "ProfileLocation";
    textdomain "autoinst";

    import "AutoinstConfig";
    import "AutoInstallRules";
    import "StorageDevices";
    import "Mode";
    import "Installation";
    import "Popup";
    import "Label";
    import "URL";


    include "autoinstall/autoinst_dialogs.ycp";
    include "autoinstall/io.ycp";


    /**
     * Constructor
     * @return void
     */
    global define void ProfileLocation ()
        ``{
        return;
    }


    /**
     * Initiate retrieving of control files and Rules.
     * @return boolean
     */
    global define boolean Process() ``{

        boolean ok = false;
        boolean ret = false;

        y2milestone("Path to remote control file: %1", AutoinstConfig::filepath);

        string localfile  =  AutoinstConfig::xml_tmpfile;

        boolean is_directory = false;

        if( AutoinstConfig::scheme == "relurl" ) {
            // FIXME:
            // file                  # local file

            AutoinstConfig::scheme   = (string)SCR::Read(.etc.install_inf.InstMode);
            if( AutoinstConfig::scheme == "hd" || AutoinstConfig::scheme == "harddisk" || AutoinstConfig::scheme == "disk" ) {
                string part = (string)SCR::Read(.etc.install_inf.Partition);
                AutoinstConfig::scheme = "device";
                AutoinstConfig::host = part;
                AutoinstConfig::filepath = (string)SCR::Read(.etc.install_inf.Serverdir) + "/" + AutoinstConfig::filepath;;
            } else {
                if( AutoinstConfig::scheme == "cd" || AutoinstConfig::scheme == "cdrom" ) {
                    AutoinstConfig::scheme = "file";
                }
                if( size(AutoinstConfig::filepath) > 0 ) {
                    AutoinstConfig::filepath = (string)SCR::Read(.etc.install_inf.Serverdir)+"/"+AutoinstConfig::host+"/"+AutoinstConfig::filepath;
                } else {
                    AutoinstConfig::filepath = (string)SCR::Read(.etc.install_inf.Serverdir)+"/"+AutoinstConfig::host;
                }
                if( (string)SCR::Read(.etc.install_inf.Server) != nil ) {
                    AutoinstConfig::host     = (string)SCR::Read(.etc.install_inf.Server);
                }
            }

            y2milestone("relurl for profile changed to: %1://%2%3",AutoinstConfig::scheme, AutoinstConfig::host, AutoinstConfig::filepath);
            SCR::Write ( .etc.install_inf.ayrelurl, sformat("%1://%2/%3",AutoinstConfig::scheme, AutoinstConfig::host, AutoinstConfig::filepath) );
            SCR::Write (.etc.install_inf, nil);
        } else if( AutoinstConfig::scheme == "label" ) {
            y2milestone("searching label");
            foreach( string device, map v, Storage::GetTargetMap(), ``{
                y2milestone("looking on %1", device);
                if( v["label"]:"" == AutoinstConfig::host ) { // FIXME: does not work. Label was "msdos" while testing
                    AutoinstConfig::scheme = "device";
                    AutoinstConfig::host   = substring( device, 5 );
                    y2milestone("found on %1", AutoinstConfig::host);
                    break;
                }
                foreach( map p, v["partitions"]:[],  ``{
                    if( p["label"]:"" == AutoinstConfig::host ) {
                        AutoinstConfig::scheme = "device";
                        AutoinstConfig::host   = substring( p["device"]:device , 5 );
                        y2milestone("found on %1", AutoinstConfig::host);
                        break;
                    }
                    y2milestone("not found on %1", p["device"]:"hm?");
                });
                if( AutoinstConfig::scheme == "device" )
                    break;
            });
            if( AutoinstConfig::scheme == "label" )
                Popup::Error( _("label not found while looking for autoyast profile") );
        }
        string filename = basename(AutoinstConfig::filepath);


        if ( filename != "" )
        {
            y2milestone("File=%1", filename);
            y2milestone("Get %1://%2/%3 to %4", AutoinstConfig::scheme, AutoinstConfig::host, AutoinstConfig::filepath, localfile);
            ret = Get(AutoinstConfig::scheme,
                    AutoinstConfig::host,
                    AutoinstConfig::filepath,
                    localfile
                    );
            if (!ret)
            {
                // autoyast hit an error while fetching it's config file
                string error = _("An error occurred while fetching the profile:\n");
                Popup::Error( error + GET_error );
                return false;
            }
            string tmp = (string)SCR::Read (.target.string, localfile);
            list<string> l = splitstring (tmp, "\n");
            while ( tmp != nil && l[0]:"" == "-----BEGIN PGP MESSAGE-----" ) {
                y2milestone("encrypted profile found");
                UI::OpenDialog(
                           `VBox(
                                 `Label( _("Encrypted AutoYaST profile. Please enter the correct password.") ),
                                 `Password(`id(`password), ""),
                                 `PushButton(`id(`ok), _("&OK") )
                                 )
                           );
                string p = "";
                any button = nil;
                repeat {
                    button = UI::UserInput();
                    p = (string) UI::QueryWidget(`id(`password), `Value);
                } until ( button == `ok );
                UI::CloseDialog();
                SCR::Execute( .target.bash, sformat("gpg2 --batch --output \"/tmp/decrypt.xml\" --passphrase \"%1\" %2",AutoinstConfig::ShellEscape(p), localfile) );
                if( SCR::Read (.target.size, "/tmp/decrypt.xml") > 0 ) {
                    SCR::Execute( .target.bash, sformat("mv /tmp/decrypt.xml %1",localfile) );
                    y2milestone("decrypted. Moving now /tmp/decrypt.xml to %1",localfile);
                    tmp = (string)SCR::Read (.target.string, localfile);
                    l = splitstring (tmp, "\n");
                }
            }
        }
        else
        {
            is_directory = true;
        }

        AutoinstConfig::directory = dirname(AutoinstConfig::filepath);

        y2milestone("Dir=%1", AutoinstConfig::directory);
        y2milestone("Fetching Rules File");

        // Get rules file

        boolean mkdir = (boolean)SCR::Execute(.target.mkdir, AutoinstConfig::local_rules_location);
        if (!mkdir)
        {
            y2error("Error creating directory: %1", AutoinstConfig::local_rules_location );
        }

        if (is_directory)
        {
            ret = Get(AutoinstConfig::scheme,
                    AutoinstConfig::host,
                    AutoinstConfig::directory + "/" +  AutoinstConfig::remote_rules_location ,
                    AutoinstConfig::local_rules_file);
        }
        else
        {
            ret = false;
        }

        if (ret)
        {
            AutoInstallRules::userrules = true;
        }
        else
        {
            AutoInstallRules::userrules = false;
            SCR::Execute(.target.remove, AutoinstConfig::local_rules_file );
        }

        boolean process_rules = true;
        boolean try_default_rules = false;
        if ( AutoInstallRules::userrules)
        {
            y2milestone("Reading Rules File");
            AutoInstallRules::Read();
            // returns false if no rules have matched
            ret = AutoInstallRules::GetRules();
            if (!ret)
            {
                try_default_rules = true;
            }
        }
        else
        {
            // required for classes too. Without it, they only work with rules together
            try_default_rules = true;
        }

        // Hex IP, MAC Address.
        if (try_default_rules)
        {
            y2milestone("Creating default Rules");
            if (is_directory)
            {
                // Create rules for hex ip and mac address
                AutoInstallRules::CreateDefault();
            }
            else
            {
                // Create rules for file
                AutoInstallRules::CreateFile( filename );
            }
            ret = AutoInstallRules::GetRules();
            if (!ret)
            {
                return false;
            }
        }

        if (process_rules)
        {
            boolean rulesret = AutoInstallRules::Process( AutoinstConfig::xml_tmpfile);
            y2milestone("rulesret=%1", rulesret);
            return rulesret;
        }

        return true;
    }




}

ACC SHELL 2018