diff --git a/test/test.ts b/test/test.ts index 8aac4b4..9d88688 100644 --- a/test/test.ts +++ b/test/test.ts @@ -52,4 +52,14 @@ tap.test('should return a change Observable', async tools => { subscription.unsubscribe(); }); +// SERVICES +tap.test('should activate swarm mode', async () => { + await testDockerHost.activateSwarm(); +}); + +tap.test('should list all services', async tools => { + const services = await docker.DockerService.getServices(testDockerHost); + console.log(services); +}); + tap.start(); diff --git a/ts/docker.classes.host.ts b/ts/docker.classes.host.ts index 2186479..a5d75cd 100644 --- a/ts/docker.classes.host.ts +++ b/ts/docker.classes.host.ts @@ -65,10 +65,10 @@ export class DockerHost { /** * activates docker swarm */ - public async activateSwarm(addvertisementIpArg: string) { + public async activateSwarm(addvertisementIpArg?: string) { const response = await this.request('POST', '/swarm/init', { ListenAddr: '0.0.0.0:2377', - AdvertiseAddr: `${addvertisementIpArg}:2377`, + AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377`: undefined, DataPathPort: 4789, DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'], SubnetSize: 24, diff --git a/ts/docker.classes.service.ts b/ts/docker.classes.service.ts index e1d76b3..b8009f1 100644 --- a/ts/docker.classes.service.ts +++ b/ts/docker.classes.service.ts @@ -3,4 +3,39 @@ import * as interfaces from './interfaces'; import { DockerHost } from './docker.classes.host'; -export class DockerService {} +export class DockerService { + // STATIC + public static async getServices(dockerHost: DockerHost) { + const services: DockerService[] = []; + const response = await dockerHost.request('GET', '/services'); + for (const serviceObject of response.body) { + services.push(new DockerService(dockerHost, serviceObject)); + } + return services; + } + + + /** + * creates a service + */ + public static async createService(dockerHost: DockerHost, serviceCreationDescriptor: interfaces.IServiceCreationDescriptor) { + dockerHost.request('POST', '/services/create', { + Name: serviceCreationDescriptor.Name, + TaskTemplate: { + ContainerSpec: { + Image: serviceCreationDescriptor.Image, + Labels: serviceCreationDescriptor.Labels + } + }, + Labels: serviceCreationDescriptor.Labels + }); + } + + // INSTANCE + public dockerHost: DockerHost; + + constructor(dockerHostArg: DockerHost, serviceObject) { + this.dockerHost = dockerHostArg; + Object.assign(this, serviceObject); + } +} diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts index c309b1d..af9eddf 100644 --- a/ts/interfaces/index.ts +++ b/ts/interfaces/index.ts @@ -3,3 +3,5 @@ export * from './image'; export * from './label'; export * from './network'; export * from './port'; +export * from './service'; + diff --git a/ts/interfaces/service.ts b/ts/interfaces/service.ts new file mode 100644 index 0000000..c0146b1 --- /dev/null +++ b/ts/interfaces/service.ts @@ -0,0 +1,7 @@ +import * as interfaces from './' + +export interface IServiceCreationDescriptor { + Name: string; + Image: string; + Labels: interfaces.TLabels; +} \ No newline at end of file