smartnginx/ts/smartnginx.classes.nginxconfig.ts

66 lines
2.1 KiB
TypeScript
Raw Normal View History

2016-07-06 01:14:44 +00:00
import * as plugins from "./smartnginx.plugins";
2016-07-06 04:33:31 +00:00
import * as paths from "./smartnginx.paths";
import * as command from "./smartnginx.command";
2016-07-08 01:36:51 +00:00
import * as snippets from "./smartnginx.snippets"
2016-07-06 06:30:33 +00:00
import {NginxZone} from "./smartnginx.classes.nginxzone";
2016-07-06 04:33:31 +00:00
let allConfigs:NginxConfig[] = [];
2016-07-06 01:14:44 +00:00
export class NginxConfig {
2016-07-08 02:24:07 +00:00
zones:NginxZone[] = [];
2016-07-12 21:11:57 +00:00
cert:plugins.cert.Cert; // the Cert Instance from which the config gets its certificates
2016-07-06 04:33:31 +00:00
isDeployed:boolean = false;
constructor(optionsArg:plugins.cert.ICertConstructorOptions){
2016-07-12 21:11:57 +00:00
this.cert = new plugins.cert.Cert({
cfEmail:optionsArg.cfEmail,
cfKey:optionsArg.cfKey,
sslDir:paths.nginxCertBase,
gitOriginRepo:optionsArg.gitOriginRepo,
testMode:optionsArg.testMode
2016-07-12 21:11:57 +00:00
});
2016-07-06 01:14:44 +00:00
};
// interact with Zones
addZone(zoneArg:NginxZone){
2016-07-08 02:24:07 +00:00
this.zones.push(zoneArg);
2016-07-06 01:14:44 +00:00
}
listZones():NginxZone[] {
return this.zones;
};
removeZones(zoneArg:NginxZone){
}
// handle deployment of zones
2016-07-06 04:33:31 +00:00
deploy(nginxRestartArg:boolean = false){
2016-07-12 21:11:57 +00:00
let done = plugins.q.defer();
2016-07-08 02:24:07 +00:00
plugins.smartfile.fs.removeSync(paths.nginxConfigBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxZoneBase);
2016-07-12 21:11:57 +00:00
plugins.smartfile.fs.ensureDirSync(paths.nginxCertBase);
2016-07-06 04:33:31 +00:00
for(let config of allConfigs){
config.isDeployed = false;
};
this.isDeployed = true;
2016-07-12 21:11:57 +00:00
// write base config
2016-07-08 01:36:51 +00:00
plugins.smartfile.memory.toFsSync(
snippets.getBaseConfigString(),
plugins.path.join(paths.nginxConfigBase,"nginx.conf")
);
2016-07-12 21:11:57 +00:00
// deploy zones
let promiseArray = [];
for(let zone of this.zones){
promiseArray.push(zone.deploy(this.cert));
2016-07-08 02:24:07 +00:00
};
2016-07-12 21:11:57 +00:00
plugins.q.all(promiseArray)
.then(() => {
// restart nginx
if(nginxRestartArg){
command.restart(this);
};
done.resolve();
});
return done.promise;
2016-07-06 01:14:44 +00:00
};
};