Esempio n. 1
0
        public static void Main(string[] args)
        {
            OptionException oe;

            //Parse command line options
            CommandLineWrapper commandLineOptions = null;

            try
            {
                commandLineOptions = new CommandLineWrapper(args);
            }
            catch (OptionException e)
            {
                Console.Error.WriteLine("ScriptRunner start failed:");
                Console.Error.WriteLine(e.Message);

                commandLineOptions.PrintHelp();

                Environment.Exit(0xA0);
            }

            //Set up logging
            try
            {
                DirectoryInfo logDirectory;
                if (!string.IsNullOrEmpty(commandLineOptions.LogDirectory))
                {
                    logDirectory = new DirectoryInfo(commandLineOptions.LogDirectory);
                }
                else
                {
                    //Default the log directory to the current working directory
                    logDirectory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
                }

                //Create a log file in the specified directory and set it up for writing
                Logger.InitialiseLogFile(logDirectory);
            }
            catch (IOException e)
            {
                Console.Error.WriteLine("ScriptRunner failed to initialise log file:");
                Console.Error.WriteLine(e.Message);
                Environment.Exit(1);
            }

            //Set up the logger
            if (commandLineOptions.LogStdOut)
            {
                Logger.logToStandardOut();
            }

            if (commandLineOptions.LogDebug)
            {
                Logger.enableDebugLogging();
            }

            Logger.logAndEcho(ScriptRunnerVersion.getVersionString());

            //Main branch - call the relevant subprocess based on supplied arguments
            bool lError = false;

            try
            {
                if (commandLineOptions.RunDirectory != null)
                {
                    ScriptRunner.run(commandLineOptions);
                    if (commandLineOptions.hasOption(CommandLineOption.NO_EXEC))
                    {
                        Logger.logAndEcho("-noexec parse completed successfully");
                    }
                    else
                    {
                        Logger.logAndEcho("Promotion completed successfully");
                    }
                }
                else if (commandLineOptions.hasOption(CommandLineOption.BUILD))
                {
                    ScriptBuilder.run(commandLineOptions);
                    Logger.logAndEcho("Build completed successfully");
                }
                else if (commandLineOptions.hasOption(CommandLineOption.INSTALL))
                {
                    Logger.logAndEcho("Installing ScriptRunner");
                    Installer.run(commandLineOptions);
                    //Haul up to latest version
                    Updater.run(commandLineOptions);
                    Logger.logAndEcho("Install completed successfully");
                }
                else if (commandLineOptions.hasOption(CommandLineOption.UPDATE))
                {
                    Logger.logAndEcho("Checking Scriptrunner is up to date");
                    Updater.run(commandLineOptions);
                    Logger.logAndEcho("Update check completed successfully");
                }
                else if (commandLineOptions.hasOption(CommandLineOption.PARSE_SCRIPTS))
                {
                    List <String> lFileList = commandLineOptions.getOptionValues(CommandLineOption.PARSE_SCRIPTS);
                    Logger.logAndEcho("Parsing " + lFileList.size() + " PatchScript" + (lFileList.size() != 1 ? "s" : ""));
                    lError = !PatchScript.printScriptsToStandardOut(new File(System.getProperty("user.dir")), lFileList);
                }

                //Print a message to standard out if warnings were encountered
                int lWarnCount = Logger.getWarningCount();
                if (Logger.getWarningCount() > 0)
                {
                    Logger.logAndEcho(lWarnCount + " warning" + (lWarnCount != 1 ? "s were" : " was") + " detected during execution; please review the log for details");
                }
            }
            catch (Throwable th)
            {
                String timeStamp = Logger.LOG_FILE_LOG_TIMESTAMP_FORMAT.format(new Date());
                Console.Error.WriteLine("[" + timeStamp + "] Error encountered while running ScriptRunner (see log for details):");
                Console.Error.WriteLine(th.Message);
                if (!commandLineOptions.hasOption(CommandLineOption.RUN))
                {
                    //Error will already have been logged by runner; for all others log it now
                    Logger.logError(th);
                }
                lError = true;
            }
            finally
            {
                Logger.finaliseLogs();
            }

            //Exit with the correct code
            Environment.Exit(lError ? 1 : 0);
        }
