smartnginx/ts/smartnginx.classes.nginxconfig.ts
2016-07-12 23:11:57 +02:00

65 lines
2.1 KiB
TypeScript

import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as command from "./smartnginx.command";
import * as snippets from "./smartnginx.snippets"
import {NginxZone} from "./smartnginx.classes.nginxzone";
let allConfigs:NginxConfig[] = [];
export class NginxConfig {
zones:NginxZone[] = [];
cert:plugins.cert.Cert; // the Cert Instance from which the config gets its certificates
isDeployed:boolean = false;
constructor(optionsArg:plugins.cert.CertConstructorOptions){
this.cert = new plugins.cert.Cert({
cfEmail:optionsArg.cfEmail,
cfKey:optionsArg.cfKey,
sslDir:optionsArg.sslDir,
gitOriginRepo:optionsArg.gitOriginRepo
});
};
// interact with Zones
addZone(zoneArg:NginxZone){
this.zones.push(zoneArg);
}
listZones():NginxZone[] {
return this.zones;
};
removeZones(zoneArg:NginxZone){
}
// handle deployment of zones
deploy(nginxRestartArg:boolean = false){
let done = plugins.q.defer();
plugins.smartfile.fs.removeSync(paths.nginxConfigBase);
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(),
plugins.path.join(paths.nginxConfigBase,"nginx.conf")
);
// deploy zones
let promiseArray = [];
for(let zone of this.zones){
promiseArray.push(zone.deploy(this.cert));
};
plugins.q.all(promiseArray)
.then(() => {
// restart nginx
if(nginxRestartArg){
command.restart(this);
};
done.resolve();
});
return done.promise;
};
};