Esempio n. 1
0
        /// <summary>Runs the job (.xml file) specified on the command line.</summary>
        /// <returns>True if something was run.</returns>
        private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings)
        {
            if (appSettings.ContainsKey("FileName"))
            {
                string jobFileName = appSettings["FileName"];

                if (File.Exists(jobFileName))
                {
                    appSettings.TryGetValue("APSIMXExecutable", out string executable);

                    string jobXML  = File.ReadAllText(jobFileName);
                    string jobName = Path.GetFileNameWithoutExtension(jobFileName);

                    var environment = new APSIM.Cloud.Shared.RuntimeEnvironment
                    {
                        APSIMRevision  = appSettings["APSIMRevision"],
                        RuntimePackage = appSettings["RuntimePackage"],
                    };

                    RunYPJob job = new RunYPJob(jobXML, environment)
                    {
                        ApsimXExecutable = executable
                    };
                    if (job.Errors.Count == 0)
                    {
                        IJobRunner runner = new JobRunnerAsync();
                        runner.Run(job, wait: true);
                    }

                    if (job.AllFilesZipped != null)
                    {
                        string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip");
                        using (Stream s = File.Create(destZipFileName))
                        {
                            job.AllFilesZipped.Seek(0, SeekOrigin.Begin);
                            job.AllFilesZipped.CopyTo(s);
                        }
                    }

                    if (job.Errors.Count > 0)
                    {
                        string msg = string.Empty;
                        foreach (string error in job.Errors)
                        {
                            msg += error + Environment.NewLine;
                        }
                        throw new Exception(msg);
                    }
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 2
0
 /// <summary>Constructor</summary>
 /// <param name="sims">The list of APSIM simulations to run</param>
 /// <param name="environment">The runtime environment to use for the run</param>
 public RunYPJob(List <APSIMSpecification> sims, RuntimeEnvironment environment)
 {
     simulationsToRun = sims;
     this.environment = environment;
     Initialise();
 }
Esempio n. 3
0
 /// <summary>Constructor</summary>
 /// <param name="xml">The YieldProphet xml to run</param>
 /// <param name="environment">The runtime environment to use for the run</param>
 public RunYPJob(string xml, RuntimeEnvironment environment)
 {
     specXMLToRun     = xml;
     this.environment = environment;
     Initialise();
 }
Esempio n. 4
0
 /// <summary>Constructor</summary>
 /// <param name="yp">The YieldProphet spec to run</param>
 /// <param name="environment">The runtime environment to use for the run</param>
 public RunYPJob(YieldProphet yp, RuntimeEnvironment environment)
 {
     specToRun        = yp;
     this.environment = environment;
     Initialise();
 }
Esempio n. 5
0
        /// <summary>
        /// Ensure the correct runtime is ready to go. Return a path to the correct folder
        /// that contains the APSIM executables.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static string SetupRunTimeEnvironment(RuntimeEnvironment environment)
        {
            string binFolder         = null;
            string environmentFolder = null;
            string url = null;
            string commandLineArguments = null;

            if (environment.APSIMRevision != null)
            {
                environmentFolder = environment.APSIMRevision;
                if (environment.RuntimePackage != null)
                {
                    environmentFolder += "-";
                    environmentFolder += environment.RuntimePackage;
                }
                url       = @"http://bob.apsim.info/files/" + environment.APSIMRevision + ".binaries.WINDOWS.INTEL.exe";
                binFolder = Path.Combine(environmentFolder, "Temp", "Model");
            }
            if (environment.APSIMxBuildNumber > 0)
            {
                url = WebUtilities.CallRESTService <string>("http://www.apsim.info/APSIM.Builds.Service/Builds.svc/GetURLOfVersionForIssue?issueid=" +
                                                            environment.APSIMxBuildNumber);
                environmentFolder = "ApsimX-" + environment.APSIMxBuildNumber;
                if (environment.RuntimePackage != null)
                {
                    environmentFolder += "-";
                    environmentFolder += environment.RuntimePackage;
                }
                commandLineArguments = "/SILENT /NOICONS /DIR=\".\"";
                binFolder            = Path.Combine(environmentFolder, "Bin");
            }
            else if (environment.AusfarmRevision != null)
            {
                environmentFolder = environment.AusfarmRevision;
                if (environment.RuntimePackage != null)
                {
                    environmentFolder += "-";
                    environmentFolder += environment.RuntimePackage;
                }
                string packageFileName = Path.Combine("RuntimePackages", environment.AusfarmRevision + ".zip");
                ZipUtilities.UnZipFiles(packageFileName, environmentFolder, null);
                binFolder = Path.Combine(environmentFolder, "Ausfarm");
            }

            environmentFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), environmentFolder);
            if (!Directory.Exists(environmentFolder))
            {
                string downloadedFileName = Path.Combine(environmentFolder, "Temp.exe");

                // Download the file
                DownloadAndExecuteFile(environmentFolder, url, commandLineArguments, downloadedFileName);

                // Copy in the extra runtime packages.
                if (environment.RuntimePackage != null)
                {
                    string packageFileName = Path.Combine("RuntimePackages", environment.RuntimePackage + ".zip");
                    ZipUtilities.UnZipFiles(packageFileName, environmentFolder, null);
                }
            }

            return(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), binFolder));
        }
