2018-08-10 21:10:48 +00:00
|
|
|
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';
|
|
|
|
import { CertHandler } from './smartnginx.classes.certhandler';
|
|
|
|
let allConfigs: SmartNginx[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* main class that manages a NginxInstance
|
|
|
|
*/
|
|
|
|
export class SmartNginx {
|
|
|
|
certHandler = new CertHandler();
|
|
|
|
hosts: NginxHost[] = [];
|
|
|
|
nginxProcess: NginxProcess = new NginxProcess(this);
|
|
|
|
isDeployed: boolean = false;
|
|
|
|
constructor() {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// ===================
|
|
|
|
// interact with Hosts
|
|
|
|
// ===================
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a host
|
|
|
|
* @param nginxHostArg
|
|
|
|
*/
|
2018-08-11 13:09:19 +00:00
|
|
|
addHost(hostNameArg: string, destinationIp: string): NginxHost {
|
|
|
|
const nginxHost = new NginxHost(this, {
|
|
|
|
hostName: hostNameArg,
|
|
|
|
destination: destinationIp
|
|
|
|
})
|
|
|
|
this.hosts.push(nginxHost);
|
|
|
|
return nginxHost;
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 13:09:19 +00:00
|
|
|
getNginxHostByHostName(hostNameArg: string): NginxHost {
|
|
|
|
return this.hosts.find(nginxHost => {
|
|
|
|
return nginxHost.hostName === hostNameArg;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* listHosts
|
|
|
|
*/
|
|
|
|
listHosts(): NginxHost[] {
|
|
|
|
return this.hosts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a Host
|
|
|
|
* @param nginxHostArg
|
|
|
|
*/
|
|
|
|
removeHost(nginxHostArg: NginxHost) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean all hosts
|
|
|
|
*/
|
|
|
|
clean() {
|
|
|
|
this.hosts = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deploy the current stack and restart nginx
|
|
|
|
*/
|
|
|
|
async deploy() {
|
|
|
|
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) {
|
|
|
|
await host.deploy();
|
2019-01-01 21:34:36 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('info', `Host ${host.hostName} deployed!`);
|
2018-08-10 21:10:48 +00:00
|
|
|
this.nginxProcess.reloadConfig();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|