docker/ts/dockersock.classes.dockersock.ts

98 lines
2.9 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() {
let done = plugins.q.defer();
return done.promise;
}
getContainerId(){
}
startContainer(containerNameArg){
return this.request("POST","/containers/"+ containerNameArg +"/start");
};
stopContainer(){
return this.request("POST","/containers/"+ containerNameArg +"/stop");
}
clean() {
let done = plugins.q.defer();
return done.promise;
};
getChange(){
};
2016-06-15 22:40:32 +00:00
request(methodArg:string,routeArg:string,dataArg = {}){
let done = plugins.q.defer();
let jsonArg:string = JSON.stringify(dataArg);
let suffix:string = ""
if(methodArg == "GET") suffix = "/json";
2016-06-15 22:40:32 +00:00
let options = {
method:methodArg,
url:this.sockPath + routeArg + suffix,
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;
}
}