BREAKING CHANGE(scope): change scope to @pushrocks

This commit is contained in:
2018-08-10 23:10:48 +02:00
parent 381227406b
commit 9b043d0e0d
17 changed files with 1368 additions and 345 deletions

View File

@ -1,6 +1,6 @@
import * as plugins from "./smartnginx.plugins";
import * as plugins from './smartnginx.plugins';
// classes
export * from "./smartnginx.classes.nginxconfig";
export * from "./smartnginx.classes.nginxprocess";
export * from "./smartnginx.classes.nginxhost";
export * from './smartnginx.classes.smartnginx';
export * from './smartnginx.classes.nginxprocess';
export * from './smartnginx.classes.nginxhost';

View File

@ -0,0 +1,5 @@
import * as plugins from './smartnginx.plugins';
export class CertHandler {
}

View File

@ -1,71 +0,0 @@
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";
let allConfigs: NginxConfig[] = [];
/**
* main class that manages a NginxInstance
*/
export class NginxConfig {
hosts: NginxHost[] = [];
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) {
this.cert = new plugins.cert.Cert({
cfEmail: optionsArg.cfEmail,
cfKey: optionsArg.cfKey,
sslDir: paths.nginxCertBase,
gitOriginRepo: optionsArg.gitOriginRepo,
testMode: optionsArg.testMode
});
};
// interact with Hosts
addHost(nginxHostArg: NginxHost){
this.hosts.push(nginxHostArg);
}
listHosts(): NginxHost[]{
return this.hosts;
};
removeHost(nginxHostArg: NginxHost) {
}
clean() {
this.hosts = [];
}
// handle deployment of hosts
deploy() {
let done = plugins.q.defer();
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) {
let hostDeployedPromise = host.deploy(this.cert);
hostDeployedPromise.then(() => {
plugins.beautylog.info(`Host ${host.hostName} deployed!`);
this.nginxProcess.reloadConfig();
});
promiseArray.push(hostDeployedPromise);
};
plugins.q.all(promiseArray)
.then(() => {
done.resolve();
});
return done.promise;
};
};

View File

@ -1,43 +1,47 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as snippets from "./smartnginx.snippets"
import * as plugins from './smartnginx.plugins';
import * as paths from './smartnginx.paths';
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,
type: hostTypes,
destination: string
hostName: string;
destination: string;
}
export enum hostTypes {
reverseProxy,
static
reverseProxy
}
/**
* manages a single nginx host
*/
export class NginxHost {
hostName: string; // the host name e.g. domain name
type: hostTypes;
destination: string;
configString: string; // the actual host config file as string
constructor(optionsArg:IHostConfigData) {
this.hostName = optionsArg.hostName;
this.type = optionsArg.type;
this.destination = optionsArg.destination;
this.configString = snippets.getHostConfigString(optionsArg.hostName, optionsArg.destination);
};
deploy(certInstanceArg: plugins.cert.Cert) {
let done = plugins.q.defer();
let filePath = plugins.path.join(paths.nginxHostFileBase, this.hostName + ".conf");
// writeConfig
plugins.smartfile.memory.toFsSync(this.configString, filePath);
// get cert
certInstanceArg.getDomainCert(this.hostName)
.then(done.resolve);
return done.promise;
};
};
/**
* smartnginxInstance this NginHost belongs to
*/
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) {
this.smartnginxInstance = smartnginxInstanceArg;
this.hostName = optionsArg.hostName;
this.destination = optionsArg.destination;
this.configString = snippets.getHostConfigString(optionsArg.hostName, optionsArg.destination);
}
/**
*
* @param certInstanceArg
*/
async deploy() {
let filePath = plugins.path.join(paths.nginxHostFileBase, this.hostName + '.conf');
// writeConfig
plugins.smartfile.memory.toFsSync(this.configString, filePath);
}
}

View File

