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";
|
2016-07-24 23:54:36 +00:00
|
|
|
import * as snippets from "./smartnginx.snippets";
|
|
|
|
import { NginxZone } from "./smartnginx.classes.nginxzone";
|
|
|
|
import { NginxProcess } from "./smartnginx.classes.nginxprocess";
|
|
|
|
let allConfigs: NginxConfig[] = [];
|
2016-07-06 04:33:31 +00:00
|
|
|
|
2016-07-06 01:14:44 +00:00
|
|
|
export class NginxConfig {
|
2016-07-24 23:54:36 +00:00
|
|
|
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) {
|
2016-07-12 21:11:57 +00:00
|
|
|
this.cert = new plugins.cert.Cert({
|
2016-07-24 23:54:36 +00:00
|
|
|
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
|
2016-07-24 23:54:36 +00:00
|
|
|
addZone(zoneArg: NginxZone){
|
2016-07-08 02:24:07 +00:00
|
|
|
this.zones.push(zoneArg);
|
2016-07-06 01:14:44 +00:00
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
listZones(): NginxZone[]{
|
2016-07-06 01:14:44 +00:00
|
|
|
return this.zones;
|
|
|
|
};
|
2016-07-24 23:54:36 +00:00
|
|
|
removeZones(zoneArg: NginxZone) {
|
2016-07-06 01:14:44 +00:00
|
|
|
|
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
clean() {
|
2016-07-21 00:24:54 +00:00
|
|
|
this.zones = [];
|
|
|
|
}
|
2016-07-06 01:14:44 +00:00
|
|
|
// handle deployment of zones
|
2016-07-24 23:54:36 +00:00
|
|
|
deploy() {
|
2016-07-12 21:11:57 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-07-08 02:24:07 +00:00
|
|
|
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-24 23:54:36 +00:00
|
|
|
for (let config of allConfigs) {
|
2016-07-06 04:33:31 +00:00
|
|
|
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(),
|
2016-07-24 23:54:36 +00:00
|
|
|
paths.nginxConfFile
|
2016-07-08 01:36:51 +00:00
|
|
|
);
|
2016-07-12 21:11:57 +00:00
|
|
|
// deploy zones
|
|
|
|
let promiseArray = [];
|
2016-07-24 23:54:36 +00:00
|
|
|
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);
|
2016-07-08 02:24:07 +00:00
|
|
|
};
|
2016-07-12 21:11:57 +00:00
|
|
|
plugins.q.all(promiseArray)
|
|
|
|
.then(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
2016-07-24 23:54:36 +00:00
|
|
|
|
2016-07-12 21:11:57 +00:00
|
|
|
return done.promise;
|
2016-07-06 01:14:44 +00:00
|
|
|
};
|
|
|
|
};
|