Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
85448a21fc | |||
3b85c4a37e | |||
67dd650dce | |||
92eaf5f19a | |||
fcf9a61b1d | |||
fbd258a876 | |||
c65b8fc1af | |||
b7b588d713 | |||
00d672c135 | |||
6a1e778b49 | |||
4cfb26f62f | |||
7ba3ad0b21 | |||
cd93ec2560 | |||
ede884930e | |||
1049920efd | |||
b7a34403c5 | |||
987e19372a | |||
0b97aaee91 | |||
b87ca1fa03 | |||
824f872fa5 |
@ -22,6 +22,7 @@ release:
|
||||
stage: release
|
||||
environment: npmjs-com_registry
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
|
1170
package-lock.json
generated
1170
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartnginx",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.14",
|
||||
"private": false,
|
||||
"description": "control nginx from node, TypeScript ready",
|
||||
"main": "dist/index.js",
|
||||
@ -24,17 +24,20 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartnginx#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartfile": "^6.0.6",
|
||||
"@pushrocks/smartlog": "^2.0.1",
|
||||
"@pushrocks/lik": "^3.0.4",
|
||||
"@pushrocks/smartfile": "^6.0.11",
|
||||
"@pushrocks/smartlog": "^2.0.9",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"@pushrocks/smartshell": "^2.0.6",
|
||||
"@pushrocks/smartstring": "^3.0.0"
|
||||
"@pushrocks/smartshell": "^2.0.13",
|
||||
"@pushrocks/smartstring": "^3.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.12",
|
||||
"@gitzone/tstest": "^1.0.13",
|
||||
"@pushrocks/tapbundle": "^3.0.5",
|
||||
"qenv": "^1.1.7"
|
||||
"@gitzone/tsbuild": "^2.1.4",
|
||||
"@gitzone/tsrun": "^1.1.17",
|
||||
"@gitzone/tstest": "^1.0.18",
|
||||
"@pushrocks/qenv": "^3.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"tslint": "^5.12.0",
|
||||
"tslint-config-prettier": "^1.17.0"
|
||||
}
|
||||
}
|
||||
|
18
test/test.ts
18
test/test.ts
@ -1,32 +1,34 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import path = require('path');
|
||||
|
||||
import { Qenv } from 'qenv';
|
||||
let testQenv = new Qenv(process.cwd(), path.join(process.cwd(), '.nogit'));
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
|
||||
|
||||
import * as smartnginx from '../ts';
|
||||
import * as smartnginx from '../ts/index';
|
||||
|
||||
let testSmartNginx: smartnginx.SmartNginx;
|
||||
let testNginxZone01: smartnginx.NginxHost;
|
||||
let testNginxZone02: smartnginx.NginxHost;
|
||||
|
||||
tap.test('should create a valid instance of SmartNginx', async () => {
|
||||
testSmartNginx = new smartnginx.SmartNginx();
|
||||
testSmartNginx = new smartnginx.SmartNginx({});
|
||||
expect(testSmartNginx).to.be.instanceof(smartnginx.SmartNginx);
|
||||
});
|
||||
|
||||
tap.test(`should produce an instance of NginxConfig`, async () => {
|
||||
testNginxZone01 = new smartnginx.NginxHost(testSmartNginx, {
|
||||
hostName: 'test100.bleu.de',
|
||||
destination: '192.192.192.191'
|
||||
destination: '192.192.192.191',
|
||||
privateKey: 'some private',
|
||||
publicKey: 'some public'
|
||||
});
|
||||
testNginxZone02 = new smartnginx.NginxHost(testSmartNginx, {
|
||||
hostName: 'test102.bleu.de',
|
||||
destination: '192.192.192.192'
|
||||
destination: '192.192.192.192',
|
||||
privateKey: 'some private',
|
||||
publicKey: 'some public'
|
||||
});
|
||||
expect(testNginxZone01).to.be.instanceof(smartnginx.NginxHost);
|
||||
console.log(testNginxZone01.configString);
|
||||
});
|
||||
|
||||
tap.test('.addZone() should add a zone to NginxConfig Object', async () => {
|
||||
|
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,29 +13,41 @@ 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;
|
||||
this.configString = snippets.getHostConfigString(optionsArg.hostName, optionsArg.destination);
|
||||
this.privateKey = optionsArg.privateKey;
|
||||
this.publicKey = optionsArg.publicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param certInstanceArg
|
||||
*/
|
||||
async deploy() {
|
||||
let filePath = plugins.path.join(paths.nginxHostFileBase, this.hostName + '.conf');
|
||||
public async deploy() {
|
||||
const filePathConfig = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.conf`);
|
||||
const filePathPrivate = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.private.pem`);
|
||||
const filePathPublic = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.public.pem`);
|
||||
// writeConfig
|
||||
plugins.smartfile.memory.toFsSync(this.configString, filePath);
|
||||
plugins.smartfile.memory.toFsSync(this.configString, filePathConfig);
|
||||
|
||||
// write ssl
|
||||
plugins.smartfile.memory.toFsSync(this.privateKey, filePathPrivate);
|
||||
plugins.smartfile.memory.toFsSync(this.publicKey, filePathPublic);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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.info('started Nginx!');
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
plugins.smartlog.defaultLogger.log('info', 'started Nginx!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.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.info('stopped Nginx!');
|
||||
this.smartNginxRef.logger.log('info', 'stopped Nginx!');
|
||||
} else {
|
||||
plugins.smartlog.defaultLogger.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.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 };
|
||||
|
@ -4,7 +4,7 @@ export let getBaseConfigString = () => {
|
||||
let baseConfig = plugins.smartstring.indent.normalize(`
|
||||
user www-data;
|
||||
worker_processes auto;
|
||||
pid /run/nginx.pid;
|
||||
pid /run/nginx/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 768;
|
||||
@ -63,7 +63,7 @@ export let getBaseConfigString = () => {
|
||||
# Virtual Host Configs
|
||||
##
|
||||
|
||||
include ${paths.nginxHostFileBase}/*.conf;
|
||||
include ${paths.nginxHostDirPath}/*.conf;
|
||||
include /etc/nginx/sites-enabled/*;
|
||||
}
|
||||
daemon off;
|
||||
@ -86,12 +86,15 @@ export let getHostConfigString = (hostNameArg: string, destinationIpArg: string)
|
||||
server {
|
||||
listen *:443 ssl;
|
||||
server_name ${hostNameArg};
|
||||
ssl_certificate ${paths.nginxCertBase}/${hostNameArg}/fullchain.pem;
|
||||
ssl_certificate_key ${paths.nginxCertBase}/${hostNameArg}/privkey.pem;
|
||||
ssl_certificate ${paths.nginxHostDirPath}/${hostNameArg}.public.pem;
|
||||
ssl_certificate_key ${paths.nginxHostDirPath}/${hostNameArg}.private.pem;
|
||||
|
||||
location / {
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_pass http://${hostNameArg};
|
||||
include /etc/nginx/proxy_params;
|
||||
}
|
||||
location ~ /\.git {
|
||||
deny all;
|
||||
|
17
tslint.json
Normal file
17
tslint.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Reference in New Issue
Block a user