Exemple #1
0
        internal static IEnumerable <DockerContainerInstance> GetLocalDockerContainers(string hostname, out int totalContainers)
        {
            totalContainers = 0;
            int containerCount = 0;
            List <DockerContainerInstance> containers = new List <DockerContainerInstance>();

            DockerCommandSettings settings = new DockerCommandSettings(hostname, false);

            settings.SetCommand(dockerPSCommand, dockerPSArgs);

            RunDockerCommand(settings, delegate(string args)
            {
                if (args.Trim()[0] == '{')
                {
                    if (DockerContainerInstance.TryCreate(args, out DockerContainerInstance containerInstance))
                    {
                        containers.Add(containerInstance);
                    }
                    containerCount++;
                }
            });

            totalContainers = containerCount;
            return(containers);
        }
Exemple #2
0
        public static IEnumerable <IContainerInstance> GetLocalDockerContainers()
        {
            List <DockerContainerInstance> containers    = new List <DockerContainerInstance>();
            LocalSingleCommandRunner       commandRunner = new LocalSingleCommandRunner(dockerCommand, dockerPSArgs);
            StringBuilder errorSB  = new StringBuilder();
            int?          exitCode = null;

            try
            {
                ManualResetEvent resetEvent = new ManualResetEvent(false);
                commandRunner.ErrorOccured += ((sender, args) =>
                {
                    resetEvent.Set();
                });

                commandRunner.Closed += ((sender, args) =>
                {
                    exitCode = args;
                    resetEvent.Set();
                });

                commandRunner.OutputReceived += ((sender, args) =>
                {
                    if (!string.IsNullOrWhiteSpace(args))
                    {
                        if (args.Trim()[0] != '{')
                        {
                            // output isn't json, command Error
                            string errorMessage = string.Format(CultureInfo.CurrentCulture, UIResources.CommandExecutionErrorFormat, dockerCommand, args);
                            throw new CommandFailedException(errorMessage);
                        }

                        var containerInstance = DockerContainerInstance.Create(args);
                        if (containerInstance != null)
                        {
                            containers.Add(containerInstance);
                        }
                    }
                });

                commandRunner.Run();
                // TODO: Switch to cancellable wait
                resetEvent.WaitOne();

                // might need to throw an exception here too??
                if (exitCode != 0)
                {
                    Debug.Fail(FormattableString.Invariant($"Exit Code: {exitCode}"));
                    return(null);
                }

                return(containers);
            }
            catch (Win32Exception ex)
            {
                // docker doesn't exist
                string errorMessage = string.Format(CultureInfo.CurrentCulture, UIResources.CommandExecutionErrorFormat, dockerCommand, ex.Message);
                throw new CommandFailedException(errorMessage, ex);
            }
        }
        /// <summary>
        /// Create a DockerContainerInstance from the results of docker ps in JSON format
        /// </summary>
        public static bool TryCreate(string json, out DockerContainerInstance instance)
        {
            instance = null;
            try
            {
                JObject obj = JObject.Parse(json);
                instance = obj.ToObject <DockerContainerInstance>();
            }
            catch (Exception e)
            {
                HostTelemetry.SendEvent(TelemetryHelper.Event_DockerPSParseFailure, new KeyValuePair <string, object>[] {
                    new KeyValuePair <string, object>(TelemetryHelper.Property_ExceptionName, e.GetType().Name)
                });

                string error = e.ToString();
                VsOutputWindowWrapper.WriteLine(StringResources.Error_DockerPSParseFailed.FormatCurrentCultureWithArgs(json, error), StringResources.Docker_PSName);
                Debug.Fail(error);
            }
            return(instance != null);
        }