Esempio n. 6
0
 /// <summary>Constructor</summary>
 /// <param name="f4p">Job specification</param>
 /// <param name="environment">The runtime environment to use for the run</param>
 public RunF4PJob(Farm4Prophet f4p, RuntimeEnvironment environment)
 {
     workingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
     Directory.CreateDirectory(workingDirectory);
     Initialise(f4p, environment);
 }
Esempio n. 7
0
        /// <summary>Runs the job (.xml file) specified on the command line.</summary>
        /// <returns>True if something was run.</returns>
        private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings)
        {
            if (appSettings.ContainsKey("FileName"))
            {
                string jobFileName = appSettings["FileName"];

                if (File.Exists(jobFileName))
                {
                    var ypEnvironment = new APSIM.Cloud.Shared.RuntimeEnvironment
                    {
                        APSIMRevision  = appSettings["APSIMRevision"],
                        RuntimePackage = appSettings["RuntimePackage"],
                    };

                    if (appSettings.ContainsKey("UpdateFile"))
                    {
                        YieldProphetOld.UpdateFile(jobFileName);
                        return(true);
                    }
                    else if (appSettings.ContainsKey("ConvertToAPSIM"))
                    {
                        string   jobXML = File.ReadAllText(jobFileName);
                        RunYPJob job    = new RunYPJob(jobXML, ypEnvironment, createSims: false);
                        AllocConsole();
                        Console.WriteLine(job.WorkingDirectory);
                        Console.WriteLine();
                        if (job.Errors != null)
                        {
                            string msg = null;
                            foreach (string error in job.Errors)
                            {
                                msg += error + Environment.NewLine;
                            }
                            throw new Exception(msg);
                        }
                        return(true);
                    }
                    else
                    {
                        appSettings.TryGetValue("APSIMXExecutable", out string executable);

                        string jobXML  = File.ReadAllText(jobFileName);
                        string jobName = Path.GetFileNameWithoutExtension(jobFileName);

                        IYPJob job = null;
                        if (jobXML.Contains("Farm4Prophet"))
                        {
                            var environment = new APSIM.Cloud.Shared.RuntimeEnvironment
                            {
                                AusfarmRevision = appSettings["AusfarmRevision"],
                            };
                            job = new RunF4PJob(jobXML, environment);
                        }
                        else
                        {
                            job = new RunYPJob(jobXML, ypEnvironment)
                            {
                                ApsimXExecutable = executable
                            };
                        }
                        if (job.Errors == null || job.Errors.Count == 0)
                        {
                            IJobRunner runner = new JobRunnerAsync();
                            runner.Run(job as IJobManager, wait: true);
                        }

                        if (job.AllFilesZipped != null)
                        {
                            string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip");
                            using (Stream s = File.Create(destZipFileName))
                            {
                                job.AllFilesZipped.Seek(0, SeekOrigin.Begin);
                                job.AllFilesZipped.CopyTo(s);
                            }
                        }

                        if (job.Errors != null && job.Errors.Count > 0)
                        {
                            string msg = string.Empty;
                            foreach (string error in job.Errors)
                            {
                                msg += error + Environment.NewLine;
                            }
                            throw new Exception(msg);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }