public ContainerInfo(Pipelines.ContainerResource container, Boolean isJobContainer = true)
        {
            this.ContainerName = container.Alias;

            string containerImage = container.Properties.Get <string>("image");

            ArgUtil.NotNullOrEmpty(containerImage, nameof(containerImage));

            this.ContainerImage            = containerImage;
            this.ContainerDisplayName      = $"{container.Alias}_{Pipelines.Validation.NameValidation.Sanitize(containerImage)}_{Guid.NewGuid().ToString("N").Substring(0, 6)}";
            this.ContainerRegistryEndpoint = container.Endpoint?.Id ?? Guid.Empty;
            this.ContainerCreateOptions    = container.Properties.Get <string>("options");
            this.SkipContainerImagePull    = container.Properties.Get <bool>("localimage");
            _environmentVariables          = container.Environment != null ? new Dictionary <string, string>(container.Environment) : new Dictionary <string, string>();
            this.ContainerCommand          = container.Properties.Get <string>("command", defaultValue: "");
            this.IsJobContainer            = isJobContainer;
            this._imageOS = PlatformUtil.HostOS;
            _pathMappings = new Dictionary <string, string>(PlatformUtil.RunningOnWindows ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);

            if (container.Ports?.Count > 0)
            {
                foreach (var port in container.Ports)
                {
                    UserPortMappings[port] = port;
                }
            }
            if (container.Volumes?.Count > 0)
            {
                foreach (var volume in container.Volumes)
                {
                    UserMountVolumes[volume] = volume;
                }
            }
        }
Example #2
0
 public bool PreferredOnPlatform(PlatformUtil.OS os)
 {
     if (os == PlatformUtil.OS.Windows)
     {
         return(Platforms?.Any(x => string.Equals(x, os.ToString(), StringComparison.OrdinalIgnoreCase)) ?? false);
     }
     return(false);
 }
 public string TranslateContainerPathForImageOS(PlatformUtil.OS runningOs, string path)
 {
     if (!string.IsNullOrEmpty(path))
     {
         if (runningOs == PlatformUtil.OS.Windows && ImageOS == PlatformUtil.OS.Linux)
         {
             return(translateWindowsDriveRegex.Replace(path, "/").Replace("\\", "/"));
         }
     }
     return(path);
 }
Example #4
0
        public HandlerData GetHandlerData(IExecutionContext ExecutionContext, ExecutionData currentExecution, PlatformUtil.OS hostOS)
        {
            ArgUtil.NotNull(ExecutionContext, nameof(ExecutionContext));
            if (currentExecution == null)
            {
                return(null);
            }

            if ((currentExecution.All.Any(x => x is PowerShell3HandlerData)) &&
                (currentExecution.All.Any(x => x is PowerShellHandlerData && x.Platforms != null && x.Platforms.Contains("windows", StringComparer.OrdinalIgnoreCase))))
            {
                // When task contains both PS and PS3 implementations, we will always prefer PS3 over PS regardless of the platform pinning.
                Trace.Info("Ignore platform pinning for legacy PowerShell execution handler.");
                var legacyPShandler = currentExecution.All.Where(x => x is PowerShellHandlerData).FirstOrDefault();
                legacyPShandler.Platforms = null;
            }

            var targetOS = hostOS;

            var stepTarget = ExecutionContext.StepTarget();
            var preferPowershellHandler = true;

            if (!AgentKnobs.PreferPowershellHandlerOnContainers.GetValue(ExecutionContext).AsBoolean() && stepTarget != null)
            {
                targetOS = stepTarget.ExecutionOS;
                if (stepTarget is ContainerInfo)
                {
                    if ((currentExecution.All.Any(x => x is PowerShell3HandlerData)) &&
                        (currentExecution.All.Any(x => x is NodeHandlerData || x is Node10HandlerData)))
                    {
                        Trace.Info($"Since we are targeting a container, we will prefer a node handler if one is available");
                        preferPowershellHandler = false;
                    }
                }
            }
            Trace.Info($"Get handler data for target platform {targetOS.ToString()}");
            return(currentExecution.All
                   .OrderBy(x => !(x.PreferredOnPlatform(targetOS) && (preferPowershellHandler || !(x is PowerShell3HandlerData))))  // Sort true to false.
                   .ThenBy(x => x.Priority)
                   .FirstOrDefault());
        }
 private void HandleContainerImageOSChange(ContainerInfo container, PlatformUtil.OS oldOs)
 {
     // if the Image OS Changed, we need to retranslate all the variables we have that may contain paths
     Variables.Transform((x) => container.TranslateContainerPathForImageOS(oldOs, x));
 }
        public HandlerData GetHandlerData(IExecutionContext ExecutionContext, ExecutionData currentExecution, PlatformUtil.OS hostOS)
        {
            if (currentExecution == null)
            {
                return(null);
            }

            if ((currentExecution.All.Any(x => x is PowerShell3HandlerData)) &&
                (currentExecution.All.Any(x => x is PowerShellHandlerData && x.Platforms != null && x.Platforms.Contains("windows", StringComparer.OrdinalIgnoreCase))))
            {
                // When task contains both PS and PS3 implementations, we will always prefer PS3 over PS regardless of the platform pinning.
                Trace.Info("Ignore platform pinning for legacy PowerShell execution handler.");
                var legacyPShandler = currentExecution.All.Where(x => x is PowerShellHandlerData).FirstOrDefault();
                legacyPShandler.Platforms = null;
            }

            var targetOS = hostOS;

            var stepTarget = ExecutionContext.StepTarget();
            var preferPowershellHandler             = true;
            var preferPowershellHandlerOnContainers = ExecutionContext.Variables.GetBoolean("agent.preferPowerShellOnContainers")
                                                      ?? StringUtil.ConvertToBoolean(System.Environment.GetEnvironmentVariable("AGENT_PREFER_POWERSHELL_ON_CONTAINERS"), false);

            if (!preferPowershellHandlerOnContainers && stepTarget != null)
            {
                targetOS = stepTarget.ExecutionOS;
                if (stepTarget is ContainerInfo)
                {
                    if ((currentExecution.All.Any(x => x is PowerShell3HandlerData)) &&
                        (currentExecution.All.Any(x => x is NodeHandlerData || x is Node10HandlerData)))
                    {
                        Trace.Info($"Since we are targeting a container, we will prefer a node handler if one is available");
                        preferPowershellHandler = false;
                    }
                }
            }
            Trace.Info($"Get handler data for target platform {targetOS.ToString()}");
            return(currentExecution.All
                   .OrderBy(x => !(x.PreferredOnPlatform(targetOS) && (preferPowershellHandler || !(x is PowerShell3HandlerData))))  // Sort true to false.
                   .ThenBy(x => x.Priority)
                   .FirstOrDefault());
        }
 public string TranslateContainerPathForImageOS(PlatformUtil.OS runningOs, string path)
 {
     return(path);
 }