Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
d9558f7843 | |||
19e67ffdcc | |||
e983b66c28 | |||
c79f6a698f | |||
0ef098e9c8 | |||
d56350ff28 | |||
a4dc4e7950 | |||
424e911804 | |||
b5c4727bae | |||
b6f3fbf8a9 | |||
7241e7a8fd | |||
ae37148ece | |||
65c37bdd6f | |||
6acbe30e2e | |||
eb6f7889d0 | |||
e39da5fee9 | |||
b07628bb0b | |||
5815f9b202 | |||
846ea9997e | |||
de54db33ad | |||
314cb692ac | |||
73f8ded3fe | |||
a28b10ac51 | |||
927e2e0acc | |||
c496405818 | |||
020737e21b |
@ -39,6 +39,8 @@ snyk:
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
services:
|
||||
- docker:18-dind
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
@ -48,7 +50,7 @@ testLTS:
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
|
732
package-lock.json
generated
732
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mojoio/docker",
|
||||
"version": "1.0.42",
|
||||
"version": "1.0.55",
|
||||
"description": "easy communication with docker remote api from node, TypeScript ready",
|
||||
"private": false,
|
||||
"main": "dist/index.js",
|
||||
@ -27,19 +27,23 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^3.0.10",
|
||||
"@pushrocks/lik": "^3.0.11",
|
||||
"@pushrocks/smartjson": "^3.0.8",
|
||||
"@pushrocks/smartlog": "^2.0.19",
|
||||
"@pushrocks/smartnetwork": "^1.1.14",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrequest": "^1.1.16",
|
||||
"rxjs": "^6.5.2"
|
||||
"@pushrocks/smartrequest": "^1.1.26",
|
||||
"@pushrocks/smartstring": "^3.0.10",
|
||||
"@pushrocks/smartversion": "^2.0.4",
|
||||
"rxjs": "^6.5.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.11",
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/tapbundle": "^3.0.11",
|
||||
"@types/node": "^12.7.1",
|
||||
"tslint": "^5.18.0",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.7.4",
|
||||
"tslint": "^5.19.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"files": [
|
||||
|
55
test/test.ts
55
test/test.ts
@ -1,13 +1,18 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as docker from '../ts/index';
|
||||
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
|
||||
tap.test('should list containers', async () => {
|
||||
const containers = await testDockerHost.getContainers();
|
||||
@ -37,7 +42,7 @@ tap.test('should remove a network', async () => {
|
||||
tap.test('should pull an image from imagetag', async () => {
|
||||
const image = await docker.DockerImage.createFromRegistry(testDockerHost, {
|
||||
imageUrl: 'hosttoday/ht-docker-node',
|
||||
tag: 'alpine'
|
||||
imageTag: 'alpine'
|
||||
});
|
||||
expect(image).to.be.instanceOf(docker.DockerImage);
|
||||
console.log(image);
|
||||
@ -52,14 +57,58 @@ tap.test('should return a change Observable', async tools => {
|
||||
subscription.unsubscribe();
|
||||
});
|
||||
|
||||
// SECRETS
|
||||
tap.test('should create a secret', async () => {
|
||||
const mySecret = await docker.DockerSecret.createSecret(testDockerHost, {
|
||||
name: 'testSecret',
|
||||
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
|
||||
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);
|
||||
const services = await testDockerHost.getServices();
|
||||
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',
|
||||
labels: {},
|
||||
contentArg: '{"hi": "wow"}'
|
||||
});
|
||||
const testService = await DockerService.createService(testDockerHost, {
|
||||
Image: 'nginx:latest',
|
||||
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();
|
||||
|
@ -49,7 +49,10 @@ export class DockerContainer {
|
||||
if (response.statusCode < 300) {
|
||||
plugins.smartlog.defaultLogger.log('info', 'Container created successfully');
|
||||
} else {
|
||||
plugins.smartlog.defaultLogger.log('error', 'There has been a problem when creating the container');
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'error',
|
||||
'There has been a problem when creating the container'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
/**
|
||||
@ -12,8 +13,16 @@ export class DockerHost {
|
||||
* the constructor to instantiate a new docker sock instance
|
||||
* @param pathArg
|
||||
*/
|
||||
constructor(pathArg: string = 'http://unix:/var/run/docker.sock:') {
|
||||
this.socketPath = pathArg;
|
||||
constructor(pathArg?: string) {
|
||||
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 passArg
|
||||
*/
|
||||
public async auth(registryArg: string, userArg: string, passArg: string) {
|
||||
// TODO: implement Docker Registry authentication
|
||||
await this.request('POST', '');
|
||||
public async auth(registryUrl: string, userArg: string, passArg: string) {
|
||||
const response = await this.request('POST', '/auth', {
|
||||
serveraddress: registryUrl,
|
||||
username: userArg,
|
||||
password: passArg,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,6 +53,14 @@ export class DockerHost {
|
||||
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
|
||||
*/
|
||||
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', {
|
||||
ListenAddr: '0.0.0.0:2377',
|
||||
AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377`: undefined,
|
||||
AdvertiseAddr: addvertisementIp,
|
||||
DataPathPort: 4789,
|
||||
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
|
||||
SubnetSize: 24,
|
||||
@ -92,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);
|
||||
@ -107,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
|
||||
);
|
||||
|
@ -13,7 +13,7 @@ export class DockerImage {
|
||||
return images;
|
||||
}
|
||||
|
||||
public static async findImageByName (dockerHost: DockerHost, imageNameArg: string) {
|
||||
public static async findImageByName(dockerHost: DockerHost, imageNameArg: string) {
|
||||
const images = await this.getImages(dockerHost);
|
||||
return images.find(image => {
|
||||
return image.RepoTags.includes(imageNameArg);
|
||||
@ -24,20 +24,48 @@ export class DockerImage {
|
||||
dockerHostArg: DockerHost,
|
||||
creationObject: interfaces.IImageCreationDescriptor
|
||||
): 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(
|
||||
'POST',
|
||||
`/images/create?fromImage=${encodeURIComponent(
|
||||
creationObject.imageUrl
|
||||
)}&tag=${encodeURIComponent(creationObject.tag)}`
|
||||
imageUrlObject.imageUrl
|
||||
)}&tag=${encodeURIComponent(imageUrlObject.imageTag)}`
|
||||
);
|
||||
if (response.statusCode < 300) {
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'info',
|
||||
`Successfully pulled image ${creationObject.imageUrl} from the registry`
|
||||
`Successfully pulled image ${imageUrlObject.imageUrl} from the registry`
|
||||
);
|
||||
const originTag = `${creationObject.imageUrl}:${creationObject.tag}`;
|
||||
console.log(originTag)
|
||||
const image = await DockerImage.findImageByName(dockerHostArg, originTag);
|
||||
const image = await DockerImage.findImageByName(dockerHostArg, imageUrlObject.imageOriginTag);
|
||||
return image;
|
||||
} else {
|
||||
plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`);
|
||||
@ -85,32 +113,17 @@ 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) {}
|
||||
|
||||
/**
|
||||
* pulls the latest version from the registry
|
||||
*/
|
||||
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, {
|
||||
imageUrl: dockerImageUrl,
|
||||
tag: dockerImageTag
|
||||
imageUrl: this.RepoTags[0]
|
||||
});
|
||||
Object.assign(this, updatedImage);
|
||||
// TODO: Compare image digists before and after
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
@ -41,12 +43,15 @@ export class DockerNetwork {
|
||||
Attachable: true,
|
||||
Ingress: false
|
||||
});
|
||||
if (response.statusCode < 300 ) {
|
||||
if (response.statusCode < 300) {
|
||||
plugins.smartlog.defaultLogger.log('info', 'Created network successfully');
|
||||
return await DockerNetwork.getNetworkByName(dockerHost, networkCreationDescriptor.Name);
|
||||
} else {
|
||||
plugins.smartlog.defaultLogger.log('error', 'There has been an error creating the wanted network');
|
||||
return null
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'error',
|
||||
'There has been an error creating the wanted network'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,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];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
73
ts/docker.classes.secret.ts
Normal file
73
ts/docker.classes.secret.ts
Normal file
@ -0,0 +1,73 @@
|
||||
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 response = await dockerHostArg.request('POST', '/secrets/create', {
|
||||
Name: secretDescriptor.name,
|
||||
Labels: secretDescriptor.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;
|
||||
};
|
||||
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}`);
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@ import * as plugins from './docker.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { DockerHost } from './docker.classes.host';
|
||||
import { DockerImage } from './docker.classes.image';
|
||||
import { DockerSecret } from './docker.classes.secret';
|
||||
|
||||
export class DockerService {
|
||||
// STATIC
|
||||
@ -9,33 +11,170 @@ 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<DockerService> {
|
||||
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) {
|
||||
dockerHost.request('POST', '/services/create', {
|
||||
public static async createService(
|
||||
dockerHost: DockerHost,
|
||||
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
||||
): Promise<DockerService> {
|
||||
// lets get the image
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'info',
|
||||
`downloading image for service ${serviceCreationDescriptor.Name}`
|
||||
);
|
||||
const serviceImage = await DockerImage.createFromRegistry(dockerHost, {
|
||||
imageUrl: serviceCreationDescriptor.Image
|
||||
});
|
||||
|
||||
const serviceVersion = serviceImage.Labels.version;
|
||||
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: {
|
||||
ContainerSpec: {
|
||||
Image: serviceCreationDescriptor.Image,
|
||||
Labels: serviceCreationDescriptor.Labels
|
||||
}
|
||||
Labels: serviceCreationDescriptor.Labels,
|
||||
Secrets: secretArray
|
||||
},
|
||||
Labels: serviceCreationDescriptor.Labels
|
||||
UpdateConfig: {
|
||||
Parallelism: 0,
|
||||
Delay: 0,
|
||||
FailureAction: 'pause',
|
||||
Monitor: 15000000000,
|
||||
MaxFailureRatio: 0.15
|
||||
},
|
||||
ForceUpdate: 1
|
||||
},
|
||||
Labels: serviceCreationDescriptor.Labels,
|
||||
Networks: networkArray
|
||||
});
|
||||
|
||||
const createdService = await DockerService.getServiceByName(
|
||||
dockerHost,
|
||||
serviceCreationDescriptor.Name
|
||||
);
|
||||
return createdService;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public dockerHost: DockerHost;
|
||||
public dockerHostRef: DockerHost;
|
||||
|
||||
constructor(dockerHostArg: DockerHost, serviceObject) {
|
||||
this.dockerHost = dockerHostArg;
|
||||
Object.assign(this, serviceObject);
|
||||
public ID: string;
|
||||
public Version: { Index: number };
|
||||
public CreatedAt: string;
|
||||
public UpdatedAt: string;
|
||||
public Spec: {
|
||||
Name: string;
|
||||
Labels: interfaces.TLabels; // ZBD
|
||||
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 + 1}`,
|
||||
{
|
||||
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 updateFromRegistry() {
|
||||
// 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 needs to be updated');
|
||||
this.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,16 @@
|
||||
// @pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as smartnetwork from '@pushrocks/smartnetwork';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartstring from '@pushrocks/smartstring'
|
||||
import * as smartversion from '@pushrocks/smartversion';
|
||||
|
||||
smartlog.defaultLogger.enableConsole();
|
||||
|
||||
export { lik, smartlog, smartpromise, smartrequest };
|
||||
export { lik, smartjson, smartlog, smartnetwork, smartpromise, smartrequest, smartstring, smartversion };
|
||||
|
||||
// third party
|
||||
import * as rxjs from 'rxjs';
|
||||
|
@ -2,4 +2,5 @@ export * from './docker.classes.host';
|
||||
export * from './docker.classes.container';
|
||||
export * from './docker.classes.image';
|
||||
export * from './docker.classes.network';
|
||||
export * from './docker.classes.secret';
|
||||
export * from './docker.classes.service';
|
||||
|
@ -1,4 +1,4 @@
|
||||
export interface IImageCreationDescriptor {
|
||||
imageUrl: string;
|
||||
tag: string;
|
||||
imageTag?: string;
|
||||
}
|
@ -3,5 +3,5 @@ export * from './image';
|
||||
export * from './label';
|
||||
export * from './network';
|
||||
export * from './port';
|
||||
export * from './secret';
|
||||
export * from './service';
|
||||
|
||||
|
7
ts/interfaces/secret.ts
Normal file
7
ts/interfaces/secret.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import * as interfaces from './';
|
||||
|
||||
export interface ISecretCreationDescriptor {
|
||||
name: string;
|
||||
contentArg: any;
|
||||
labels: interfaces.TLabels;
|
||||
}
|
@ -1,7 +1,12 @@
|
||||
import * as interfaces from './'
|
||||
import * as interfaces from './';
|
||||
import { DockerNetwork } from '../docker.classes.network';
|
||||
import { DockerSecret } from '../docker.classes.secret';
|
||||
|
||||
export interface IServiceCreationDescriptor {
|
||||
Name: string;
|
||||
Image: string;
|
||||
Labels: interfaces.TLabels;
|
||||
networks: DockerNetwork[];
|
||||
networkAlias: string;
|
||||
secrets: DockerSecret[];
|
||||
}
|
Reference in New Issue
Block a user