docker/ts/dockersock.classes.dockersock.ts

202 lines
7.0 KiB
TypeScript
Raw Normal View History

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