-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hooks.cs
95 lines (76 loc) · 3.16 KB
/
Hooks.cs
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
using keyvault_certsync.Models;
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace keyvault_certsync
{
public static class Hooks
{
private static readonly Dictionary<string, List<DownloadResult>> postHooks = new();
public static void AddPostHook(string command, IEnumerable<DownloadResult> results)
{
if (!postHooks.ContainsKey(command))
postHooks.Add(command, new List<DownloadResult>());
postHooks[command].AddRange(results);
}
public static int RunPostHooks()
{
bool hookFailed = false;
foreach(var entry in postHooks)
{
if (RunPostHook(entry.Key, entry.Value) != 0)
hookFailed = true;
}
return hookFailed ? -1 : 0;
}
public static int RunDeployHook(string command, DownloadResult result)
{
string[] parts = command.Split(new[] { ' ' }, 2);
var startInfo = new ProcessStartInfo(parts[0]);
startInfo.EnvironmentVariables.Add("CERTIFICATE_NAME", result.CertificateName);
startInfo.EnvironmentVariables.Add("CERTIFICATE_THUMBPRINT", result.Thumbprint);
if (!string.IsNullOrEmpty(result.Path))
startInfo.EnvironmentVariables.Add("CERTIFICATE_PATH", result.Path);
if (parts.Length > 1)
startInfo.Arguments = parts[1];
return RunHook(startInfo, "Deploy");
}
private static int RunPostHook(string command, IEnumerable<DownloadResult> results)
{
string[] parts = command.Split(new[] { ' ' }, 2);
var startInfo = new ProcessStartInfo(parts[0]);
startInfo.EnvironmentVariables.Add("CERTIFICATE_NAMES", string.Join(",", results.Select(s => s.CertificateName)));
startInfo.EnvironmentVariables.Add("CERTIFICATE_THUMBPRINTS", string.Join(",", results.Select(s => s.Thumbprint)));
if (parts.Length > 1)
startInfo.Arguments = parts[1];
return RunHook(startInfo, "Post");
}
private static int RunHook(ProcessStartInfo startInfo, string type)
{
int exitCode;
try
{
using var process = Process.Start(startInfo);
process.WaitForExit();
exitCode = process.ExitCode;
}
catch (Exception ex)
{
Log.Error(ex, "{HookType} hook '{Hook}' '{HookArguments}' failed to run",
type, startInfo.FileName, startInfo.Arguments);
return -1;
}
if (exitCode == 0)
{
Log.Information("{HookType} hook '{Hook}' '{HookArguments}' completed successfully",
type, startInfo.FileName, startInfo.Arguments);
return 0;
}
Log.Warning("{HookType} hook '{Hook}' '{HookArguments}' completed with exit code {ExitCode}",
type, startInfo.FileName, startInfo.Arguments, exitCode);
return exitCode;
}
}
}