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';
|
2019-01-09 11:15:28 +00:00
|
|
|
import { IHostConfig } from './interfaces/hostconfig';
|
2018-08-10 21:10:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* main class that manages a NginxInstance
|
|
|
|
*/
|
|
|
|
export class SmartNginx {
|
2019-01-09 11:15:28 +00:00
|
|
|
public logger: plugins.smartlog.Smartlog;
|
|
|
|
private hosts = new plugins.lik.Objectmap<NginxHost>();
|
|
|
|
public nginxProcess: NginxProcess = new NginxProcess(this);
|
|
|
|
constructor(optionsArg: { logger?: plugins.smartlog.Smartlog }) {
|
|
|
|
optionsArg.logger
|
|
|
|
? (this.logger = optionsArg.logger)
|
|
|
|
: (this.logger = plugins.smartlog.defaultLogger);
|
|
|
|
}
|
2018-08-10 21:10:48 +00:00
|
|
|
|
|
|
|
// ===================
|
|
|
|
// interact with Hosts
|
|
|
|
// ===================
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a host
|
|
|
|
* @param nginxHostArg
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public addHost(optionsArg: IHostConfig): NginxHost {
|
|
|
|
const nginxHost = new NginxHost(this, optionsArg);
|
|
|
|
this.hosts.add(nginxHost);
|
2018-08-11 13:09:19 +00:00
|
|
|
return nginxHost;
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 11:15:28 +00:00
|
|
|
/**
|
|
|
|
* Gets a NginxHost by hostname
|
|
|
|
* @param hostNameArg
|
|
|
|
*/
|
|
|
|
public getNginxHostByHostName(hostNameArg: string): NginxHost {
|
2018-08-11 13:09:19 +00:00
|
|
|
return this.hosts.find(nginxHost => {
|
|
|
|
return nginxHost.hostName === hostNameArg;
|
2019-01-09 11:15:28 +00:00
|
|
|
});
|
2018-08-11 13:09:19 +00:00
|
|
|
}
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* listHosts
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public listHosts(): NginxHost[] {
|
|
|
|
return this.hosts.getArray();
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a Host
|
|
|
|
* @param nginxHostArg
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public removeHost(nginxHostArg: NginxHost) {
|
|
|
|
this.hosts.remove(nginxHostArg);
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* clean all hosts
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public wipeHosts() {
|
|
|
|
this.hosts.wipe();
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deploy the current stack and restart nginx
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public async deploy() {
|
2018-08-10 21:10:48 +00:00
|
|
|
// write base config
|
2019-01-09 11:15:28 +00:00
|
|
|
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath);
|
2018-08-10 21:10:48 +00:00
|
|
|
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
|
2019-01-09 11:15:28 +00:00
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
// deploy hosts
|
2019-01-09 11:15:28 +00:00
|
|
|
plugins.smartfile.fs.ensureEmptyDirSync(paths.nginxHostDirPath);
|
|
|
|
for (const host of this.hosts.getArray()) {
|
2018-08-10 21:10:48 +00:00
|
|
|
await host.deploy();
|
2019-01-09 11:15:28 +00:00
|
|
|
this.logger.log('info', `Host ${host.hostName} deployed!`);
|
|
|
|
}
|
|
|
|
this.nginxProcess.reloadConfig();
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
}
|