fix(core): update

This commit is contained in:
2019-09-12 14:45:36 +02:00
parent e983b66c28
commit 19e67ffdcc
11 changed files with 209 additions and 30 deletions

View File

@ -45,6 +45,7 @@ export class DockerImage {
} because it is already tagged with ${imageTag}`
);
} else {
imageUrlObject.imageUrl = imageUrl;
imageUrlObject.imageTag = imageTag;
}
} else if (!imageUrlObject.imageTag) {

View 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}`);
}
}

View File

@ -3,6 +3,7 @@ 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
@ -17,7 +18,10 @@ export class DockerService {
return services;
}
public static async getServiceByName(dockerHost: DockerHost, networkName: string): Promise<DockerService> {
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;
@ -33,7 +37,10 @@ export class DockerService {
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
): Promise<DockerService> {
// 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, {
imageUrl: serviceCreationDescriptor.Image
});
@ -49,12 +56,34 @@ export class DockerService {
});
}
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
},
UpdateConfig: {
Parallelism: 0,
Delay: 0,
FailureAction: 'pause',
Monitor: 15000000000,
MaxFailureRatio: 0.15
},
ForceUpdate: 1
},
@ -62,13 +91,16 @@ export class DockerService {
Networks: networkArray
});
const createdService = await DockerService.getServiceByName(dockerHost, serviceCreationDescriptor.Name);
const createdService = await DockerService.getServiceByName(
dockerHost,
serviceCreationDescriptor.Name
);
return createdService;
}
// INSTANCE
public dockerHostRef: DockerHost;
public ID: string;
public Version: { Index: number };
public CreatedAt: string;
@ -80,14 +112,23 @@ export class DockerService {
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[]]
Networks: [any[]];
};
public Endpoint: { Spec: {}, VirtualIPs: [any[]] };
public Endpoint: { Spec: {}; VirtualIPs: [any[]] };
constructor(dockerHostArg: DockerHost) {
this.dockerHostRef = dockerHostArg;
@ -96,19 +137,19 @@ export class DockerService {
public async update() {
const labels: interfaces.TLabels = {
...this.Spec.Labels,
version: 'x.x.x'
};
const dockerData = await this.dockerHostRef.request('POST', `/servces/${this.ID}/update?version=${this.Version.Index}`, {
Name: this.Spec.Name,
TaskTemplate: {
ContainerSpec: {
Image: this.Spec.TaskTemplate.ContainerSpec.Image,
Labels: labels
},
ForceUpdate: 1
},
Labels: labels,
});
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);
}
@ -116,17 +157,17 @@ export class DockerService {
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
}
public async reReadFromDockerEngine () {
public async reReadFromDockerEngine() {
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
Object.assign(this, dockerData);
}
public async updateFromRegistry () {
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
imageUrl: this.Spec.TaskTemplate.ContainerSpec.Image
});
const imageVersion = new plugins.smartversion.SmartVersion(dockerImage.Labels.version);
@ -135,8 +176,5 @@ export class DockerService {
console.log('service needs to be updated');
this.update();
}
}
}

View File

@ -1,14 +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, smartnetwork, smartpromise, smartrequest, smartversion };
export { lik, smartjson, smartlog, smartnetwork, smartpromise, smartrequest, smartstring, smartversion };
// third party
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.image';
export * from './docker.classes.network';
export * from './docker.classes.secret';
export * from './docker.classes.service';

View File

@ -3,4 +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
View File

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

View File

@ -1,5 +1,6 @@
import * as interfaces from './';
import { DockerNetwork } from '../docker.classes.network';
import { DockerSecret } from '../docker.classes.secret';
export interface IServiceCreationDescriptor {
Name: string;
@ -7,4 +8,5 @@ export interface IServiceCreationDescriptor {
Labels: interfaces.TLabels;
networks: DockerNetwork[];
networkAlias: string;
secrets: DockerSecret[];
}