docker/ts/dockersock.classes.dockersock.ts

131 lines
4.3 KiB
TypeScript
Raw Normal View History

import "typings-global"
import * as plugins from "./dockersock.plugins";
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-06-16 04:43:34 +00:00
pullImage(imageLabel:string){
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-16 06:47:21 +00:00
createContainer(imageNameArg,pullFirst:boolean = true){
2016-06-16 04:43:34 +00:00
return this.request("POST","/containers/create","",{
2016-06-16 06:47:21 +00:00
"image":imageNameArg
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;
};
getChange(){
};
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:{
"Content-Type":"application/json"
},
body:jsonArg
};
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);
console.log(res);
done.reject(err);
};
});
return done.promise;
}
2016-06-16 06:47:21 +00:00
requestStream(methodArg,routeArg,endArg:boolean = true){
let done = plugins.q.defer();
if(methodArg == "POST"){
let requestStream = plugins.request.post(this.sockPath + routeArg)
.on("response",(response) => {
if(response.statusCode == 200){
if(endArg == true){
console.log("ending request");
response.emit("end");
} else {
console.log("streaming forever");
}
done.resolve(response);
} else {
done.reject();
}
})
}
return done.promise;
}
}