Compare commits

..

40 Commits

Author SHA1 Message Date
ad67849d45 1.0.63 2019-09-13 18:16:22 +02:00
0e4e07a912 fix(core): update 2019-09-13 18:16:21 +02:00
1fbc09f557 1.0.62 2019-09-13 18:15:46 +02:00
d6201b864c fix(core): update 2019-09-13 18:15:45 +02:00
ea5e552192 1.0.61 2019-09-13 17:54:18 +02:00
1afe5c6e16 fix(core): update 2019-09-13 17:54:17 +02:00
eb0dc96dbd 1.0.60 2019-09-13 16:57:22 +02:00
55f45b1c3a fix(core): update 2019-09-13 16:57:21 +02:00
87ff0f01bb 1.0.59 2019-09-13 14:45:35 +02:00
dd1939d7b2 fix(core): update 2019-09-13 14:45:35 +02:00
5a2a5f1248 1.0.58 2019-09-13 14:40:38 +02:00
9767b8767a fix(core): update 2019-09-13 14:40:38 +02:00
546e139b46 1.0.57 2019-09-13 13:20:01 +02:00
28d70bb49f fix(core): update 2019-09-13 13:20:01 +02:00
b71f134abd 1.0.56 2019-09-12 14:52:56 +02:00
968b3c7449 fix(core): update 2019-09-12 14:52:55 +02:00
d9558f7843 1.0.55 2019-09-12 14:45:36 +02:00
19e67ffdcc fix(core): update 2019-09-12 14:45:36 +02:00
e983b66c28 1.0.54 2019-09-11 20:25:46 +02:00
c79f6a698f fix(core): update 2019-09-11 20:25:45 +02:00
0ef098e9c8 1.0.53 2019-09-08 19:22:20 +02:00
d56350ff28 fix(core): update 2019-09-08 19:22:20 +02:00
a4dc4e7950 1.0.52 2019-09-08 16:34:26 +02:00
424e911804 fix(core): update 2019-09-08 16:34:26 +02:00
b5c4727bae 1.0.51 2019-08-16 21:45:22 +02:00
b6f3fbf8a9 fix(core): update 2019-08-16 21:45:21 +02:00
7241e7a8fd 1.0.50 2019-08-16 21:34:36 +02:00
ae37148ece fix(core): update 2019-08-16 21:34:35 +02:00
65c37bdd6f 1.0.49 2019-08-16 21:21:31 +02:00
6acbe30e2e fix(core): update 2019-08-16 21:21:30 +02:00
eb6f7889d0 1.0.48 2019-08-16 21:10:03 +02:00
e39da5fee9 fix(core): update 2019-08-16 21:10:03 +02:00
b07628bb0b 1.0.47 2019-08-16 21:07:59 +02:00
5815f9b202 fix(core): update 2019-08-16 21:07:59 +02:00
846ea9997e 1.0.46 2019-08-16 18:32:42 +02:00
de54db33ad fix(core): update 2019-08-16 18:32:41 +02:00
314cb692ac 1.0.45 2019-08-16 18:21:55 +02:00
73f8ded3fe fix(core): update 2019-08-16 18:21:55 +02:00
a28b10ac51 1.0.44 2019-08-16 14:46:48 +02:00
927e2e0acc fix(core): update 2019-08-16 14:46:48 +02:00
15 changed files with 1017 additions and 284 deletions

View File

@ -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

