Example #1
0
 /// <summary>
 /// Opens the key vault.
 /// </summary>
 /// <param name="m">The monitor to use.</param>
 /// <param name="passPhrase">The key vault pass phrase.</param>
 /// <returns>True on success.</returns>
 public bool OpenKeyVault(IActivityMonitor m, string passPhrase)
 {
     if (!CheckPassPhraseConstraints(m, passPhrase))
     {
         return(false);
     }
     if (_passPhrase != null)
     {
         m.Info($"Key Vault is already opened.");
         return(true);
     }
     if (KeyVaultFileExists)
     {
         try
         {
             var keys = KeyVault.DecryptValues(File.ReadAllText(KeyVaultPath), passPhrase);
             m.OpenInfo($"Opening existing Key Vault with keys: {keys.Keys.Concatenate()}.");
             _store.ImportSecretKeys(m, keys);
             _passPhrase = passPhrase;
             _vaultContent.Clear();
             _vaultContent.AddRange(keys);
         }
         catch (Exception ex)
         {
             m.Error("Unable to open the key vault.", ex);
             return(false);
         }
     }
     else
     {
         _passPhrase = passPhrase;
         m.OpenInfo($"New Key Vault opened.");
     }
     if (_store.Infos.Any(s => !s.IsSecretAvailable))
     {
         using (m.OpenWarn($"Missing secrets:"))
         {
             foreach (var s in _store.Infos.Where(s => !s.IsSecretAvailable))
             {
                 m.Warn(s.ToString());
             }
         }
     }
     m.CloseGroup();
     return(true);
 }
