Compare commits
66 Commits
Author | SHA1 | Date | |
---|---|---|---|
e94f0edafb | |||
c417d96e48 | |||
c5b1dbb46e | |||
893a2e2709 | |||
01f0dc6b20 | |||
736a42066c | |||
44403863d1 | |||
5be21fa100 | |||
042167c01e | |||
db38d038ef | |||
a4d8a46360 | |||
84e5c10129 | |||
98a2871f08 | |||
9bb847210a | |||
91d4ba5715 | |||
3ee2988964 | |||
221f1f6237 | |||
0a6fbf588e | |||
1a8546af6e | |||
9e01bdb8e2 | |||
33833fbc6c | |||
97b5678fe4 | |||
9b96984413 | |||
0de412b842 | |||
64bcce9e23 | |||
390812ec33 | |||
a672bb920f | |||
c3fabb6107 | |||
b4ebdbae47 | |||
c55d2dcf10 | |||
7274d5ea8a | |||
1ec733c2a9 | |||
3ef14d8ac7 | |||
f332bf95fe | |||
6acad8a306 | |||
6ae672f707 | |||
3d263e2181 | |||
85639f29af | |||
85448a21fc | |||
3b85c4a37e | |||
67dd650dce | |||
92eaf5f19a | |||
fcf9a61b1d | |||
fbd258a876 | |||
c65b8fc1af | |||
b7b588d713 | |||
00d672c135 | |||
6a1e778b49 | |||
4cfb26f62f | |||
7ba3ad0b21 | |||
cd93ec2560 | |||
ede884930e | |||
1049920efd | |||
b7a34403c5 | |||
987e19372a | |||
0b97aaee91 | |||
b87ca1fa03 | |||
824f872fa5 | |||
80248c77d0 | |||
f592150646 | |||
f24652936a | |||
79af6c4a68 | |||
fafc757930 | |||
ead5e1a7bd | |||
ad57be180a | |||
7cc4ce02c9 |
@ -1,4 +1,4 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
image: hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
@ -10,16 +10,19 @@ before_script:
|
|||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm install
|
||||||
- npmci npm test stable
|
- npmci npm test stable
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
environment: npmjs-com_registry
|
environment: npmjs-com_registry
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
|
8
npmextra.json
Normal file
8
npmextra.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"npmci": {
|
||||||
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"npmdocker": {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
1224
package-lock.json
generated
1224
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
28
package.json
28
package.json
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartnginx",
|
"name": "@pushrocks/smartnginx",
|
||||||
"version": "2.0.0",
|
"version": "2.0.33",
|
||||||
|
"private": false,
|
||||||
"description": "control nginx from node, TypeScript ready",
|
"description": "control nginx from node, TypeScript ready",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
@ -23,18 +24,21 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartnginx#README",
|
"homepage": "https://gitlab.com/pushrocks/smartnginx#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartfile": "^6.0.6",
|
"@pushrocks/lik": "^3.0.4",
|
||||||
"@pushrocks/smartlog": "^2.0.1",
|
"@pushrocks/smartfile": "^6.0.11",
|
||||||
|
"@pushrocks/smartlog": "^2.0.11",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
"@pushrocks/smartshell": "^2.0.6",
|
"@pushrocks/smartshell": "^2.0.13",
|
||||||
"@pushrocks/smartstring": "^3.0.0"
|
"@pushrocks/smartstring": "^3.0.8",
|
||||||
|
"@pushrocks/smartunique": "^3.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.4",
|
||||||
"@gitzone/tsrun": "^1.1.12",
|
"@gitzone/tsrun": "^1.1.17",
|
||||||
"@gitzone/tstest": "^1.0.13",
|
"@gitzone/tstest": "^1.0.18",
|
||||||
"@pushrocks/tapbundle": "^3.0.5",
|
"@pushrocks/qenv": "^4.0.0",
|
||||||
"qenv": "^1.1.7"
|
"@pushrocks/tapbundle": "^3.0.7",
|
||||||
},
|
"tslint": "^5.12.1",
|
||||||
"private": true
|
"tslint-config-prettier": "^1.17.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
32
test/test.ts
32
test/test.ts
@ -1,43 +1,53 @@
|
|||||||
import { tap, expect } from '@pushrocks/tapbundle';
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import path = require('path');
|
import path = require('path');
|
||||||
|
|
||||||
import { Qenv } from 'qenv';
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
let testQenv = new Qenv(process.cwd(), path.join(process.cwd(), '.nogit'));
|
const testQenv = new Qenv('./', './.nogit/');
|
||||||
|
|
||||||
|
import * as smartnginx from '../ts/index';
|
||||||
import * as smartnginx from '../ts';
|
|
||||||
|
|
||||||
let testSmartNginx: smartnginx.SmartNginx;
|
let testSmartNginx: smartnginx.SmartNginx;
|
||||||
let testNginxZone01: smartnginx.NginxHost;
|
let testNginxZone01: smartnginx.NginxHost;
|
||||||
let testNginxZone02: smartnginx.NginxHost;
|
let testNginxZone02: smartnginx.NginxHost;
|
||||||
|
|
||||||
tap.test('should create a valid instance of SmartNginx', async () => {
|
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);
|
expect(testSmartNginx).to.be.instanceof(smartnginx.SmartNginx);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test(`should produce an instance of NginxConfig`, async () => {
|
tap.test(`should produce an instance of NginxConfig`, async () => {
|
||||||
testNginxZone01 = new smartnginx.NginxHost(testSmartNginx, {
|
testNginxZone01 = new smartnginx.NginxHost(testSmartNginx, {
|
||||||
hostName: 'test100.bleu.de',
|
hostName: 'test100.bleu.de',
|
||||||
destination: '192.192.192.191'
|
destination: '192.192.192.191',
|
||||||
|
destinationPort: 3000,
|
||||||
|
privateKey: 'some private',
|
||||||
|
publicKey: 'some public'
|
||||||
});
|
});
|
||||||
testNginxZone02 = new smartnginx.NginxHost(testSmartNginx, {
|
testNginxZone02 = new smartnginx.NginxHost(testSmartNginx, {
|
||||||
hostName: 'test102.bleu.de',
|
hostName: 'test102.bleu.de',
|
||||||
destination: '192.192.192.192'
|
destination: '192.192.192.192',
|
||||||
|
destinationPort: 3050,
|
||||||
|
privateKey: 'some private',
|
||||||
|
publicKey: 'some public'
|
||||||
});
|
});
|
||||||
expect(testNginxZone01).to.be.instanceof(smartnginx.NginxHost);
|
expect(testNginxZone01).to.be.instanceof(smartnginx.NginxHost);
|
||||||
console.log(testNginxZone01.configString);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('.addZone() should add a zone to NginxConfig Object', async () => {
|
tap.test('.addHostCandidate() should add a zone to NginxConfig Object', async () => {
|
||||||
testSmartNginx.addHost(testNginxZone01);
|
testSmartNginx.addHostCandidate(testNginxZone01);
|
||||||
testSmartNginx.addHost(testNginxZone02);
|
testSmartNginx.addHostCandidate(testNginxZone02);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('.deploy() should deploy a config from an instance', async () => {
|
tap.test('.deploy() should deploy a config from an instance', async () => {
|
||||||
await testSmartNginx.deploy();
|
await testSmartNginx.deploy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should not redeploy', async () => {
|
||||||
|
testSmartNginx.addHostCandidate(testNginxZone01);
|
||||||
|
testSmartNginx.addHostCandidate(testNginxZone02);
|
||||||
|
await testSmartNginx.deploy();
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('.stop() should end the process', async () => {
|
tap.test('.stop() should end the process', async () => {
|
||||||
testSmartNginx.nginxProcess.stop();
|
testSmartNginx.nginxProcess.stop();
|
||||||
});
|
});
|
||||||
|
7
ts/interfaces/hostconfig.ts
Normal file
7
ts/interfaces/hostconfig.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface IHostConfig {
|
||||||
|
hostName: string;
|
||||||
|
destination: string;
|
||||||
|
destinationPort: number;
|
||||||
|
privateKey: string;
|
||||||
|
publicKey: string;
|
||||||
|
}
|
0
ts/interfaces/index.ts
Normal file
0
ts/interfaces/index.ts
Normal file
@ -1,5 +0,0 @@
|
|||||||
import * as plugins from './smartnginx.plugins';
|
|
||||||
|
|
||||||
export class CertHandler {
|
|
||||||
|
|
||||||
}
|
|
@ -4,13 +4,7 @@ import * as snippets from './smartnginx.snippets';
|
|||||||
|
|
||||||
import { SmartNginx } from './smartnginx.classes.smartnginx';
|
import { SmartNginx } from './smartnginx.classes.smartnginx';
|
||||||
|
|
||||||
/**
|
import { IHostConfig } from './interfaces/hostconfig';
|
||||||
* the host config data that NginxHost needs to create a valid instance
|
|
||||||
*/
|
|
||||||
export interface IHostConfigData {
|
|
||||||
hostName: string;
|
|
||||||
destination: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum hostTypes {
|
export enum hostTypes {
|
||||||
reverseProxy
|
reverseProxy
|
||||||
@ -19,29 +13,45 @@ export enum hostTypes {
|
|||||||
/**
|
/**
|
||||||
* manages a single nginx host
|
* manages a single nginx host
|
||||||
*/
|
*/
|
||||||
export class NginxHost {
|
export class NginxHost implements IHostConfig {
|
||||||
/**
|
/**
|
||||||
* smartnginxInstance this NginHost belongs to
|
* smartnginxInstance this NginHost belongs to
|
||||||
*/
|
*/
|
||||||
smartnginxInstance: SmartNginx
|
public smartnginxInstance: SmartNginx;
|
||||||
|
|
||||||
hostName: string; // the host name e.g. domain name
|
public hostName: string; // the host name e.g. domain name
|
||||||
destination: string;
|
public destination: string;
|
||||||
configString: string; // the actual host config file as string
|
public destinationPort: number;
|
||||||
constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfigData) {
|
public configString: string; // the actual host config file as string
|
||||||
|
public privateKey: string;
|
||||||
|
public publicKey: string;
|
||||||
|
|
||||||
|
constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfig) {
|
||||||
this.smartnginxInstance = smartnginxInstanceArg;
|
this.smartnginxInstance = smartnginxInstanceArg;
|
||||||
this.hostName = optionsArg.hostName;
|
this.hostName = optionsArg.hostName;
|
||||||
this.destination = optionsArg.destination;
|
this.destination = optionsArg.destination;
|
||||||
this.configString = snippets.getHostConfigString(optionsArg.hostName, optionsArg.destination);
|
this.destinationPort = optionsArg.destinationPort;
|
||||||
|
this.privateKey = optionsArg.privateKey;
|
||||||
|
this.publicKey = optionsArg.publicKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param certInstanceArg
|
* @param certInstanceArg
|
||||||
*/
|
*/
|
||||||
async deploy() {
|
public async deploy() {
|
||||||
let filePath = plugins.path.join(paths.nginxHostFileBase, this.hostName + '.conf');
|
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
|
// writeConfig
|
||||||
plugins.smartfile.memory.toFsSync(this.configString, filePath);
|
this.configString = snippets.getHostConfigString(this.hostName, this.destination, this.destinationPort);
|
||||||
|
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 { Smartshell } from '@pushrocks/smartshell';
|
||||||
|
|
||||||
|
import { ChildProcess } from 'child_process';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* manages a nginxprocess for an NginxConfig
|
* manages a nginxprocess for an NginxConfig
|
||||||
*/
|
*/
|
||||||
export class NginxProcess {
|
export class NginxProcess {
|
||||||
started: boolean = false;
|
public started: boolean = false;
|
||||||
nginxConfig: SmartNginx;
|
public smartNginxRef: SmartNginx;
|
||||||
nginxChildProcess: plugins.childProcess.ChildProcess;
|
private nginxChildProcess: ChildProcess;
|
||||||
smartshellInstance = new Smartshell({
|
private smartshellInstance = new Smartshell({
|
||||||
executor: 'bash'
|
executor: 'bash'
|
||||||
});
|
});
|
||||||
constructor(nginxConfigArg) {
|
|
||||||
this.nginxConfig = nginxConfigArg;
|
constructor(nginxRefArg: SmartNginx) {
|
||||||
|
this.smartNginxRef = nginxRefArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start nginx
|
* start nginx
|
||||||
*/
|
*/
|
||||||
start() {
|
public async start() {
|
||||||
let done = plugins.smartpromise.defer();
|
if (!this.nginxChildProcess) {
|
||||||
if (typeof this.nginxChildProcess == 'undefined') {
|
this.nginxChildProcess = (await this.smartshellInstance.execStreaming(
|
||||||
this.nginxChildProcess = plugins.childProcess.exec(
|
`nginx -c ${paths.nginxConfFile}`
|
||||||
`nginx -c ${paths.nginxConfFile}`,
|
)).childProcess;
|
||||||
function(error, stdout, stderr) {
|
|
||||||
console.log(`stdout: ${stdout}`);
|
|
||||||
console.log(`stderr: ${stderr}`);
|
|
||||||
if (error !== null) {
|
|
||||||
console.log(`exec error: ${error}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
this.started = true;
|
this.started = true;
|
||||||
plugins.smartlog.defaultLogger.info('started Nginx!');
|
plugins.smartlog.defaultLogger.log('info', 'started Nginx!');
|
||||||
done.resolve();
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reload config
|
* reload config
|
||||||
*/
|
*/
|
||||||
reloadConfig() {
|
public async reloadConfig() {
|
||||||
let done = plugins.smartpromise.defer();
|
if (!this.started) {
|
||||||
if (this.started == false) {
|
|
||||||
this.start();
|
this.start();
|
||||||
} else {
|
} else {
|
||||||
this.smartshellInstance.exec('nginx -s reload');
|
await this.smartshellInstance.exec('nginx -s reload');
|
||||||
}
|
}
|
||||||
plugins.smartlog.defaultLogger.info('NginxProcess has loaded the new config!');
|
this.smartNginxRef.logger.log('info', 'NginxProcess has loaded the new config!');
|
||||||
done.resolve();
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stop the nginx instance
|
* stop the nginx instance
|
||||||
*/
|
*/
|
||||||
stop() {
|
public async stop() {
|
||||||
let done = plugins.smartpromise.defer();
|
if (this.nginxChildProcess) {
|
||||||
if (typeof this.nginxChildProcess != 'undefined') {
|
|
||||||
this.smartshellInstance.exec('nginx -s quit');
|
this.smartshellInstance.exec('nginx -s quit');
|
||||||
this.started = false;
|
this.started = false;
|
||||||
plugins.smartlog.defaultLogger.info('stopped Nginx!');
|
this.smartNginxRef.logger.log('info', 'stopped Nginx!');
|
||||||
} else {
|
} 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
|
* checks if nginx is in path
|
||||||
*/
|
*/
|
||||||
check(): boolean {
|
public check(): boolean {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,24 @@ import * as paths from './smartnginx.paths';
|
|||||||
import * as snippets from './smartnginx.snippets';
|
import * as snippets from './smartnginx.snippets';
|
||||||
import { NginxHost } from './smartnginx.classes.nginxhost';
|
import { NginxHost } from './smartnginx.classes.nginxhost';
|
||||||
import { NginxProcess } from './smartnginx.classes.nginxprocess';
|
import { NginxProcess } from './smartnginx.classes.nginxprocess';
|
||||||
import { CertHandler } from './smartnginx.classes.certhandler';
|
import { IHostConfig } from './interfaces/hostconfig';
|
||||||
let allConfigs: SmartNginx[] = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* main class that manages a NginxInstance
|
* main class that manages a NginxInstance
|
||||||
*/
|
*/
|
||||||
export class SmartNginx {
|
export class SmartNginx {
|
||||||
certHandler = new CertHandler();
|
public logger: plugins.smartlog.Smartlog;
|
||||||
hosts: NginxHost[] = [];
|
|
||||||
nginxProcess: NginxProcess = new NginxProcess(this);
|
|
||||||
isDeployed: boolean = false;
|
|
||||||
constructor() {
|
|
||||||
|
|
||||||
};
|
// the objectmaps
|
||||||
|
private deployedHosts = new plugins.lik.Objectmap<NginxHost>();
|
||||||
|
private hostCandidates = 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
|
// interact with Hosts
|
||||||
@ -26,49 +30,96 @@ export class SmartNginx {
|
|||||||
* add a host
|
* add a host
|
||||||
* @param nginxHostArg
|
* @param nginxHostArg
|
||||||
*/
|
*/
|
||||||
addHost(nginxHostArg: NginxHost) {
|
public addHostCandidate(optionsArg: IHostConfig): NginxHost {
|
||||||
this.hosts.push(nginxHostArg);
|
const nginxHost = new NginxHost(this, optionsArg);
|
||||||
|
this.hostCandidates.add(nginxHost);
|
||||||
|
return nginxHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a NginxHost by hostname
|
||||||
|
* @param hostNameArg
|
||||||
|
*/
|
||||||
|
public getDeployedNginxHostByHostName(hostNameArg: string): NginxHost {
|
||||||
|
return this.deployedHosts.find(nginxHost => {
|
||||||
|
return nginxHost.hostName === hostNameArg;
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* listHosts
|
* listHosts
|
||||||
*/
|
*/
|
||||||
listHosts(): NginxHost[] {
|
public async listDeployedHosts(): Promise<NginxHost[]> {
|
||||||
return this.hosts;
|
return this.deployedHosts.getArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* remove a Host
|
* remove a Host
|
||||||
* @param nginxHostArg
|
* @param nginxHostArg
|
||||||
*/
|
*/
|
||||||
removeHost(nginxHostArg: NginxHost) {}
|
public async removeDeployedHost(nginxHostArg: NginxHost) {
|
||||||
|
if (this.hostCandidates.isEmpty()) {
|
||||||
|
this.deployedHosts.forEach(hostArg => {
|
||||||
|
this.hostCandidates.add(hostArg);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.hostCandidates.remove(nginxHostArg);
|
||||||
|
this.deploy();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* clean all hosts
|
* check wether there has been a diverging host configuration
|
||||||
|
* this function will only redeploy the nginx configuration in case there has been a change
|
||||||
*/
|
*/
|
||||||
clean() {
|
private async areHostsDiverged(): Promise<boolean> {
|
||||||
this.hosts = [];
|
let hostCounter = 0;
|
||||||
|
let unfoundHosts = 0;
|
||||||
|
await this.hostCandidates.forEach(async hostCandidateArg => {
|
||||||
|
let foundHost = false;
|
||||||
|
await this.deployedHosts.forEach(async deployedHostArg => {
|
||||||
|
if (
|
||||||
|
hostCandidateArg.hostName === deployedHostArg.hostName &&
|
||||||
|
hostCandidateArg.destination === deployedHostArg.destination &&
|
||||||
|
hostCandidateArg.destinationPort === deployedHostArg.destinationPort
|
||||||
|
) {
|
||||||
|
hostCounter++;
|
||||||
|
foundHost = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!foundHost) {
|
||||||
|
unfoundHosts++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return (
|
||||||
|
this.deployedHosts.getArray().length !== this.hostCandidates.getArray().length ||
|
||||||
|
hostCounter !== this.deployedHosts.getArray().length ||
|
||||||
|
unfoundHosts !== 0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* deploy the current stack and restart nginx
|
* deploy the current stack and restart nginx
|
||||||
*/
|
*/
|
||||||
async deploy() {
|
public async deploy() {
|
||||||
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase);
|
if (await this.areHostsDiverged()) {
|
||||||
plugins.smartfile.fs.ensureDirSync(paths.nginxHostFileBase);
|
this.logger.log('ok', `hosts have diverged, trigger config deployment and nginx reload!`);
|
||||||
plugins.smartfile.fs.ensureDirSync(paths.nginxCertBase);
|
this.deployedHosts.wipe();
|
||||||
for (let config of allConfigs) {
|
this.deployedHosts.addArray(this.hostCandidates.getArray());
|
||||||
config.isDeployed = false;
|
this.hostCandidates.wipe();
|
||||||
}
|
|
||||||
this.isDeployed = true;
|
|
||||||
// write base config
|
// write base config
|
||||||
|
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath);
|
||||||
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
|
plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile);
|
||||||
|
|
||||||
// deploy hosts
|
// deploy hosts
|
||||||
let promiseArray = [];
|
plugins.smartfile.fs.ensureEmptyDirSync(paths.nginxHostDirPath);
|
||||||
for (let host of this.hosts) {
|
for (const host of this.deployedHosts.getArray()) {
|
||||||
await host.deploy();
|
await host.deploy();
|
||||||
plugins.smartlog.defaultLogger.info(`Host ${host.hostName} deployed!`);
|
this.logger.log('info', `Host ${host.hostName} deployed!`);
|
||||||
|
}
|
||||||
this.nginxProcess.reloadConfig();
|
this.nginxProcess.reloadConfig();
|
||||||
};
|
} else {
|
||||||
|
this.logger.log('info', `hosts have not diverged, skipping nginx reload`);
|
||||||
|
this.hostCandidates.wipe();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
import * as plugins from './smartnginx.plugins';
|
import * as plugins from './smartnginx.plugins';
|
||||||
|
|
||||||
// directories
|
// directories
|
||||||
export let packageBase = plugins.path.join(__dirname, '../');
|
export const packageBase = plugins.path.join(__dirname, '../');
|
||||||
export let nginxConfigBase = plugins.path.join(packageBase, 'nginxconfig');
|
export const nginxConfigDirPath = plugins.path.join(packageBase, 'nginxconfig');
|
||||||
|
export const nginxHostDirPath = plugins.path.join(nginxConfigDirPath, 'hosts');
|
||||||
export let nginxHostFileBase = plugins.path.join(nginxConfigBase, 'hosts');
|
|
||||||
export let nginxCertBase = plugins.path.join(nginxConfigBase, 'cert');
|
|
||||||
|
|
||||||
// files
|
// files
|
||||||
export let nginxConfFile = plugins.path.join(nginxConfigBase, 'nginx.conf');
|
export const nginxConfFile = plugins.path.join(nginxConfigDirPath, 'nginx.conf');
|
@ -1,17 +1,15 @@
|
|||||||
import * as smartlog from '@pushrocks/smartlog';
|
// native
|
||||||
import * as childProcess from 'child_process';
|
|
||||||
import * as path from 'path';
|
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 smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartshell from '@pushrocks/smartshell';
|
import * as smartshell from '@pushrocks/smartshell';
|
||||||
import * as smartfile from '@pushrocks/smartfile';
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
import * as smartstring from '@pushrocks/smartstring';
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
export {
|
export { lik, smartlog, smartpromise, smartshell, smartfile, smartstring, smartunique };
|
||||||
smartlog,
|
|
||||||
childProcess,
|
|
||||||
path,
|
|
||||||
smartpromise,
|
|
||||||
smartshell,
|
|
||||||
smartfile,
|
|
||||||
smartstring
|
|
||||||
}
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import * as plugins from './smartnginx.plugins';
|
import * as plugins from './smartnginx.plugins';
|
||||||
import * as paths from './smartnginx.paths';
|
import * as paths from './smartnginx.paths';
|
||||||
export let getBaseConfigString = () => {
|
export let getBaseConfigString = () => {
|
||||||
let baseConfig = plugins.smartstring.indent.normalize(`
|
const baseConfig = plugins.smartstring.indent.normalize(`
|
||||||
user www-data;
|
user www-data;
|
||||||
worker_processes auto;
|
worker_processes auto;
|
||||||
pid /run/nginx.pid;
|
pid /run/nginx/nginx.pid;
|
||||||
|
|
||||||
events {
|
events {
|
||||||
worker_connections 768;
|
worker_connections 768;
|
||||||
@ -63,7 +63,7 @@ export let getBaseConfigString = () => {
|
|||||||
# Virtual Host Configs
|
# Virtual Host Configs
|
||||||
##
|
##
|
||||||
|
|
||||||
include ${paths.nginxHostFileBase}/*.conf;
|
include ${paths.nginxHostDirPath}/*.conf;
|
||||||
include /etc/nginx/sites-enabled/*;
|
include /etc/nginx/sites-enabled/*;
|
||||||
}
|
}
|
||||||
daemon off;
|
daemon off;
|
||||||
@ -71,13 +71,18 @@ export let getBaseConfigString = () => {
|
|||||||
return baseConfig;
|
return baseConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string) => {
|
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string, destinationPortArg = 80) => {
|
||||||
let hostConfig = plugins.smartstring.indent.normalize(`
|
const uniqueString = plugins.smartunique.shortId();
|
||||||
upstream ${hostNameArg} {
|
const hostConfig = plugins.smartstring.indent.normalize(`
|
||||||
server ${destinationIpArg};
|
upstream ${uniqueString} {
|
||||||
|
keepalive 100;
|
||||||
|
server ${destinationIpArg}:${destinationPortArg};
|
||||||
}
|
}
|
||||||
|
|
||||||
server {
|
server {
|
||||||
|
# The keepalive parameter sets the maximum number of idle keepalive connections
|
||||||
|
# to upstream servers that are preserved in the cache of each worker process. When
|
||||||
|
# this number is exceeded, the least recently used connections are closed.
|
||||||
listen *:80 ;
|
listen *:80 ;
|
||||||
server_name ${hostNameArg};
|
server_name ${hostNameArg};
|
||||||
rewrite ^ https://${hostNameArg}$request_uri? permanent;
|
rewrite ^ https://${hostNameArg}$request_uri? permanent;
|
||||||
@ -86,15 +91,16 @@ export let getHostConfigString = (hostNameArg: string, destinationIpArg: string)
|
|||||||
server {
|
server {
|
||||||
listen *:443 ssl;
|
listen *:443 ssl;
|
||||||
server_name ${hostNameArg};
|
server_name ${hostNameArg};
|
||||||
ssl_certificate ${paths.nginxCertBase}/${hostNameArg}/fullchain.pem;
|
ssl_certificate ${paths.nginxHostDirPath}/${hostNameArg}.public.pem;
|
||||||
ssl_certificate_key ${paths.nginxCertBase}/${hostNameArg}/privkey.pem;
|
ssl_certificate_key ${paths.nginxHostDirPath}/${hostNameArg}.private.pem;
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
proxy_pass http://${hostNameArg};
|
proxy_http_version 1.1;
|
||||||
include /etc/nginx/proxy_params;
|
proxy_buffering off;
|
||||||
}
|
proxy_set_header Host $http_host;
|
||||||
location ~ /\.git {
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
deny all;
|
proxy_set_header X-Forwarded-For $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_pass http://${uniqueString};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
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