762
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/docker", "name": "@mojoio/docker",
"version": "1.0.43", "version": "1.0.63",
"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,25 @@
}, },
"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/smartfile": "^7.0.4",
"@pushrocks/smartjson": "^3.0.8",
"@pushrocks/smartlog": "^2.0.19", "@pushrocks/smartlog": "^2.0.19",
"@pushrocks/smartnetwork": "^1.1.14",
"@pushrocks/smartpath": "^4.0.1",
"@pushrocks/smartpromise": "^3.0.2", "@pushrocks/smartpromise": "^3.0.2",
"@pushrocks/smartrequest": "^1.1.16", "@pushrocks/smartrequest": "^1.1.26",
"rxjs": "^6.5.2" "@pushrocks/smartstring": "^3.0.10",
"@pushrocks/smartversion": "^2.0.4",
"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": [

View File

@ -4,10 +4,14 @@ import * as docker 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();
@ -37,7 +41,7 @@ tap.test('should remove a network', async () => {
tap.test('should pull an image from imagetag', async () => { tap.test('should pull an image from imagetag', async () => {
const image = await docker.DockerImage.createFromRegistry(testDockerHost, { const image = await docker.DockerImage.createFromRegistry(testDockerHost, {
imageUrl: 'hosttoday/ht-docker-node', imageUrl: 'hosttoday/ht-docker-node',
tag: 'alpine' imageTag: 'alpine'
}); });
expect(image).to.be.instanceOf(docker.DockerImage); expect(image).to.be.instanceOf(docker.DockerImage);
console.log(image); console.log(image);
@ -52,14 +56,63 @@ tap.test('should return a change Observable', async tools => {
subscription.unsubscribe(); subscription.unsubscribe();
}); });
// SECRETS
tap.test('should create a secret', async () => {
const mySecret = await docker.DockerSecret.createSecret(testDockerHost, {
name: 'testSecret',
version: '1.0.3',
contentArg: `{ "hi": "wow"}`,
labels: {}
});
console.log(mySecret);
});
tap.test('should remove a secret by name', async () => {
const mySecret = await docker.DockerSecret.getSecretByName(testDockerHost, 'testSecret');
await mySecret.remove();
});
// SERVICES // SERVICES
tap.test('should activate swarm mode', async () => { tap.test('should activate swarm mode', async () => {
await testDockerHost.activateSwarm(); await testDockerHost.activateSwarm();
}); });
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);
}); });
tap.test('should create a service', async () => {
const testNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, {
Name: 'testNetwork'
});
const testSecret = await docker.DockerSecret.createSecret(testDockerHost, {
name: 'serviceSecret',
version: '0.0.1',
labels: {},
contentArg: '{"hi": "wow"}'
});
const testImage = await docker.DockerImage.createFromRegistry(testDockerHost, {
imageUrl: 'nginx:latest'
});
const testService = await docker.DockerService.createService(testDockerHost, {
image: testImage,
labels: {
'testlabel': 'hi'
},
name: 'testService',
networks: [testNetwork],
networkAlias: 'testService',
secrets: [testSecret]
});
await testSecret.update(`{"updated": "socool"}`);
await testService.update();
await testService.remove();
await testNetwork.remove();
await testSecret.remove();
});
tap.start(); tap.start();

View File

@ -1,19 +1,29 @@
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 {
/** /**
* the path where the docker sock can be found * the path where the docker sock can be found
*/ */
public socketPath: string; public socketPath: string;
private registryToken: string = '';
/** /**
* 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 +31,35 @@ 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
});
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);
this.registryToken = plugins.smartstring.base64.encode(response.body.IdentityToken);
}
/**
* sets an auth token
* @param authToken
*/
public setAuthToken(authToken: string) {
this.registryToken = authToken;
}
/**
* gets the token from the .docker/config.json file for GitLab registry
*/
public async getGitlabComTokenFromDockerConfig() {
const dockerConfigPath = plugins.smartpath.get.home('~/.docker/config.json');
const configObject = plugins.smartfile.fs.toObjectSync(dockerConfigPath);
this.registryToken = configObject.auths['registry.gitlab.com'].auth;
} }
/** /**
@ -33,6 +69,11 @@ export class DockerHost {
return await DockerNetwork.getNetworks(this); return await DockerNetwork.getNetworks(this);
} }
/**
*
*/
/** /**
* gets all containers * gets all containers
*/ */
@ -41,6 +82,14 @@ export class DockerHost {
return containerArray; return containerArray;
} }
/**
* gets all services
*/
public async getServices() {
const serviceArray = await DockerService.getServices(this);
return serviceArray;
}
/** /**
* *
*/ */
@ -66,9 +115,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,
@ -90,9 +151,11 @@ export class DockerHost {
method: methodArg, method: methodArg,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-Registry-Auth': this.registryToken,
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 +170,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
); );

View File