@ -1,74 +1,82 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import { NginxConfig } from "./smartnginx.classes.nginxconfig";
import { NginxHost } from "./smartnginx.classes.nginxhost";
import * as plugins from './smartnginx.plugins';
import * as paths from './smartnginx.paths';
import { SmartNginx } from './smartnginx.classes.smartnginx';
import { NginxHost } from './smartnginx.classes.nginxhost';
import { Smartshell } from '@pushrocks/smartshell';
/**
* manages a nginxprocess for an NginxConfig
*/
export class NginxProcess {
started: boolean = false;
nginxConfig:NginxConfig;
nginxChildProcess: plugins.childProcess.ChildProcess;
constructor(nginxConfigArg) {
this.nginxConfig = nginxConfigArg;
};
started: boolean = false;
nginxConfig: SmartNginx;
nginxChildProcess: plugins.childProcess.ChildProcess;
smartshellInstance = new Smartshell({
executor: 'bash'
});
constructor(nginxConfigArg) {
this.nginxConfig = nginxConfigArg;
}
/**
* start nginx
*/
start() {
let done = plugins.q.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}`);
};
});
};
this.started = true;
plugins.beautylog.info("started Nginx!");
done.resolve();
return done.promise;
};
/**
* 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}`);
}
}
);
}
this.started = true;
plugins.smartlog.defaultLogger.info('started Nginx!');
done.resolve();
return done.promise;
}
/**
* reload config
*/
reloadConfig(){
let done = plugins.q.defer();
if(this.started == false){
this.start();
} else {
plugins.shelljs.exec("nginx -s reload");
};
plugins.beautylog.ok("NginxProcess has loaded the new config!")
done.resolve();
return done.promise;
};
/**
* reload config
*/
reloadConfig() {
let done = plugins.smartpromise.defer();
if (this.started == false) {
this.start();
} else {
this.smartshellInstance.exec('nginx -s reload');
}
plugins.smartlog.defaultLogger.info('NginxProcess has loaded the new config!');
done.resolve();
return done.promise;
}
/**
* stop the nginx instance
*/
stop() {
let done = plugins.q.defer();
if (typeof this.nginxChildProcess != "undefined") {
plugins.shelljs.exec("nginx -s quit");
this.started = false;
plugins.beautylog.info("stopped Nginx!");
} else {
plugins.beautylog.log("nginx already stopped!");
};
done.resolve();
return done.promise;
};
/**
* stop the nginx instance
*/
stop() {
let done = plugins.smartpromise.defer();
if (typeof this.nginxChildProcess != 'undefined') {
this.smartshellInstance.exec('nginx -s quit');
this.started = false;
plugins.smartlog.defaultLogger.info('stopped Nginx!');
} else {
plugins.smartlog.defaultLogger.info('nginx already stopped!');
}
done.resolve();
return done.promise;
}
/**
* checks if nginx is in path
*/
check(): boolean {
return;
};
/**
* checks if nginx is in path
*/
check(): boolean {
return;
}
}

View File

@ -0,0 +1,74 @@
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
*/
addHost(nginxHostArg: NginxHost) {
this.hosts.push(nginxHostArg);
}
/**
* 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();
plugins.smartlog.defaultLogger.info(`Host ${host.hostName} deployed!`);
this.nginxProcess.reloadConfig();
};
}
}

View File

@ -1,10 +1,11 @@
import * as plugins from "./smartnginx.plugins"
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 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');
// files
export let nginxConfFile = plugins.path.join(nginxConfigBase,"nginx.conf");
export let nginxConfFile = plugins.path.join(nginxConfigBase, 'nginx.conf');

View File

@ -1,9 +1,17 @@
import "typings-global";
export import beautylog = require("beautylog");
export import cert = require("cert");
export import childProcess = require("child_process");
export import path = require("path");
export import q = require("q");
export import shelljs = require("shelljs");
export import smartfile = require("smartfile");
export import smartstring = require("smartstring");
import * as smartlog from '@pushrocks/smartlog';
import * as childProcess from 'child_process';
import * as path from 'path';
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
}

View File

@ -1,7 +1,7 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as plugins from './smartnginx.plugins';
import * as paths from './smartnginx.paths';
export let getBaseConfigString = () => {
let baseConfig = plugins.smartstring.indent.normalize(`
let baseConfig = plugins.smartstring.indent.normalize(`
user www-data;
worker_processes auto;
pid /run/nginx.pid;
@ -68,12 +68,11 @@ export let getBaseConfigString = () => {
}
daemon off;
`);
return baseConfig;
}
return baseConfig;
};
export let getHostConfigString = (hostNameArg:string,destinationIpArg:string) => {
let hostConfig = plugins.smartstring.indent.normalize(`
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string) => {
let hostConfig = plugins.smartstring.indent.normalize(`
upstream ${hostNameArg} {
server ${destinationIpArg};
}
@ -99,6 +98,5 @@ export let getHostConfigString = (hostNameArg:string,destinationIpArg:string) =>
}
}
`);
return hostConfig;
return hostConfig;
};