2018-07-17 06:39:37 +00:00
|
|
|
import * as plugins from './dockersock.plugins';
|
|
|
|
import { DockerContainer } from './docker.classes.container';
|
2018-07-16 21:52:50 +00:00
|
|
|
|
|
|
|
export class DockerHost {
|
|
|
|
/**
|
|
|
|
* the path where the docker sock can be found
|
|
|
|
*/
|
|
|
|
sockPath: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* keeping track of currently active requests to safely end this module at any time
|
|
|
|
*/
|
|
|
|
requestObjectmap = new plugins.lik.Objectmap<any>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor to instantiate a new docker sock instance
|
|
|
|
* @param pathArg
|
|
|
|
*/
|
2018-07-17 06:39:37 +00:00
|
|
|
constructor(pathArg: string = 'http://unix:/var/run/docker.sock:') {
|
2018-07-16 21:52:50 +00:00
|
|
|
this.sockPath = pathArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* authenticate against a registry
|
|
|
|
* @param userArg
|
|
|
|
* @param passArg
|
|
|
|
*/
|
|
|
|
auth(registryArg: string, userArg: string, passArg: string) {
|
|
|
|
let done = plugins.smartpromise.defer();
|
2018-07-17 06:39:37 +00:00
|
|
|
this.request('POST', '');
|
2018-07-16 21:52:50 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
async getContainers() {
|
|
|
|
const containerArray = await DockerContainer.getContainers(this);
|
|
|
|
return containerArray;
|
2019-01-09 23:28:12 +00:00
|
|
|
}
|
2019-01-09 23:24:35 +00:00
|
|
|
|
|
|
|
async getEventObservable(): Promise<plugins.rxjs.Observable<any>> {
|
|
|
|
const response = await this.requestStreaming('GET', '/events');
|
|
|
|
return plugins.rxjs.Observable.create(observer => {
|
|
|
|
response.on('data', data => {
|
2019-01-18 01:42:13 +00:00
|
|
|
const eventString = data.toString();
|
|
|
|
try {
|
|
|
|
const eventObject = JSON.parse(eventString);
|
|
|
|
observer.next(eventObject);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2019-01-09 23:24:35 +00:00
|
|
|
});
|
|
|
|
return () => {
|
|
|
|
response.emit('end');
|
|
|
|
};
|
|
|
|
});
|
2018-07-16 21:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fire a request
|
|
|
|
*/
|
|
|
|
async request(methodArg: string, routeArg: string, dataArg = {}) {
|
2018-07-17 06:39:37 +00:00
|
|
|
const requestUrl = `${this.sockPath}${routeArg}`;
|
2018-07-16 21:52:50 +00:00
|
|
|
const response = await plugins.smartrequest.request(requestUrl, {
|
|
|
|
method: methodArg,
|
|
|
|
headers: {
|
2018-07-17 06:39:37 +00:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Host: 'docker.sock'
|
2018-07-16 21:52:50 +00:00
|
|
|
},
|
|
|
|
requestBody: dataArg
|
|
|
|
});
|
2018-07-17 06:39:37 +00:00
|
|
|
return response;
|
2018-07-16 21:52:50 +00:00
|
|
|
}
|
2019-01-09 23:24:35 +00:00
|
|
|
|
|
|
|
async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
|
|
|
|
const requestUrl = `${this.sockPath}${routeArg}`;
|
|
|
|
const response = await plugins.smartrequest.request(
|
2019-01-09 23:28:12 +00:00
|
|
|
requestUrl,
|
|
|
|
{
|
|
|
|
method: methodArg,
|
|
|
|
headers: {
|
|
|
|
// 'Content-Type': 'application/json',
|
|
|
|
Host: 'docker.sock'
|
|
|
|
},
|
|
|
|
requestBody: null
|
2019-01-09 23:24:35 +00:00
|
|
|
},
|
2019-01-09 23:28:12 +00:00
|
|
|
true
|
2019-01-09 23:24:35 +00:00
|
|
|
);
|
|
|
|
console.log(response.statusCode);
|
|
|
|
console.log(response.body);
|
|
|
|
return response;
|
|
|
|
}
|
2018-07-16 21:52:50 +00:00
|
|
|
}
|