smartnginx/ts/smartnginx.classes.nginxconfig.ts

72 lines
2.3 KiB
TypeScript
Raw Permalink 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";
2016-07-24 23:54:36 +00:00
import * as snippets from "./smartnginx.snippets";
import { NginxHost } from "./smartnginx.classes.nginxhost";
2016-07-24 23:54:36 +00:00
import { NginxProcess } from "./smartnginx.classes.nginxprocess";
let allConfigs: NginxConfig[] = [];
2016-07-06 04:33:31 +00:00
/**
* main class that manages a NginxInstance
*/
2016-07-06 01:14:44 +00:00
export class NginxConfig {
hosts: NginxHost[] = [];
2016-07-24 23:54:36 +00:00
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 Hosts
addHost(nginxHostArg: NginxHost){
this.hosts.push(nginxHostArg);
2016-07-06 01:14:44 +00:00
}
listHosts(): NginxHost[]{
return this.hosts;
2016-07-06 01:14:44 +00:00
};
removeHost(nginxHostArg: NginxHost) {
2016-07-06 01:14:44 +00:00
}
2016-07-24 23:54:36 +00:00
clean() {
this.hosts = [];
2016-07-21 00:24:54 +00:00
}
// handle deployment of hosts
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.nginxHostFileBase);
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
);
// deploy hosts
2016-07-12 21:11:57 +00:00
let promiseArray = [];
for (let host of this.hosts) {
let hostDeployedPromise = host.deploy(this.cert);
hostDeployedPromise.then(() => {
plugins.beautylog.info(`Host ${host.hostName} deployed!`);
2016-07-24 23:54:36 +00:00
this.nginxProcess.reloadConfig();
});
promiseArray.push(hostDeployedPromise);
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-25 00:57:17 +00:00
2016-07-12 21:11:57 +00:00
return done.promise;
2016-07-06 01:14:44 +00:00
};
};