8
8
using System . Threading . Tasks ;
9
9
using System . Diagnostics ;
10
10
using System . Threading ;
11
+ using System . Text . RegularExpressions ;
11
12
12
13
namespace easyWSL
13
14
{
14
15
class DistroInstaller
15
16
{
16
- public class TokenFromResponse
17
+ public class autorizationResponse
17
18
{
18
19
public string token { get ; set ; }
19
20
public string access_token { get ; set ; }
@@ -25,7 +26,7 @@ public class TokenFromResponse
25
26
}
26
27
27
28
28
- public static void InstallDistro ( string distroID , string distroName , string distroPath , string easyWSLDataDirectory , string easyWSLDirectory )
29
+ public static void InstallDistro ( string distroImage , string distroName , string distroPath , string easyWSLDataDirectory , string easyWSLDirectory )
29
30
{
30
31
31
32
void StartProcessSilently ( string processName , string processArguments )
@@ -54,6 +55,20 @@ string GetRequest(string url)
54
55
return responseStream ;
55
56
}
56
57
58
+ string GetRequestWithHeader ( string url , string token , string type )
59
+ {
60
+ HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
61
+ request . Headers . Add ( "Authorization" , "Bearer " + token ) ;
62
+ request . Accept = type ;
63
+ HttpWebResponse response = ( HttpWebResponse ) request . GetResponse ( ) ;
64
+ Stream receiveStream = response . GetResponseStream ( ) ;
65
+ StreamReader readStream = new StreamReader ( receiveStream , Encoding . UTF8 ) ;
66
+ string responseStream = readStream . ReadToEnd ( ) ;
67
+ response . Close ( ) ;
68
+ readStream . Close ( ) ;
69
+ return responseStream ;
70
+ }
71
+
57
72
void GetRequestWithHeaderToFile ( string url , string token , string type , string fileName )
58
73
{
59
74
HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
@@ -65,74 +80,80 @@ void GetRequestWithHeaderToFile(string url, string token, string type, string fi
65
80
byte [ ] buffer = new byte [ bufferSize ] ;
66
81
67
82
FileStream fileStream = File . Create ( fileName ) ;
68
- while ( ( bytesRead = receiveStream . Read ( buffer , 0 , bufferSize ) ) != 0 )
83
+ while ( ( bytesRead = receiveStream . Read ( buffer , 0 , bufferSize ) ) != 0 )
69
84
{
70
85
fileStream . Write ( buffer , 0 , bytesRead ) ;
71
86
}
72
-
87
+
73
88
response . Close ( ) ;
74
89
fileStream . Close ( ) ;
75
90
}
76
91
77
92
78
- SortedDictionary < string , Sources > sources = JsonSerializer . Deserialize < SortedDictionary < string , Sources > > ( File . ReadAllText ( "sources.json" ) , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
93
+ dynamic sources = JsonSerializer . Deserialize < Sources > ( File . ReadAllText ( "sources.json" ) ) ;
79
94
string repository = "" , tag = "" , registry = "registry-1.docker.io" , authorizationUrl = "https://auth.docker.io/token" , registryUrl = "registry.docker.io" ;
80
95
81
- if ( sources [ distroID ] . Image . Contains ( '/' ) )
96
+
97
+ if ( distroImage . Contains ( '/' ) )
82
98
{
83
- string [ ] imageArray = sources [ distroID ] . Image . Split ( '/' ) ;
99
+ string [ ] imageArray = distroImage . Split ( '/' ) ;
84
100
tag = "latest" ;
85
- repository = sources [ distroID ] . Image ;
101
+ repository = distroImage ;
86
102
}
87
103
88
104
else
89
105
{
90
- string [ ] imageArray = sources [ distroID ] . Image . Split ( ':' ) ;
106
+ string [ ] imageArray = distroImage . Split ( ':' ) ;
91
107
string imgage = imageArray [ 0 ] ;
92
108
tag = imageArray [ 1 ] ;
93
109
repository = $ "library/{ imgage } ";
94
110
}
95
111
112
+ dynamic autorizationResponse = JsonSerializer . Deserialize < autorizationResponse > ( GetRequest ( $ "{ authorizationUrl } ?service={ registryUrl } &scope=repository:{ repository } :pull") ) ;
96
113
97
- dynamic tokenFromResponse = JsonSerializer . Deserialize < TokenFromResponse > ( GetRequest ( $ "{ authorizationUrl } ?service={ registryUrl } &scope=repository:{ repository } :pull") ) ;
114
+ string layersResponse = GetRequestWithHeader ( $ "https://{ registry } /v2/{ repository } /manifests/{ tag } ", autorizationResponse . token , "application/vnd.docker.distribution.manifest.v2+json" ) ;
115
+
116
+ MatchCollection layersRegex = Regex . Matches ( layersResponse , @"sha256:\w{64}" ) ;
117
+ var layersList = layersRegex . Cast < Match > ( ) . Select ( match => match . Value ) . ToList ( ) ;
118
+ layersList . RemoveAt ( 0 ) ;
98
119
99
120
string layersDirectory = $ "{ easyWSLDataDirectory } \\ layers";
100
121
Directory . CreateDirectory ( layersDirectory ) ;
101
122
102
- string concatTarCommand = $ " cf { layersDirectory } \\ install.tar";
123
+ string concatTarCommand = $ " cf { layersDirectory } \\ { distroName } - install.tar";
103
124
104
125
int count = 0 ;
105
- foreach ( string layer in sources [ distroID ] . Layers )
126
+ foreach ( string layer in layersList )
106
127
{
107
128
count ++ ;
108
129
Console . WriteLine ( $ "Downloading { count } . layer ...") ;
109
130
110
- tokenFromResponse = JsonSerializer . Deserialize < TokenFromResponse > ( GetRequest ( $ "{ authorizationUrl } ?service={ registryUrl } &scope=repository:{ repository } :pull") ) ;
131
+ autorizationResponse = JsonSerializer . Deserialize < autorizationResponse > ( GetRequest ( $ "{ authorizationUrl } ?service={ registryUrl } &scope=repository:{ repository } :pull") ) ;
111
132
112
- string layerName = $ "layer{ count } .tar.bz";
133
+ string layerName = $ "{ distroName } - layer{ count } .tar.bz";
113
134
string layerPath = $ "{ layersDirectory } \\ { layerName } ";
114
135
115
- GetRequestWithHeaderToFile ( $ "https://{ registry } /v2/{ repository } /blobs/{ layer } ", tokenFromResponse . token , "application/vnd.docker.distribution.manifest.v2+json" , layerPath ) ;
136
+ GetRequestWithHeaderToFile ( $ "https://{ registry } /v2/{ repository } /blobs/{ layer } ", autorizationResponse . token , "application/vnd.docker.distribution.manifest.v2+json" , layerPath ) ;
116
137
concatTarCommand += $ " @{ layerPath } ";
117
138
}
118
-
139
+
119
140
120
141
Console . WriteLine ( "Creating install.tar file ..." ) ;
121
- if ( sources [ distroID ] . Layers . Count == 1 )
142
+ if ( layersList . Count == 1 )
122
143
{
123
- File . Move ( $ "{ layersDirectory } \\ layer1.tar.bz", $ "{ layersDirectory } \\ install.tar.bz") ;
144
+ File . Move ( $ "{ layersDirectory } \\ { distroName } - layer1.tar.bz", $ "{ layersDirectory } \\ { distroName } - install.tar.bz") ;
124
145
125
146
Console . WriteLine ( "Registering the distro ..." ) ;
126
- StartProcessSilently ( "wsl.exe" , $ "--import { distroName } { distroPath } { easyWSLDataDirectory } \\ layers\\ install.tar.bz") ;
147
+ StartProcessSilently ( "wsl.exe" , $ "--import { distroName } { distroPath } { easyWSLDataDirectory } \\ layers\\ { distroName } - install.tar.bz") ;
127
148
}
128
149
else
129
150
{
130
151
StartProcessSilently ( $ "{ easyWSLDirectory } \\ dep\\ bsdtar.exe", concatTarCommand ) ;
131
152
132
153
Console . WriteLine ( "Registering the distro ..." ) ;
133
- StartProcessSilently ( "wsl.exe" , $ "--import { distroName } { distroPath } { easyWSLDataDirectory } \\ layers\\ install.tar") ;
154
+ StartProcessSilently ( "wsl.exe" , $ "--import { distroName } { distroPath } { easyWSLDataDirectory } \\ layers\\ { distroName } - install.tar") ;
134
155
}
135
-
156
+
136
157
137
158
Console . WriteLine ( "Cleaning up ..." ) ;
138
159
Directory . Delete ( layersDirectory , true ) ;
0 commit comments