smartnginx/ts/smartnginx.classes.nginxconfig.ts

72 lines
2.3 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";
let allConfigs: NginxConfig[] = [];
/**
* main class that manages a NginxInstance
*/
export class NginxConfig {
hosts: NginxHost[] = [];
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 Hosts
addHost(nginxHostArg: NginxHost){
this.hosts.push(nginxHostArg);
}
listHosts(): NginxHost[]{
return this.hosts;
};
removeHost(nginxHostArg: NginxHost) {
}
clean() {
this.hosts = [];
}
// handle deployment of hosts
deploy() {
let done = plugins.q.defer();
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) {
let hostDeployedPromise = host.deploy(this.cert);
hostDeployedPromise.then(() => {
plugins.beautylog.info(`Host ${host.hostName} deployed!`);
this.nginxProcess.reloadConfig();
});
promiseArray.push(hostDeployedPromise);
};
plugins.q.all(promiseArray)
.then(() => {
done.resolve();
});
return done.promise;
};
};