forked from lblod/docker-monitor-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.js
161 lines (150 loc) · 5.17 KB
/
container.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { query, update, sparqlEscapeUri, sparqlEscapeString, sparqlEscapeDateTime, uuid } from 'mu';
const PREFIXES = `
PREFIX docker: <https://w3.org/ns/bde/docker#>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
`;
class Container {
constructor({id, name, uri, status, labels, image}){
this.id = id;
this.name = name;
this.uri = uri;
this.status = status;
this.labels = labels;
this.image = image;
this._dirty = false;
}
async setStatus(status) {
if(this.status != status) {
this.status = status;
await this.save(true);
}
}
async remove() {
await this.setStatus('removed');
}
static async findAll() {
// Note that this does not fetch labels, as those are immutable.
// We also don't fetch containers marked as remove, as a removed container
// no longer exists as Docker container on the system (neither running or stopped)
// and can thus never show up again.
const result = await query(`
${PREFIXES}
SELECT DISTINCT ?uri ?id ?name ?status
FROM ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)}
WHERE {
?uri a docker:Container;
docker:id ?id;
docker:name ?name;
docker:state/docker:status ?status.
FILTER (?status != "removed")
}
`);
const bindingKeys = result.head.vars;
const objects = result.results.bindings.map( (r) => {
let obj = {};
bindingKeys.forEach((key) => {
if (r[key])
obj[key] = r[key].value;
});
return new this(obj);
});
return objects;
}
static async getLabels(uri) {
const result = await query(`
${PREFIXES}
SELECT DISTINCT ?label ?labelKey ?labelValue
FROM ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)}
WHERE {
${sparqlEscapeUri(uri)} docker:label ?label.
?label docker:key ?labelKey.
?label docker:value ?labelValue.
}
`);
const labels = {};
for (let result of result.results.bindings) {
labels[result.labelKey.value] = { uri: result.label.value, value: result.labelValue.value};
}
return labels;
}
update(newInformation) {
// Name and status are the only two properties of a running container (that we keep track of) that can change.
if(this.name != newInformation.name) {
this.name = newInformation.name;
this._dirty = true;
}
if(this.status != newInformation.status) {
this.status = newInformation.status;
this._dirty = true;
}
}
async save(force=false) {
// Don't update if nothing has changed.
if (!this._dirty && !force) {
return;
}
if (this.uri) {
// assume it already exists in the database if we have a uri
await update(`
${PREFIXES}
WITH ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)}
DELETE {
${sparqlEscapeUri(this.uri)} docker:name ?name.
?stateURI docker:status ?status.
?stateURI ext:updatedAt ?updatedAt.
}
INSERT {
${sparqlEscapeUri(this.uri)} docker:name ${sparqlEscapeString(this.name)}.
?stateURI docker:status ${sparqlEscapeString(this.status)};
ext:updatedAt ${sparqlEscapeDateTime(new Date())}
}
WHERE {
${sparqlEscapeUri(this.uri)} a docker:Container;
docker:id ?id;
docker:name ?name;
docker:state ?stateURI.
?stateURI docker:status ?status.
OPTIONAL { ?stateURI ext:updatedAt ?updatedAt }
}`);
}
else {
// it's not persisted yet
const stateURI = `http://data.lblod.info/id/docker-state/${uuid()}`;
const containerURI = `http://data.lblod.info/id/docker-container/${this.id}`;
await update(`
${PREFIXES}
INSERT DATA {
GRAPH ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)} {
${sparqlEscapeUri(containerURI)} a docker:Container;
docker:id ${sparqlEscapeString(this.id)};
docker:name ${sparqlEscapeString(this.name)};
docker:image ${sparqlEscapeString(this.image)};
docker:state ${sparqlEscapeUri(stateURI)}.
${sparqlEscapeUri(stateURI)} a docker:State;
docker:status ${sparqlEscapeString(this.status)};
ext:updatedAt ${sparqlEscapeDateTime(new Date())}.
${this.labelTriples(containerURI).join("\n")}
}
}
`);
}
this._dirty = false;
}
labelTriples(objUri=this.uri) {
const triples = new Array();
if (!this.labels && this.uri)
this.labels = Container.getLabels(this.uri);
for (let key of Object.keys(this.labels)) {
let value = this.labels[key];
let uri = value.uri ? value.uri : `http://data.lblod.info/id/container-label/${uuid()}`;
triples.push(`
${sparqlEscapeUri(objUri)} docker:label ${sparqlEscapeUri(uri)}.
${sparqlEscapeUri(uri)} a docker:ContainerLabel;
docker:key ${sparqlEscapeString(key)};
docker:value ${sparqlEscapeString(value.value)}.
`);
}
return triples;
}
}
export default Container;