Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
d9558f7843 | |||
19e67ffdcc | |||
e983b66c28 | |||
c79f6a698f | |||
0ef098e9c8 | |||
d56350ff28 |
54
package-lock.json
generated
54
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.52",
|
"version": "1.0.55",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -180,6 +180,16 @@
|
|||||||
"vinyl-file": "^3.0.0"
|
"vinyl-file": "^3.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@pushrocks/smartjson": {
|
||||||
|
"version": "3.0.8",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartjson/-/smartjson-3.0.8.tgz",
|
||||||
|
"integrity": "sha512-EjC3611RSZaZmK+nXxXrYDBxdxYWtrxjOrZtQzbYn0yM33KSCH0sLIAG8B2wYZVAOj4A2pC8mVxFSJ1w3iRFHg==",
|
||||||
|
"requires": {
|
||||||
|
"@types/fast-json-stable-stringify": "^2.0.0",
|
||||||
|
"fast-json-stable-stringify": "^2.0.0",
|
||||||
|
"lodash.clonedeep": "^4.5.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@pushrocks/smartlog": {
|
"@pushrocks/smartlog": {
|
||||||
"version": "2.0.19",
|
"version": "2.0.19",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartlog/-/smartlog-2.0.19.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartlog/-/smartlog-2.0.19.tgz",
|
||||||
@ -249,9 +259,9 @@
|
|||||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
||||||
},
|
},
|
||||||
"@pushrocks/smartrequest": {
|
"@pushrocks/smartrequest": {
|
||||||
"version": "1.1.23",
|
"version": "1.1.26",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.23.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.26.tgz",
|
||||||
"integrity": "sha512-Hws3YfzIE0b/E3aTkSugLskKWBq7e8HDXEN+RlRyTFONxW/XONKJFTw4mp3jk+puWpYGDoOTcP+Ua4jd19z9pA==",
|
"integrity": "sha512-8tYY+N42qdSTN5x40m6Afqmjo8HwkuZW+iWdMwO8PFxhrymcx8kuf5rYcG0wVLGtBjDgKgVNidb8GgFEhG40Yw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@types/form-data": "^2.2.1",
|
"@types/form-data": "^2.2.1",
|
||||||
@ -313,6 +323,22 @@
|
|||||||
"luxon": "^1.16.0"
|
"luxon": "^1.16.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@pushrocks/smartversion": {
|
||||||
|
"version": "2.0.4",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartversion/-/smartversion-2.0.4.tgz",
|
||||||
|
"integrity": "sha512-LrtaWRn1DD2zIUr8nVAsKwZ+Y55TFIVZ0z3xjwAQ46SoiTrFWooeQ2Xw18DqsEo1cEqMZyGAskaJkOaRkXHtUA==",
|
||||||
|
"requires": {
|
||||||
|
"@types/semver": "^6.0.2",
|
||||||
|
"semver": "^6.3.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"semver": {
|
||||||
|
"version": "6.3.0",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/semver/-/semver-6.3.0.tgz",
|
||||||
|
"integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw=="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"@pushrocks/tapbundle": {
|
"@pushrocks/tapbundle": {
|
||||||
"version": "3.0.13",
|
"version": "3.0.13",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2ftapbundle/-/tapbundle-3.0.13.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2ftapbundle/-/tapbundle-3.0.13.tgz",
|
||||||
@ -363,6 +389,11 @@
|
|||||||
"resolved": "https://verdaccio.lossless.one/@types%2fdefault-gateway/-/default-gateway-3.0.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/@types%2fdefault-gateway/-/default-gateway-3.0.0.tgz",
|
||||||
"integrity": "sha512-Pg0Vygi/mDsW+jAlknUf9ECJAcF6GyluMzXofb7hs4iLlichbdQLveweUN0SLb4TI6drRvaMoINcMzPQb0XB+w=="
|
"integrity": "sha512-Pg0Vygi/mDsW+jAlknUf9ECJAcF6GyluMzXofb7hs4iLlichbdQLveweUN0SLb4TI6drRvaMoINcMzPQb0XB+w=="
|
||||||
},
|
},
|
||||||
|
"@types/fast-json-stable-stringify": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/@types%2ffast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-mky/O83TXmGY39P1H9YbUpjV6l6voRYlufqfFCvel8l1phuy8HRjdWc1rrPuN53ITBJlbyMSV6z3niOySO5pgQ=="
|
||||||
|
},
|
||||||
"@types/figures": {
|
"@types/figures": {
|
||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
"resolved": "https://verdaccio.lossless.one/@types%2ffigures/-/figures-3.0.1.tgz",
|
"resolved": "https://verdaccio.lossless.one/@types%2ffigures/-/figures-3.0.1.tgz",
|
||||||
@ -409,6 +440,11 @@
|
|||||||
"resolved": "https://verdaccio.lossless.one/@types%2fportscanner/-/portscanner-2.1.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/@types%2fportscanner/-/portscanner-2.1.0.tgz",
|
||||||
"integrity": "sha512-II0FA05TSt4r7PBUvYTqub+yrXTRu7L/PQnTtjcJYzAeBts6xHM/OA2MKyeQ80sFxX7p+0BTo68Nh1P3N6Oj/g=="
|
"integrity": "sha512-II0FA05TSt4r7PBUvYTqub+yrXTRu7L/PQnTtjcJYzAeBts6xHM/OA2MKyeQ80sFxX7p+0BTo68Nh1P3N6Oj/g=="
|
||||||
},
|
},
|
||||||
|
"@types/semver": {
|
||||||
|
"version": "6.0.2",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/@types%2fsemver/-/semver-6.0.2.tgz",
|
||||||
|
"integrity": "sha512-G1Ggy7/9Nsa1Jt2yiBR2riEuyK2DFNnqow6R7cromXPMNynackRY1vqFTLz/gwnef1LHokbXThcPhqMRjUbkpQ=="
|
||||||
|
},
|
||||||
"@types/vinyl": {
|
"@types/vinyl": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"resolved": "https://verdaccio.lossless.one/@types%2fvinyl/-/vinyl-2.0.3.tgz",
|
"resolved": "https://verdaccio.lossless.one/@types%2fvinyl/-/vinyl-2.0.3.tgz",
|
||||||
@ -902,6 +938,11 @@
|
|||||||
"strip-eof": "^1.0.0"
|
"strip-eof": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fast-json-stable-stringify": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
|
||||||
|
"integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I="
|
||||||
|
},
|
||||||
"figures": {
|
"figures": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://verdaccio.lossless.one/figures/-/figures-3.0.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/figures/-/figures-3.0.0.tgz",
|
||||||
@ -1193,6 +1234,11 @@
|
|||||||
"resolved": "https://verdaccio.lossless.one/lodash/-/lodash-4.17.15.tgz",
|
"resolved": "https://verdaccio.lossless.one/lodash/-/lodash-4.17.15.tgz",
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
|
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
|
||||||
},
|
},
|
||||||
|
"lodash.clonedeep": {
|
||||||
|
"version": "4.5.0",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
||||||
|
"integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8="
|
||||||
|
},
|
||||||
"lodash.isfinite": {
|
"lodash.isfinite": {
|
||||||
"version": "3.3.2",
|
"version": "3.3.2",
|
||||||
"resolved": "https://verdaccio.lossless.one/lodash.isfinite/-/lodash.isfinite-3.3.2.tgz",
|
"resolved": "https://verdaccio.lossless.one/lodash.isfinite/-/lodash.isfinite-3.3.2.tgz",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.52",
|
"version": "1.0.55",
|
||||||
"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",
|
||||||
@ -28,10 +28,13 @@
|
|||||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^3.0.11",
|
"@pushrocks/lik": "^3.0.11",
|
||||||
|
"@pushrocks/smartjson": "^3.0.8",
|
||||||
"@pushrocks/smartlog": "^2.0.19",
|
"@pushrocks/smartlog": "^2.0.19",
|
||||||
"@pushrocks/smartnetwork": "^1.1.14",
|
"@pushrocks/smartnetwork": "^1.1.14",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@pushrocks/smartrequest": "^1.1.23",
|
"@pushrocks/smartrequest": "^1.1.26",
|
||||||
|
"@pushrocks/smartstring": "^3.0.10",
|
||||||
|
"@pushrocks/smartversion": "^2.0.4",
|
||||||
"rxjs": "^6.5.3"
|
"rxjs": "^6.5.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
39
test/test.ts
39
test/test.ts
@ -5,12 +5,12 @@ 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 () => {
|
tap.test('should create a docker swarm', async () => {
|
||||||
|
await testDockerHost.activateSwarm();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Containers
|
// Containers
|
||||||
@ -57,13 +57,29 @@ 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',
|
||||||
|
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,15 +87,28 @@ 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 testSecret = await docker.DockerSecret.createSecret(testDockerHost, {
|
||||||
|
name: 'serviceSecret',
|
||||||
|
labels: {},
|
||||||
|
contentArg: '{"hi": "wow"}'
|
||||||
|
});
|
||||||
|
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'
|
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();
|
||||||
|
@ -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 {
|
||||||
/**
|
/**
|
||||||
@ -52,6 +53,14 @@ export class DockerHost {
|
|||||||
return containerArray;
|
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',
|
'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);
|
||||||
@ -130,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
|
||||||
);
|
);
|
||||||
|
@ -45,6 +45,7 @@ export class DockerImage {
|
|||||||
} because it is already tagged with ${imageTag}`
|
} because it is already tagged with ${imageTag}`
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
imageUrlObject.imageUrl = imageUrl;
|
||||||
imageUrlObject.imageTag = imageTag;
|
imageUrlObject.imageTag = imageTag;
|
||||||
}
|
}
|
||||||
} else if (!imageUrlObject.imageTag) {
|
} else if (!imageUrlObject.imageTag) {
|
||||||
|
@ -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];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
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}`);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ import * as interfaces from './interfaces';
|
|||||||
|
|
||||||
import { DockerHost } from './docker.classes.host';
|
import { DockerHost } from './docker.classes.host';
|
||||||
import { DockerImage } from './docker.classes.image';
|
import { DockerImage } from './docker.classes.image';
|
||||||
|
import { DockerSecret } from './docker.classes.secret';
|
||||||
|
|
||||||
export class DockerService {
|
export class DockerService {
|
||||||
// STATIC
|
// STATIC
|
||||||
@ -10,24 +11,43 @@ 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, {
|
||||||
imageUrl: serviceCreationDescriptor.Image
|
imageUrl: serviceCreationDescriptor.Image
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const serviceVersion = serviceImage.Labels.version;
|
||||||
|
serviceCreationDescriptor.Labels.version = serviceVersion;
|
||||||
|
|
||||||
const networkArray: any[] = [];
|
const networkArray: any[] = [];
|
||||||
for (const network of serviceCreationDescriptor.networks) {
|
for (const network of serviceCreationDescriptor.networks) {
|
||||||
networkArray.push({
|
networkArray.push({
|
||||||
@ -36,28 +56,125 @@ export class DockerService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerHost.request('POST', '/services/create', {
|
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,
|
Name: serviceCreationDescriptor.Name,
|
||||||
TaskTemplate: {
|
TaskTemplate: {
|
||||||
ContainerSpec: {
|
ContainerSpec: {
|
||||||
Image: serviceCreationDescriptor.Image,
|
Image: serviceCreationDescriptor.Image,
|
||||||
Labels: serviceCreationDescriptor.Labels
|
Labels: serviceCreationDescriptor.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
|
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; // 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
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,13 +1,16 @@
|
|||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
import * as lik from '@pushrocks/lik';
|
import * as lik from '@pushrocks/lik';
|
||||||
|
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 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';
|
||||||
|
import * as smartstring from '@pushrocks/smartstring'
|
||||||
|
import * as smartversion from '@pushrocks/smartversion';
|
||||||
|
|
||||||
smartlog.defaultLogger.enableConsole();
|
smartlog.defaultLogger.enableConsole();
|
||||||
|
|
||||||
export { lik, smartlog, smartnetwork, smartpromise, smartrequest };
|
export { lik, smartjson, smartlog, smartnetwork, smartpromise, smartrequest, smartstring, smartversion };
|
||||||
|
|
||||||
// third party
|
// third party
|
||||||
import * as rxjs from 'rxjs';
|
import * as rxjs from 'rxjs';
|
||||||
|
@ -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';
|
||||||
|
@ -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';
|
||||||
|
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,5 +1,6 @@
|
|||||||
import * as interfaces from './';
|
import * as interfaces from './';
|
||||||
import { DockerNetwork } from '../docker.classes.network';
|
import { DockerNetwork } from '../docker.classes.network';
|
||||||
|
import { DockerSecret } from '../docker.classes.secret';
|
||||||
|
|
||||||
export interface IServiceCreationDescriptor {
|
export interface IServiceCreationDescriptor {
|
||||||
Name: string;
|
Name: string;
|
||||||
@ -7,4 +8,5 @@ export interface IServiceCreationDescriptor {
|
|||||||
Labels: interfaces.TLabels;
|
Labels: interfaces.TLabels;
|
||||||
networks: DockerNetwork[];
|
networks: DockerNetwork[];
|
||||||
networkAlias: string;
|
networkAlias: string;
|
||||||
|
secrets: DockerSecret[];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user