From d56350ff28e84b2c16175237db17bcc553752720 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sun, 8 Sep 2019 19:22:20 +0200 Subject: [PATCH] fix(core): update --- package-lock.json | 6 ++--- package.json | 2 +- test/test.ts | 10 ++++---- ts/docker.classes.host.ts | 17 ++++++++++--- ts/docker.classes.network.ts | 9 ++++--- ts/docker.classes.service.ts | 46 +++++++++++++++++++++++++++++------- 6 files changed, 66 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index ea47110..7118fa5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -249,9 +249,9 @@ "integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ==" }, "@pushrocks/smartrequest": { - "version": "1.1.23", - "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.23.tgz", - "integrity": "sha512-Hws3YfzIE0b/E3aTkSugLskKWBq7e8HDXEN+RlRyTFONxW/XONKJFTw4mp3jk+puWpYGDoOTcP+Ua4jd19z9pA==", + "version": "1.1.26", + "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.26.tgz", + "integrity": "sha512-8tYY+N42qdSTN5x40m6Afqmjo8HwkuZW+iWdMwO8PFxhrymcx8kuf5rYcG0wVLGtBjDgKgVNidb8GgFEhG40Yw==", "requires": { "@pushrocks/smartpromise": "^3.0.2", "@types/form-data": "^2.2.1", diff --git a/package.json b/package.json index cce5f49..c9b27dd 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@pushrocks/smartlog": "^2.0.19", "@pushrocks/smartnetwork": "^1.1.14", "@pushrocks/smartpromise": "^3.0.2", - "@pushrocks/smartrequest": "^1.1.23", + "@pushrocks/smartrequest": "^1.1.26", "rxjs": "^6.5.3" }, "devDependencies": { diff --git a/test/test.ts b/test/test.ts index f11802e..201f463 100644 --- a/test/test.ts +++ b/test/test.ts @@ -5,12 +5,12 @@ import { DockerService } from '../ts/index'; let testDockerHost: docker.DockerHost; 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); }); tap.test('should create a docker swarm', async () => { - + await testDockerHost.activateSwarm(); }); // Containers @@ -63,7 +63,7 @@ tap.test('should activate swarm mode', async () => { }); tap.test('should list all services', async tools => { - const services = await docker.DockerService.getServices(testDockerHost); + const services = await testDockerHost.getServices(); console.log(services); }); @@ -71,7 +71,7 @@ tap.test('should create a service', async () => { const testNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, { Name: 'testNetwork' }); - await DockerService.createService(testDockerHost, { + const testService = await DockerService.createService(testDockerHost, { Image: 'nginx:latest', Labels: { 'testlabel': 'hi' @@ -80,6 +80,8 @@ tap.test('should create a service', async () => { networks: [testNetwork], networkAlias: 'testService' }); + await testService.remove(); + await testNetwork.remove(); }); tap.start(); diff --git a/ts/docker.classes.host.ts b/ts/docker.classes.host.ts index 07cb7d9..f89ea1e 100644 --- a/ts/docker.classes.host.ts +++ b/ts/docker.classes.host.ts @@ -1,6 +1,7 @@ import * as plugins from './docker.plugins'; import { DockerContainer } from './docker.classes.container'; import { DockerNetwork } from './docker.classes.network'; +import { DockerService } from './docker.classes.service'; export class DockerHost { /** @@ -52,6 +53,14 @@ export class DockerHost { return containerArray; } + /** + * gets all services + */ + public async getServices() { + const serviceArray = await DockerService.getServices(this); + return serviceArray; + } + /** * */ @@ -115,7 +124,8 @@ export class DockerHost { 'Content-Type': 'application/json', Host: 'docker.sock' }, - requestBody: dataArg + requestBody: dataArg, + keepAlive: false }); if (response.statusCode !== 200) { console.log(response.body); @@ -130,10 +140,11 @@ export class DockerHost { { method: methodArg, headers: { - // 'Content-Type': 'application/json', + 'Content-Type': 'application/json', Host: 'docker.sock' }, - requestBody: null + requestBody: null, + keepAlive: false }, true ); diff --git a/ts/docker.classes.network.ts b/ts/docker.classes.network.ts index bccb607..7ae7e05 100644 --- a/ts/docker.classes.network.ts +++ b/ts/docker.classes.network.ts @@ -8,7 +8,9 @@ export class DockerNetwork { const dockerNetworks: DockerNetwork[] = []; const response = await dockerHost.request('GET', '/networks'); 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; } @@ -78,11 +80,8 @@ export class DockerNetwork { ]; }; - constructor(dockerHostArg: DockerHost, dockerNetworkObjectArg: any) { + constructor(dockerHostArg: DockerHost) { this.dockerHost = dockerHostArg; - Object.keys(dockerNetworkObjectArg).forEach(keyArg => { - this[keyArg] = dockerNetworkObjectArg[keyArg]; - }); } /** diff --git a/ts/docker.classes.service.ts b/ts/docker.classes.service.ts index dc06bb0..19aadd9 100644 --- a/ts/docker.classes.service.ts +++ b/ts/docker.classes.service.ts @@ -10,18 +10,28 @@ export class DockerService { const services: DockerService[] = []; const response = await dockerHost.request('GET', '/services'); 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; } + public static async getServiceByName(dockerHost: DockerHost, networkName: string): Promise { + const allServices = await DockerService.getServices(dockerHost); + const wantedService = allServices.find(service => { + return service.Spec.Name === networkName; + }); + return wantedService; + } + /** * creates a service */ public static async createService( dockerHost: DockerHost, serviceCreationDescriptor: interfaces.IServiceCreationDescriptor - ) { + ): Promise { // lets get the image plugins.smartlog.defaultLogger.log('info', `downloading image for service ${serviceCreationDescriptor.Name}`); const serviceImage = await DockerImage.createFromRegistry(dockerHost, { @@ -36,7 +46,7 @@ export class DockerService { }); } - dockerHost.request('POST', '/services/create', { + const response = await dockerHost.request('POST', '/services/create', { Name: serviceCreationDescriptor.Name, TaskTemplate: { ContainerSpec: { @@ -47,17 +57,37 @@ export class DockerService { Labels: serviceCreationDescriptor.Labels, Networks: networkArray }); + + const createdService = await DockerService.getServiceByName(dockerHost, serviceCreationDescriptor.Name); + return createdService; } // INSTANCE - public dockerHost: DockerHost; + public dockerHostRef: DockerHost; + + public ID: string; + public Version: { Index: number }; + 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, serviceObject) { - this.dockerHost = dockerHostArg; - Object.assign(this, serviceObject); + + constructor(dockerHostArg: DockerHost) { + this.dockerHostRef = dockerHostArg; } - update() { + public async update() { } + + public async remove() { + await this.dockerHostRef.request('DELETE', `/services/${this.ID}`); + } }