docker/ts/docker.classes.host.ts

59 lines
1.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';
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;
}
/**
* 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
console.log(requestUrl);
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
}
}