Skip to content

Commit 034040c

Browse files
author
Nirjan Chapagain
committed
docker support
1 parent bc7e0c5 commit 034040c

9 files changed

Lines changed: 784 additions & 4 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace VirtualClient.Contracts
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Runtime.InteropServices;
9+
10+
/// <summary>
11+
/// Tracks whether execution is happening inside a container context
12+
/// and provides the effective platform for component execution.
13+
/// </summary>
14+
public class ContainerExecutionContext
15+
{
16+
private static ContainerExecutionContext current;
17+
18+
/// <summary>
19+
/// Gets or sets the current container execution context.
20+
/// </summary>
21+
public static ContainerExecutionContext Current
22+
{
23+
get => current ??= new ContainerExecutionContext();
24+
set => current = value;
25+
}
26+
27+
/// <summary>
28+
/// True if running with container mode enabled (--image was passed).
29+
/// </summary>
30+
public bool IsContainerMode { get; set; }
31+
32+
/// <summary>
33+
/// The container image being used.
34+
/// </summary>
35+
public string Image { get; set; }
36+
37+
/// <summary>
38+
/// The platform inside the container (typically Linux).
39+
/// </summary>
40+
public PlatformID ContainerPlatform { get; set; } = PlatformID.Unix;
41+
42+
/// <summary>
43+
/// The CPU architecture inside the container.
44+
/// </summary>
45+
public Architecture ContainerArchitecture { get; set; } = Architecture.X64;
46+
47+
/// <summary>
48+
/// Gets the effective platform - container platform if in container mode,
49+
/// otherwise the host platform.
50+
/// </summary>
51+
public PlatformID EffectivePlatform => this.IsContainerMode
52+
? this.ContainerPlatform
53+
: Environment.OSVersion.Platform;
54+
55+
/// <summary>
56+
/// Gets the effective architecture.
57+
/// </summary>
58+
public Architecture EffectiveArchitecture => this.IsContainerMode
59+
? this.ContainerArchitecture
60+
: RuntimeInformation.ProcessArchitecture;
61+
62+
/// <summary>
63+
/// Container configuration from profile.
64+
/// </summary>
65+
public ContainerConfiguration Configuration { get; set; }
66+
}
67+
68+
/// <summary>
69+
/// Container configuration from profile's Container section.
70+
/// </summary>
71+
public class ContainerConfiguration
72+
{
73+
/// <summary>
74+
/// Default image (can be overridden by --image CLI).
75+
/// </summary>
76+
public string Image { get; set; }
77+
78+
/// <summary>
79+
/// Standard mount configuration.
80+
/// </summary>
81+
public ContainerMountConfig Mounts { get; set; } = new ContainerMountConfig();
82+
83+
/// <summary>
84+
/// Working directory inside container.
85+
/// </summary>
86+
public string WorkingDirectory { get; set; } = "/vc";
87+
88+
/// <summary>
89+
/// Environment variables to pass to container.
90+
/// </summary>
91+
public IDictionary<string, string> EnvironmentVariables { get; set; }
92+
93+
/// <summary>
94+
/// Additional mount paths beyond the defaults.
95+
/// </summary>
96+
public IList<string> AdditionalMounts { get; set; }
97+
98+
/// <summary>
99+
/// Pull policy: Always, IfNotPresent, Never.
100+
/// </summary>
101+
public string PullPolicy { get; set; } = "IfNotPresent";
102+
}
103+
104+
/// <summary>
105+
/// Standard VC directory mounts configuration.
106+
/// </summary>
107+
public class ContainerMountConfig
108+
{
109+
/// <summary>
110+
/// Mount the packages directory (/vc/packages).
111+
/// </summary>
112+
public bool Packages { get; set; } = true;
113+
114+
/// <summary>
115+
/// Mount the logs directory (/vc/logs).
116+
/// </summary>
117+
public bool Logs { get; set; } = true;
118+
119+
/// <summary>
120+
/// Mount the state directory (/vc/state).
121+
/// </summary>
122+
public bool State { get; set; } = true;
123+
124+
/// <summary>
125+
/// Mount the temp directory (/vc/temp).
126+
/// </summary>
127+
public bool Temp { get; set; } = true;
128+
}
129+
}

src/VirtualClient/VirtualClient.Contracts/VirtualClientComponent.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected VirtualClientComponent(IServiceCollection dependencies, IDictionary<st
109109
this.Metadata = new Dictionary<string, IConvertible>(StringComparer.OrdinalIgnoreCase);
110110
this.MetadataContract = new MetadataContract();
111111
this.PlatformSpecifics = this.systemInfo.PlatformSpecifics;
112-
this.Platform = this.systemInfo.Platform;
112+
// this.Platform = this.systemInfo.Platform;
113113
this.SupportedRoles = new List<string>();
114114
this.CleanupTasks = new List<Action>();
115115
this.Extensions = new Dictionary<string, JToken>();
@@ -400,7 +400,19 @@ protected set
400400
/// <summary>
401401
/// The OS/system platform (e.g. Windows, Unix).
402402
/// </summary>
403-
public PlatformID Platform { get; }
403+
public PlatformID Platform
404+
{
405+
get
406+
{
407+
// If container mode is active, report container platform
408+
if (ContainerExecutionContext.Current.IsContainerMode)
409+
{
410+
return ContainerExecutionContext.Current.ContainerPlatform;
411+
}
412+
413+
return this.systemInfo.Platform;
414+
}
415+
}
404416

405417
/// <summary>
406418
/// Provides OS/system platform specific information.
@@ -637,6 +649,19 @@ protected string PlatformArchitectureName
637649
{
638650
get
639651
{
652+
// If container mode is active, report container platform/architecture
653+
if (ContainerExecutionContext.Current.IsContainerMode)
654+
{
655+
string os = ContainerExecutionContext.Current.ContainerPlatform == PlatformID.Unix ? "linux" : "win";
656+
string arch = ContainerExecutionContext.Current.ContainerArchitecture switch
657+
{
658+
Architecture.X64 => "x64",
659+
Architecture.Arm64 => "arm64",
660+
_ => "x64"
661+
};
662+
return $"{os}-{arch}";
663+
}
664+
640665
return this.PlatformSpecifics.PlatformArchitectureName;
641666
}
642667
}

0 commit comments

Comments
 (0)