-
Notifications
You must be signed in to change notification settings - Fork 225
Refactor Docker transport into shared base classes and strategy pattern #1579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WardenGnaw
wants to merge
4
commits into
feature/podman
Choose a base branch
from
dev/waan/podman
base: feature/podman
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
91462a9
Refactor Docker transport into shared base classes and strategy pattern
WardenGnaw dfc3bc9
Address review feedback: remove unused field, add type check
WardenGnaw c99be5b
Address review: seal classes, use OrdinalIgnoreCase, remove defaults
WardenGnaw b4281e6
Hoist CultureInfo allocation outside loop
WardenGnaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.SSHDebugPS | ||
| { | ||
| /// <summary> | ||
| /// Identifies the container runtime to query when discovering containers. | ||
| /// </summary> | ||
| public enum ContainerRuntimeType | ||
| { | ||
| Unknown, | ||
| Docker | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.SSHDebugPS.Utilities; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.SSHDebugPS | ||
| { | ||
| internal abstract class ContainerTransportSettingsBase : IPipeTransportSettings | ||
| { | ||
| protected abstract string SubCommand { get; } | ||
| protected abstract string SubCommandArgs { get; } | ||
|
|
||
| private readonly string _windowsExe; | ||
| private readonly string _unixExe; | ||
| private readonly string _hostnameFormat; | ||
|
|
||
| internal string HostName { get; private set; } | ||
| internal bool HostIsUnix { get; private set; } | ||
|
|
||
| public ContainerTransportSettingsBase(string hostname, bool hostIsUnix, string windowsExe, string unixExe, string hostnameFormat) | ||
| { | ||
| HostIsUnix = hostIsUnix; | ||
| _windowsExe = windowsExe; | ||
| _unixExe = unixExe; | ||
| _hostnameFormat = hostnameFormat; | ||
| if (!string.IsNullOrWhiteSpace(hostname)) | ||
| { | ||
| HostName = hostname; | ||
| } | ||
| else | ||
| { | ||
| HostName = string.Empty; | ||
| } | ||
| } | ||
|
|
||
| public ContainerTransportSettingsBase(ContainerTransportSettingsBase settings) | ||
| : this(settings.HostName, settings.HostIsUnix, settings._windowsExe, settings._unixExe, settings._hostnameFormat) | ||
| { } | ||
|
|
||
| // 0 = command parameters (e.g. --host/--url) | ||
| // 1 = subcommand (e.g. exec, cp, ps) | ||
| // 2 = subcommand parameters | ||
| private const string _baseCommandFormat = "{0} {1} {2}"; | ||
|
|
||
| private string GenerateExeCommandArgs() | ||
| { | ||
| var hostnameArg = string.Empty; | ||
| if (!string.IsNullOrWhiteSpace(this.HostName)) | ||
| hostnameArg = _hostnameFormat.FormatInvariantWithArgs(this.HostName); | ||
|
|
||
| return _baseCommandFormat.FormatInvariantWithArgs(hostnameArg, SubCommand, SubCommandArgs); | ||
| } | ||
|
|
||
| #region IPipeTransportSettings | ||
|
|
||
| public string CommandArgs => GenerateExeCommandArgs(); | ||
|
|
||
| public string Command => HostIsUnix ? _unixExe : _windowsExe; | ||
|
|
||
| #endregion | ||
| } | ||
|
|
||
| internal abstract class ContainerTargetTransportSettings : ContainerTransportSettingsBase | ||
| { | ||
| internal string ContainerName { get; private set; } | ||
|
|
||
| public ContainerTargetTransportSettings(string hostname, string containerName, bool hostIsUnix, string windowsExe, string unixExe, string hostnameFormat) | ||
| : base(hostname, hostIsUnix, windowsExe, unixExe, hostnameFormat) | ||
| { | ||
| ContainerName = containerName; | ||
| } | ||
|
|
||
| public ContainerTargetTransportSettings(ContainerTargetTransportSettings settings) | ||
| : base(settings) | ||
| { | ||
| ContainerName = settings.ContainerName; | ||
| } | ||
|
|
||
| protected override string SubCommand => throw new System.NotImplementedException(); | ||
| protected override string SubCommandArgs => throw new System.NotImplementedException(); | ||
| } | ||
|
|
||
| internal abstract class ContainerExecSettings : ContainerTargetTransportSettings | ||
| { | ||
| private bool _runInShell; | ||
| private string _commandToExecute; | ||
| // 0 = container, 1 = command to execute | ||
| private const string _subCommandArgsFormat = "{0} {1}"; | ||
| private const string _subCommandArgsFormatWithShell = "{0} /bin/sh -c \"{1}\""; | ||
| private const string _subCommandArgsFormatWithShellLinuxHost = "{0} /bin/sh -c '{1}'"; | ||
| private const string _interactiveFlag = "-i "; | ||
|
|
||
| private bool _makeInteractive; | ||
|
|
||
| public ContainerExecSettings(ContainerTargetTransportSettings settings, string command, bool runInShell, bool makeInteractive = true) | ||
| : base(settings) | ||
| { | ||
| Debug.Assert(!string.IsNullOrWhiteSpace(command), "Exec command cannot be null"); | ||
| _runInShell = runInShell; | ||
| _commandToExecute = command; | ||
| _makeInteractive = makeInteractive; | ||
| } | ||
|
|
||
| protected override string SubCommand => "exec"; | ||
| protected override string SubCommandArgs | ||
| { | ||
| get | ||
| { | ||
| string subCommandFormat = this.HostIsUnix ? _subCommandArgsFormatWithShellLinuxHost : _subCommandArgsFormatWithShell; | ||
| // Escape single quotes on Linux so variable resolution does not happen until it is in the container. | ||
| string command = this.HostIsUnix ? _commandToExecute.Replace("'", "'\\''") : _commandToExecute; | ||
| return (_makeInteractive ? _interactiveFlag : string.Empty) + | ||
| (_runInShell ? subCommandFormat : _subCommandArgsFormat).FormatInvariantWithArgs(ContainerName, command); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal abstract class ContainerCopySettings : ContainerTargetTransportSettings | ||
| { | ||
| // {0} = container, {1} = source, {2} = destination | ||
| private const string _copyFormatToContainer = "{1} {0}:{2}"; | ||
|
|
||
| private string _sourcePath; | ||
| private string _destinationPath; | ||
|
|
||
| public ContainerCopySettings(string hostname, string sourcePath, string destinationPath, string containerName, bool hostIsUnix, string windowsExe, string unixExe, string hostnameFormat) | ||
| : base(hostname, containerName, hostIsUnix, windowsExe, unixExe, hostnameFormat) | ||
| { | ||
| _sourcePath = sourcePath; | ||
| _destinationPath = destinationPath; | ||
| } | ||
|
|
||
| public ContainerCopySettings(ContainerTargetTransportSettings settings, string sourcePath, string destinationPath) | ||
| : base(settings) | ||
| { | ||
| _sourcePath = sourcePath; | ||
| _destinationPath = destinationPath; | ||
| } | ||
|
|
||
| protected override string SubCommand => "cp"; | ||
| protected override string SubCommandArgs => _copyFormatToContainer.FormatInvariantWithArgs(ContainerName, _sourcePath, _destinationPath); | ||
| } | ||
|
|
||
| internal abstract class ContainerCommandSettings : ContainerTransportSettingsBase | ||
| { | ||
| private string _cmd; | ||
| private string _args; | ||
|
|
||
| public ContainerCommandSettings(string hostname, bool hostIsUnix, string windowsExe, string unixExe, string hostnameFormat) | ||
| : base(hostname, hostIsUnix, windowsExe, unixExe, hostnameFormat) | ||
| { } | ||
|
|
||
| public void SetCommand(string cmd, string args) | ||
| { | ||
| _cmd = cmd; | ||
| _args = args; | ||
| } | ||
|
|
||
| protected override string SubCommand => _cmd; | ||
| protected override string SubCommandArgs => _args; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using Microsoft.SSHDebugPS.Docker; | ||
| using Microsoft.SSHDebugPS.UI; | ||
|
|
||
| namespace Microsoft.SSHDebugPS | ||
| { | ||
| internal sealed class DockerDiscoveryStrategy : IContainerDiscoveryStrategy | ||
| { | ||
| private const string unknownOS = "Unknown"; | ||
|
|
||
| public string ConnectionLabel => UIResources.ConnectionLabel; | ||
| public string HostnameLabel => UIResources.HostnameLabel; | ||
| public string HostnameTip => UIResources.HostnameTip; | ||
| public string ConnectionToolTip => UIResources.ConnectionToolTip; | ||
| public string HostnameAutomationName => UIResources.HostnameAutomationName; | ||
|
|
||
| public IEnumerable<DockerContainerInstance> GetLocalContainers(string hostname, out int totalContainers) | ||
| { | ||
| return DockerHelper.GetLocalDockerContainers(hostname, out totalContainers); | ||
| } | ||
|
|
||
| public IEnumerable<DockerContainerInstance> GetRemoteContainers(IConnection connection, string hostname, out int totalContainers) | ||
| { | ||
| return DockerHelper.GetRemoteDockerContainers(connection, hostname, out totalContainers); | ||
| } | ||
|
|
||
| public void AssignPlatforms(IEnumerable<DockerContainerInstance> containers, string hostname) | ||
| { | ||
| if (!containers.Any()) | ||
| return; | ||
|
|
||
| string serverOS; | ||
| if (DockerHelper.TryGetServerOS(hostname, out serverOS)) | ||
| { | ||
| bool lcow; | ||
| DockerHelper.TryGetLCOW(hostname, out lcow); | ||
| TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; | ||
|
|
||
| if (lcow && serverOS.IndexOf("windows", StringComparison.OrdinalIgnoreCase) >= 0) | ||
| { | ||
| foreach (DockerContainerInstance container in containers) | ||
| { | ||
| string containerPlatform = string.Empty; | ||
| if (DockerHelper.TryGetContainerPlatform(hostname, container.Name, out containerPlatform)) | ||
| { | ||
| container.Platform = textInfo.ToTitleCase(containerPlatform); | ||
| } | ||
| else | ||
| { | ||
| container.Platform = unknownOS; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| string platform = textInfo.ToTitleCase(serverOS); | ||
| foreach (DockerContainerInstance container in containers) | ||
| { | ||
| container.Platform = platform; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| foreach (DockerContainerInstance container in containers) | ||
| { | ||
| container.Platform = unknownOS; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.