docker/ts/docker.classes.host.ts

198 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-10-17 07:36:35 +00:00
import * as plugins from './docker.plugins.js';
import { DockerContainer } from './docker.classes.container.js';
import { DockerNetwork } from './docker.classes.network.js';
import { DockerService } from './docker.classes.service.js';
import { logger } from './docker.logging.js';
2024-02-02 15:54:07 +00:00
import path from 'path';
2018-07-16 21:52:50 +00:00
2019-09-13 20:09:35 +00:00
export interface IAuthData {
serveraddress: string;
username: string;
password: string;
}
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;
2019-09-13 15:54:17 +00:00
private registryToken: string = '';
2018-07-16 21:52:50 +00:00
/**
* the constructor to instantiate a new docker sock instance
* @param pathArg
*/
2019-08-16 19:21:30 +00:00
constructor(pathArg?: string) {
let pathToUse: string;
if (pathArg) {
pathToUse = pathArg;
2024-02-02 15:54:07 +00:00
} else if (process.env.DOCKER_HOST) {
pathToUse = process.env.DOCKER_HOST;
2019-08-16 19:21:30 +00:00
} else if (process.env.CI) {
2019-08-16 19:34:35 +00:00
pathToUse = 'http://docker:2375/';
2019-08-16 19:21:30 +00:00
} else {
pathToUse = 'http://unix:/var/run/docker.sock:';
}
2024-02-02 15:54:07 +00:00
if (pathToUse.startsWith('unix:///')) {
2024-02-02 15:55:51 +00:00
pathToUse = pathToUse.replace('unix://', 'http://unix:');
}
if (pathToUse.endsWith('.sock')) {
pathToUse = pathToUse.replace('.sock', '.sock:');
2024-02-02 15:54:07 +00:00
}
console.log(`using docker sock at ${pathToUse}`);
2019-08-16 19:21:30 +00:00
this.socketPath = pathToUse;
2018-07-16 21:52:50 +00:00
}
/**
* authenticate against a registry
* @param userArg
* @param passArg
*/
2019-09-13 20:09:35 +00:00
public async auth(authData: IAuthData) {
const response = await this.request('POST', '/auth', authData);
2019-09-13 15:54:17 +00:00
if (response.body.Status !== 'Login Succeeded') {
console.log(`Login failed with ${response.body.Status}`);
throw new Error(response.body.Status);
}
console.log(response.body.Status);
2022-10-17 09:00:42 +00:00
this.registryToken = plugins.smartstring.base64.encode(plugins.smartjson.stringify(authData));
2019-08-14 21:21:54 +00:00
}
2019-09-13 16:15:45 +00:00
/**
* gets the token from the .docker/config.json file for GitLab registry
*/
2019-09-13 16:16:21 +00:00
public async getGitlabComTokenFromDockerConfig() {
2019-09-13 16:15:45 +00:00
const dockerConfigPath = plugins.smartpath.get.home('~/.docker/config.json');
const configObject = plugins.smartfile.fs.toObjectSync(dockerConfigPath);
2019-09-13 20:09:35 +00:00
const gitlabAuthBase64 = configObject.auths['registry.gitlab.com'].auth;
const gitlabAuth: string = plugins.smartstring.base64.decode(gitlabAuthBase64);
const gitlabAuthArray = gitlabAuth.split(':');
await this.auth({
username: gitlabAuthArray[0],
password: gitlabAuthArray[1],
2020-09-30 16:35:24 +00:00
serveraddress: 'registry.gitlab.com',
2019-09-18 15:29:43 +00:00
});
2019-09-13 16:15:45 +00:00
}
2019-08-14 21:21:54 +00:00
/**
* 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-09-13 16:15:45 +00:00
/**
2019-09-13 16:20:12 +00:00
*
2019-09-13 16:15:45 +00:00
*/
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-09-08 17:22:20 +00:00
/**
* gets all services
*/
public async getServices() {
const serviceArray = await DockerService.getServices(this);
return serviceArray;
}
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');
2020-09-30 16:35:24 +00:00
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-09-08 14:34:26 +00:00
// determine advertisement address
let addvertisementIp: string;
if (addvertisementIpArg) {
addvertisementIp = addvertisementIpArg;
} else {
const smartnetworkInstance = new plugins.smartnetwork.SmartNetwork();
const defaultGateway = await smartnetworkInstance.getDefaultGateway();
if (defaultGateway) {
addvertisementIp = defaultGateway.ipv4.address;
}
}
2019-08-15 16:50:13 +00:00
const response = await this.request('POST', '/swarm/init', {
ListenAddr: '0.0.0.0:2377',
2019-09-08 14:34:26 +00:00
AdvertiseAddr: addvertisementIp,
2019-08-15 16:50:13 +00:00
DataPathPort: 4789,
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
SubnetSize: 24,
2020-09-30 16:35:24 +00:00
ForceNewCluster: false,
2019-08-15 16:50:13 +00:00
});
if (response.statusCode === 200) {
2020-09-30 16:27:43 +00:00
logger.log('info', 'created Swam succesfully');
2019-08-15 16:50:13 +00:00
} else {
2020-09-30 16:27:43 +00:00
logger.log('error', 'could not initiate swarm');
2019-08-15 16:50:13 +00:00
}
}
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',
2019-09-13 15:54:17 +00:00
'X-Registry-Auth': this.registryToken,
2020-09-30 16:35:24 +00:00
Host: 'docker.sock',
2018-07-16 21:52:50 +00:00
},
2019-09-08 17:22:20 +00:00
requestBody: dataArg,
2020-09-30 16:35:24 +00:00
keepAlive: false,
2018-07-16 21:52:50 +00:00
});
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: {
2019-09-08 17:22:20 +00:00
'Content-Type': 'application/json',
2019-09-13 20:09:35 +00:00
'X-Registry-Auth': this.registryToken,
2020-09-30 16:35:24 +00:00
Host: 'docker.sock',
2019-01-09 23:28:12 +00:00
},
2019-09-08 17:22:20 +00:00
requestBody: null,
2020-09-30 16:35:24 +00:00
keepAlive: false,
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
}