fix(core): update

This commit is contained in:
Philipp Kunz 2019-01-18 00:45:29 +01:00
parent 85448a21fc
commit 85639f29af
5 changed files with 95 additions and 54 deletions

40
package-lock.json generated
View File

@ -90,9 +90,9 @@
} }
}, },
"@pushrocks/qenv": { "@pushrocks/qenv": {
"version": "3.0.2", "version": "4.0.0",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fqenv/-/qenv-3.0.2.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fqenv/-/qenv-4.0.0.tgz",
"integrity": "sha512-gXW9Cib6izqY7W1MssryFBrjIoKwadftjv5G3y/BE4LboCkGazqjMJsea6z6G79rwvX1M+yxEEcIdIsDvBBSiA==", "integrity": "sha512-3/odPJaGp3ALtcSG/+N3dGtFX/CVBed9a5XKDa0bv7LoKCgYZeQwpF3jkCnNPjs7GKyzOzALHHID1L+zcb1RXw==",
"dev": true, "dev": true,
"requires": { "requires": {
"@pushrocks/smartfile": "^6.0.6", "@pushrocks/smartfile": "^6.0.6",
@ -162,11 +162,11 @@
} }
}, },
"@pushrocks/smartlog": { "@pushrocks/smartlog": {
"version": "2.0.9", "version": "2.0.11",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartlog/-/smartlog-2.0.9.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartlog/-/smartlog-2.0.11.tgz",
"integrity": "sha512-F2u3O8OOQ7QXgg9o0lI7HnqEhSQZo2A8u0EuNbQH+Kj2oL62xDwjAg6KkINwH7ObSWpaldZ+I28prG0JxaRzvg==", "integrity": "sha512-V8SMzAAqDpl1+CJ9b3w5e/1ARjk4JOPZ35qZIllVBhHr8meCqnPE2immlfBYWmEp1xy3ntdAA+Lgewtu+iVk6A==",
"requires": { "requires": {
"@pushrocks/smartlog-interfaces": "^2.0.2" "@pushrocks/smartlog-interfaces": "^2.0.5"
} }
}, },
"@pushrocks/smartlog-interfaces": { "@pushrocks/smartlog-interfaces": {
@ -227,12 +227,12 @@
} }
}, },
"@pushrocks/smartstring": { "@pushrocks/smartstring": {
"version": "3.0.5", "version": "3.0.8",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartstring/-/smartstring-3.0.5.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartstring/-/smartstring-3.0.8.tgz",
"integrity": "sha512-Bws2s6+Pc0HIXA35QH6cl6LJywhZ1U5icjyYMx+1kH8nURG+njzkacfarr9NWqyVYYOVu7SgXLzp9sR0eH7k9g==", "integrity": "sha512-twmCGF47+TJoiCpj6t5MxwcKDDJEATuKqJXxftcHOlXWW743LJ/1bCpMueDdtumo5EheUfSxb43aRse3DU3wig==",
"requires": { "requires": {
"crypto-random-string": "^1.0.0", "crypto-random-string": "^1.0.0",
"js-base64": "^2.4.9", "js-base64": "^2.5.0",
"normalize-newline": "^3.0.0", "normalize-newline": "^3.0.0",
"randomatic": "^3.1.1", "randomatic": "^3.1.1",
"strip-indent": "^2.0.0" "strip-indent": "^2.0.0"
@ -774,9 +774,9 @@
} }
}, },
"glob": { "glob": {
"version": "7.1.2", "version": "7.1.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", "resolved": "https://verdaccio.lossless.one/glob/-/glob-7.1.3.tgz",
"integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"fs.realpath": "^1.0.0", "fs.realpath": "^1.0.0",
@ -965,9 +965,9 @@
} }
}, },
"math-random": { "math-random": {
"version": "1.0.1", "version": "1.0.4",
"resolved": "https://registry.npmjs.org/math-random/-/math-random-1.0.1.tgz", "resolved": "https://verdaccio.lossless.one/math-random/-/math-random-1.0.4.tgz",
"integrity": "sha1-izqsWIuKZuSXXjzepn97sylgH6w=" "integrity": "sha512-rUxjysqif/BZQH2yhd5Aaq7vXMSx9NdEsQcyA07uEzIvxgI7zIr33gGsh+RU0/XjmQpCW7RsVof1vlkvQVCK5A=="
}, },
"mem": { "mem": {
"version": "4.0.0", "version": "4.0.0",
@ -1410,9 +1410,9 @@
"integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==" "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ=="
}, },
"tslint": { "tslint": {
"version": "5.12.0", "version": "5.12.1",
"resolved": "https://verdaccio.lossless.one/tslint/-/tslint-5.12.0.tgz", "resolved": "https://verdaccio.lossless.one/tslint/-/tslint-5.12.1.tgz",
"integrity": "sha512-CKEcH1MHUBhoV43SA/Jmy1l24HJJgI0eyLbBNSRyFlsQvb9v6Zdq+Nz2vEOH00nC5SUx4SneJ59PZUS/ARcokQ==", "integrity": "sha512-sfodBHOucFg6egff8d1BvuofoOQ/nOeYNfbp7LDlKBcLNrL3lmS5zoiDGyOMdT7YsEXAwWpTdAHwOGOc8eRZAw==",
"dev": true, "dev": true,
"requires": { "requires": {
"babel-code-frame": "^6.22.0", "babel-code-frame": "^6.22.0",

View File

@ -26,18 +26,18 @@
"dependencies": { "dependencies": {
"@pushrocks/lik": "^3.0.4", "@pushrocks/lik": "^3.0.4",
"@pushrocks/smartfile": "^6.0.11", "@pushrocks/smartfile": "^6.0.11",
"@pushrocks/smartlog": "^2.0.9", "@pushrocks/smartlog": "^2.0.11",
"@pushrocks/smartpromise": "^2.0.5", "@pushrocks/smartpromise": "^2.0.5",
"@pushrocks/smartshell": "^2.0.13", "@pushrocks/smartshell": "^2.0.13",
"@pushrocks/smartstring": "^3.0.5" "@pushrocks/smartstring": "^3.0.8"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.4", "@gitzone/tsbuild": "^2.1.4",
"@gitzone/tsrun": "^1.1.17", "@gitzone/tsrun": "^1.1.17",
"@gitzone/tstest": "^1.0.18", "@gitzone/tstest": "^1.0.18",
"@pushrocks/qenv": "^3.0.2", "@pushrocks/qenv": "^4.0.0",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^3.0.7",
"tslint": "^5.12.0", "tslint": "^5.12.1",
"tslint-config-prettier": "^1.17.0" "tslint-config-prettier": "^1.17.0"
} }
} }

View File

@ -31,9 +31,9 @@ tap.test(`should produce an instance of NginxConfig`, async () => {
expect(testNginxZone01).to.be.instanceof(smartnginx.NginxHost); expect(testNginxZone01).to.be.instanceof(smartnginx.NginxHost);
}); });
tap.test('.addZone() should add a zone to NginxConfig Object', async () => { tap.test('.addHostCandidate() should add a zone to NginxConfig Object', async () => {
testSmartNginx.addHost(testNginxZone01); testSmartNginx.addHostCandidate(testNginxZone01);
testSmartNginx.addHost(testNginxZone02); testSmartNginx.addHostCandidate(testNginxZone02);
}); });
tap.test('.deploy() should deploy a config from an instance', async () => { tap.test('.deploy() should deploy a config from an instance', async () => {

View File

@ -17,13 +17,13 @@ export class NginxHost implements IHostConfig {
/** /**
* smartnginxInstance this NginHost belongs to * smartnginxInstance this NginHost belongs to
*/ */
smartnginxInstance: SmartNginx; public smartnginxInstance: SmartNginx;
hostName: string; // the host name e.g. domain name public hostName: string; // the host name e.g. domain name
destination: string; public destination: string;
configString: string; // the actual host config file as string public configString: string; // the actual host config file as string
privateKey: string; public privateKey: string;
publicKey: string; public publicKey: string;
constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfig) { constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfig) {
this.smartnginxInstance = smartnginxInstanceArg; this.smartnginxInstance = smartnginxInstanceArg;

View File

@ -10,7 +10,11 @@ import { IHostConfig } from './interfaces/hostconfig';
*/ */
export class SmartNginx { export class SmartNginx {
public logger: plugins.smartlog.Smartlog; public logger: plugins.smartlog.Smartlog;
private hosts = new plugins.lik.Objectmap<NginxHost>();
// the objectmaps
private deployedHosts = new plugins.lik.Objectmap<NginxHost>();
private hostCandidates = new plugins.lik.Objectmap<NginxHost>();
public nginxProcess: NginxProcess = new NginxProcess(this); public nginxProcess: NginxProcess = new NginxProcess(this);
constructor(optionsArg: { logger?: plugins.smartlog.Smartlog }) { constructor(optionsArg: { logger?: plugins.smartlog.Smartlog }) {
optionsArg.logger optionsArg.logger
@ -26,9 +30,9 @@ export class SmartNginx {
* add a host * add a host
* @param nginxHostArg * @param nginxHostArg
*/ */
public addHost(optionsArg: IHostConfig): NginxHost { public addHostCandidate(optionsArg: IHostConfig): NginxHost {
const nginxHost = new NginxHost(this, optionsArg); const nginxHost = new NginxHost(this, optionsArg);
this.hosts.add(nginxHost); this.hostCandidates.add(nginxHost);
return nginxHost; return nginxHost;
} }
@ -36,47 +40,84 @@ export class SmartNginx {
* Gets a NginxHost by hostname * Gets a NginxHost by hostname
* @param hostNameArg * @param hostNameArg
*/ */
public getNginxHostByHostName(hostNameArg: string): NginxHost { public getDeployedNginxHostByHostName(hostNameArg: string): NginxHost {
return this.hosts.find(nginxHost => { return this.deployedHosts.find(nginxHost => {
return nginxHost.hostName === hostNameArg; return nginxHost.hostName === hostNameArg;
}); });
} }
/** /**
* listHosts * listHosts
*/ */
public listHosts(): NginxHost[] { public async listDeployedHosts(): Promise<NginxHost[]> {
return this.hosts.getArray(); return this.deployedHosts.getArray();
} }
/** /**
* remove a Host * remove a Host
* @param nginxHostArg * @param nginxHostArg
*/ */
public removeHost(nginxHostArg: NginxHost) { public async removeDeployedHost(nginxHostArg: NginxHost) {
this.hosts.remove(nginxHostArg); if (this.hostCandidates.isEmpty()) {
this.deployedHosts.forEach(hostArg => {
this.hostCandidates.add(hostArg);
});
}
this.hostCandidates.remove(nginxHostArg);
this.deploy();
} }
/** /**
* clean all hosts * check wether there has been a diverging host configuration
* this function will only redeploy the nginx configuration in case there has been a change
*/ */
public wipeHosts() { private areHostsDiverged(): boolean {
this.hosts.wipe(); let hostCounter = 0;
let unfoundHosts = 0;
this.hostCandidates.forEach(hostCandidateArg => {
let foundHost = false;
this.deployedHosts.forEach(deployedHostArg => {
if (
hostCandidateArg.hostName === deployedHostArg.hostName &&
hostCandidateArg.destination === deployedHostArg.destination
) {
hostCounter++;
foundHost = true;
}
});
if (!foundHost) {
unfoundHosts++;
}
});
return (
this.deployedHosts.getArray.length !== this.hostCandidates.getArray().length ||
hostCounter !== this.deployedHosts.getArray().length ||
unfoundHosts !== 0
);
} }
/** /**
* deploy the current stack and restart nginx * deploy the current stack and restart nginx
*/ */
public async deploy() { public async deploy() {
// write base config if (this.areHostsDiverged()) {
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath); this.logger.log('ok', `hosts have diverged, trigger config deployment and nginx reload!`);
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile); this.deployedHosts.wipe();
this.deployedHosts.addArray(this.hostCandidates.getArray());
this.hostCandidates.wipe();
// deploy hosts // write base config
plugins.smartfile.fs.ensureEmptyDirSync(paths.nginxHostDirPath); plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath);
for (const host of this.hosts.getArray()) { plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
await host.deploy();
this.logger.log('info', `Host ${host.hostName} deployed!`); // deploy hosts
plugins.smartfile.fs.ensureEmptyDirSync(paths.nginxHostDirPath);
for (const host of this.deployedHosts.getArray()) {
await host.deploy();
this.logger.log('info', `Host ${host.hostName} deployed!`);
}
this.nginxProcess.reloadConfig();
} else {
this.logger.log('info', `hosts have not diverged, skipping nginx reload`);
} }
this.nginxProcess.reloadConfig();
} }
} }