fix(core): update
This commit is contained in:
6
ts/interfaces/hostconfig.ts
Normal file
6
ts/interfaces/hostconfig.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface IHostConfig {
|
||||
hostName: string;
|
||||
destination: string;
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
}
|
0
ts/interfaces/index.ts
Normal file
0
ts/interfaces/index.ts
Normal file
@ -1,15 +0,0 @@
|
||||
import * as plugins from './smartnginx.plugins';
|
||||
|
||||
export class CertHandler {
|
||||
private _readyDeferred = plugins.smartpromise.defer();
|
||||
certHandlerReady = this._readyDeferred.promise;
|
||||
constructor() {} // nothing to do here for now
|
||||
|
||||
/**
|
||||
* ensure a cert is at the right location
|
||||
* @param hostName
|
||||
*/
|
||||
async ensureCertForHost(hostName) {
|
||||
|
||||
}
|
||||
}
|
@ -4,13 +4,7 @@ import * as snippets from './smartnginx.snippets';
|
||||
|
||||
import { SmartNginx } from './smartnginx.classes.smartnginx';
|
||||
|
||||
/**
|
||||
* the host config data that NginxHost needs to create a valid instance
|
||||
*/
|
||||
export interface IHostConfigData {
|
||||
hostName: string;
|
||||
destination: string;
|
||||
}
|
||||
import { IHostConfig } from './interfaces/hostconfig';
|
||||
|
||||
export enum hostTypes {
|
||||
reverseProxy
|
||||
@ -19,16 +13,19 @@ export enum hostTypes {
|
||||
/**
|
||||
* manages a single nginx host
|
||||
*/
|
||||
export class NginxHost {
|
||||
/**
|
||||
export class NginxHost implements IHostConfig {
|
||||
/**
|
||||
* smartnginxInstance this NginHost belongs to
|
||||
*/
|
||||
smartnginxInstance: SmartNginx
|
||||
smartnginxInstance: SmartNginx;
|
||||
|
||||
hostName: string; // the host name e.g. domain name
|
||||
destination: string;
|
||||
configString: string; // the actual host config file as string
|
||||
constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfigData) {
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
|
||||
constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfig) {
|
||||
this.smartnginxInstance = smartnginxInstanceArg;
|
||||
this.hostName = optionsArg.hostName;
|
||||
this.destination = optionsArg.destination;
|
||||
@ -39,8 +36,8 @@ export class NginxHost {
|
||||
*
|
||||
* @param certInstanceArg
|
||||
*/
|
||||
async deploy() {
|
||||
let filePath = plugins.path.join(paths.nginxHostFileBase, this.hostName + '.conf');
|
||||
public async deploy() {
|
||||
const filePath = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.conf`);
|
||||
// writeConfig
|
||||
plugins.smartfile.memory.toFsSync(this.configString, filePath);
|
||||
}
|
||||
|
@ -5,78 +5,65 @@ import { NginxHost } from './smartnginx.classes.nginxhost';
|
||||
|
||||
import { Smartshell } from '@pushrocks/smartshell';
|
||||
|
||||
import { ChildProcess } from 'child_process';
|
||||
|
||||
/**
|
||||
* manages a nginxprocess for an NginxConfig
|
||||
*/
|
||||
export class NginxProcess {
|
||||
started: boolean = false;
|
||||
nginxConfig: SmartNginx;
|
||||
nginxChildProcess: plugins.childProcess.ChildProcess;
|
||||
smartshellInstance = new Smartshell({
|
||||
public started: boolean = false;
|
||||
public smartNginxRef: SmartNginx;
|
||||
private nginxChildProcess: ChildProcess;
|
||||
private smartshellInstance = new Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
constructor(nginxConfigArg) {
|
||||
this.nginxConfig = nginxConfigArg;
|
||||
|
||||
constructor(nginxRefArg: SmartNginx) {
|
||||
this.smartNginxRef = nginxRefArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* start nginx
|
||||
*/
|
||||
start() {
|
||||
let done = plugins.smartpromise.defer();
|
||||
if (typeof this.nginxChildProcess == 'undefined') {
|
||||
this.nginxChildProcess = plugins.childProcess.exec(
|
||||
`nginx -c ${paths.nginxConfFile}`,
|
||||
function(error, stdout, stderr) {
|
||||
console.log(`stdout: ${stdout}`);
|
||||
console.log(`stderr: ${stderr}`);
|
||||
if (error !== null) {
|
||||
console.log(`exec error: ${error}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
public async start() {
|
||||
if (!this.nginxChildProcess) {
|
||||
this.nginxChildProcess = (await this.smartshellInstance.execStreaming(
|
||||
`nginx -c ${paths.nginxConfFile}`
|
||||
)).childProcess;
|
||||
}
|
||||
this.started = true;
|
||||
plugins.smartlog.defaultLogger.log('info', 'started Nginx!');
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* reload config
|
||||
*/
|
||||
reloadConfig() {
|
||||
let done = plugins.smartpromise.defer();
|
||||
if (this.started == false) {
|
||||
public async reloadConfig() {
|
||||
if (!this.started) {
|
||||
this.start();
|
||||
} else {
|
||||
this.smartshellInstance.exec('nginx -s reload');
|
||||
await this.smartshellInstance.exec('nginx -s reload');
|
||||
}
|
||||
plugins.smartlog.defaultLogger.log('info', 'NginxProcess has loaded the new config!');
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
this.smartNginxRef.logger.log('info', 'NginxProcess has loaded the new config!');
|
||||
}
|
||||
|
||||
/**
|
||||
* stop the nginx instance
|
||||
*/
|
||||
stop() {
|
||||
let done = plugins.smartpromise.defer();
|
||||
if (typeof this.nginxChildProcess != 'undefined') {
|
||||
public async stop() {
|
||||
if (this.nginxChildProcess) {
|
||||
this.smartshellInstance.exec('nginx -s quit');
|
||||
this.started = false;
|
||||
plugins.smartlog.defaultLogger.log('info', 'stopped Nginx!');
|
||||
this.smartNginxRef.logger.log('info', 'stopped Nginx!');
|
||||
} else {
|
||||
plugins.smartlog.defaultLogger.log('info', 'nginx already stopped!');
|
||||
this.smartNginxRef.logger.log('info', 'nginx already stopped!');
|
||||
}
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if nginx is in path
|
||||
*/
|
||||
check(): boolean {
|
||||
public check(): boolean {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -3,20 +3,20 @@ 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[] = [];
|
||||
import { IHostConfig } from './interfaces/hostconfig';
|
||||
|
||||
/**
|
||||
* main class that manages a NginxInstance
|
||||
*/
|
||||
export class SmartNginx {
|
||||
certHandler = new CertHandler();
|
||||
hosts: NginxHost[] = [];
|
||||
nginxProcess: NginxProcess = new NginxProcess(this);
|
||||
isDeployed: boolean = false;
|
||||
constructor() {
|
||||
|
||||
};
|
||||
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
|
||||
@ -26,61 +26,57 @@ export class SmartNginx {
|
||||
* add a host
|
||||
* @param nginxHostArg
|
||||
*/
|
||||
addHost(hostNameArg: string, destinationIp: string): NginxHost {
|
||||
const nginxHost = new NginxHost(this, {
|
||||
hostName: hostNameArg,
|
||||
destination: destinationIp
|
||||
})
|
||||
this.hosts.push(nginxHost);
|
||||
public addHost(optionsArg: IHostConfig): NginxHost {
|
||||
const nginxHost = new NginxHost(this, optionsArg);
|
||||
this.hosts.add(nginxHost);
|
||||
return nginxHost;
|
||||
}
|
||||
|
||||
getNginxHostByHostName(hostNameArg: string): NginxHost {
|
||||
/**
|
||||
* Gets a NginxHost by hostname
|
||||
* @param hostNameArg
|
||||
*/
|
||||
public getNginxHostByHostName(hostNameArg: string): NginxHost {
|
||||
return this.hosts.find(nginxHost => {
|
||||
return nginxHost.hostName === hostNameArg;
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* listHosts
|
||||
*/
|
||||
listHosts(): NginxHost[] {
|
||||
return this.hosts;
|
||||
public listHosts(): NginxHost[] {
|
||||
return this.hosts.getArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* remove a Host
|
||||
* @param nginxHostArg
|
||||
*/
|
||||
removeHost(nginxHostArg: NginxHost) {}
|
||||
|
||||
public removeHost(nginxHostArg: NginxHost) {
|
||||
this.hosts.remove(nginxHostArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* clean all hosts
|
||||
*/
|
||||
clean() {
|
||||
this.hosts = [];
|
||||
public wipeHosts() {
|
||||
this.hosts.wipe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public async deploy() {
|
||||
// write base config
|
||||
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath);
|
||||
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
|
||||
|
||||
// deploy hosts
|
||||
let promiseArray = [];
|
||||
for (let host of this.hosts) {
|
||||
plugins.smartfile.fs.ensureEmptyDirSync(paths.nginxHostDirPath);
|
||||
for (const host of this.hosts.getArray()) {
|
||||
await host.deploy();
|
||||
plugins.smartlog.defaultLogger.log('info', `Host ${host.hostName} deployed!`);
|
||||
this.nginxProcess.reloadConfig();
|
||||
};
|
||||
this.logger.log('info', `Host ${host.hostName} deployed!`);
|
||||
}
|
||||
this.nginxProcess.reloadConfig();
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
import * as plugins from './smartnginx.plugins';
|
||||
|
||||
// directories
|
||||
export let packageBase = plugins.path.join(__dirname, '../');
|
||||
export let nginxConfigBase = plugins.path.join(packageBase, 'nginxconfig');
|
||||
|
||||
export let nginxHostFileBase = plugins.path.join(nginxConfigBase, 'hosts');
|
||||
export let nginxCertBase = plugins.path.join(nginxConfigBase, 'cert');
|
||||
export const packageBase = plugins.path.join(__dirname, '../');
|
||||
export const nginxConfigDirPath = plugins.path.join(packageBase, 'nginxconfig');
|
||||
export const nginxHostDirPath = plugins.path.join(nginxConfigDirPath, 'hosts');
|
||||
|
||||
// files
|
||||
export let nginxConfFile = plugins.path.join(nginxConfigBase, 'nginx.conf');
|
||||
export const nginxConfFile = plugins.path.join(nginxConfigDirPath, 'nginx.conf');
|
@ -1,17 +1,14 @@
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as childProcess from 'child_process';
|
||||
// native
|
||||
import * as path from 'path';
|
||||
|
||||
export { path };
|
||||
|
||||
// @pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartshell from '@pushrocks/smartshell';
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
|
||||
export {
|
||||
smartlog,
|
||||
childProcess,
|
||||
path,
|
||||
smartpromise,
|
||||
smartshell,
|
||||
smartfile,
|
||||
smartstring
|
||||
}
|
||||
export { lik, smartlog, smartpromise, smartshell, smartfile, smartstring };
|
||||
|
Reference in New Issue
Block a user