2016-06-14 07:26:54 +00:00
|
|
|
import "typings-global"
|
|
|
|
import * as plugins from "./dockersock.plugins";
|
2016-07-31 19:05:41 +00:00
|
|
|
import { Observable } from "rxjs";
|
2016-06-14 07:26:54 +00:00
|
|
|
|
2016-06-15 23:56:53 +00:00
|
|
|
export class Dockersock {
|
2016-07-31 19:05:41 +00:00
|
|
|
sockPath: string;
|
|
|
|
requestObjectmap: plugins.lik.Objectmap = new plugins.lik.Objectmap();
|
|
|
|
constructor(pathArg: string = "http://unix:/var/run/docker.sock:") {
|
2016-06-14 07:26:54 +00:00
|
|
|
this.sockPath = pathArg;
|
|
|
|
}
|
2016-06-15 23:34:23 +00:00
|
|
|
|
|
|
|
// methods
|
2016-07-31 19:05:41 +00:00
|
|
|
auth(userArg: string, passArg: string) {
|
2016-06-16 02:45:22 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-07-31 19:05:41 +00:00
|
|
|
this.request("POST", "");
|
2016-06-16 02:45:22 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
2016-06-15 23:34:23 +00:00
|
|
|
listContainers() {
|
|
|
|
let done = plugins.q.defer();
|
2016-07-31 19:05:41 +00:00
|
|
|
this.request("GET", "/containers")
|
2016-06-15 23:34:23 +00:00
|
|
|
.then(done.resolve);
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
listContainersDetailed() {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
let detailedDataObject = [];
|
|
|
|
this.listContainers()
|
|
|
|
.then((dataArg) => {
|
|
|
|
let recursiveCounter = 0;
|
2016-06-16 02:18:10 +00:00
|
|
|
let makeDetailed = () => {
|
2016-07-31 19:05:41 +00:00
|
|
|
if (typeof dataArg[recursiveCounter] != "undefined") {
|
|
|
|
this.request("GET", "/containers/" + dataArg[recursiveCounter].Id)
|
2016-06-16 02:18:10 +00:00
|
|
|
.then((dataArg2) => {
|
|
|
|
detailedDataObject.push(dataArg2);
|
|
|
|
recursiveCounter++;
|
|
|
|
// recursive call
|
|
|
|
makeDetailed();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done.resolve(detailedDataObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
makeDetailed();
|
2016-06-15 23:34:23 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
listContainersRunning() {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
listContainersStopped() {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
listImages() {
|
2016-07-31 19:05:41 +00:00
|
|
|
return this.request("GET", "/images", "?all=true");
|
2016-06-16 04:43:34 +00:00
|
|
|
}
|
2016-07-31 19:05:41 +00:00
|
|
|
listImagesDangling() {
|
|
|
|
return this.request("GET", "/images", "?dangling=true");
|
2016-06-15 23:34:23 +00:00
|
|
|
}
|
2016-07-31 19:05:41 +00:00
|
|
|
pullImage(imageLabelArg: string) {
|
2016-07-17 22:54:41 +00:00
|
|
|
let imageLabel = encodeURI(imageLabelArg);
|
2016-07-31 19:05:41 +00:00
|
|
|
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
|
2016-06-16 04:43:34 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
createContainer(optionsArg, pullFirstArg: boolean = true) {
|
2016-06-17 00:38:00 +00:00
|
|
|
let done = plugins.q.defer();
|
|
|
|
let create = () => {
|
2016-07-31 19:05:41 +00:00
|
|
|
return this.request("POST", "/containers/create", "", optionsArg);
|
2016-06-17 00:38:00 +00:00
|
|
|
}
|
2016-07-31 19:05:41 +00:00
|
|
|
if (pullFirstArg) {
|
2016-06-17 00:38:00 +00:00
|
|
|
this.pullImage(optionsArg.Image)
|
|
|
|
.then(create)
|
|
|
|
.then(done.resolve);
|
|
|
|
} else {
|
|
|
|
create()
|
|
|
|
.then(done.resolve)
|
|
|
|
}
|
|
|
|
return done.promise;
|
2016-06-16 04:43:34 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
getContainerId() {
|
2016-06-16 02:45:22 +00:00
|
|
|
|
2016-06-16 04:43:34 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
startContainer(containerNameArg) {
|
|
|
|
return this.request("POST", "/containers/" + containerNameArg + "/start");
|
2016-06-16 02:45:22 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
stopContainer(containerNameArg) {
|
|
|
|
return this.request("POST", "/containers/" + containerNameArg + "/stop");
|
2016-06-16 04:43:34 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
removeContainer(containerNameArg) {
|
|
|
|
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
|
2016-06-16 04:43:34 +00:00
|
|
|
};
|
2016-06-15 23:34:23 +00:00
|
|
|
clean() {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
return done.promise;
|
2016-06-16 02:45:22 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
callOnChange(cb: Function) {
|
2016-06-17 00:21:48 +00:00
|
|
|
let cbPromise;
|
2016-07-31 19:05:41 +00:00
|
|
|
let changeBuffered: boolean = false; // when cb is running then buffer any consequent change
|
2016-06-17 00:21:48 +00:00
|
|
|
let requestStream = plugins.request.get(this.sockPath + "/events");
|
2016-07-31 19:05:41 +00:00
|
|
|
requestStream.on("response", (response) => {
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
plugins.beautylog.ok("request returned status 200, so we are good!");
|
|
|
|
} else {
|
|
|
|
plugins.beautylog.error("request returned error: " + response.statusCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
requestStream.on("data", (data: Buffer) => {
|
2016-06-17 00:21:48 +00:00
|
|
|
let status = JSON.parse(data.toString()).status;
|
|
|
|
plugins.beautylog.logReduced(status);
|
2016-07-31 19:05:41 +00:00
|
|
|
if (typeof cbPromise == "undefined" || cbPromise.state == "pending") {
|
2016-06-17 00:21:48 +00:00
|
|
|
cbPromise = cb();
|
|
|
|
} else if (changeBuffered) {
|
|
|
|
changeBuffered = true;
|
|
|
|
cbPromise.then(() => {
|
|
|
|
changeBuffered = false;
|
|
|
|
cbPromise = cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-07-31 19:05:41 +00:00
|
|
|
requestStream.on("end", () => {
|
|
|
|
|
|
|
|
});
|
2016-06-16 02:45:22 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
getChangeObservable() {
|
2016-07-29 18:41:27 +00:00
|
|
|
let options = {
|
2016-07-31 19:05:41 +00:00
|
|
|
method: "GET",
|
|
|
|
url: this.sockPath + "/events",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Host": "docker.sock"
|
2016-07-29 18:41:27 +00:00
|
|
|
}
|
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
let requestStream = plugins.request(options, (err, res, body) => {
|
2016-07-29 18:41:27 +00:00
|
|
|
if (!err && res.statusCode == 200) {
|
|
|
|
} else {
|
|
|
|
console.log(err);
|
|
|
|
console.log(res);
|
|
|
|
};
|
|
|
|
});
|
2016-07-31 19:05:41 +00:00
|
|
|
requestStream.on("response", (response) => {
|
|
|
|
this.requestObjectmap.add(response);
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
plugins.beautylog.ok("request returned status 200, so we are good!");
|
|
|
|
} else {
|
|
|
|
plugins.beautylog.error("request returned error: " + response.statusCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let changeObservable = Observable.fromEvent(requestStream, "data");
|
|
|
|
requestStream.on("end", () => {
|
|
|
|
this.requestObjectmap.remove(requestStream);
|
2016-07-11 19:55:10 +00:00
|
|
|
});
|
2016-07-12 11:36:34 +00:00
|
|
|
return changeObservable;
|
2016-07-11 19:55:10 +00:00
|
|
|
}
|
2016-07-31 19:05:41 +00:00
|
|
|
request(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
2016-06-15 22:40:32 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-07-31 19:05:41 +00:00
|
|
|
let jsonArg: string = JSON.stringify(dataArg);
|
|
|
|
let suffix: string = "";
|
|
|
|
if (methodArg == "GET") suffix = "/json";
|
2016-06-15 22:40:32 +00:00
|
|
|
let options = {
|
2016-07-31 19:05:41 +00:00
|
|
|
method: methodArg,
|
|
|
|
url: this.sockPath + routeArg + suffix + queryArg,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Host": "docker.sock"
|
2016-06-15 22:40:32 +00:00
|
|
|
},
|
2016-07-31 19:05:41 +00:00
|
|
|
body: jsonArg
|
2016-06-15 22:40:32 +00:00
|
|
|
};
|
2016-07-17 23:35:04 +00:00
|
|
|
//console.log(options);
|
2016-07-31 19:05:41 +00:00
|
|
|
plugins.request(options, (err, res, body) => {
|
2016-06-15 22:40:32 +00:00
|
|
|
if (!err && res.statusCode == 200) {
|
|
|
|
var responseObj = JSON.parse(body);
|
|
|
|
done.resolve(responseObj);
|
|
|
|
} else {
|
|
|
|
console.log(err);
|
2016-07-17 23:35:04 +00:00
|
|
|
console.log(res);
|
2016-06-15 22:40:32 +00:00
|
|
|
done.reject(err);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return done.promise;
|
2016-06-14 07:26:54 +00:00
|
|
|
}
|
2016-07-31 19:05:41 +00:00
|
|
|
requestStream(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
2016-06-16 06:47:21 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-07-31 19:05:41 +00:00
|
|
|
let jsonArg: string = JSON.stringify(dataArg);
|
|
|
|
let suffix: string = "";
|
2016-07-18 00:50:28 +00:00
|
|
|
let options = {
|
2016-07-31 19:05:41 +00:00
|
|
|
method: methodArg,
|
|
|
|
url: this.sockPath + routeArg + suffix + queryArg,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Host": "docker.sock"
|
2016-07-18 00:50:28 +00:00
|
|
|
},
|
2016-07-31 19:05:41 +00:00
|
|
|
body: jsonArg
|
2016-07-18 00:50:28 +00:00
|
|
|
};
|
2016-07-31 19:05:41 +00:00
|
|
|
let requestStream = plugins.request(options, (err, res, body) => {
|
2016-07-18 00:50:28 +00:00
|
|
|
if (!err && res.statusCode == 200) {
|
2016-06-16 18:25:18 +00:00
|
|
|
done.resolve();
|
2016-07-18 00:50:28 +00:00
|
|
|
} else {
|
|
|
|
console.log(err);
|
|
|
|
console.log(res);
|
|
|
|
done.reject(err);
|
|
|
|
};
|
|
|
|
});
|
2016-07-31 19:05:41 +00:00
|
|
|
requestStream.on("response", (response) => {
|
|
|
|
this.requestObjectmap.add(response);
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
plugins.beautylog.ok("request returned status 200, so we are good!");
|
|
|
|
} else {
|
|
|
|
plugins.beautylog.error("request returned error: " + response.statusCode);
|
|
|
|
done.reject(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
requestStream.on("data", (data: Buffer) => {
|
2016-07-18 00:50:28 +00:00
|
|
|
let status;
|
|
|
|
status = JSON.parse(data.toString()).status;
|
|
|
|
plugins.beautylog.logReduced(status);
|
|
|
|
});
|
2016-07-31 19:05:41 +00:00
|
|
|
requestStream.on("end", () => {
|
|
|
|
this.requestObjectmap.remove(requestStream);
|
|
|
|
});
|
2016-06-16 06:47:21 +00:00
|
|
|
return done.promise;
|
2016-07-31 19:05:41 +00:00
|
|
|
};
|
|
|
|
endRequests() {
|
|
|
|
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
|
|
|
|
itemArg.emit("end");
|
|
|
|
});
|
|
|
|
this.requestObjectmap.wipe();
|
|
|
|
};
|
2016-06-14 07:26:54 +00:00
|
|
|
}
|