import * as plugins from "./smartnginx.plugins"; import * as paths from "./smartnginx.paths"; import * as snippets from "./smartnginx.snippets"; import { NginxZone } from "./smartnginx.classes.nginxzone"; import { NginxProcess } from "./smartnginx.classes.nginxprocess"; let allConfigs: NginxConfig[] = []; export class NginxConfig { zones: NginxZone[] = []; cert: plugins.cert.Cert; // the Cert Instance from which the config gets its certificates nginxProcess: NginxProcess = new NginxProcess(this); isDeployed: boolean = false; constructor(optionsArg: plugins.cert.ICertConstructorOptions) { this.cert = new plugins.cert.Cert({ cfEmail: optionsArg.cfEmail, cfKey: optionsArg.cfKey, sslDir: paths.nginxCertBase, gitOriginRepo: optionsArg.gitOriginRepo, testMode: optionsArg.testMode }); }; // interact with Zones addZone(zoneArg: NginxZone){ this.zones.push(zoneArg); } listZones(): NginxZone[]{ return this.zones; }; removeZones(zoneArg: NginxZone) { } clean() { this.zones = []; } // handle deployment of zones deploy() { let done = plugins.q.defer(); plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase); plugins.smartfile.fs.ensureDirSync(paths.nginxZoneBase); 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 zones let promiseArray = []; for (let zone of this.zones) { let zoneDeployedPromise = zone.deploy(this.cert); zoneDeployedPromise.then(() => { plugins.beautylog.info(`Zone ${zone.zoneName} deployed!`); this.nginxProcess.reloadConfig(); }); promiseArray.push(zoneDeployedPromise); }; plugins.q.all(promiseArray) .then(() => { done.resolve(); }); return done.promise; }; };