smartnginx/ts/smartnginx.classes.smartnginx.ts
2019-01-01 22:34:36 +01:00

87 lines
2.1 KiB
TypeScript

import * as plugins from './smartnginx.plugins';
import * as paths from './smartnginx.paths';
import * as snippets from './smartnginx.snippets';
import { NginxHost } from './smartnginx.classes.nginxhost';
import { NginxProcess } from './smartnginx.classes.nginxprocess';
import { CertHandler } from './smartnginx.classes.certhandler';
let allConfigs: SmartNginx[] = [];
/**
* main class that manages a NginxInstance
*/
export class SmartNginx {
certHandler = new CertHandler();
hosts: NginxHost[] = [];
nginxProcess: NginxProcess = new NginxProcess(this);
isDeployed: boolean = false;
constructor() {
};
// ===================
// interact with Hosts
// ===================
/**
* add a host
* @param nginxHostArg
*/
addHost(hostNameArg: string, destinationIp: string): NginxHost {
const nginxHost = new NginxHost(this, {
hostName: hostNameArg,
destination: destinationIp
})
this.hosts.push(nginxHost);
return nginxHost;
}
getNginxHostByHostName(hostNameArg: string): NginxHost {
return this.hosts.find(nginxHost => {
return nginxHost.hostName === hostNameArg;
})
}
/**
* listHosts
*/
listHosts(): NginxHost[] {
return this.hosts;
}
/**
* remove a Host
* @param nginxHostArg
*/
removeHost(nginxHostArg: NginxHost) {}
/**
* clean all hosts
*/
clean() {
this.hosts = [];
}
/**
* deploy the current stack and restart nginx
*/
async deploy() {
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxHostFileBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxCertBase);
for (let config of allConfigs) {
config.isDeployed = false;
}
this.isDeployed = true;
// write base config
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
// deploy hosts
let promiseArray = [];
for (let host of this.hosts) {
await host.deploy();
plugins.smartlog.defaultLogger.log('info', `Host ${host.hostName} deployed!`);
this.nginxProcess.reloadConfig();
};
}
}