@ -15,29 +15,63 @@ export class DockerImage {
public static async findImageByName(dockerHost: DockerHost, imageNameArg: string) { public static async findImageByName(dockerHost: DockerHost, imageNameArg: string) {
const images = await this.getImages(dockerHost); const images = await this.getImages(dockerHost);
return images.find(image => { const result = images.find(image => {
return image.RepoTags.includes(imageNameArg); if (image.RepoTags) {
return image.RepoTags.includes(imageNameArg);
} else {
return false;
}
}); });
return result;
} }
public static async createFromRegistry( public static async createFromRegistry(
dockerHostArg: DockerHost, dockerHostArg: DockerHost,
creationObject: interfaces.IImageCreationDescriptor creationObject: interfaces.IImageCreationDescriptor
): Promise<DockerImage> { ): Promise<DockerImage> {
// lets create a sanatized imageUrlObject
const imageUrlObject: {
imageUrl: string;
imageTag: string;
imageOriginTag: string;
} = {
imageUrl: creationObject.imageUrl,
imageTag: creationObject.imageTag,
imageOriginTag: null
};
if (imageUrlObject.imageUrl.includes(':')) {
const imageUrl = imageUrlObject.imageUrl.split(':')[0];
const imageTag = imageUrlObject.imageUrl.split(':')[1];
if (imageUrlObject.imageTag) {
throw new Error(
`imageUrl ${imageUrlObject.imageUrl} can't be tagged with ${
imageUrlObject.imageTag
} because it is already tagged with ${imageTag}`
);
} else {
imageUrlObject.imageUrl = imageUrl;
imageUrlObject.imageTag = imageTag;
}
} else if (!imageUrlObject.imageTag) {
imageUrlObject.imageTag = 'latest';
}
imageUrlObject.imageOriginTag = `${imageUrlObject.imageUrl}:${imageUrlObject.imageTag}`;
// lets actually create the image
const response = await dockerHostArg.request( const response = await dockerHostArg.request(
'POST', 'POST',
`/images/create?fromImage=${encodeURIComponent( `/images/create?fromImage=${encodeURIComponent(
creationObject.imageUrl imageUrlObject.imageUrl
)}&tag=${encodeURIComponent(creationObject.tag)}` )}&tag=${encodeURIComponent(imageUrlObject.imageTag)}`
); );
if (response.statusCode < 300) { if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(
'info', 'info',
`Successfully pulled image ${creationObject.imageUrl} from the registry` `Successfully pulled image ${imageUrlObject.imageUrl} from the registry`
); );
const originTag = `${creationObject.imageUrl}:${creationObject.tag}`; const image = await DockerImage.findImageByName(dockerHostArg, imageUrlObject.imageOriginTag);
console.log(originTag);
const image = await DockerImage.findImageByName(dockerHostArg, originTag);
return image; return image;
} else { } else {
plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`); plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`);
@ -86,27 +120,27 @@ export class DockerImage {
} }
/** /**
* returns a boolean wether the image has a upstream image * tag an image
* @param newTag
*/ */
public isUpstreamImage(): boolean { public async tagImage(newTag) {
// TODO: implement isUpastreamImage throw new Error('.tagImage is not yet implemented');
return this.RepoTags.length > 0;
} }
public tagImage(newTag) {}
/** /**
* pulls the latest version from the registry * pulls the latest version from the registry
*/ */
public async pullLatestImageFromRegistry(): Promise<boolean> { public async pullLatestImageFromRegistry(): Promise<boolean> {
const dockerImageUrl = this.RepoTags[0].split(':')[0];
const dockerImageTag = this.RepoTags[0].split(':')[1];
const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, { const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, {
imageUrl: dockerImageUrl, imageUrl: this.RepoTags[0]
tag: dockerImageTag
}); });
Object.assign(this, updatedImage); Object.assign(this, updatedImage);
// TODO: Compare image digists before and after // TODO: Compare image digists before and after
return true; return true;
} }
// get stuff
public async getVersion() {
return this.Labels.version;
}
} }

View File

