docker/ts/docker.classes.host.ts

122 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-08-15 16:50:13 +00:00
import * as plugins from './docker.plugins';
2018-07-17 06:39:37 +00:00
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() {
2019-08-15 16:50:13 +00:00
return await 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-15 16:50:13 +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
}
2019-08-15 16:50:13 +00:00
/**
* activates docker swarm
*/
2019-08-16 10:48:40 +00:00
public async activateSwarm(addvertisementIpArg?: string) {
2019-08-15 16:50:13 +00:00
const response = await this.request('POST', '/swarm/init', {
ListenAddr: '0.0.0.0:2377',
2019-08-16 10:48:56 +00:00
AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377` : undefined,
2019-08-15 16:50:13 +00:00
DataPathPort: 4789,
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
SubnetSize: 24,
ForceNewCluster: false
});
if (response.statusCode === 200) {
plugins.smartlog.defaultLogger.log('info', 'created Swam succesfully');
} else {
plugins.smartlog.defaultLogger.log('error', 'could not initiate swarm');
}
}
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
});
2019-08-15 16:50:13 +00:00
if (response.statusCode !== 200) {
console.log(response.body);
}
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
}