forked from dotnet/source-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (101 loc) · 4.2 KB
/
Program.cs
File metadata and controls
119 lines (101 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using Mono.Options;
namespace UploadIndexStage1
{
class Program
{
static async Task Main(string[] args)
{
string sourceFolder = null;
string repoName = null;
string blobContainerSasUrl = null;
var options = new OptionSet
{
{"i=", "The source folder", i => sourceFolder = i},
{"n=", "The repo name", n => repoName = n},
{"o=", "The destination blob container url, can also be in the BLOB_CONTAINER_URL environment variable", o => blobContainerSasUrl = o},
};
List<string> extra = options.Parse(args);
if (extra.Any())
{
Fatal($"Unexpected argument {extra.First()}");
}
if (string.IsNullOrEmpty(sourceFolder))
{
Fatal("Missing argument -i");
}
if (string.IsNullOrEmpty(repoName))
{
Fatal("Missing argument -n");
}
if (string.IsNullOrEmpty(blobContainerSasUrl))
{
blobContainerSasUrl = Environment.GetEnvironmentVariable("BLOB_CONTAINER_URL");
}
if (string.IsNullOrEmpty(blobContainerSasUrl))
{
Fatal("Missing argument -o");
}
var containerClient = new BlobContainerClient(new Uri(blobContainerSasUrl));
string newBlobName = $"{repoName}/{DateTime.UtcNow:O}.tar.gz";
BlobClient newBlobClient = containerClient.GetBlobClient(newBlobName);
Console.WriteLine($"Uploading folder {sourceFolder} to blob {new UriBuilder(newBlobClient.Uri) {Fragment = "", Query = ""}.Uri.AbsoluteUri}");
await using (var outputFileStream = new MemoryStream())
{
await using (var gzoStream = new GZipOutputStream(outputFileStream){ IsStreamOwner = false })
using (var tarArchive = TarArchive.CreateOutputTarArchive(gzoStream, Encoding.UTF8))
{
string sourceRoot = Path.GetFullPath(sourceFolder).Replace('\\', '/').TrimEnd('/');
void AddEntry(string path)
{
string normalizedPath = Path.GetFullPath(path).Replace('\\', '/');
var e = TarEntry.CreateEntryFromFile(path);
e.Name = normalizedPath.Substring(sourceRoot.Length).TrimStart('/');
Console.WriteLine($"Adding {path} as {e.Name}");
tarArchive.WriteEntry(e, false);
}
void AddFolder(string path)
{
AddEntry(path);
foreach (string file in Directory.GetFiles(path))
{
AddEntry(file);
}
foreach (string dir in Directory.GetDirectories(path))
{
AddFolder(dir);
}
}
AddFolder(sourceRoot);
}
outputFileStream.Position = 0;
await newBlobClient.UploadAsync(outputFileStream);
}
Console.WriteLine("Cleaning up old blobs");
List<BlobItem> blobs = containerClient.GetBlobs(prefix: repoName + "/").ToList();
List<BlobItem> toDelete = blobs.OrderByDescending(b => b.Name).Skip(10).ToList();
foreach (BlobItem d in toDelete)
{
Console.WriteLine($"Deleting blob {d.Name}");
await containerClient.DeleteBlobAsync(d.Name);
}
Console.WriteLine("Finished.");
}
[DoesNotReturn]
private static void Fatal(string msg)
{
Console.Error.WriteLine($"fatal: {msg}");
Environment.Exit(-1);
}
}
}