/
Program.fs
370 lines (327 loc) · 15 KB
/
Program.fs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
open System
open System.IO
open System.Linq
open System.Threading.Tasks
open System.Xml
open Fake.IO
open Fake.Core
open CliWrap
open CliWrap.Buffered
open System.Text
open Fastenshtein
open Octokit
open ICSharpCode.SharpZipLib.Tar
open ICSharpCode.SharpZipLib.GZip
open PulumiSchema.Types
/// Recursively tries to find the parent of a file starting from a directory
let rec findParent (directory: string) (fileToFind: string) =
let path = if Directory.Exists(directory) then directory else Directory.GetParent(directory).FullName
let files = Directory.GetFiles(path)
if files.Any(fun file -> Path.GetFileName(file).ToLower() = fileToFind.ToLower())
then path
else findParent (DirectoryInfo(path).Parent.FullName) fileToFind
let repositoryRoot = findParent __SOURCE_DIRECTORY__ "README.md";
let pulumiCliBinary() : Task<string> = task {
try
// try to get the version of pulumi installed on the system
let! version =
Cli.Wrap("pulumi")
.WithArguments("version")
.WithValidation(CommandResultValidation.ZeroExitCode)
.ExecuteAsync()
return "pulumi"
with
| _ ->
// when pulumi is not installed, try to get the version of of the dev build
// installed on the system using `make install` in the pulumi repo
let homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
let pulumiPath = System.IO.Path.Combine(homeDir, ".pulumi-dev", "bin", "pulumi")
if System.IO.File.Exists pulumiPath then
return pulumiPath
elif System.IO.File.Exists $"{pulumiPath}.exe" then
return $"{pulumiPath}.exe"
else
return "pulumi"
}
let schemaFromPulumi(pluginName: string) = task {
let packageName = $"{pluginName}@2.0.0"
let! binary = pulumiCliBinary()
let! output =
Cli.Wrap(binary)
.WithArguments($"package get-schema {packageName}")
.WithValidation(CommandResultValidation.None)
.ExecuteBufferedAsync()
if output.ExitCode <> 0 then
return Error output.StandardError
else
return Ok output.StandardOutput
}
let parseSchemaFromPulumi(pluginName: string) =
task {
let! schema = schemaFromPulumi(pluginName)
return
match schema with
| Ok schema -> Ok (PulumiSchema.Parser.parseSchema schema)
| Error error -> Error error
}
|> Async.AwaitTask
|> Async.RunSynchronously
let runTests() =
if Shell.Exec("dotnet", "run", Path.Combine(repositoryRoot, "src", "Tests")) <> 0
then failwithf "Unit tests failed"
let integrationTests() =
if Shell.Exec("dotnet", "run", Path.Combine(repositoryRoot, "src", "LocalTester")) <> 0
then failwithf "Integration tests failed"
let buildSolution() =
if Shell.Exec("dotnet", "build -c Release", Path.Combine(repositoryRoot, "src")) <> 0
then failwithf "Build failed"
let azureNativeModuleVersions() =
let npmPackagePathSegments = [|
repositoryRoot
"integration_tests"
"simpleStorage"
"typescript"
"node_modules"
"@pulumi"
"azure-native"
|]
let npmPackagePath = Path.Combine npmPackagePathSegments
let availableModules = Directory.EnumerateDirectories npmPackagePath
Map.ofList [
for availableModulePath in availableModules do
let moduleName = DirectoryInfo(availableModulePath).Name
let availableVersions =
Directory.EnumerateDirectories availableModulePath
|> Seq.map (fun dir -> DirectoryInfo(dir).Name)
|> Seq.filter (fun dir -> dir.StartsWith "v")
moduleName, List.ofSeq availableVersions
]
let converterVersion() =
let projectFilePath = Path.Combine(repositoryRoot, "src", "PulumiBicepConverter", "PulumiBicepConverter.fsproj")
let content = File.ReadAllText projectFilePath
let doc = XmlDocument()
use content = new MemoryStream(Encoding.UTF8.GetBytes content)
doc.Load(content)
doc.GetElementsByTagName("Version").[0].InnerText
let artifacts = Path.Combine(repositoryRoot, "artifacts")
let createTarGz (source: string) (target: string) =
let outStream = File.Create target
let gzipOutput = new GZipOutputStream(outStream)
let tarArchive = TarArchive.CreateOutputTarArchive(gzipOutput);
for file in Directory.GetFiles source do
let tarEntry = TarEntry.CreateEntryFromFile file
tarEntry.Name <- Path.GetFileName file
tarArchive.WriteEntry(tarEntry, false)
tarArchive.Close()
let rec createArtifacts() =
let version = converterVersion()
let cwd = Path.Combine(repositoryRoot, "src", "PulumiBicepConverter")
let runtimes = [
"linux-x64"
"linux-arm64"
"osx-x64"
"osx-arm64"
"win-x64"
"win-arm64"
]
Shell.deleteDirs [
Path.Combine(cwd, "bin")
Path.Combine(cwd, "obj")
artifacts
]
let binary = "pulumi-converter-bicep"
for runtime in runtimes do
printfn $"Building binary {binary} for {runtime}"
let args = [
"publish"
"--configuration Release"
$"--runtime {runtime}"
"--self-contained true"
"-p:PublishSingleFile=true"
"/p:DebugType=None"
"/p:DebugSymbols=false"
]
let exitCode = Shell.Exec("dotnet", String.concat " " args, cwd)
if exitCode <> 0 then
failwith $"failed to build for runtime {runtime}"
Directory.create artifacts
for runtime in runtimes do
let publishPath = Path.Combine(cwd, "bin", "Release", "net7.0", runtime, "publish")
let destinationRuntime =
match runtime with
| "osx-x64" -> "darwin-amd64"
| "osx-arm64" -> "darwin-arm64"
| "linux-x64" -> "linux-amd64"
| "linux-arm64" -> "linux-arm64"
| "win-x64" -> "windows-amd64"
| "win-arm64" -> "windows-arm64"
| _ -> runtime
let destination = Path.Combine(artifacts, $"{binary}-v{version}-{destinationRuntime}.tar.gz")
createTarGz publishPath destination
let inline await (task: Task<'t>) =
task
|> Async.AwaitTask
|> Async.RunSynchronously
let releaseVersion (release: Release) =
if not (String.IsNullOrWhiteSpace(release.Name)) then
release.Name.Substring(1, release.Name.Length - 1)
elif not (String.IsNullOrWhiteSpace(release.TagName)) then
release.TagName.Substring(1, release.TagName.Length - 1)
else
""
let createAndPublishArtifacts() =
let version = converterVersion()
let github = new GitHubClient(ProductHeaderValue "PulumiBicepConverter")
let githubToken = Environment.GetEnvironmentVariable "GITHUB_TOKEN"
// only assign github token to the client when it is available (usually in Github CI)
if not (isNull githubToken) then
printfn "GITHUB_TOKEN is available"
github.Credentials <- Credentials(githubToken)
else
printfn "GITHUB_TOKEN is not set"
let githubUsername = "pulumi"
let githubRepo = "pulumi-converter-bicep"
let releases = await (github.Repository.Release.GetAll(githubUsername, githubRepo))
let alreadyReleased = releases |> Seq.exists (fun release -> releaseVersion release = version)
if alreadyReleased then
printfn $"Release v{version} already exists, skipping publish"
else
printfn $"Preparing artifacts to release v{version}"
createArtifacts()
let releaseInfo = NewRelease($"v{version}")
let release = await (github.Repository.Release.Create(githubUsername, githubRepo, releaseInfo))
for file in Directory.EnumerateFiles artifacts do
let asset = ReleaseAssetUpload()
asset.FileName <- Path.GetFileName file
asset.ContentType <- "application/tar"
asset.RawData <- File.OpenRead(file)
let uploadedAsset = await (github.Repository.Release.UploadAsset(release, asset))
printfn $"Uploaded {uploadedAsset.Name} into assets of v{version}"
[<EntryPoint>]
let main(args: string[]) : int =
match args with
| [| "tests" |] ->
runTests()
| [| "integration-tests" |] ->
integrationTests()
| [| "build" |] ->
buildSolution()
| [| "version" |] ->
printfn $"{converterVersion()}"
| [| "publish" |] ->
createAndPublishArtifacts()
| [| "create-artifacts" |] ->
createArtifacts()
| [| "write-schema-subset" |] ->
match parseSchemaFromPulumi "azure-native" with
| Error errorMessage -> printfn $"couldn't parse azure-native schema: {errorMessage}"
| Ok azureNativeSchema ->
let resourcesWhichRequireResourceGroupName =
azureNativeSchema.resources
|> Map.toList
|> List.filter (fun (token, resource) ->
match Map.tryFind "resourceGroupName" resource.inputProperties with
| Some property -> property.required
| _ -> false)
|> List.map fst
let mainParameterToQueryExistingResources =
azureNativeSchema.resources
|> Map.toList
|> List.map fst
|> List.collect (fun token ->
match token.Split ":" with
| [| ns; moduleName; resourceName |] ->
let getterInvokeToken = $"{ns}:{moduleName}:get{resourceName}"
match Map.tryFind getterInvokeToken azureNativeSchema.functions with
| Some functionSchema ->
match functionSchema.inputs with
| None -> [ ]
| Some inputs ->
inputs.properties
|> Map.toList
|> List.filter (fun (inputProperty, property) ->
let propertyName = inputProperty.ToLower()
propertyName <> "resourcegroupname" && propertyName.EndsWith "name" && property.required)
|> List.map fst
|> function
| [] -> []
| elements when List.contains "name" elements -> [ getterInvokeToken, "name" ]
| elements ->
elements
|> List.minBy (fun property -> Levenshtein.Distance(property.ToLower(),resourceName))
|> fun value -> [ getterInvokeToken, value ]
| None -> []
| _ -> [])
|> List.append [ "azure-native:resources:getResourceGroup", "resourceGroupName" ]
let nameParameterForResources =
azureNativeSchema.resources
|> Map.toList
|> List.collect (fun (token, resource) ->
match token.Split ":" with
| [| ns; moduleName; resourceName |] ->
resource.inputProperties
|> Map.toList
|> List.map fst
|> List.filter (fun propertyName ->
let name = propertyName.ToLower()
name <> "resourcegroupname" && (name.EndsWith "name" || name.EndsWith "id"))
|> function
| [] -> []
| elements ->
elements
|> List.minBy (fun property ->
let propName = property.ToLower().Replace("name", "").Replace("id", "")
Levenshtein.Distance(propName,resourceName))
|> fun value -> [ token, value ]
| _ -> [])
let resourcesWhichHaveInputPropertiesObject =
azureNativeSchema.resources
|> Map.toList
|> List.filter (fun (token, resource) ->
match Map.tryFind "properties" resource.inputProperties with
| Some property ->
match property.schemaType with
| SchemaType.Object _ -> true
| SchemaType.Ref _ -> true
| _ -> false
| _ -> false)
|> List.map fst
printfn $"There are {resourcesWhichRequireResourceGroupName.Length} resources which require resourceGroupName"
let targetFile = Path.Combine(repositoryRoot, "src", "Converter", "Schema.fs")
let content = StringBuilder()
let append (input: string) = content.Append input |> ignore
append "module Converter.Schema\n\n"
append "let resourcesWhichRequireResourceGroupName = [|\n"
for resourceToken in resourcesWhichRequireResourceGroupName do
append $" \"{resourceToken}\"\n"
append "|]"
append "\n\n"
append "let resourcesWhichHaveInputPropertiesObject = [|\n"
for resourceToken in resourcesWhichHaveInputPropertiesObject do
append $" \"{resourceToken}\"\n"
append "|]"
append "\n\n"
append "let nameParameterForExistingResources = Map.ofArray [|\n"
for invokeToken, parameterName in mainParameterToQueryExistingResources do
append $" \"{invokeToken}\", \"{parameterName}\"\n"
append "|]"
append "\n\n"
append "let nameParameterForResources = Map.ofArray [|\n"
for token, parameterName in nameParameterForResources do
append $" \"{token}\", \"{parameterName}\"\n"
append "|]"
append "\n\n"
append "let availableModuleVersions = Map.ofArray [|\n"
for availableModule in azureNativeModuleVersions() do
let moduleName = availableModule.Key
let moduleVersions =
availableModule.Value
|> List.map (fun version -> $"\"{version}\"")
|> String.concat ";"
append $" \"{moduleName}\", [{moduleVersions}]\n"
append "|]"
append "\n\n"
File.WriteAllText(targetFile, content.ToString())
printfn $"Written to {targetFile}"
| otherwise -> printfn $"Unknown build arguments provided %A{otherwise}"
0