Skip to content

Commit 3978fa0

Browse files
committed
there is now no need to specify the layers in sources.json
1 parent d27ea04 commit 3978fa0

35 files changed

+292
-96
lines changed

.vs/ProjectSettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"CurrentProjectSetting": null
3+
}

.vs/easyWSL/v16/.suo

3.5 KB
Binary file not shown.

.vs/slnx.sqlite

228 KB
Binary file not shown.

easyWSL/DistroInstaller.cs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
using System.Threading.Tasks;
99
using System.Diagnostics;
1010
using System.Threading;
11+
using System.Text.RegularExpressions;
1112

1213
namespace easyWSL
1314
{
1415
class DistroInstaller
1516
{
16-
public class TokenFromResponse
17+
public class autorizationResponse
1718
{
1819
public string token { get; set; }
1920
public string access_token { get; set; }
@@ -54,6 +55,20 @@ string GetRequest(string url)
5455
return responseStream;
5556
}
5657

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+
5772
void GetRequestWithHeaderToFile(string url, string token, string type, string fileName)
5873
{
5974
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
@@ -65,11 +80,11 @@ void GetRequestWithHeaderToFile(string url, string token, string type, string fi
6580
byte[] buffer = new byte[bufferSize];
6681

6782
FileStream fileStream = File.Create(fileName);
68-
while((bytesRead = receiveStream.Read(buffer, 0, bufferSize)) != 0)
83+
while ((bytesRead = receiveStream.Read(buffer, 0, bufferSize)) != 0)
6984
{
7085
fileStream.Write(buffer, 0, bytesRead);
7186
}
72-
87+
7388
response.Close();
7489
fileStream.Close();
7590
}
@@ -78,6 +93,7 @@ void GetRequestWithHeaderToFile(string url, string token, string type, string fi
7893
SortedDictionary<string, Sources> sources = JsonSerializer.Deserialize<SortedDictionary<string, Sources>>(File.ReadAllText("sources.json"), new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
7994
string repository = "", tag = "", registry = "registry-1.docker.io", authorizationUrl = "https://auth.docker.io/token", registryUrl = "registry.docker.io";
8095

96+
8197
if (sources[distroID].Image.Contains('/'))
8298
{
8399
string[] imageArray = sources[distroID].Image.Split('/');
@@ -93,32 +109,47 @@ void GetRequestWithHeaderToFile(string url, string token, string type, string fi
93109
repository = $"library/{imgage}";
94110
}
95111

112+
dynamic autorizationResponse = JsonSerializer.Deserialize<autorizationResponse>(GetRequest($"{authorizationUrl}?service={registryUrl}&scope=repository:{repository}:pull"));
113+
114+
string layersResponse = GetRequestWithHeader($"https://{registry}/v2/{repository}/manifests/{tag}", autorizationResponse.token, "application/vnd.docker.distribution.manifest.v2+json");
115+
116+
117+
Console.WriteLine(layersResponse);
118+
119+
MatchCollection layersRegex = Regex.Matches(layersResponse, @"sha256:\w{64}");
120+
var layersList = layersRegex.Cast<Match>().Select(match => match.Value).ToList();
121+
layersList.RemoveAt(0);
122+
123+
foreach (string layer in layersList)
124+
{
125+
Console.WriteLine(layer);
126+
}
127+
96128

97-
dynamic tokenFromResponse = JsonSerializer.Deserialize<TokenFromResponse>(GetRequest($"{authorizationUrl}?service={registryUrl}&scope=repository:{repository}:pull"));
98129

99130
string layersDirectory = $"{easyWSLDataDirectory}\\layers";
100131
Directory.CreateDirectory(layersDirectory);
101132

102133
string concatTarCommand = $" cf {layersDirectory}\\install.tar";
103134

104135
int count = 0;
105-
foreach (string layer in sources[distroID].Layers)
136+
foreach (string layer in layersList)
106137
{
107138
count++;
108139
Console.WriteLine($"Downloading {count}. layer ...");
109140

110-
tokenFromResponse = JsonSerializer.Deserialize<TokenFromResponse>(GetRequest($"{authorizationUrl}?service={registryUrl}&scope=repository:{repository}:pull"));
141+
autorizationResponse = JsonSerializer.Deserialize<autorizationResponse>(GetRequest($"{authorizationUrl}?service={registryUrl}&scope=repository:{repository}:pull"));
111142

112143
string layerName = $"layer{count}.tar.bz";
113144
string layerPath = $"{layersDirectory}\\{layerName}";
114145

115-
GetRequestWithHeaderToFile($"https://{registry}/v2/{repository}/blobs/{layer}", tokenFromResponse.token, "application/vnd.docker.distribution.manifest.v2+json", layerPath);
146+
GetRequestWithHeaderToFile($"https://{registry}/v2/{repository}/blobs/{layer}", autorizationResponse.token, "application/vnd.docker.distribution.manifest.v2+json", layerPath);
116147
concatTarCommand += $" @{layerPath} ";
117148
}
118149

119150

120151
Console.WriteLine("Creating install.tar file ...");
121-
if(sources[distroID].Layers.Count == 1)
152+
if (sources[distroID].Layers.Count == 1)
122153
{
123154
File.Move($"{layersDirectory}\\layer1.tar.bz", $"{layersDirectory}\\install.tar.bz");
124155

@@ -132,7 +163,7 @@ void GetRequestWithHeaderToFile(string url, string token, string type, string fi
132163
Console.WriteLine("Registering the distro ...");
133164
StartProcessSilently("wsl.exe", $"--import {distroName} {distroPath} {easyWSLDataDirectory}\\layers\\install.tar");
134165
}
135-
166+
136167

137168
Console.WriteLine("Cleaning up ...");
138169
Directory.Delete(layersDirectory, true);
20.6 KB
Binary file not shown.
20.4 KB
Binary file not shown.
138 KB
Binary file not shown.
113 KB
Binary file not shown.
16.6 KB
Binary file not shown.
56.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)