Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
91d4ba5715 | |||
3ee2988964 | |||
221f1f6237 | |||
0a6fbf588e | |||
1a8546af6e | |||
9e01bdb8e2 | |||
33833fbc6c | |||
97b5678fe4 | |||
9b96984413 | |||
0de412b842 | |||
64bcce9e23 | |||
390812ec33 | |||
a672bb920f | |||
c3fabb6107 | |||
b4ebdbae47 | |||
c55d2dcf10 | |||
7274d5ea8a | |||
1ec733c2a9 | |||
3ef14d8ac7 | |||
f332bf95fe | |||
6acad8a306 | |||
6ae672f707 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartnginx",
|
"name": "@pushrocks/smartnginx",
|
||||||
"version": "2.0.15",
|
"version": "2.0.26",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartnginx",
|
"name": "@pushrocks/smartnginx",
|
||||||
"version": "2.0.15",
|
"version": "2.0.26",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "control nginx from node, TypeScript ready",
|
"description": "control nginx from node, TypeScript ready",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -19,12 +19,14 @@ 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',
|
privateKey: 'some private',
|
||||||
publicKey: 'some public'
|
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',
|
privateKey: 'some private',
|
||||||
publicKey: 'some public'
|
publicKey: 'some public'
|
||||||
});
|
});
|
||||||
@ -40,6 +42,12 @@ 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();
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
export interface IHostConfig {
|
export interface IHostConfig {
|
||||||
hostName: string;
|
hostName: string;
|
||||||
destination: string;
|
destination: string;
|
||||||
|
destinationPort: number;
|
||||||
privateKey: string;
|
privateKey: string;
|
||||||
publicKey: string;
|
publicKey: string;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ export class NginxHost implements IHostConfig {
|
|||||||
|
|
||||||
public hostName: string; // the host name e.g. domain name
|
public hostName: string; // the host name e.g. domain name
|
||||||
public destination: string;
|
public destination: string;
|
||||||
|
public destinationPort: number;
|
||||||
public configString: string; // the actual host config file as string
|
public configString: string; // the actual host config file as string
|
||||||
public privateKey: string;
|
public privateKey: string;
|
||||||
public publicKey: string;
|
public publicKey: string;
|
||||||
@ -29,7 +30,7 @@ export class NginxHost implements 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.privateKey = optionsArg.privateKey;
|
||||||
this.publicKey = optionsArg.publicKey;
|
this.publicKey = optionsArg.publicKey;
|
||||||
}
|
}
|
||||||
@ -42,7 +43,10 @@ export class NginxHost implements IHostConfig {
|
|||||||
const filePathConfig = plugins.path.join(paths.nginxHostDirPath, `${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 filePathPrivate = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.private.pem`);
|
||||||
const filePathPublic = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.public.pem`);
|
const filePathPublic = plugins.path.join(paths.nginxHostDirPath, `${this.hostName}.public.pem`);
|
||||||
|
|
||||||
|
|
||||||
// writeConfig
|
// writeConfig
|
||||||
|
this.configString = snippets.getHostConfigString(this.hostName, this.destination, this.destinationPort);
|
||||||
plugins.smartfile.memory.toFsSync(this.configString, filePathConfig);
|
plugins.smartfile.memory.toFsSync(this.configString, filePathConfig);
|
||||||
|
|
||||||
// write ssl
|
// write ssl
|
||||||
|
@ -70,15 +70,16 @@ export class SmartNginx {
|
|||||||
* check wether there has been a diverging host configuration
|
* check wether there has been a diverging host configuration
|
||||||
* this function will only redeploy the nginx configuration in case there has been a change
|
* this function will only redeploy the nginx configuration in case there has been a change
|
||||||
*/
|
*/
|
||||||
private areHostsDiverged(): boolean {
|
private async areHostsDiverged(): Promise<boolean> {
|
||||||
let hostCounter = 0;
|
let hostCounter = 0;
|
||||||
let unfoundHosts = 0;
|
let unfoundHosts = 0;
|
||||||
this.hostCandidates.forEach(hostCandidateArg => {
|
await this.hostCandidates.forEach(async hostCandidateArg => {
|
||||||
let foundHost = false;
|
let foundHost = false;
|
||||||
this.deployedHosts.forEach(deployedHostArg => {
|
await this.deployedHosts.forEach(async deployedHostArg => {
|
||||||
if (
|
if (
|
||||||
hostCandidateArg.hostName === deployedHostArg.hostName &&
|
hostCandidateArg.hostName === deployedHostArg.hostName &&
|
||||||
hostCandidateArg.destination === deployedHostArg.destination
|
hostCandidateArg.destination === deployedHostArg.destination &&
|
||||||
|
hostCandidateArg.destinationPort === deployedHostArg.destinationPort
|
||||||
) {
|
) {
|
||||||
hostCounter++;
|
hostCounter++;
|
||||||
foundHost = true;
|
foundHost = true;
|
||||||
@ -89,7 +90,7 @@ export class SmartNginx {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
return (
|
return (
|
||||||
this.deployedHosts.getArray.length !== this.hostCandidates.getArray().length ||
|
this.deployedHosts.getArray().length !== this.hostCandidates.getArray().length ||
|
||||||
hostCounter !== this.deployedHosts.getArray().length ||
|
hostCounter !== this.deployedHosts.getArray().length ||
|
||||||
unfoundHosts !== 0
|
unfoundHosts !== 0
|
||||||
);
|
);
|
||||||
@ -99,7 +100,7 @@ export class SmartNginx {
|
|||||||
* deploy the current stack and restart nginx
|
* deploy the current stack and restart nginx
|
||||||
*/
|
*/
|
||||||
public async deploy() {
|
public async deploy() {
|
||||||
if (this.areHostsDiverged()) {
|
if (await this.areHostsDiverged()) {
|
||||||
this.logger.log('ok', `hosts have diverged, trigger config deployment and nginx reload!`);
|
this.logger.log('ok', `hosts have diverged, trigger config deployment and nginx reload!`);
|
||||||
this.deployedHosts.wipe();
|
this.deployedHosts.wipe();
|
||||||
this.deployedHosts.addArray(this.hostCandidates.getArray());
|
this.deployedHosts.addArray(this.hostCandidates.getArray());
|
||||||
@ -118,6 +119,7 @@ export class SmartNginx {
|
|||||||
this.nginxProcess.reloadConfig();
|
this.nginxProcess.reloadConfig();
|
||||||
} else {
|
} else {
|
||||||
this.logger.log('info', `hosts have not diverged, skipping nginx reload`);
|
this.logger.log('info', `hosts have not diverged, skipping nginx reload`);
|
||||||
|
this.hostCandidates.wipe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
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/nginx.pid;
|
pid /run/nginx/nginx.pid;
|
||||||
@ -71,33 +71,37 @@ export let getBaseConfigString = () => {
|
|||||||
return baseConfig;
|
return baseConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string) => {
|
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string, portArg = 80) => {
|
||||||
let hostConfig = plugins.smartstring.indent.normalize(`
|
const hostConfig = plugins.smartstring.indent.normalize(`
|
||||||
upstream ${hostNameArg} {
|
upstream ${hostNameArg} {
|
||||||
|
keepalive 100;
|
||||||
server ${destinationIpArg};
|
server ${destinationIpArg};
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen *:443 ssl;
|
listen *:443 ssl http2;
|
||||||
server_name ${hostNameArg};
|
server_name ${hostNameArg};
|
||||||
ssl_certificate ${paths.nginxHostDirPath}/${hostNameArg}.public.pem;
|
ssl_certificate ${paths.nginxHostDirPath}/${hostNameArg}.public.pem;
|
||||||
ssl_certificate_key ${paths.nginxHostDirPath}/${hostNameArg}.private.pem;
|
ssl_certificate_key ${paths.nginxHostDirPath}/${hostNameArg}.private.pem;
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
proxy_set_header Host $http_host;
|
http2_push_preload on;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_redirect off;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_buffering off;
|
||||||
proxy_pass http://${hostNameArg};
|
proxy_set_header Host $http_host;
|
||||||
}
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
location ~ /\.git {
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
deny all;
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_pass http://${hostNameArg}:${portArg};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
Reference in New Issue
Block a user