now has possibility to end streaming requests
This commit is contained in:
@ -1,22 +1,23 @@
|
||||
import "typings-global"
|
||||
import * as plugins from "./dockersock.plugins";
|
||||
import {Observable} from "rxjs";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
export class Dockersock {
|
||||
sockPath:string;
|
||||
constructor(pathArg:string = "http://unix:/var/run/docker.sock:"){
|
||||
sockPath: string;
|
||||
requestObjectmap: plugins.lik.Objectmap = new plugins.lik.Objectmap();
|
||||
constructor(pathArg: string = "http://unix:/var/run/docker.sock:") {
|
||||
this.sockPath = pathArg;
|
||||
}
|
||||
|
||||
// methods
|
||||
auth(userArg:string,passArg:string){
|
||||
auth(userArg: string, passArg: string) {
|
||||
let done = plugins.q.defer();
|
||||
this.request("POST","");
|
||||
this.request("POST", "");
|
||||
return done.promise;
|
||||
}
|
||||
listContainers() {
|
||||
let done = plugins.q.defer();
|
||||
this.request("GET","/containers")
|
||||
this.request("GET", "/containers")
|
||||
.then(done.resolve);
|
||||
return done.promise;
|
||||
};
|
||||
@ -27,8 +28,8 @@ export class Dockersock {
|
||||
.then((dataArg) => {
|
||||
let recursiveCounter = 0;
|
||||
let makeDetailed = () => {
|
||||
if(typeof dataArg[recursiveCounter] != "undefined"){
|
||||
this.request("GET","/containers/" + dataArg[recursiveCounter].Id)
|
||||
if (typeof dataArg[recursiveCounter] != "undefined") {
|
||||
this.request("GET", "/containers/" + dataArg[recursiveCounter].Id)
|
||||
.then((dataArg2) => {
|
||||
detailedDataObject.push(dataArg2);
|
||||
recursiveCounter++;
|
||||
@ -52,21 +53,21 @@ export class Dockersock {
|
||||
return done.promise;
|
||||
}
|
||||
listImages() {
|
||||
return this.request("GET","/images","?all=true");
|
||||
return this.request("GET", "/images", "?all=true");
|
||||
}
|
||||
listImagesDangling(){
|
||||
return this.request("GET","/images","?dangling=true");
|
||||
listImagesDangling() {
|
||||
return this.request("GET", "/images", "?dangling=true");
|
||||
}
|
||||
pullImage(imageLabelArg:string){
|
||||
pullImage(imageLabelArg: string) {
|
||||
let imageLabel = encodeURI(imageLabelArg);
|
||||
return this.requestStream("POST","/images/create?fromImage=" + imageLabel);
|
||||
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
|
||||
};
|
||||
createContainer(optionsArg,pullFirstArg:boolean = true){
|
||||
createContainer(optionsArg, pullFirstArg: boolean = true) {
|
||||
let done = plugins.q.defer();
|
||||
let create = () => {
|
||||
return this.request("POST","/containers/create","",optionsArg);
|
||||
return this.request("POST", "/containers/create", "", optionsArg);
|
||||
}
|
||||
if(pullFirstArg){
|
||||
if (pullFirstArg) {
|
||||
this.pullImage(optionsArg.Image)
|
||||
.then(create)
|
||||
.then(done.resolve);
|
||||
@ -76,37 +77,37 @@ export class Dockersock {
|
||||
}
|
||||
return done.promise;
|
||||
};
|
||||
getContainerId(){
|
||||
getContainerId() {
|
||||
|
||||
};
|
||||
startContainer(containerNameArg){
|
||||
return this.request("POST","/containers/"+ containerNameArg +"/start");
|
||||
startContainer(containerNameArg) {
|
||||
return this.request("POST", "/containers/" + containerNameArg + "/start");
|
||||
};
|
||||
stopContainer(containerNameArg){
|
||||
return this.request("POST","/containers/"+ containerNameArg +"/stop");
|
||||
stopContainer(containerNameArg) {
|
||||
return this.request("POST", "/containers/" + containerNameArg + "/stop");
|
||||
};
|
||||
removeContainer(containerNameArg){
|
||||
return this.request("DELETE","/containers/" + containerNameArg + "?v=1");
|
||||
removeContainer(containerNameArg) {
|
||||
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
|
||||
};
|
||||
clean() {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
};
|
||||
callOnChange(cb:Function){
|
||||
callOnChange(cb: Function) {
|
||||
let cbPromise;
|
||||
let changeBuffered:boolean = false; // when cb is running then buffer any consequent change
|
||||
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) => {
|
||||
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"){
|
||||
if (typeof cbPromise == "undefined" || cbPromise.state == "pending") {
|
||||
cbPromise = cb();
|
||||
} else if (changeBuffered) {
|
||||
changeBuffered = true;
|
||||
@ -116,54 +117,56 @@ export class Dockersock {
|
||||
});
|
||||
}
|
||||
});
|
||||
requestStream.on("end",()=> {
|
||||
|
||||
});
|
||||
requestStream.on("end", () => {
|
||||
|
||||
});
|
||||
};
|
||||
getChangeObservable(){
|
||||
getChangeObservable() {
|
||||
let options = {
|
||||
method:"GET",
|
||||
url:this.sockPath + "/events",
|
||||
headers:{
|
||||
"Content-Type":"application/json",
|
||||
"Host":"docker.sock"
|
||||
method: "GET",
|
||||
url: this.sockPath + "/events",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
}
|
||||
};
|
||||
let requestStream = plugins.request(options,(err, res, body) => {
|
||||
let requestStream = plugins.request(options, (err, res, body) => {
|
||||
if (!err && res.statusCode == 200) {
|
||||
} else {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
};
|
||||
});
|
||||
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);
|
||||
}
|
||||
});
|
||||
let changeObservable = Observable.fromEvent(requestStream,"data");
|
||||
requestStream.on("end",()=> {
|
||||
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);
|
||||
});
|
||||
return changeObservable;
|
||||
}
|
||||
request(methodArg:string,routeArg:string,queryArg:string = "", dataArg = {}){
|
||||
request(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
||||
let done = plugins.q.defer();
|
||||
let jsonArg:string = JSON.stringify(dataArg);
|
||||
let suffix:string = "";
|
||||
if(methodArg == "GET") suffix = "/json";
|
||||
let jsonArg: string = JSON.stringify(dataArg);
|
||||
let suffix: string = "";
|
||||
if (methodArg == "GET") suffix = "/json";
|
||||
let options = {
|
||||
method:methodArg,
|
||||
url:this.sockPath + routeArg + suffix + queryArg,
|
||||
headers:{
|
||||
"Content-Type":"application/json",
|
||||
"Host":"docker.sock"
|
||||
method: methodArg,
|
||||
url: this.sockPath + routeArg + suffix + queryArg,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
},
|
||||
body:jsonArg
|
||||
body: jsonArg
|
||||
};
|
||||
//console.log(options);
|
||||
plugins.request(options,(err, res, body) => {
|
||||
plugins.request(options, (err, res, body) => {
|
||||
if (!err && res.statusCode == 200) {
|
||||
var responseObj = JSON.parse(body);
|
||||
done.resolve(responseObj);
|
||||
@ -175,20 +178,20 @@ export class Dockersock {
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
requestStream(methodArg:string,routeArg:string,queryArg:string = "", dataArg = {}){
|
||||
requestStream(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
||||
let done = plugins.q.defer();
|
||||
let jsonArg:string = JSON.stringify(dataArg);
|
||||
let suffix:string = "";
|
||||
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"
|
||||
method: methodArg,
|
||||
url: this.sockPath + routeArg + suffix + queryArg,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
},
|
||||
body:jsonArg
|
||||
body: jsonArg
|
||||
};
|
||||
let requestStream = plugins.request(options,(err, res, body) => {
|
||||
let requestStream = plugins.request(options, (err, res, body) => {
|
||||
if (!err && res.statusCode == 200) {
|
||||
done.resolve();
|
||||
} else {
|
||||
@ -197,19 +200,29 @@ export class Dockersock {
|
||||
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) => {
|
||||
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) => {
|
||||
let status;
|
||||
status = JSON.parse(data.toString()).status;
|
||||
plugins.beautylog.logReduced(status);
|
||||
});
|
||||
requestStream.on("end", () => {
|
||||
this.requestObjectmap.remove(requestStream);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
};
|
||||
endRequests() {
|
||||
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
|
||||
itemArg.emit("end");
|
||||
});
|
||||
this.requestObjectmap.wipe();
|
||||
};
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import "typings-global";
|
||||
export import beautylog = require("beautylog");
|
||||
export import lik = require("lik");
|
||||
export import q = require("q");
|
||||
export import request = require("request");
|
||||
export import rxjs = require("rxjs");
|
Reference in New Issue
Block a user