Example #2
0
        bool DumpGitFolders(IActivityMonitor m, IEnumerable <GitFolder> gitFolders)
        {
            bool      isLogFilterDefault = false;
            LogFilter final = m.ActualFilter;

            if (final == LogFilter.Undefined)
            {
                final = ActivityMonitor.DefaultFilter;
                isLogFilterDefault = true;
            }
            var msg = $"Monitor filters: User:'******' => Final:'{final}'{(isLogFilterDefault ? "(AppDomain's default)" : "")}.";

            m.UnfilteredLog(ActivityMonitor.Tags.Empty, LogLevel.Info, msg, m.NextLogTime(), null);

            int  gitFoldersCount    = 0;
            bool hasPluginInitError = false;
            var  dirty = new List <string>();

            foreach (var git in gitFolders)
            {
                ++gitFoldersCount;
                string commitAhead = git.Head.AheadOriginCommitCount != null ? $"{git.Head.AheadOriginCommitCount} commits ahead origin" : "Untracked";
                using (m.OpenInfo($"{git.SubPath} - branch: {git.CurrentBranchName} ({commitAhead})."))
                {
                    string pluginInfo;
                    if (!git.EnsureCurrentBranchPlugins(m))
                    {
                        hasPluginInitError = true;
                        pluginInfo         = "Plugin initialization error.";
                    }
                    else
                    {
                        pluginInfo = $"({git.PluginManager.BranchPlugins[git.CurrentBranchName].Count} plugins)";
                    }

                    if (git.CheckCleanCommit(m))
                    {
                        m.CloseGroup("Up-to-date. " + pluginInfo);
                    }
                    else
                    {
                        dirty.Add(git.SubPath);
                        m.CloseGroup("Dirty. " + pluginInfo);
                    }
                }
            }
            if (gitFoldersCount == 0)
            {
                m.Error("No git folder found.");
            }
            else
            {
                m.CloseGroup($"{dirty.Count} dirty (out of {gitFoldersCount}).");
                if (dirty.Count > 0)
                {
                    m.Info($"Dirty: {dirty.Concatenate()}");
                }
                var byActiveBranch = gitFolders.GroupBy(g => g.CurrentBranchName);
                if (byActiveBranch.Count() > 1)
                {
                    using (m.OpenWarn($"{byActiveBranch.Count()} different branches:"))
                    {
                        foreach (var b in byActiveBranch)
                        {
                            using (m.OpenWarn($"Branch '{b.Key}':"))
                            {
                                m.Warn(b.Select(g => g.SubPath.Path).Concatenate());
                            }
                        }
                    }
                }
                else
                {
                    m.Info($"All {gitFoldersCount} git folders are on '{byActiveBranch.First().Key}' branch.");
                }

                if (hasPluginInitError)
                {
                    m.Error("At least one git folder is unable to initialize its plugins.");
                }
            }
            return(!hasPluginInitError);
        }
Example #3
0
 /// <summary>
 /// Dumps the result of the compilation into a monitor.
 /// </summary>
 /// <param name="monitor">The monitor to use.</param>
 /// <param name="dumpSources">Optionnaly dumps the source as another <see cref="CK.Core.LogLevel"/>.</param>
 public void LogResult(IActivityMonitor monitor, LogLevel?dumpSources = null)
 {
     if (monitor == null)
     {
         throw new ArgumentNullException(nameof(monitor));
     }
     using (monitor.OpenInfo("Code Generation information."))
     {
         if (LoadConflicts != null && LoadConflicts.Count > 0)
         {
             using (monitor.OpenWarn($"{LoadConflicts.Count} assembly load conflict(s)."))
             {
                 foreach (var e in LoadConflicts)
                 {
                     if (e.Resolved != null)
                     {
                         monitor.Warn(e.ToString());
                     }
                     else
                     {
                         monitor.Error(e.ToString());
                     }
                 }
             }
         }
         if (Success)
         {
             monitor.Info(CompilationSkipped ? "Source code parsing succeeded." : "Source code compilation succeeded.");
             if (dumpSources.HasValue)
             {
                 DumpSources(monitor, dumpSources.Value);
             }
         }
         else
         {
             using (monitor.OpenError(CompilationSkipped ? "Parsing failed." : "Compilation failed."))
             {
                 if (EmitError != null)
                 {
                     monitor.Error(EmitError);
                 }
                 if (EmitResult != null)
                 {
                     if (!EmitResult.Success)
                     {
                         using (monitor.OpenInfo($"{EmitResult.Diagnostics.Count()} Compilation diagnostics."))
                         {
                             foreach (var diag in EmitResult.Diagnostics)
                             {
                                 monitor.Trace(diag.ToString());
                             }
                         }
                     }
                 }
                 else
                 {
                     Debug.Assert(CompilationSkipped);
                     using (monitor.OpenInfo($"{ParseDiagnostics.Count()} Parsing diagnostics."))
                     {
                         foreach (var diag in ParseDiagnostics)
                         {
                             monitor.Trace(diag.ToString());
                         }
                     }
                 }
                 if (dumpSources.HasValue)
                 {
                     DumpSources(monitor, dumpSources.Value);
                 }
             }
         }
         if (AssemblyLoadError != null)
         {
             monitor.Error("Generated assembly load failed.", AssemblyLoadError);
         }
         monitor.CloseGroup(Assembly != null
                                     ? "Generated assembly successfuly loaded."
                                     : (Success ? "Succeeded." : "Failed."));
     }
 }
Example #4
0
        static void RunCommand(IActivityMonitor m, UserHost host, string rep)
        {
            var handlers      = host.CommandRegister.GetCommands(rep).ToList();
            var handlersBySig = handlers
                                .GroupBy(c => c.PayloadSignature)
                                .ToList();

            if (handlersBySig.Count == 0)
            {
                m.Warn($"Pattern '{rep}' has no match.");
                return;
            }
            if (handlersBySig.Count != 1)
            {
                using (m.OpenWarn($"Pattern '{rep}' matches require {handlersBySig.Count} different payloads."))
                {
                    foreach (var c in handlersBySig)
                    {
                        m.Warn($"{c.Key ?? "<No payload>"}: {c.Select( n => n.UniqueName.ToString() ).Concatenate() }");
                    }
                }
                return;
            }
            var  firstHandler  = handlers[0];
            bool?parallelRun   = firstHandler.ParallelRun;
            bool?globalConfirm = firstHandler.ConfirmationRequired;
            bool?backgroundRun = firstHandler.BackgroundRun;

            if (handlers.Any(h => h.ConfirmationRequired != globalConfirm.Value))
            {
                m.Error("Different ConfirmationRequired found among commands. All commands must have the same ConfirmationRequired ");
                return;
            }
            if (handlers.Any(h => h.BackgroundRun != backgroundRun))
            {
                m.Error("Different BackgroundCommandAttribute found among commands. All commands must have the same attributes and identically configured attribute.");
                return;
            }
            if (handlers.Any(h => h.ParallelRun != parallelRun))
            {
                m.Error("Different ParallelCommandAttribute found among commands. All commands must have the same attributes and identically configured attribute.");
                return;
            }
            var payload = firstHandler.CreatePayload();

            if (!ReadPayload(m, payload))
            {
                return;
            }

            if (parallelRun == null)
            {
                Console.WriteLine("The selected command(s) can run in parallel. Do you want to run in parallel ?");
                parallelRun = YesOrNo();
            }

            if (backgroundRun == null)
            {
                Console.WriteLine("The selected command(s) can run in background. Do you want to run in background ?");
                backgroundRun = YesOrNo();
            }

            if (globalConfirm == true)
            {
                Console.WriteLine("Confirm execution of:");
                foreach (var h in handlers)
                {
                    Console.WriteLine(h.UniqueName);
                }
                DumpPayLoad(payload);
                if (!YesOrNo())
                {
                    return;
                }
            }
            if (backgroundRun.Value)
            {
                throw new NotImplementedException();
            }

            if (!parallelRun.Value)
            {
                foreach (var h in handlers)
                {
                    if (host.ApplicationLifetime.StopRequested(m))
                    {
                        break;
                    }
                    h.Execute(m, payload);
                }
            }
            else
            {
                var tasks = handlers.Select(h => Task.Run(() =>
                {
                    ActivityMonitor monitor = new ActivityMonitor();
                    h.Execute(monitor, payload);
                })).ToArray();
                Task.WaitAll(tasks);
            }
        }