Exemple #4
0
        internal static IEnumerable <DockerContainerInstance> GetRemoteDockerContainers(IConnection connection, string hostname, out int totalContainers)
        {
            totalContainers = 0;
            SSHConnection sshConnection = connection as SSHConnection;
            List <string> outputLines   = new List <string>();
            StringBuilder errorSB       = new StringBuilder();

            if (sshConnection == null)
            {
                return(null);
            }

            List <DockerContainerInstance> containers = new List <DockerContainerInstance>();

            DockerCommandSettings settings = new DockerCommandSettings(hostname, true);

            settings.SetCommand(dockerPSCommand, dockerPSArgs);

            RemoteCommandRunner commandRunner = new RemoteCommandRunner(settings, sshConnection, handleRawOutput: false);

            ManualResetEvent resetEvent = new ManualResetEvent(false);
            int exitCode = 0;

            commandRunner.ErrorOccured += ((sender, args) =>
            {
                errorSB.Append(args);
            });

            commandRunner.Closed += ((sender, args) =>
            {
                exitCode = args;
                resetEvent.Set();
            });

            commandRunner.OutputReceived += ((sender, line) =>
            {
                if (!string.IsNullOrWhiteSpace(line))
                {
                    Debug.Assert(line.IndexOf('\n') < 0, "Why does `line` have embedded newline characters?");

                    // If it isn't json, assume its an error message
                    if (line.Trim()[0] != '{')
                    {
                        errorSB.Append(line);
                    }

                    outputLines.Add(line);
                }
            });

            commandRunner.Start();

            bool cancellationRequested = false;

            VS.VSOperationWaiter.Wait(UIResources.QueryingForContainersMessage, false, (cancellationToken) =>
            {
                while (!resetEvent.WaitOne(2000) && !cancellationToken.IsCancellationRequested)
                {
                }
                cancellationRequested = cancellationToken.IsCancellationRequested;
            });

            if (!cancellationRequested)
            {
                if (exitCode != 0)
                {
                    // if the exit code is not zero, then the output we received possibly is the error message
                    string exceptionMessage = UIResources.CommandExecutionErrorWithExitCodeFormat.FormatCurrentCultureWithArgs(
                        "{0} {1}".FormatInvariantWithArgs(settings.Command, settings.CommandArgs),
                        exitCode,
                        errorSB.ToString());

                    throw new CommandFailedException(exceptionMessage);
                }

                foreach (var item in outputLines)
                {
                    if (DockerContainerInstance.TryCreate(item, out DockerContainerInstance containerInstance))
                    {
                        containers.Add(containerInstance);
                    }
                    totalContainers++;
                }
            }

            return(containers);
        }
Exemple #5
0
        public static IEnumerable <DockerContainerInstance> GetLocalDockerContainers(string hostname)
        {
            List <DockerContainerInstance> containers = new List <DockerContainerInstance>();

            DockerCommandSettings settings = new DockerCommandSettings(hostname, false);

            settings.SetCommand(dockerPSCommand, dockerPSArgs);

            LocalCommandRunner commandRunner = new LocalCommandRunner(settings);

            StringBuilder errorSB  = new StringBuilder();
            int?          exitCode = null;

            try
            {
                ManualResetEvent resetEvent = new ManualResetEvent(false);
                commandRunner.ErrorOccured += ((sender, args) =>
                {
                    if (!string.IsNullOrWhiteSpace(args.ErrorMessage))
                    {
                        errorSB.AppendLine(args.ErrorMessage);
                    }
                    resetEvent.Set();
                });

                commandRunner.Closed += ((sender, args) =>
                {
                    exitCode = args;
                    resetEvent.Set();
                });

                commandRunner.OutputReceived += ((sender, args) =>
                {
                    if (!string.IsNullOrWhiteSpace(args))
                    {
                        if (args.Trim()[0] != '{')
                        {
                            // output isn't json, command Error
                            errorSB.Append(args);
                        }
                        else
                        {
                            var containerInstance = DockerContainerInstance.Create(args);
                            if (containerInstance != null)
                            {
                                containers.Add(containerInstance);
                            }
                        }
                    }
                });

                commandRunner.Start();

                bool cancellationRequested = false;
                VS.VSOperationWaiter.Wait(UIResources.QueryingForContainersMessage, false, (cancellationToken) =>
                {
                    while (!resetEvent.WaitOne(2000) && !cancellationToken.IsCancellationRequested)
                    {
                    }
                    cancellationRequested = cancellationToken.IsCancellationRequested;
                });

                if (!cancellationRequested)
                {
                    // might need to throw an exception here too??
                    if (exitCode.GetValueOrDefault(-1) != 0)
                    {
                        // if the exit code is not zero, then the output we received possibly is the error message
                        string exceptionMessage = UIResources.CommandExecutionErrorWithExitCodeFormat.FormatCurrentCultureWithArgs(
                            "{0} {1}".FormatInvariantWithArgs(settings.Command, settings.CommandArgs),
                            exitCode,
                            errorSB.ToString());

                        throw new CommandFailedException(exceptionMessage);
                    }

                    if (errorSB.Length > 0)
                    {
                        throw new CommandFailedException(errorSB.ToString());
                    }

                    return(containers);
                }

                return(new List <DockerContainerInstance>());
            }
            catch (Win32Exception ex)
            {
                // docker doesn't exist
                string errorMessage = UIResources.CommandExecutionErrorFormat.FormatCurrentCultureWithArgs(settings.CommandArgs, ex.Message);
                throw new CommandFailedException(errorMessage, ex);
            }
        }
