-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathcleanup_storage.js
108 lines (91 loc) · 2.79 KB
/
cleanup_storage.js
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
/**
* Usage: node cleanup_storage.js <action> [<keyfile>]
*
* action:
* "print" or "delete"
* keyfile:
* The key.json file to use for authentication
*
* Setup:
* npm install pkgcloud
*/
//~~ setup
// imports
var pkgcloud = require('pkgcloud'),
fs = require('fs'),
path = require('path');
// polyfills
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (str) {
return !this.indexOf(str);
}
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
//~~ argument parsing
// "delete" -> delete, "print" -> only print
if (process.argv.length < 3) {
console.log("Missing mandatory action parameter");
process.exit();
}
var action = process.argv[2];
// key file to use => ./key.json or second command line argument
var keyfile = path.join(__dirname, 'key.json');
if (process.argv.length >= 4) {
keyfile = process.argv[3];
}
//~~ helpers
var sortByDate = function(a, b) {
if (a.timeCreated < b.timeCreated) return 1;
if (a.timeCreated > b.timeCreated) return -1;
return 0;
}
//~~ action and go
// construct client
var client = require('pkgcloud').storage.createClient({
provider: 'google',
keyFilename: keyfile, // path to a JSON key file
});
var container = "octoprint";
// fetch our files and render our page
var matchers = [
{
matcher: function(obj) { return !obj.name.startsWith("stable/") && !obj.name.startsWith("bananapi-m1/") && /octopi-(wheezy|jessie)-/.test(obj.name); },
limit: 14
},
{
matcher: function(obj) { return /^bananapi-m1\//.test(obj.name); },
limit: 14
}
]
var now = new Date();
client.getFiles(container, function (err, files) {
matchers.forEach(function(m) {
var cutoff = new Date();
cutoff.setDate(now.getDate() - m.limit);
var filesToDelete = files.filter(m.matcher)
.filter(function(obj) { return new Date(Date.parse(obj.timeCreated)) < cutoff });
filesToDelete.forEach(function (file) {
if (action == "delete") {
client.removeFile(container, encodeURIComponent(file.name), function(err) {
if (err) {
console.log("Error deleting " + file.name + ": " + err);
} else {
console.log("Deleted " + file.name + " on " + container);
}
});
} else {
console.log("Would now delete " + file.name + " on " + container);
}
});
});
});