docker/ts/docker.classes.host.ts

97 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-07-17 06:39:37 +00:00
import * as plugins from './dockersock.plugins';
import { DockerContainer } from './docker.classes.container';
2019-08-14 21:21:54 +00:00
import { DockerNetwork } from './docker.classes.network';
2018-07-16 21:52:50 +00:00
export class DockerHost {
/**
* the path where the docker sock can be found
*/
2019-08-14 21:21:54 +00:00
public socketPath: string;
2018-07-16 21:52:50 +00:00
/**
* 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:') {
2019-08-14 21:21:54 +00:00
this.socketPath = pathArg;
2018-07-16 21:52:50 +00:00
}
/**
* authenticate against a registry
* @param userArg
* @param passArg
*/
2019-08-14 21:21:54 +00:00
public async auth(registryArg: string, userArg: string, passArg: string) {
// TODO: implement Docker Registry authentication
await this.request('POST', '');
}
/**
* gets all networks
*/
public async getNetworks() {
DockerNetwork.getNetworks(this);
2018-07-16 21:52:50 +00:00
}
/**
2019-08-14 21:21:54 +00:00
* gets all containers
2018-07-16 21:52:50 +00:00
*/
2019-08-14 21:21:54 +00:00
public async getContainers() {
2018-07-16 21:52:50 +00:00
const containerArray = await DockerContainer.getContainers(this);
return containerArray;
2019-01-09 23:28:12 +00:00
}
2019-01-09 23:24:35 +00:00
2019-08-14 21:21:54 +00:00
public async getEventObservable(): Promise<plugins.rxjs.Observable<any>> {
2019-01-09 23:24:35 +00:00
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
*/
2019-08-14 21:21:54 +00:00
public async request(methodArg: string, routeArg: string, dataArg = {}) {
const requestUrl = `${this.socketPath}${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
2019-08-14 21:21:54 +00:00
public async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
const requestUrl = `${this.socketPath}${routeArg}`;
2019-01-09 23:24:35 +00:00
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
}