update
This commit is contained in:
parent
91d7f583e2
commit
dd35e5d7d2
20
dist/dockersock.classes.dockersock.d.ts
vendored
20
dist/dockersock.classes.dockersock.d.ts
vendored
@ -1,4 +1,3 @@
|
||||
/// <reference types="request" />
|
||||
/// <reference types="q" />
|
||||
import "typings-global";
|
||||
import * as plugins from "./dockersock.plugins";
|
||||
@ -6,7 +5,7 @@ import { Observable } from "rxjs";
|
||||
import { Objectmap } from 'lik';
|
||||
export declare class Dockersock {
|
||||
sockPath: string;
|
||||
requestObjectmap: Objectmap<plugins.request.Request>;
|
||||
requestObjectmap: Objectmap<any>;
|
||||
constructor(pathArg?: string);
|
||||
auth(userArg: string, passArg: string): plugins.q.Promise<{}>;
|
||||
listContainers(): plugins.q.Promise<{}>;
|
||||
@ -27,7 +26,24 @@ export declare class Dockersock {
|
||||
* gets you an observable that reports changes in the docker infrastructure
|
||||
*/
|
||||
getChangeObservable(): Observable<{}>;
|
||||
/**
|
||||
* fire a request
|
||||
* @param methodArg
|
||||
* @param routeArg
|
||||
* @param queryArg
|
||||
* @param dataArg
|
||||
*/
|
||||
request(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
|
||||
/**
|
||||
* fire a streaming request
|
||||
* @param methodArg
|
||||
* @param routeArg
|
||||
* @param queryArg
|
||||
* @param dataArg
|
||||
*/
|
||||
requestStream(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
|
||||
/**
|
||||
* end all currently streaming requests
|
||||
*/
|
||||
endRequests(): void;
|
||||
}
|
||||
|
19
dist/dockersock.classes.dockersock.js
vendored
19
dist/dockersock.classes.dockersock.js
vendored
File diff suppressed because one or more lines are too long
@ -6,7 +6,7 @@
|
||||
"npmdocker":{
|
||||
"baseImage":"hosttoday/ht-docker-node:npmci",
|
||||
"command":"(npmci command yarn global add npmts) && npmts",
|
||||
"dockerSock":"true"
|
||||
"dockerSock": true
|
||||
},
|
||||
"npmci": {
|
||||
"globalNpmTools": [
|
||||
|
13
package.json
13
package.json
@ -5,7 +5,7 @@
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "npmdocker"
|
||||
"test": "(npmts --notest && npmdocker)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -26,16 +26,15 @@
|
||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||
"dependencies": {
|
||||
"@types/q": "1.x.x",
|
||||
"@types/request": "0.x.x",
|
||||
"beautylog": "^6.1.5",
|
||||
"lik": "^1.0.27",
|
||||
"beautylog": "^6.1.10",
|
||||
"lik": "^1.0.38",
|
||||
"q": "^1.5.0",
|
||||
"request": "^2.81.0",
|
||||
"rxjs": "^5.2.0",
|
||||
"typings-global": "^1.0.14"
|
||||
"rxjs": "^5.4.2",
|
||||
"typings-global": "^1.0.19"
|
||||
},
|
||||
"devDependencies": {
|
||||
"observable-to-promise": "^0.5.0",
|
||||
"tapbundle": "^1.0.5"
|
||||
"tapbundle": "^1.1.1"
|
||||
}
|
||||
}
|
||||
|
26
test/test.ts
26
test/test.ts
@ -8,29 +8,27 @@ let testDockersock: Dockersock;
|
||||
tap.test("should create a new Dockersock instance", async () => {
|
||||
testDockersock = new Dockersock();
|
||||
return expect(testDockersock).to.be.instanceof(Dockersock);
|
||||
}).catch(tap.threw);
|
||||
})
|
||||
|
||||
tap.test("should list containers", async () => {
|
||||
await testDockersock.listContainers()
|
||||
.then(async (dataArg) => {
|
||||
console.log(dataArg);
|
||||
});
|
||||
}).catch(tap.threw);
|
||||
let data = await testDockersock.listContainers()
|
||||
console.log(data)
|
||||
})
|
||||
|
||||
tap.test("should list detailed containers", async () => {
|
||||
await testDockersock.listContainersDetailed()
|
||||
.then(async (dataArg) => {
|
||||
console.log(dataArg);
|
||||
});
|
||||
}).catch(tap.threw);
|
||||
let data = await testDockersock.listContainersDetailed()
|
||||
console.log(data)
|
||||
})
|
||||
|
||||
tap.test("should pull an image from imagetag", async () => {
|
||||
await testDockersock.pullImage("hosttoday/ht-docker-node:npmci")
|
||||
}).catch(tap.threw);
|
||||
})
|
||||
|
||||
/*tap.test("should return a change Objservable", async () => {
|
||||
tap.test("should return a change Objservable", async () => {
|
||||
let myObservable = testDockersock.getChangeObservable();
|
||||
testDockersock.endRequests();
|
||||
let testPromise = observableToPromise(myObservable)
|
||||
return await expect(testPromise).to.eventually.be.fulfilled
|
||||
}).catch(tap.threw);*/
|
||||
})
|
||||
|
||||
tap.start()
|
||||
|
@ -3,241 +3,260 @@ import * as plugins from "./dockersock.plugins";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
// interfaces
|
||||
import { Objectmap } from 'lik'
|
||||
import { Objectmap } from 'lik'
|
||||
|
||||
export class Dockersock {
|
||||
sockPath: string;
|
||||
requestObjectmap = new plugins.lik.Objectmap<plugins.request.Request>();
|
||||
constructor(pathArg: string = "http://unix:/var/run/docker.sock:") {
|
||||
this.sockPath = pathArg;
|
||||
}
|
||||
sockPath: string;
|
||||
requestObjectmap = new plugins.lik.Objectmap<plugins.request.Request>();
|
||||
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;
|
||||
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);
|
||||
}
|
||||
};
|
||||
// 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;
|
||||
let makeDetailed = () => {
|
||||
if (typeof dataArg[ recursiveCounter ] != "undefined") {
|
||||
this.request("GET", "/containers/" + dataArg[ recursiveCounter ].Id)
|
||||
.then((dataArg2) => {
|
||||
detailedDataObject.push(dataArg2);
|
||||
recursiveCounter++;
|
||||
// recursive call
|
||||
makeDetailed();
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
listContainersRunning() {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
}
|
||||
listContainersStopped() {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
}
|
||||
listImages() {
|
||||
return this.request("GET", "/images", "?all=true");
|
||||
}
|
||||
listImagesDangling() {
|
||||
return this.request("GET", "/images", "?dangling=true");
|
||||
}
|
||||
pullImage(imageLabelArg: string) {
|
||||
let imageLabel = encodeURI(imageLabelArg);
|
||||
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
|
||||
};
|
||||
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;
|
||||
};
|
||||
getContainerId() {
|
||||
|
||||
};
|
||||
startContainer(containerNameArg) {
|
||||
return this.request("POST", "/containers/" + containerNameArg + "/start");
|
||||
};
|
||||
stopContainer(containerNameArg) {
|
||||
return this.request("POST", "/containers/" + containerNameArg + "/stop");
|
||||
};
|
||||
removeContainer(containerNameArg) {
|
||||
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
|
||||
};
|
||||
clean() {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
};
|
||||
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", () => {
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* gets you an observable that reports changes in the docker infrastructure
|
||||
*/
|
||||
getChangeObservable() {
|
||||
let options = {
|
||||
method: "GET",
|
||||
url: this.sockPath + "/events",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
}
|
||||
});
|
||||
} else {
|
||||
done.resolve(detailedDataObject);
|
||||
}
|
||||
};
|
||||
let requestStream = plugins.request(options, (err, res, body) => {
|
||||
if (!err && res.statusCode == 200) {
|
||||
} else {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
};
|
||||
});
|
||||
let incomingMessage
|
||||
requestStream.on("response", (response) => {
|
||||
incomingMessage = response
|
||||
this.requestObjectmap.add(incomingMessage);
|
||||
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(incomingMessage);
|
||||
});
|
||||
return changeObservable;
|
||||
makeDetailed();
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
listContainersRunning () {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
}
|
||||
listContainersStopped () {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
}
|
||||
listImages () {
|
||||
return this.request("GET", "/images", "?all=true");
|
||||
}
|
||||
listImagesDangling () {
|
||||
return this.request("GET", "/images", "?dangling=true");
|
||||
}
|
||||
pullImage (imageLabelArg: string) {
|
||||
let imageLabel = encodeURI(imageLabelArg);
|
||||
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
|
||||
};
|
||||
createContainer (optionsArg, pullFirstArg: boolean = true) {
|
||||
let done = plugins.q.defer();
|
||||
let create = () => {
|
||||
return this.request("POST", "/containers/create", "", optionsArg);
|
||||
}
|
||||
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 options = {
|
||||
method: methodArg,
|
||||
url: this.sockPath + routeArg + suffix + queryArg,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
},
|
||||
body: jsonArg
|
||||
};
|
||||
//console.log(options);
|
||||
plugins.request(options, (err, res, body) => {
|
||||
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;
|
||||
if (pullFirstArg) {
|
||||
this.pullImage(optionsArg.Image)
|
||||
.then(create)
|
||||
.then(done.resolve);
|
||||
} else {
|
||||
create()
|
||||
.then(done.resolve)
|
||||
}
|
||||
requestStream(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
||||
let done = plugins.q.defer();
|
||||
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) {
|
||||
done.resolve();
|
||||
} else {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
done.reject(err);
|
||||
};
|
||||
return done.promise;
|
||||
};
|
||||
getContainerId () {
|
||||
|
||||
};
|
||||
startContainer (containerNameArg) {
|
||||
return this.request("POST", "/containers/" + containerNameArg + "/start");
|
||||
};
|
||||
stopContainer (containerNameArg) {
|
||||
return this.request("POST", "/containers/" + containerNameArg + "/stop");
|
||||
};
|
||||
removeContainer (containerNameArg) {
|
||||
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
|
||||
};
|
||||
clean () {
|
||||
let done = plugins.q.defer();
|
||||
return done.promise;
|
||||
};
|
||||
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();
|
||||
});
|
||||
let incomingMessage
|
||||
requestStream.on("response", (response) => {
|
||||
incomingMessage = response
|
||||
this.requestObjectmap.add(incomingMessage);
|
||||
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(incomingMessage);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
requestStream.on("end", () => {
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* gets you an observable that reports changes in the docker infrastructure
|
||||
*/
|
||||
getChangeObservable () {
|
||||
let options = {
|
||||
method: "GET",
|
||||
url: this.sockPath + "/events",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
}
|
||||
};
|
||||
let requestStream = plugins.request(options, (err, res, body) => {
|
||||
if (!err && res.statusCode == 200) {
|
||||
} else {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
};
|
||||
});
|
||||
let incomingMessage
|
||||
requestStream.on("response", (response) => {
|
||||
incomingMessage = response
|
||||
this.requestObjectmap.add(incomingMessage);
|
||||
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(incomingMessage);
|
||||
});
|
||||
return changeObservable;
|
||||
}
|
||||
|
||||
|
||||
endRequests() {
|
||||
setTimeout(() => {
|
||||
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
|
||||
itemArg.emit("end");
|
||||
});
|
||||
this.requestObjectmap.wipe();
|
||||
}, 5000);
|
||||
/**
|
||||
* fire a request
|
||||
* @param methodArg
|
||||
* @param routeArg
|
||||
* @param queryArg
|
||||
* @param 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 options = {
|
||||
method: methodArg,
|
||||
url: this.sockPath + routeArg + suffix + queryArg,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "docker.sock"
|
||||
},
|
||||
body: jsonArg
|
||||
};
|
||||
//console.log(options);
|
||||
plugins.request(options, (err, res, body) => {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* fire a streaming request
|
||||
* @param methodArg
|
||||
* @param routeArg
|
||||
* @param queryArg
|
||||
* @param dataArg
|
||||
*/
|
||||
requestStream (methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
||||
let done = plugins.q.defer();
|
||||
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) {
|
||||
done.resolve();
|
||||
} else {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
done.reject(err);
|
||||
};
|
||||
});
|
||||
let incomingMessage
|
||||
requestStream.on("response", (response) => {
|
||||
incomingMessage = response
|
||||
this.requestObjectmap.add(incomingMessage);
|
||||
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(incomingMessage);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* end all currently streaming requests
|
||||
*/
|
||||
endRequests () {
|
||||
setTimeout(() => {
|
||||
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
|
||||
itemArg.emit("end");
|
||||
});
|
||||
this.requestObjectmap.wipe();
|
||||
}, 5000);
|
||||
};
|
||||
}
|
@ -3,4 +3,4 @@ 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");
|
||||
export import rxjs = require("rxjs");
|
||||
|
Loading…
Reference in New Issue
Block a user