Beispiel #1
0
        static int Main(string[] args)
        {
            Utility.SetSmvVar("workingDir", Directory.GetCurrentDirectory());
            Utility.SetSmvVar("logFilePath", null);
            Utility.SetSmvVar("assemblyDir", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
            Utility.SetSmvVar("configFilePath", Path.Combine(Utility.GetSmvVar("workingDir"), configXmlFileName));
            Utility.SetSmvVar("smvLogFileNamePrefix", buildLogFileNamePrefix);
            try
            {
                Console.BufferHeight = Int16.MaxValue - 1;
            }
            catch (Exception)
            {
            }
            // Process commandline arguments.
            // Note that ProcessArgs will return false if execution should not continue.
            // This happens in cases such as /help, /getAvailableModules, /searchmodules
            if (!ProcessArgs(args))
            {
                return(-1);
            }
            if (useDb)
            {
                try
                {
                    using (var database = new SmvDbEntities())
                    {
                        SmvDb.Task task = database.Tasks.Where((x) => x.TaskID == Utility.taskId).FirstOrDefault();
                        if (task != null)
                        {
                            string argsString = string.Join(" ", args);
                            task.Arguments = argsString;

                            database.SaveChanges();
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.LogFatalError("Exception while updating database " + e);
                }
            }
            // Get the SMV version name.
            string smvVersionTxtPath = Path.Combine(Utility.GetSmvVar("assemblyDir"), "SmvVersionName.txt");

            if (!File.Exists(smvVersionTxtPath))
            {
                Log.LogFatalError("SmvVersionName.txt must exist in the SMV bin directory.");
            }
            string[] lines = File.ReadAllLines(smvVersionTxtPath);
            if (lines.Length < 1)
            {
                Log.LogFatalError("SmvVersionName.txt is empty.");
            }
            Utility.version = lines[0];

            // Consume specified configuration file
            smvConfig = Utility.GetSMVConfig();
            if (smvConfig == null)
            {
                Log.LogFatalError("Could not load Config file");
            }

            // Set the variables defined in the Variables node in the config file
            LoadGlobalVariables(smvConfig.Variables);

            // Project file value from command line overrides the Config value
            if (!String.IsNullOrEmpty(Utility.GetSmvVar("projectFileArg")))
            {
                Utility.SetSmvVar("projectFile", Utility.GetSmvVar("projectFileArg"));
            }

            bool   buildResult = false;
            bool   analysisResult = false;
            double buildTime = 0, analysisTime = 0;
            int    localThreadCount = Environment.ProcessorCount;

            if (Utility.GetSmvVar("localThreads") != null)
            {
                localThreadCount = int.Parse(Utility.GetSmvVar("localThreads"));
            }
            Log.LogInfo(String.Format("Running local scheduler with {0} threads", localThreadCount));

            // Load the cloud config from an XML file.

            SMVCloudConfig cloudConfig = null;

            // Set up the schedulers.
            Utility.scheduler = new MasterSMVActionScheduler();
            LocalSMVActionScheduler localScheduler = new LocalSMVActionScheduler(localThreadCount);
            CloudSMVActionScheduler cloudScheduler = null;

            if (cloud)
            {
                cloudConfig    = Utility.GetSMVCloudConfig();
                cloudScheduler = new CloudSMVActionScheduler(cloudConfig);
            }
            Utility.scheduler.AddScheduler("local", localScheduler);
            Utility.scheduler.AddScheduler("cloud", cloudScheduler);
            // Do build if specified in the configuration file
            if (smvConfig.Build != null)
            {
                Stopwatch sw = Stopwatch.StartNew();

                // Populate the actions dictionary that will be used by the schedulers.
                Utility.PopulateActionsDictionary(smvConfig.Build);

                if (string.IsNullOrEmpty(Utility.GetSmvVar("projectFile")))
                {
                    Utility.scheduler.Dispose();
                    Log.LogFatalError("Project file not set");
                }

                List <SMVActionResult> buildActionsResult = Utility.ExecuteActions(Utility.GetRootActions(smvConfig.Build));
                buildResult = Utility.IsExecuteActionsSuccessful(buildActionsResult);
                if (Utility.plugin != null && buildResult == false)
                {
                    Utility.plugin.Finally(true);
                }

                if (Utility.plugin != null)
                {
                    Utility.plugin.PostBuild(smvConfig.Build);
                }
                sw.Stop();
                buildTime = sw.Elapsed.TotalSeconds;
            }

            // If build succeeded or it was not specified, do analysis (if specified and called)
            if (smvConfig.Build == null || buildResult)
            {
                if (smvConfig.Analysis != null)
                {
                    if (doAnalysis)
                    {
                        Stopwatch sw = Stopwatch.StartNew();
                        Utility.PopulateActionsDictionary(smvConfig.Analysis);

                        if (Utility.plugin != null)
                        {
                            Log.LogInfo("Using plugin " + Utility.plugin + " for analysis.");
                            analysisResult = Utility.plugin.DoPluginAnalysis(smvConfig.Analysis);

                            Utility.plugin.PostAnalysis(smvConfig.Analysis);
                        }
                        else
                        {
                            List <SMVActionResult> analysisActionsResult = Utility.ExecuteActions(Utility.GetRootActions(smvConfig.Analysis));
                            analysisResult = Utility.IsExecuteActionsSuccessful(analysisActionsResult);
                        }

                        if (!analysisResult)
                        {
                            Utility.scheduler.Dispose();
                            Utility.plugin.Finally(true);
                            Log.LogFatalError("Analysis failed.");
                        }

                        sw.Stop();
                        analysisTime = sw.Elapsed.TotalSeconds;
                    }
                }
                Utility.plugin.Finally(false);
            }
            else
            {
                Utility.plugin.Finally(true);
                Utility.scheduler.Dispose();
                Log.LogFatalError("Build failed, skipping Analysis.");
            }

            Utility.PrintResult(Utility.result, (int)buildTime, (int)analysisTime, true);

            Log.LogInfo(String.Format("Total time taken {0} seconds", (int)(buildTime + analysisTime)));

            if (Utility.plugin != null)
            {
                int bugCount = Utility.plugin.GenerateBugsCount();
                Log.LogInfo("Found " + bugCount + " bugs!");
                if (useDb)
                {
                    try
                    {
                        using (var database = new SmvDbEntities())
                        {
                            SmvDb.Task task = database.Tasks.Where((x) => x.TaskID == Utility.taskId).FirstOrDefault();
                            if (task != null)
                            {
                                string bugCountString = bugCount.ToString();
                                task.Bugs = bugCountString;
                                database.SaveChanges();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Utility.scheduler.Dispose();
                        Log.LogFatalError("Exception while updating database " + e);
                        return(-1);
                    }
                }
            }
            localScheduler.Dispose();
            if (cloud)
            {
                cloudScheduler.Dispose();
            }

            if (makeDefectsPortable)
            {
                foreach (string bugDirectory in Directory.EnumerateDirectories(Path.Combine(Utility.smvVars["smvOutputDir"], "Bugs")))
                {
                    try
                    {
                        Utility.makeDefectPortable(bugDirectory);
                    }
                    catch (Exception e)
                    {
                        Log.LogFatalError("Exception occurred when making defect portable." + e.ToString());
                    }
                }
                Log.LogInfo("Defects, if any, made portable successfully");
            }
            return(Convert.ToInt32(Utility.scheduler.errorsEncountered));
        }
Beispiel #2
0
        public override bool OnStart()
        {
            try
            {
                // Set the maximum number of concurrent connections .
                System.Net.ServicePointManager.DefaultConnectionLimit = 12;

                // Set options for blob storage.
                options = new BlobRequestOptions();
                options.ServerTimeout = TimeSpan.FromMinutes(10);
                options.RetryPolicy   = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);

                // Get the connection strings.
                string assemblyDir         = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                string cloudConfigPath     = Path.Combine(assemblyDir, CloudConstants.CloudConfigXmlFileName);
                string cloudConfigContents = Utility.ReadFile(cloudConfigPath);
                string schemaPath          = Path.Combine(assemblyDir, CloudConstants.CloudConfigXsdFileName);
                using (var c = new StringReader(cloudConfigContents))
                {
                    if (!Utility.ValidateXmlFile(schemaPath, c))
                    {
                        Trace.TraceError("Could not load cloud config from file: " + cloudConfigPath);
                        return(false);
                    }
                }
                var serializer = new XmlSerializer(typeof(SMVCloudConfig));
                using (var reader = new StringReader(cloudConfigContents))
                {
                    cloudConfig = (SMVCloudConfig)serializer.Deserialize(reader);
                }

                bool done = false;
                while (!done)
                {
                    try
                    {
                        var storageAccount = CloudStorageAccount.Parse(cloudConfig.StorageConnectionString.value);

                        // Setup queue storage.
                        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                        queueClient.DefaultRequestOptions             = new QueueRequestOptions();
                        queueClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval),
                                                                                             CloudConstants.MaxRetries);
                        inputQueue = queueClient.GetQueueReference(CloudConstants.InputQueueName);

                        // Setup blob storage.
                        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                        blobClient.DefaultRequestOptions             = new BlobRequestOptions();
                        blobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval),
                                                                                            CloudConstants.MaxRetries);
                        jobsContainer     = blobClient.GetContainerReference(CloudConstants.InputBlobContainerName);
                        resultsContainer  = blobClient.GetContainerReference(CloudConstants.OutputBlobContainerName);
                        versionsContainer = blobClient.GetContainerReference(CloudConstants.VersionsContainerName);

                        // Setup table storage.
                        var tableStorage = storageAccount.CreateCloudTableClient();
                        tableStorage.DefaultRequestOptions             = new TableRequestOptions();
                        tableStorage.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(CloudConstants.RetryBackoffInterval), CloudConstants.MaxRetries);
                        CloudTable table = tableStorage.GetTableReference(CloudConstants.TableName);
                        tableDataSource = new ActionsTableDataSource(table);

                        // Setup the service bus topic.
                        var namespaceManager = NamespaceManager.CreateFromConnectionString(cloudConfig.ServiceBusConnectionString.value);
                        if (!namespaceManager.TopicExists(CloudConstants.ResultsTopicName))
                        {
                            namespaceManager.CreateTopic(CloudConstants.ResultsTopicName);
                        }

                        done = true;
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError("Failure trying to connect to Azure storage: " + e.ToString());
                    }
                }

                // Get paths to important directories in the file system. Remove the trailing slash from the paths.
                LocalResource localResource = RoleEnvironment.GetLocalResource(CloudConstants.SmvWorkingDirectoryResourceName);
                workingDirectory = localResource.RootPath.Remove(localResource.RootPath.Length - 1);

                localResource    = RoleEnvironment.GetLocalResource(CloudConstants.SmvResultsDirectoryResourceName);
                resultsDirectory = localResource.RootPath.Remove(localResource.RootPath.Length - 1);

                localResource        = RoleEnvironment.GetLocalResource(CloudConstants.SmvDirectoryResourceName);
                smvVersionsDirectory = localResource.RootPath.Remove(localResource.RootPath.Length - 1);
            }
            catch (Exception e)
            {
                Trace.TraceError("Exception while running OnStart(): " + e.ToString());
                return(false);
            }

            Utility.result = null;

            return(base.OnStart());
        }