@ -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];
});
} }
/** /**

View File

@ -0,0 +1,83 @@
import * as plugins from './docker.plugins';
import { DockerHost } from './docker.classes.host';
// interfaces
import * as interfaces from './interfaces';
export class DockerSecret {
// STATIC
public static async getSecrets(dockerHostArg: DockerHost) {
const response = await dockerHostArg.request('GET', '/secrets');
const secrets: DockerSecret[] = [];
for (const secret of response.body) {
const dockerSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(dockerSecretInstance, secret);
secrets.push(dockerSecretInstance);
}
return secrets;
}
public static async getSecretByID (dockerHostArg: DockerHost, idArg: string) {
const secrets = await this.getSecrets(dockerHostArg);
return secrets.find(secret => secret.ID === idArg);
}
public static async getSecretByName (dockerHostArg: DockerHost, nameArg: string) {
const secrets = await this.getSecrets(dockerHostArg);
return secrets.find(secret => secret.Spec.Name === nameArg);
}
public static async createSecret(dockerHostArg: DockerHost, secretDescriptor: interfaces.ISecretCreationDescriptor) {
const labels: interfaces.TLabels = {
...secretDescriptor.labels,
version: secretDescriptor.version
};
const response = await dockerHostArg.request('POST', '/secrets/create', {
Name: secretDescriptor.name,
Labels: labels,
Data: plugins.smartstring.base64.encode(secretDescriptor.contentArg)
});
const newSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(newSecretInstance, response.body);
Object.assign (newSecretInstance, await DockerSecret.getSecretByID(dockerHostArg, newSecretInstance.ID));
return newSecretInstance;
}
// INSTANCE
public ID: string;
public Spec: {
Name: string;
Labels: interfaces.TLabels;
};
public Version: {
Index:string;
};
public dockerHost: DockerHost;
constructor(dockerHostArg: DockerHost) {
this.dockerHost = dockerHostArg;
}
/**
* updates a secret
*/
public async update (contentArg: string) {
const route = `/secrets/${this.ID}/update?=version=${this.Version.Index}`;
const response = await this.dockerHost.request('POST', `/secrets/${this.ID}/update?version=${this.Version.Index}`, {
Name: this.Spec.Name,
Labels: this.Spec.Labels,
Data: plugins.smartstring.base64.encode(contentArg)
});
}
public async remove () {
await this.dockerHost.request('DELETE', `/secrets/${this.ID}`);
}
// get things
public async getVersion() {
return this.Spec.Labels.version;
}
}

View File

