/
RetentionPolicy.cs
153 lines (133 loc) · 5.72 KB
/
RetentionPolicy.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Calamari.Deployment.Journal;
using Calamari.Integration.FileSystem;
using Calamari.Integration.Time;
namespace Calamari.Deployment.Retention
{
public class RetentionPolicy : IRetentionPolicy
{
readonly ICalamariFileSystem fileSystem;
readonly IDeploymentJournal deploymentJournal;
readonly IClock clock;
public RetentionPolicy(ICalamariFileSystem fileSystem, IDeploymentJournal deploymentJournal, IClock clock)
{
this.fileSystem = fileSystem;
this.deploymentJournal = deploymentJournal;
this.clock = clock;
}
public void ApplyRetentionPolicy(string retentionPolicySet, int? days, int? releases)
{
var deployments = deploymentJournal
.GetAllJournalEntries()
.Where(x => x.RetentionPolicySet == retentionPolicySet)
.ToList();
var preservedEntries = new List<JournalEntry>();
if (days.HasValue && days.Value > 0)
{
Log.Info($"Keeping deployments from the last {days} days");
deployments = deployments
.Where(InstalledBeforePolicyDayCutoff(days.Value, preservedEntries))
.ToList();
}
else if (releases.HasValue && releases.Value > 0)
{
Log.Info($"Keeping this deployment and the previous {releases} successful deployments");
// Keep the current release, plus specified releases value
// Unsuccessful releases are not included in the count of releases to keep
deployments = deployments
.OrderByDescending(deployment => deployment.InstalledOn)
.SkipWhile(SuccessfulCountLessThanPolicyCount(releases.Value, preservedEntries))
.ToList();
}
else
{
Log.Info($"Keeping all releases");
return;
}
if (!deployments.Any())
{
Log.Info("Did not find any deployments to clean up");
}
foreach (var deployment in deployments)
{
DeleteExtractionDestination(deployment, preservedEntries);
DeleteExtractionSource(deployment, preservedEntries);
}
deploymentJournal.RemoveJournalEntries(deployments.Select(x => x.Id));
}
void DeleteExtractionSource(JournalEntry deployment, List<JournalEntry> preservedEntries)
{
if (string.IsNullOrWhiteSpace(deployment.ExtractedFrom)
|| !fileSystem.FileExists(deployment.ExtractedFrom)
|| preservedEntries.Any(entry => deployment.ExtractedFrom.Equals(entry.ExtractedFrom, StringComparison.Ordinal)))
return;
Log.Info($"Removing package file '{deployment.ExtractedFrom}'");
fileSystem.DeleteFile(deployment.ExtractedFrom, FailureOptions.IgnoreFailure);
}
void DeleteExtractionDestination(JournalEntry deployment, List<JournalEntry> preservedEntries)
{
if (!fileSystem.DirectoryExists(deployment.ExtractedTo)
|| preservedEntries.Any(entry => deployment.ExtractedTo.Equals(entry.ExtractedTo, StringComparison.Ordinal)))
return;
Log.Info($"Removing directory '{deployment.ExtractedTo}'");
fileSystem.PurgeDirectory(deployment.ExtractedTo, FailureOptions.IgnoreFailure);
try
{
fileSystem.DeleteDirectory(deployment.ExtractedTo);
}
catch (Exception ex)
{
Log.VerboseFormat("Could not delete directory '{0}' because some files could not be deleted: {1}",
deployment.ExtractedFrom, ex.Message);
}
}
static Func<JournalEntry, bool> SuccessfulCountLessThanPolicyCount(int releases, List<JournalEntry> preservedEntries)
{
return journalEntry =>
{
if (preservedEntries.Count() > releases)
{
return false;
}
if (journalEntry.WasSuccessful)
{
preservedEntries.Add(journalEntry);
Log.Verbose($"Keeping {journalEntry.ExtractedTo} and {journalEntry.ExtractedFrom} as it is the {FormatWithThPostfix(preservedEntries.Count)}most recent successful release");
}
return true;
};
}
Func<JournalEntry, bool> InstalledBeforePolicyDayCutoff(int days, List<JournalEntry> preservedEntries)
{
return journalEntry =>
{
var installedAgo = (clock.GetUtcTime() - journalEntry.InstalledOn);
if (installedAgo.TotalDays > days)
return true;
Log.Verbose($"Keeping {journalEntry.ExtractedTo} and {journalEntry.ExtractedFrom} as it was installed {installedAgo.Days} days and {installedAgo.Hours} hours ago");
preservedEntries.Add(journalEntry);
return false;
};
}
static string FormatWithThPostfix(int value)
{
if (value == 1)
return "";
if (value == 11 || value == 12 || value == 13)
return value + "th ";
switch (value % 10)
{
case 1:
return value + "st ";
case 2:
return value + "nd ";
case 3:
return value + "rd ";
default:
return value + "th ";
}
}
}
}