Compare commits

...

15 Commits

Author SHA1 Message Date
49445d93c6 1.0.75 2019-09-21 21:57:57 +02:00
4f838837f8 fix(core): update 2019-09-21 21:57:57 +02:00
c76968bbe8 1.0.74 2019-09-20 16:29:44 +02:00
6c5e5644b1 fix(core): update 2019-09-20 16:29:43 +02:00
5cf80944fe 1.0.73 2019-09-19 20:05:57 +02:00
cdb69c5f17 fix(core): update 2019-09-19 20:05:56 +02:00
178c1d2df1 1.0.72 2019-09-18 17:29:43 +02:00
43d9da808b fix(core): update 2019-09-18 17:29:43 +02:00
15f5c38eb0 1.0.71 2019-09-15 15:08:48 +02:00
225c1be14c fix(core): update 2019-09-15 15:08:48 +02:00
44f2aab2f6 1.0.70 2019-09-13 23:08:17 +02:00
b69315f1d3 1.0.69 2019-09-13 23:05:56 +02:00
7d20804986 fix(core): update 2019-09-13 23:05:55 +02:00
0aab639fbd 1.0.68 2019-09-13 22:43:30 +02:00
794bb60dfc fix(core): update 2019-09-13 22:43:29 +02:00
8 changed files with 84 additions and 42 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/docker",
"version": "1.0.67",
"version": "1.0.75",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/docker",
"version": "1.0.67",
"version": "1.0.75",
"description": "easy communication with docker remote api from node, TypeScript ready",
"private": false,
"main": "dist/index.js",

View File

@ -87,29 +87,24 @@ tap.test('should create a service', async () => {
Name: 'testNetwork'
});
const testSecret = await docker.DockerSecret.createSecret(testDockerHost, {
name: 'serviceSecret',
name: 'testSecret',
version: '0.0.1',
labels: {},
contentArg: '{"hi": "wow"}'
});
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, {
image: testImage,
labels: {
testlabel: 'hi'
},
labels: {},
name: 'testService',
networks: [testNetwork],
networkAlias: 'testService',
secrets: [testSecret],
ports: []
ports: ['3000:80']
});
await testSecret.update(`{"updated": "socool"}`);
await testService.update();
await testService.remove();
await testNetwork.remove();
await testSecret.remove();

View File

@ -62,7 +62,7 @@ export class DockerHost {
username: gitlabAuthArray[0],
password: gitlabAuthArray[1],
serveraddress: 'registry.gitlab.com'
})
});
}
/**

View File

@ -137,6 +137,10 @@ export class DockerImage {
// get stuff
public async getVersion() {
return this.Labels.version;
if (this.Labels && this.Labels.version) {
return this.Labels.version;
} else {
return 'x.x.x';
}
}
}

View File

@ -90,4 +90,17 @@ export class DockerNetwork {
public async remove() {
const response = await this.dockerHost.request('DELETE', `/networks/${this.Id}`);
}
public async getContainersOnNetwork(): Promise<{
[key: string]: {
Name: string;
EndpointID: string;
MacAddress: string;
IPv4Address: string;
IPv6Address: string;
};
}> {
const response = await this.dockerHost.request('GET', `/networks/${this.Id}`);
return response.body.Containers;
}
}

View File

@ -50,7 +50,34 @@ export class DockerService {
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) {
networkArray.push({
Target: network.Name,
@ -59,12 +86,23 @@ export class DockerService {
}
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[] = [];
for (const secret of serviceCreationDescriptor.secrets) {
secretArray.push({
File: {
Name: 'secret.json',
Name: 'secret.json', // TODO: make sure that works with multiple secrets
UID: '33',
GID: '33',
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', {
Name: serviceCreationDescriptor.name,
TaskTemplate: {
ContainerSpec: {
Image: serviceCreationDescriptor.image.RepoTags[0],
Labels: labels,
Secrets: secretArray
Secrets: secretArray,
Mounts: mounts
},
UpdateConfig: {
Parallelism: 0,
@ -89,7 +137,10 @@ export class DockerService {
Monitor: 15000000000,
MaxFailureRatio: 0.15
},
ForceUpdate: 1
ForceUpdate: 1,
Resources: {
Limits: limits
}
},
Labels: labels,
Networks: networkArray,
@ -141,25 +192,6 @@ export class DockerService {
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}`);
}
@ -186,10 +218,4 @@ export class DockerService {
console.log(`service ${this.Spec.Name} is up to date.`);
}
}
public async updateFromRegistry() {
if (await this.needsUpdate()) {
this.update();
}
}
}

View File

@ -11,4 +11,8 @@ export interface IServiceCreationDescriptor {
networkAlias: string;
secrets: DockerSecret[];
ports: string[];
accessHostDockerSock?: boolean;
resources?: {
memorySizeMB: number
};
}