@@ -29,45 +29,35 @@ public void Report(JSONMessage value)
29
29
/// <summary>
30
30
///
31
31
/// </summary>
32
- public string Filename { get ; }
32
+ public string Arguments { get ; }
33
33
34
34
/// <summary>
35
35
///
36
36
/// </summary>
37
37
public string Command { get ; }
38
38
39
+ public string ? AttachVolume { get ; }
40
+
39
41
/// <summary>
40
42
///
41
43
/// </summary>
42
44
/// <param name="image"></param>
43
- /// <param name="filename "></param>
45
+ /// <param name="arguments "></param>
44
46
/// <param name="command"></param>
45
- /// <param name="inputKey"></param>
46
47
/// <param name="outputKey"></param>
47
- public DockerChain ( string image = "python:3" , string filename = "main.py" , string command = "python" , string inputKey = "code" , string outputKey = "result" )
48
+ public DockerChain ( string image = "python:3" , string arguments = "main.py" , string command = "python" , string ? attachVolume = null , string outputKey = "result" )
48
49
{
49
50
Image = image ;
50
- Filename = filename ;
51
+ Arguments = arguments ;
51
52
Command = command ;
52
- InputKeys = new [ ] { inputKey } ;
53
+ AttachVolume = attachVolume ;
53
54
OutputKeys = new [ ] { outputKey } ;
54
55
55
56
using var configuration = new DockerClientConfiguration ( ) ;
56
57
_client = configuration . CreateClient ( ) ;
57
58
}
58
59
59
- private static string SanitizeCode ( string code )
60
- {
61
- if ( code . StartsWith ( "```" , StringComparison . Ordinal ) )
62
- {
63
- // remove first and last lines
64
- var lines = code . Split ( "\n " ) ;
65
- var res = string . Join ( "\n " , lines [ 1 ..^ 1 ] ) ;
66
- return res ;
67
- }
68
- return code ;
69
- }
70
-
60
+
71
61
/// <summary>
72
62
///
73
63
/// </summary>
@@ -82,37 +72,29 @@ await _client.Images.CreateImageAsync(new ImagesCreateParameters()
82
72
FromImage = Image
83
73
} , null , new SuppressProgress ( ) , CancellationToken . None ) . ConfigureAwait ( false ) ;
84
74
75
+ var binds = new List < string > ( ) ;
85
76
86
- var code = SanitizeCode ( values . Value [ InputKeys [ 0 ] ] . ToString ( ) ?? string . Empty ) ;
87
-
88
-
89
- var tempDir = Path . GetTempPath ( ) ;
90
- tempDir = Path . Combine ( tempDir , Guid . NewGuid ( ) . ToString ( ) ) ;
91
- var appDir = Path . Combine ( tempDir , "app" ) ;
92
- Directory . CreateDirectory ( appDir ) ;
93
- var tempFile = Path . Combine ( appDir , Filename ) ;
94
- await File . WriteAllTextAsync ( tempFile , code ) . ConfigureAwait ( false ) ;
95
-
96
- MemoryStream archiveStream = new MemoryStream ( ) ;
97
- await TarFile . CreateFromDirectoryAsync ( tempDir , archiveStream , false ) . ConfigureAwait ( false ) ;
98
- archiveStream . Seek ( 0 , SeekOrigin . Begin ) ;
99
77
100
- Directory . Delete ( tempDir , true ) ;
78
+ if ( AttachVolume != null )
79
+ {
80
+ var absolutePath = Path . GetFullPath ( AttachVolume ) . Replace ( "\\ " , "/" ) . Replace ( ":" , "" ) ;
81
+ binds . Add ( $ "/{ absolutePath } :/app") ;
82
+ }
101
83
102
84
var container = await _client . Containers . CreateContainerAsync ( new CreateContainerParameters ( )
103
85
{
104
86
105
87
Image = Image ,
106
- Cmd = new [ ] { Command , Filename } ,
88
+ Cmd = new [ ] { Command , Arguments } ,
107
89
WorkingDir = "/app" ,
90
+ HostConfig = new HostConfig ( )
91
+ {
92
+ Binds = binds
93
+ }
108
94
109
95
} ) . ConfigureAwait ( false ) ;
110
96
111
- await _client . Containers . ExtractArchiveToContainerAsync ( container . ID , new ContainerPathStatParameters ( )
112
- {
113
- AllowOverwriteDirWithFile = true ,
114
- Path = "/" ,
115
- } , archiveStream , CancellationToken . None ) . ConfigureAwait ( false ) ;
97
+
116
98
117
99
await _client . Containers . StartContainerAsync ( container . ID , null ) . ConfigureAwait ( false ) ;
118
100
0 commit comments