-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreadDocker.js
138 lines (127 loc) · 4.88 KB
/
readDocker.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
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
var https = require('https');
var processedRecord = 0;
var processedAutomated =0;
var processedDockerFile = 0;
var lastResultSetProcessed = false;
var concurrentFunctions = [];
var dockerFiles = {};
function push(){concurrentFunctions.push(new Object());}
function pop() {concurrentFunctions.pop();printResult();}
function readDockerSearchPage(url) {
push();
https.get(url, function (res) {
push();
var body = '';
// console.log("Got response: " + res.statusCode);
res.on('data', function (d) {
body += d;
});
res.on('end', function () {
push();
var queryResponse = JSON.parse(body);
var nextURL = queryResponse.next;
processSearchResult(queryResponse.results);
if (nextURL != null) {
readDockerSearchPage(nextURL);
} else {
lastResultSetProcessed = true;
console.log("Completed processing all the result...");
}
pop();
})
pop();
}).on('error', function (e) {
console.log("Got error: " + e.message);
});
pop();
}
function searchDocker(queryString) {
readDockerSearchPage("https://hub.docker.com/v2/search/repositories/?page=1&query=" + queryString);
}
function processSearchResult(results) {
push();
var arrayLength = results.length;
for (var i = 0; i < arrayLength; i++) {
var repo_name = results[i].repo_name;
//console.log("Processing record " + repo_name);
if (true == results[i].is_automated) {
loadDockerFile(repo_name);
}
processedRecord++;
if (processedRecord % 10 == 0 ) {
console.log("Processed: " + processedRecord + " Automated: " + processedAutomated + " Docker file processed " + processedDockerFile);
}
}
pop();
}
function loadDockerFile(processed_repo_name) {
push();
https.get("https://hub.docker.com/v2/repositories/" + processed_repo_name + "/dockerfile/", function (dockerResponse) {
push();
var dockerFileBody = '';
if (dockerResponse.statusCode == 200) {
dockerResponse.on('data', function (d) {
dockerFileBody += d;
});
dockerResponse.on('end', function () {
push();
var parsedResponse = JSON.parse(dockerFileBody);
dockerFiles[processed_repo_name] = parsedResponse.contents;
var linesInFile = dockerFiles[processed_repo_name].split('\n');
for (var lineIndex in linesInFile) {
var line = linesInFile[lineIndex];
line.trim();
if (line.indexOf("#") == 0)
continue;
var locationOfFROM = line.indexOf("FROM ");
var locationOfSemiColon = line.indexOf(":");
if (locationOfFROM > -1) {
var parentRepoName = line.substring(locationOfFROM + 5);
if (locationOfSemiColon > -1) {
parentRepoName = line.substring(locationOfFROM + 5, locationOfSemiColon);
}
parentRepoName.trim();
if (parentRepoName in dockerFiles) {
//console.log("Already loaded ")
} else {
//console.log("Loading docker file for " + parentRepoName);
loadDockerFile(parentRepoName);
}
}
}
processedDockerFile++;
pop();
});
}
pop();
}).on('error', function (dockerError) {
console.log("Could not locate docker file for " + repo_name + ". Ignoring.");
}
);
processedAutomated++;
pop();
}
function printResult() {
if (concurrentFunctions.length == 0 && lastResultSetProcessed) {
console.log("Processed: " + processedRecord + " Automated: " + processedAutomated + " Docker file processed " + processedDockerFile);
var file_dump = "";
var repo_names = Object.keys(dockerFiles);
repo_names.sort().forEach(function (key) {
console.log(key);
file_dump += "-------------------------- " + key + " ------------------------\n";
file_dump +=dockerFiles[key];
file_dump +="\n";
// console.log("(" + key + ")" + dockerFiles[key]);
});
var fs = require("fs");
fs.writeFile("./dump.txt", file_dump, function (err) {
if (err) return console.log(err);
console.log('Generated file dump.txt');
});
}
}
if (typeof process != 'undefined' && process && typeof process.argv != 'undefined' && process.argv && process.argv.length > 2) {
searchDocker(process.argv[2]);
} else {
searchDocker('storm');
}