Esempio n. 2
0
        /**
         * Verifies the contents of this manifest file by resolving all its file references, checking that referenced loaders
         * exist, and optionally checking file hashes if required.
         * @param pScriptRunner ScriptRunner to use as the base directory for file checking.
         * @throws ExManifest If any manifest entry is invalid.
         */
        public void verifyManifest(ScriptRunner pScriptRunner)
        {
            //Verify the ScriptRunner versions are compatible (unless this behaviour is explicitly overriden)
            if (!pScriptRunner.hasCommandLineOption(CommandLineOption.SKIP_VERSION_CHECK))
            {
                String lCurrVersion     = ScriptRunnerVersion.getVersionNumber();
                String lManifestVersion = mPromotionPropertyMap.get(SCRIPTRUNNER_VERSION_PROPERTY);
                if (!lCurrVersion.equals(lManifestVersion))
                {
                    throw new ExManifest("Manifest version incompatible. Version " + lCurrVersion + " cannot safely execute a manifest built by version " + lManifestVersion);
                }
            }

            //Verify all loaders exist
            for (MetadataLoader lLoader : mLoaderMap.values())
            {
                try
                {
                    pScriptRunner.resolveFile(lLoader.getLoaderFilePath());
                }
                catch (FileNotFoundException e)
                {
                    throw new ExManifest("Loader file for loader " + lLoader.getName() + " cannot be located", e);
                }
            }

            Set <String> lAllFilePaths = pScriptRunner.allFilePathsInBaseDirectory();

            //Verify all files exist and they reference valid loaders
            for (PromotionFile lPromotionFile : getPromotionFileList())
            {
                //Check file existence
                File lFile;
                try
                {
                    lFile = pScriptRunner.resolveFile(lPromotionFile.getFilePath());
                }
                catch (FileNotFoundException e)
                {
                    throw new ExManifest("Promotion file " + lPromotionFile.getFilePath() + " cannot be located", e);
                }

                lAllFilePaths.remove(pScriptRunner.relativeFilePath(lFile));

                String lFileHash;
                try
                {
                    lFileHash = lPromotionFile.generateFileHash(pScriptRunner);
                }
                catch (IOException e)
                {
                    throw new ExManifest("Could not generate hash for file " + lPromotionFile.getFilePath(), e);
                }

                //Do a hash code check if required
                if (!pScriptRunner.hasCommandLineOption(CommandLineOption.SKIP_HASH_CHECK))
                {
                    String lPropertyHash = lPromotionFile.getPropertyMap().get(ManifestBuilder.PROPERTY_NAME_FILE_HASH);
                    if (XFUtil.isNull(lPropertyHash))
                    {
                        throw new ExManifest("Cannot perform hash check for " + lPromotionFile.getFilePath() + " as the " + ManifestBuilder.PROPERTY_NAME_FILE_HASH + " property is not specified");
                    }

                    if (!lPropertyHash.equals(lFileHash))
                    {
                        throw new ExManifest("Hash verification failed for file " + lPromotionFile.getFilePath() + " - expected " + lPropertyHash + " but got " + lFileHash);
                    }
                    Logger.logDebug("Hash check OK for file " + lPromotionFile.getFilePath());
                }
            }

            //If there are files in the archive that are not listed in the manifest we should issue a warning
            if (lAllFilePaths.size() > 0)
            {
                Logger.logWarning(lAllFilePaths.size() + " file(s) found in base directory but not listed in the manifest file:");
                for (String lPath : lAllFilePaths)
                {
                    Logger.logInfo(lPath);
                }
            }
        }