Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
0ef098e9c8 | |||
d56350ff28 | |||
a4dc4e7950 | |||
424e911804 | |||
b5c4727bae | |||
b6f3fbf8a9 | |||
7241e7a8fd | |||
ae37148ece | |||
65c37bdd6f | |||
6acbe30e2e | |||
eb6f7889d0 | |||
e39da5fee9 | |||
b07628bb0b | |||
5815f9b202 | |||
846ea9997e | |||
de54db33ad |
@ -39,6 +39,8 @@ snyk:
|
|||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
|
services:
|
||||||
|
- docker:18-dind
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
@ -48,7 +50,7 @@ testLTS:
|
|||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- priv
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
|
678
package-lock.json
generated
678
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.45",
|
"version": "1.0.53",
|
||||||
"description": "easy communication with docker remote api from node, TypeScript ready",
|
"description": "easy communication with docker remote api from node, TypeScript ready",
|
||||||
"private": false,
|
"private": false,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -27,19 +27,20 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^3.0.10",
|
"@pushrocks/lik": "^3.0.11",
|
||||||
"@pushrocks/smartlog": "^2.0.19",
|
"@pushrocks/smartlog": "^2.0.19",
|
||||||
|
"@pushrocks/smartnetwork": "^1.1.14",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@pushrocks/smartrequest": "^1.1.16",
|
"@pushrocks/smartrequest": "^1.1.26",
|
||||||
"rxjs": "^6.5.2"
|
"rxjs": "^6.5.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"@pushrocks/tapbundle": "^3.0.11",
|
"@pushrocks/tapbundle": "^3.0.13",
|
||||||
"@types/node": "^12.7.1",
|
"@types/node": "^12.7.4",
|
||||||
"tslint": "^5.18.0",
|
"tslint": "^5.19.0",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
15
test/test.ts
15
test/test.ts
@ -5,10 +5,14 @@ import { DockerService } from '../ts/index';
|
|||||||
let testDockerHost: docker.DockerHost;
|
let testDockerHost: docker.DockerHost;
|
||||||
|
|
||||||
tap.test('should create a new Dockersock instance', async () => {
|
tap.test('should create a new Dockersock instance', async () => {
|
||||||
testDockerHost = new docker.DockerHost();
|
testDockerHost = new docker.DockerHost('http://unix:/var/run/docker.sock:');
|
||||||
return expect(testDockerHost).to.be.instanceof(docker.DockerHost);
|
return expect(testDockerHost).to.be.instanceof(docker.DockerHost);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should create a docker swarm', async () => {
|
||||||
|
await testDockerHost.activateSwarm();
|
||||||
|
});
|
||||||
|
|
||||||
// Containers
|
// Containers
|
||||||
tap.test('should list containers', async () => {
|
tap.test('should list containers', async () => {
|
||||||
const containers = await testDockerHost.getContainers();
|
const containers = await testDockerHost.getContainers();
|
||||||
@ -59,7 +63,7 @@ tap.test('should activate swarm mode', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should list all services', async tools => {
|
tap.test('should list all services', async tools => {
|
||||||
const services = await docker.DockerService.getServices(testDockerHost);
|
const services = await testDockerHost.getServices();
|
||||||
console.log(services);
|
console.log(services);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -67,14 +71,17 @@ tap.test('should create a service', async () => {
|
|||||||
const testNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, {
|
const testNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, {
|
||||||
Name: 'testNetwork'
|
Name: 'testNetwork'
|
||||||
});
|
});
|
||||||
await DockerService.createService(testDockerHost, {
|
const testService = await DockerService.createService(testDockerHost, {
|
||||||
Image: 'nginx:latest',
|
Image: 'nginx:latest',
|
||||||
Labels: {
|
Labels: {
|
||||||
'testlabel': 'hi'
|
'testlabel': 'hi'
|
||||||
},
|
},
|
||||||
Name: 'testService',
|
Name: 'testService',
|
||||||
networks: [testNetwork]
|
networks: [testNetwork],
|
||||||
|
networkAlias: 'testService'
|
||||||
});
|
});
|
||||||
|
await testService.remove();
|
||||||
|
await testNetwork.remove();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import * as plugins from './docker.plugins';
|
import * as plugins from './docker.plugins';
|
||||||
import { DockerContainer } from './docker.classes.container';
|
import { DockerContainer } from './docker.classes.container';
|
||||||
import { DockerNetwork } from './docker.classes.network';
|
import { DockerNetwork } from './docker.classes.network';
|
||||||
|
import { DockerService } from './docker.classes.service';
|
||||||
|
|
||||||
export class DockerHost {
|
export class DockerHost {
|
||||||
/**
|
/**
|
||||||
@ -12,8 +13,16 @@ export class DockerHost {
|
|||||||
* the constructor to instantiate a new docker sock instance
|
* the constructor to instantiate a new docker sock instance
|
||||||
* @param pathArg
|
* @param pathArg
|
||||||
*/
|
*/
|
||||||
constructor(pathArg: string = 'http://unix:/var/run/docker.sock:') {
|
constructor(pathArg?: string) {
|
||||||
this.socketPath = pathArg;
|
let pathToUse: string;
|
||||||
|
if (pathArg) {
|
||||||
|
pathToUse = pathArg;
|
||||||
|
} else if (process.env.CI) {
|
||||||
|
pathToUse = 'http://docker:2375/';
|
||||||
|
} else {
|
||||||
|
pathToUse = 'http://unix:/var/run/docker.sock:';
|
||||||
|
}
|
||||||
|
this.socketPath = pathToUse;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,9 +30,12 @@ export class DockerHost {
|
|||||||
* @param userArg
|
* @param userArg
|
||||||
* @param passArg
|
* @param passArg
|
||||||
*/
|
*/
|
||||||
public async auth(registryArg: string, userArg: string, passArg: string) {
|
public async auth(registryUrl: string, userArg: string, passArg: string) {
|
||||||
// TODO: implement Docker Registry authentication
|
const response = await this.request('POST', '/auth', {
|
||||||
await this.request('POST', '');
|
serveraddress: registryUrl,
|
||||||
|
username: userArg,
|
||||||
|
password: passArg,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,6 +53,14 @@ export class DockerHost {
|
|||||||
return containerArray;
|
return containerArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all services
|
||||||
|
*/
|
||||||
|
public async getServices() {
|
||||||
|
const serviceArray = await DockerService.getServices(this);
|
||||||
|
return serviceArray;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ -66,9 +86,21 @@ export class DockerHost {
|
|||||||
* activates docker swarm
|
* activates docker swarm
|
||||||
*/
|
*/
|
||||||
public async activateSwarm(addvertisementIpArg?: string) {
|
public async activateSwarm(addvertisementIpArg?: string) {
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const response = await this.request('POST', '/swarm/init', {
|
const response = await this.request('POST', '/swarm/init', {
|
||||||
ListenAddr: '0.0.0.0:2377',
|
ListenAddr: '0.0.0.0:2377',
|
||||||
AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377` : undefined,
|
AdvertiseAddr: addvertisementIp,
|
||||||
DataPathPort: 4789,
|
DataPathPort: 4789,
|
||||||
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
|
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
|
||||||
SubnetSize: 24,
|
SubnetSize: 24,
|
||||||
@ -92,7 +124,8 @@ export class DockerHost {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Host: 'docker.sock'
|
Host: 'docker.sock'
|
||||||
},
|
},
|
||||||
requestBody: dataArg
|
requestBody: dataArg,
|
||||||
|
keepAlive: false
|
||||||
});
|
});
|
||||||
if (response.statusCode !== 200) {
|
if (response.statusCode !== 200) {
|
||||||
console.log(response.body);
|
console.log(response.body);
|
||||||
@ -107,10 +140,11 @@ export class DockerHost {
|
|||||||
{
|
{
|
||||||
method: methodArg,
|
method: methodArg,
|
||||||
headers: {
|
headers: {
|
||||||
// 'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Host: 'docker.sock'
|
Host: 'docker.sock'
|
||||||
},
|
},
|
||||||
requestBody: null
|
requestBody: null,
|
||||||
|
keepAlive: false
|
||||||
},
|
},
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
@ -112,14 +112,6 @@ export class DockerImage {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns a boolean wether the image has a upstream image
|
|
||||||
*/
|
|
||||||
public isUpstreamImage(): boolean {
|
|
||||||
// TODO: implement isUpastreamImage
|
|
||||||
return this.RepoTags.length > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public tagImage(newTag) {}
|
public tagImage(newTag) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,7 +8,9 @@ export class DockerNetwork {
|
|||||||
const dockerNetworks: DockerNetwork[] = [];
|
const dockerNetworks: DockerNetwork[] = [];
|
||||||
const response = await dockerHost.request('GET', '/networks');
|
const response = await dockerHost.request('GET', '/networks');
|
||||||
for (const networkObject of response.body) {
|
for (const networkObject of response.body) {
|
||||||
dockerNetworks.push(new DockerNetwork(dockerHost, networkObject));
|
const dockerNetwork = new DockerNetwork(dockerHost);
|
||||||
|
Object.assign(dockerNetwork, networkObject);
|
||||||
|
dockerNetworks.push(dockerNetwork);
|
||||||
}
|
}
|
||||||
return dockerNetworks;
|
return dockerNetworks;
|
||||||
}
|
}
|
||||||
@ -78,11 +80,8 @@ export class DockerNetwork {
|
|||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(dockerHostArg: DockerHost, dockerNetworkObjectArg: any) {
|
constructor(dockerHostArg: DockerHost) {
|
||||||
this.dockerHost = dockerHostArg;
|
this.dockerHost = dockerHostArg;
|
||||||
Object.keys(dockerNetworkObjectArg).forEach(keyArg => {
|
|
||||||
this[keyArg] = dockerNetworkObjectArg[keyArg];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,18 +10,28 @@ export class DockerService {
|
|||||||
const services: DockerService[] = [];
|
const services: DockerService[] = [];
|
||||||
const response = await dockerHost.request('GET', '/services');
|
const response = await dockerHost.request('GET', '/services');
|
||||||
for (const serviceObject of response.body) {
|
for (const serviceObject of response.body) {
|
||||||
services.push(new DockerService(dockerHost, serviceObject));
|
const dockerService = new DockerService(dockerHost);
|
||||||
|
Object.assign(dockerService, serviceObject);
|
||||||
|
services.push(dockerService);
|
||||||
}
|
}
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async getServiceByName(dockerHost: DockerHost, networkName: string): Promise<DockerService> {
|
||||||
|
const allServices = await DockerService.getServices(dockerHost);
|
||||||
|
const wantedService = allServices.find(service => {
|
||||||
|
return service.Spec.Name === networkName;
|
||||||
|
});
|
||||||
|
return wantedService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a service
|
* creates a service
|
||||||
*/
|
*/
|
||||||
public static async createService(
|
public static async createService(
|
||||||
dockerHost: DockerHost,
|
dockerHost: DockerHost,
|
||||||
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
||||||
) {
|
): Promise<DockerService> {
|
||||||
// lets get the image
|
// lets get the image
|
||||||
plugins.smartlog.defaultLogger.log('info', `downloading image for service ${serviceCreationDescriptor.Name}`);
|
plugins.smartlog.defaultLogger.log('info', `downloading image for service ${serviceCreationDescriptor.Name}`);
|
||||||
const serviceImage = await DockerImage.createFromRegistry(dockerHost, {
|
const serviceImage = await DockerImage.createFromRegistry(dockerHost, {
|
||||||
@ -32,11 +42,11 @@ export class DockerService {
|
|||||||
for (const network of serviceCreationDescriptor.networks) {
|
for (const network of serviceCreationDescriptor.networks) {
|
||||||
networkArray.push({
|
networkArray.push({
|
||||||
Target: network.Name,
|
Target: network.Name,
|
||||||
Aliases: []
|
Aliases: [serviceCreationDescriptor.networkAlias]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerHost.request('POST', '/services/create', {
|
const response = await dockerHost.request('POST', '/services/create', {
|
||||||
Name: serviceCreationDescriptor.Name,
|
Name: serviceCreationDescriptor.Name,
|
||||||
TaskTemplate: {
|
TaskTemplate: {
|
||||||
ContainerSpec: {
|
ContainerSpec: {
|
||||||
@ -47,13 +57,37 @@ export class DockerService {
|
|||||||
Labels: serviceCreationDescriptor.Labels,
|
Labels: serviceCreationDescriptor.Labels,
|
||||||
Networks: networkArray
|
Networks: networkArray
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const createdService = await DockerService.getServiceByName(dockerHost, serviceCreationDescriptor.Name);
|
||||||
|
return createdService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// INSTANCE
|
// INSTANCE
|
||||||
public dockerHost: DockerHost;
|
public dockerHostRef: DockerHost;
|
||||||
|
|
||||||
constructor(dockerHostArg: DockerHost, serviceObject) {
|
public ID: string;
|
||||||
this.dockerHost = dockerHostArg;
|
public Version: { Index: number };
|
||||||
Object.assign(this, serviceObject);
|
public CreatedAt: string;
|
||||||
|
public UpdatedAt: string;
|
||||||
|
public Spec: {
|
||||||
|
Name: string;
|
||||||
|
Labels: [any]; // ZBD
|
||||||
|
TaskTemplate: [any],
|
||||||
|
Mode: [any];
|
||||||
|
Networks: [any[]]
|
||||||
|
};
|
||||||
|
public Endpoint: { Spec: {}, VirtualIPs: [any[]] };
|
||||||
|
|
||||||
|
|
||||||
|
constructor(dockerHostArg: DockerHost) {
|
||||||
|
this.dockerHostRef = dockerHostArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async update() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async remove() {
|
||||||
|
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
import * as lik from '@pushrocks/lik';
|
import * as lik from '@pushrocks/lik';
|
||||||
import * as smartlog from '@pushrocks/smartlog';
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
|
import * as smartnetwork from '@pushrocks/smartnetwork';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
|
||||||
smartlog.defaultLogger.enableConsole();
|
smartlog.defaultLogger.enableConsole();
|
||||||
|
|
||||||
export { lik, smartlog, smartpromise, smartrequest };
|
export { lik, smartlog, smartnetwork, smartpromise, smartrequest };
|
||||||
|
|
||||||
// third party
|
// third party
|
||||||
import * as rxjs from 'rxjs';
|
import * as rxjs from 'rxjs';
|
||||||
|
@ -6,4 +6,5 @@ export interface IServiceCreationDescriptor {
|
|||||||
Image: string;
|
Image: string;
|
||||||
Labels: interfaces.TLabels;
|
Labels: interfaces.TLabels;
|
||||||
networks: DockerNetwork[];
|
networks: DockerNetwork[];
|
||||||
|
networkAlias: string;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user