smartnginx/ts/smartnginx.classes.smartnginx.ts

83 lines
2.1 KiB
TypeScript
Raw Normal View History

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';
/**
* 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);
}
// ===================
// 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;
}
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
}
/**
* listHosts
*/
2019-01-09 11:15:28 +00:00
public listHosts(): NginxHost[] {
return this.hosts.getArray();
}
/**
* remove a Host
* @param nginxHostArg
*/
2019-01-09 11:15:28 +00:00
public removeHost(nginxHostArg: NginxHost) {
this.hosts.remove(nginxHostArg);
}
/**
* clean all hosts
*/
2019-01-09 11:15:28 +00:00
public wipeHosts() {
this.hosts.wipe();
}
/**
* deploy the current stack and restart nginx
*/
2019-01-09 11:15:28 +00:00
public async deploy() {
// write base config
2019-01-09 11:15:28 +00:00
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath);
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
2019-01-09 11:15:28 +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()) {
await host.deploy();
2019-01-09 11:15:28 +00:00
this.logger.log('info', `Host ${host.hostName} deployed!`);
}
this.nginxProcess.reloadConfig();
}
}