Exemple #6
0
        internal static IEnumerable <IContainerInstance> GetRemoteDockerContainers(IConnection connection, string hostname)
        {
            SSHConnection sshConnection = connection as SSHConnection;
            List <string> outputLines   = new List <string>();
            StringBuilder errorSB       = new StringBuilder();

            if (sshConnection == null)
            {
                return(null);
            }

            List <DockerContainerInstance> containers = new List <DockerContainerInstance>();

            DockerCommandSettings settings = new DockerCommandSettings(hostname, true);

            settings.SetCommand(dockerPSCommand, dockerPSArgs);

            RemoteCommandRunner commandRunner = new RemoteCommandRunner(settings, sshConnection);

            ManualResetEvent resetEvent = new ManualResetEvent(false);
            int exitCode = 0;

            commandRunner.ErrorOccured += ((sender, args) =>
            {
                errorSB.Append(args);
            });

            commandRunner.Closed += ((sender, args) =>
            {
                exitCode = args;
                resetEvent.Set();
            });

            commandRunner.OutputReceived += ((sender, args) =>
            {
                if (!string.IsNullOrWhiteSpace(args))
                {
                    // If it isn't json, assume its an error message
                    if (args.Trim()[0] != '{')
                    {
                        errorSB.Append(args);
                    }

                    // Unix line endings are '\n' so split on that for json items.
                    foreach (var item in args.Split('\n').ToList())
                    {
                        if (!string.IsNullOrWhiteSpace(item))
                        {
                            outputLines.Add(item);
                        }
                    }
                }
            });

            commandRunner.Start();

            bool cancellationRequested = false;

            VS.VSOperationWaiter.Wait(UIResources.QueryingForContainersMessage, false, (cancellationToken) =>
            {
                while (!resetEvent.WaitOne(2000) && !cancellationToken.IsCancellationRequested)
                {
                }
                cancellationRequested = cancellationToken.IsCancellationRequested;
            });

            if (!cancellationRequested)
            {
                if (exitCode != 0)
                {
                    // if the exit code is not zero, then the output we received possibly is the error message
                    string exceptionMessage = UIResources.CommandExecutionErrorWithExitCodeFormat.FormatCurrentCultureWithArgs(
                        "{0} {1}".FormatInvariantWithArgs(settings.Command, settings.CommandArgs),
                        exitCode,
                        errorSB.ToString());

                    throw new CommandFailedException(exceptionMessage);
                }

                foreach (var item in outputLines)
                {
                    containers.Add(DockerContainerInstance.Create(item));
                }
            }

            return(containers);
        }
Exemple #7
0
        internal static IEnumerable <IContainerInstance> GetRemoteDockerContainers(IConnection connection)
        {
            SSHConnection sshConnection = connection as SSHConnection;
            List <string> outputLines   = new List <string>();
            StringBuilder errorSB       = new StringBuilder();

            if (sshConnection == null)
            {
                return(null);
            }

            List <DockerContainerInstance> containers    = new List <DockerContainerInstance>();
            RemoteCommandRunner            commandRunner = new RemoteCommandRunner(dockerCommand, dockerPSArgs, sshConnection);

            ManualResetEvent resetEvent = new ManualResetEvent(false);
            int exitCode = 0;

            commandRunner.ErrorOccured += ((sender, args) =>
            {
                errorSB.Append(args);
            });

            commandRunner.Closed += ((sender, args) =>
            {
                exitCode = args;
                resetEvent.Set();
            });

            commandRunner.OutputReceived += ((sender, args) =>
            {
                if (!string.IsNullOrWhiteSpace(args))
                {
                    // If it isn't json, assume its an error message
                    if (args.Trim()[0] != '{')
                    {
                        errorSB.Append(args);
                    }

                    // Unix line endings are '\n' so split on that for json items.
                    foreach (var item in args.Split('\n').ToList())
                    {
                        if (!string.IsNullOrWhiteSpace(item))
                        {
                            outputLines.Add(item);
                        }
                    }
                }
            });

            resetEvent.WaitOne();

            if (exitCode != 0)
            {
                // if the exit code is not zero, then the output we received possibly is the error message
                string exceptionMessage = string.Format(CultureInfo.CurrentCulture,
                                                        UIResources.CommandExecutionErrorWithExitCodeFormat,
                                                        dockerCommand,
                                                        exitCode,
                                                        errorSB.ToString());

                throw new CommandFailedException(exceptionMessage);
            }

            foreach (var item in outputLines)
            {
                containers.Add(DockerContainerInstance.Create(item));
            }

            return(containers);
        }