@ -2,6 +2,8 @@ import * as plugins from './docker.plugins';
import * as interfaces from './interfaces'; import * as interfaces from './interfaces';
import { DockerHost } from './docker.classes.host'; import { DockerHost } from './docker.classes.host';
import { DockerImage } from './docker.classes.image';
import { DockerSecret } from './docker.classes.secret';
export class DockerService { export class DockerService {
// STATIC // STATIC
@ -9,35 +11,180 @@ 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> {
dockerHost.request('POST', '/services/create', { // lets get the image
Name: serviceCreationDescriptor.Name, plugins.smartlog.defaultLogger.log(
'info',
`now creating service ${serviceCreationDescriptor.name}`
);
// await serviceCreationDescriptor.image.pullLatestImageFromRegistry();
const serviceVersion = await serviceCreationDescriptor.image.getVersion();
const labels: interfaces.TLabels = {
...serviceCreationDescriptor.labels,
version: serviceVersion
};
const networkArray: any[] = [];
for (const network of serviceCreationDescriptor.networks) {
networkArray.push({
Target: network.Name,
Aliases: [serviceCreationDescriptor.networkAlias]
});
}
const secretArray: any[] = [];
for (const secret of serviceCreationDescriptor.secrets) {
secretArray.push({
File: {
Name: 'secret.json',
UID: '33',
GID: '33',
Mode: 384
},
SecretID: secret.ID,
SecretName: secret.Spec.Name
});
}
const response = await dockerHost.request('POST', '/services/create', {
Name: serviceCreationDescriptor.name,
TaskTemplate: { TaskTemplate: {
ContainerSpec: { ContainerSpec: {
Image: serviceCreationDescriptor.Image, Image: serviceCreationDescriptor.image.RepoTags[0],
Labels: serviceCreationDescriptor.Labels Labels: labels,
} Secrets: secretArray
},
UpdateConfig: {
Parallelism: 0,
Delay: 0,
FailureAction: 'pause',
Monitor: 15000000000,
MaxFailureRatio: 0.15
},
ForceUpdate: 1
}, },
Labels: serviceCreationDescriptor.Labels Labels: serviceCreationDescriptor.labels,
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: interfaces.TLabels;
TaskTemplate: {
ContainerSpec: {
Image: string;
Isolation: string;
Secrets: Array<{
File: {
Name: string;
UID: string;
GID: string;
Mode: number;
};
SecretID: string;
SecretName: string;
}>;
};
ForceUpdate: 0;
};
Mode: {};
Networks: [any[]];
};
public Endpoint: { Spec: {}; VirtualIPs: [any[]] };
constructor(dockerHostArg: DockerHost) {
this.dockerHostRef = dockerHostArg;
}
public async update() {
const labels: interfaces.TLabels = {
...this.Spec.Labels,
version: 'x.x.x'
};
const dockerData = await this.dockerHostRef.request(
'POST',
`/services/${this.ID}/update?version=${this.Version.Index}`,
{
Name: this.Spec.Name,
TaskTemplate: this.Spec.TaskTemplate,
Labels: labels,
Networks: this.Spec.Networks
}
);
Object.assign(this, dockerData);
}
public async remove() {
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
}
public async reReadFromDockerEngine() {
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
Object.assign(this, dockerData);
}
public async needsUpdate(): Promise<boolean> {
// TODO: implement digest based update recognition
await this.reReadFromDockerEngine();
const dockerImage = await DockerImage.createFromRegistry(this.dockerHostRef, {
imageUrl: this.Spec.TaskTemplate.ContainerSpec.Image
});
const imageVersion = new plugins.smartversion.SmartVersion(dockerImage.Labels.version);
const serviceVersion = new plugins.smartversion.SmartVersion(this.Spec.Labels.version);
if (imageVersion.greaterThan(serviceVersion)) {
console.log(`service ${this.Spec.Name} needs to be updated`);
return true;
} else {
console.log(`service ${this.Spec.Name} is up to date.`);
}
}
public async updateFromRegistry() {
if (await this.needsUpdate()) {
this.update();
}
} }
} }

View File

@ -1,12 +1,25 @@
// node native path
import * as path from 'path';
export {
path
};
// @pushrocks scope // @pushrocks scope
import * as lik from '@pushrocks/lik'; import * as lik from '@pushrocks/lik';
import * as smartfile from '@pushrocks/smartfile';
import * as smartjson from '@pushrocks/smartjson';
import * as smartlog from '@pushrocks/smartlog'; import * as smartlog from '@pushrocks/smartlog';
import * as smartnetwork from '@pushrocks/smartnetwork';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest'; import * as smartrequest from '@pushrocks/smartrequest';
import * as smartstring from '@pushrocks/smartstring';
import * as smartversion from '@pushrocks/smartversion';
smartlog.defaultLogger.enableConsole(); smartlog.defaultLogger.enableConsole();
export { lik, smartlog, smartpromise, smartrequest }; export { lik, smartfile, smartjson, smartlog, smartnetwork, smartpath, smartpromise, smartrequest, smartstring, smartversion };
// third party // third party
import * as rxjs from 'rxjs'; import * as rxjs from 'rxjs';

View File

@ -2,4 +2,5 @@ export * from './docker.classes.host';
export * from './docker.classes.container'; export * from './docker.classes.container';
export * from './docker.classes.image'; export * from './docker.classes.image';
export * from './docker.classes.network'; export * from './docker.classes.network';
export * from './docker.classes.secret';
export * from './docker.classes.service'; export * from './docker.classes.service';

View File

@ -1,4 +1,4 @@
export interface IImageCreationDescriptor { export interface IImageCreationDescriptor {
imageUrl: string; imageUrl: string;
tag: string; imageTag?: string;
} }

View File

@ -3,4 +3,5 @@ export * from './image';
export * from './label'; export * from './label';
export * from './network'; export * from './network';
export * from './port'; export * from './port';
export * from './secret';
export * from './service'; export * from './service';

8
ts/interfaces/secret.ts Normal file
View File

@ -0,0 +1,8 @@
import * as interfaces from './';
export interface ISecretCreationDescriptor {
name: string;
version: string;
contentArg: any;
labels: interfaces.TLabels;
}

View File

@ -1,7 +1,13 @@
import * as interfaces from './'; import * as interfaces from './';
import { DockerNetwork } from '../docker.classes.network';
import { DockerSecret } from '../docker.classes.secret';
import { DockerImage } from '../docker.classes.image';
export interface IServiceCreationDescriptor { export interface IServiceCreationDescriptor {
Name: string; name: string;
Image: string; image: DockerImage;
Labels: interfaces.TLabels; labels: interfaces.TLabels;
networks: DockerNetwork[];
networkAlias: string;
secrets: DockerSecret[];
} }