Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
53062e70d4 | |||
3e70dc465b | |||
49445d93c6 | |||
4f838837f8 | |||
c76968bbe8 | |||
6c5e5644b1 | |||
5cf80944fe | |||
cdb69c5f17 | |||
178c1d2df1 | |||
43d9da808b | |||
15f5c38eb0 | |||
225c1be14c | |||
44f2aab2f6 | |||
b69315f1d3 | |||
7d20804986 | |||
0aab639fbd | |||
794bb60dfc |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.67",
|
"version": "1.0.76",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.67",
|
"version": "1.0.76",
|
||||||
"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",
|
||||||
|
13
test/test.ts
13
test/test.ts
@ -87,29 +87,24 @@ tap.test('should create a service', async () => {
|
|||||||
Name: 'testNetwork'
|
Name: 'testNetwork'
|
||||||
});
|
});
|
||||||
const testSecret = await docker.DockerSecret.createSecret(testDockerHost, {
|
const testSecret = await docker.DockerSecret.createSecret(testDockerHost, {
|
||||||
name: 'serviceSecret',
|
name: 'testSecret',
|
||||||
version: '0.0.1',
|
version: '0.0.1',
|
||||||
labels: {},
|
labels: {},
|
||||||
contentArg: '{"hi": "wow"}'
|
contentArg: '{"hi": "wow"}'
|
||||||
});
|
});
|
||||||
const testImage = await docker.DockerImage.createFromRegistry(testDockerHost, {
|
const testImage = await docker.DockerImage.createFromRegistry(testDockerHost, {
|
||||||
imageUrl: 'nginx:latest'
|
imageUrl: 'registry.gitlab.com/hosttoday/ht-docker-static'
|
||||||
});
|
});
|
||||||
const testService = await docker.DockerService.createService(testDockerHost, {
|
const testService = await docker.DockerService.createService(testDockerHost, {
|
||||||
image: testImage,
|
image: testImage,
|
||||||
labels: {
|
labels: {},
|
||||||
testlabel: 'hi'
|
|
||||||
},
|
|
||||||
name: 'testService',
|
name: 'testService',
|
||||||
networks: [testNetwork],
|
networks: [testNetwork],
|
||||||
networkAlias: 'testService',
|
networkAlias: 'testService',
|
||||||
secrets: [testSecret],
|
secrets: [testSecret],
|
||||||
ports: []
|
ports: ['3000:80']
|
||||||
});
|
});
|
||||||
|
|
||||||
await testSecret.update(`{"updated": "socool"}`);
|
|
||||||
await testService.update();
|
|
||||||
|
|
||||||
await testService.remove();
|
await testService.remove();
|
||||||
await testNetwork.remove();
|
await testNetwork.remove();
|
||||||
await testSecret.remove();
|
await testSecret.remove();
|
||||||
|
@ -62,7 +62,7 @@ export class DockerHost {
|
|||||||
username: gitlabAuthArray[0],
|
username: gitlabAuthArray[0],
|
||||||
password: gitlabAuthArray[1],
|
password: gitlabAuthArray[1],
|
||||||
serveraddress: 'registry.gitlab.com'
|
serveraddress: 'registry.gitlab.com'
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -137,6 +137,10 @@ export class DockerImage {
|
|||||||
|
|
||||||
// get stuff
|
// get stuff
|
||||||
public async getVersion() {
|
public async getVersion() {
|
||||||
return this.Labels.version;
|
if (this.Labels && this.Labels.version) {
|
||||||
|
return this.Labels.version;
|
||||||
|
} else {
|
||||||
|
return 'x.x.x';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ 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 { DockerService } from './docker.classes.service';
|
||||||
|
|
||||||
export class DockerNetwork {
|
export class DockerNetwork {
|
||||||
public static async getNetworks(dockerHost: DockerHost): Promise<DockerNetwork[]> {
|
public static async getNetworks(dockerHost: DockerHost): Promise<DockerNetwork[]> {
|
||||||
@ -90,4 +91,28 @@ export class DockerNetwork {
|
|||||||
public async remove() {
|
public async remove() {
|
||||||
const response = await this.dockerHost.request('DELETE', `/networks/${this.Id}`);
|
const response = await this.dockerHost.request('DELETE', `/networks/${this.Id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getContainersOnNetwork(): Promise<Array<{
|
||||||
|
Name: string;
|
||||||
|
EndpointID: string;
|
||||||
|
MacAddress: string;
|
||||||
|
IPv4Address: string;
|
||||||
|
IPv6Address: string;
|
||||||
|
}>> {
|
||||||
|
const returnArray = [];
|
||||||
|
const response = await this.dockerHost.request('GET', `/networks/${this.Id}`);
|
||||||
|
for (const key of Object.keys(response.body.Containers)) {
|
||||||
|
returnArray.push(response.body.Containers[key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnArray;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getIpForService(serviceArg: DockerService) {
|
||||||
|
const containersOnNetwork = await this.getContainersOnNetwork();
|
||||||
|
const containersOfService = containersOnNetwork.filter(container => {
|
||||||
|
return container.Name.startsWith(serviceArg.Spec.Name);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,34 @@ export class DockerService {
|
|||||||
version: serviceVersion
|
version: serviceVersion
|
||||||
};
|
};
|
||||||
|
|
||||||
const networkArray: any[] = [];
|
const mounts: Array<{
|
||||||
|
/**
|
||||||
|
* the target inside the container
|
||||||
|
*/
|
||||||
|
Target: string;
|
||||||
|
/**
|
||||||
|
* The Source from which to mount the data (Volume or host path)
|
||||||
|
*/
|
||||||
|
Source: string;
|
||||||
|
Type: 'bind' | 'volume' | 'tmpfs' | 'npipe';
|
||||||
|
ReadOnly: boolean;
|
||||||
|
Consistency: 'default' | 'consistent' | 'cached' | 'delegated';
|
||||||
|
}> = [];
|
||||||
|
if (serviceCreationDescriptor.accessHostDockerSock) {
|
||||||
|
mounts.push({
|
||||||
|
Target: '/var/run/docker.sock',
|
||||||
|
Source: '/var/run/docker.sock',
|
||||||
|
Consistency: 'default',
|
||||||
|
ReadOnly: false,
|
||||||
|
Type: 'bind'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const networkArray: Array<{
|
||||||
|
Target: string;
|
||||||
|
Aliases: string[];
|
||||||
|
}> = [];
|
||||||
|
|
||||||
for (const network of serviceCreationDescriptor.networks) {
|
for (const network of serviceCreationDescriptor.networks) {
|
||||||
networkArray.push({
|
networkArray.push({
|
||||||
Target: network.Name,
|
Target: network.Name,
|
||||||
@ -59,12 +86,23 @@ export class DockerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const ports = [];
|
const ports = [];
|
||||||
|
for (const port of serviceCreationDescriptor.ports) {
|
||||||
|
const portArray = port.split(':');
|
||||||
|
const hostPort = portArray[0];
|
||||||
|
const containerPort = portArray[1];
|
||||||
|
ports.push({
|
||||||
|
Protocol: 'tcp',
|
||||||
|
PublishedPort: parseInt(hostPort, 10),
|
||||||
|
TargetPort: parseInt(containerPort, 10)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets configure secrets
|
||||||
const secretArray: any[] = [];
|
const secretArray: any[] = [];
|
||||||
for (const secret of serviceCreationDescriptor.secrets) {
|
for (const secret of serviceCreationDescriptor.secrets) {
|
||||||
secretArray.push({
|
secretArray.push({
|
||||||
File: {
|
File: {
|
||||||
Name: 'secret.json',
|
Name: 'secret.json', // TODO: make sure that works with multiple secrets
|
||||||
UID: '33',
|
UID: '33',
|
||||||
GID: '33',
|
GID: '33',
|
||||||
Mode: 384
|
Mode: 384
|
||||||
@ -74,13 +112,23 @@ export class DockerService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lets configure limits
|
||||||
|
const limits = {
|
||||||
|
MemoryBytes: 1000 * 1000000
|
||||||
|
};
|
||||||
|
|
||||||
|
if (serviceCreationDescriptor.resources) {
|
||||||
|
limits.MemoryBytes = serviceCreationDescriptor.resources.memorySizeMB * 1000000;
|
||||||
|
}
|
||||||
|
|
||||||
const response = await dockerHost.request('POST', '/services/create', {
|
const response = await dockerHost.request('POST', '/services/create', {
|
||||||
Name: serviceCreationDescriptor.name,
|
Name: serviceCreationDescriptor.name,
|
||||||
TaskTemplate: {
|
TaskTemplate: {
|
||||||
ContainerSpec: {
|
ContainerSpec: {
|
||||||
Image: serviceCreationDescriptor.image.RepoTags[0],
|
Image: serviceCreationDescriptor.image.RepoTags[0],
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
Secrets: secretArray
|
Secrets: secretArray,
|
||||||
|
Mounts: mounts
|
||||||
},
|
},
|
||||||
UpdateConfig: {
|
UpdateConfig: {
|
||||||
Parallelism: 0,
|
Parallelism: 0,
|
||||||
@ -89,7 +137,10 @@ export class DockerService {
|
|||||||
Monitor: 15000000000,
|
Monitor: 15000000000,
|
||||||
MaxFailureRatio: 0.15
|
MaxFailureRatio: 0.15
|
||||||
},
|
},
|
||||||
ForceUpdate: 1
|
ForceUpdate: 1,
|
||||||
|
Resources: {
|
||||||
|
Limits: limits
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
Networks: networkArray,
|
Networks: networkArray,
|
||||||
@ -141,25 +192,6 @@ export class DockerService {
|
|||||||
this.dockerHostRef = dockerHostArg;
|
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() {
|
public async remove() {
|
||||||
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
|
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
|
||||||
}
|
}
|
||||||
@ -186,10 +218,4 @@ export class DockerService {
|
|||||||
console.log(`service ${this.Spec.Name} is up to date.`);
|
console.log(`service ${this.Spec.Name} is up to date.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateFromRegistry() {
|
|
||||||
if (await this.needsUpdate()) {
|
|
||||||
this.update();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,8 @@ export interface IServiceCreationDescriptor {
|
|||||||
networkAlias: string;
|
networkAlias: string;
|
||||||
secrets: DockerSecret[];
|
secrets: DockerSecret[];
|
||||||
ports: string[];
|
ports: string[];
|
||||||
|
accessHostDockerSock?: boolean;
|
||||||
|
resources?: {
|
||||||
|
memorySizeMB: number
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user