BREAKING CHANGE(scope): change scope to @pushrocks
This commit is contained in:
parent
381227406b
commit
9b043d0e0d
@ -10,7 +10,7 @@ before_script:
|
|||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test stable
|
- npmci npm test stable
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
@ -20,7 +20,7 @@ release:
|
|||||||
stage: release
|
stage: release
|
||||||
environment: npmjs-com_registry
|
environment: npmjs-com_registry
|
||||||
script:
|
script:
|
||||||
- npmci publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
23
README.md
23
README.md
@ -1,26 +1,29 @@
|
|||||||
# smartnginx
|
# smartnginx
|
||||||
|
|
||||||
control nginx from node, TypeScript ready
|
control nginx from node, TypeScript ready
|
||||||
|
|
||||||
## Status
|
## Status
|
||||||
|
|
||||||
[![build status](https://gitlab.com/pushrocks/smartnginx/badges/master/build.svg)](https://gitlab.com/pushrocks/smartnginx/commits/master)
|
[![build status](https://gitlab.com/pushrocks/smartnginx/badges/master/build.svg)](https://gitlab.com/pushrocks/smartnginx/commits/master)
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* easy reverse configuration
|
- easy reverse configuration
|
||||||
* automatic letsencrypt DNS01 challenge based ssl cert generation
|
- automatic letsencrypt DNS01 challenge based ssl cert generation
|
||||||
* automatic nginx process handling zero-downtime config reloading
|
- automatic nginx process handling zero-downtime config reloading
|
||||||
* works in Docker environements
|
- works in Docker environements
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
We recommend the use of TypeScript! :)
|
We recommend the use of TypeScript! :)
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import * as smartnginx from "smartnginx";
|
import * as smartnginx from 'smartnginx';
|
||||||
myNginxConfig = new smartnginx.NginxConfig();
|
const smartnginxInstance = new smartnginx.SmartNginx();
|
||||||
myNginxZone = new smartnginx.NginxZone({
|
myNginxHost = new smartnginx.NginxHost({
|
||||||
zoneName:"some.example.com",
|
hostName: 'some.example.com',
|
||||||
type:"reverseProxy",
|
type: 'reverseProxy',
|
||||||
destination:"192.192.192.192" // some destination IP
|
destination: '192.192.192.192' // some destination IP
|
||||||
});
|
});
|
||||||
myNginxConfig.addZone(myNginxZone); // adds the zone to the config
|
myNginxConfig.addZone(myNginxZone); // adds the zone to the config
|
||||||
myNginxConfig.deploy(); // deploys the referenced NginxConfig and gracefully reloads it
|
myNginxConfig.deploy(); // deploys the referenced NginxConfig and gracefully reloads it
|
||||||
|
1073
package-lock.json
generated
Normal file
1073
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "smartnginx",
|
"name": "@pushrocks/smartnginx",
|
||||||
"version": "1.0.6",
|
"version": "1.0.6",
|
||||||
"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",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)",
|
"test": "tstest test/",
|
||||||
"cleanTest": "(rm -r nginxconfig)"
|
"cleanTest": "(rm -r nginxconfig) && npm run test",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -22,19 +23,18 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartnginx#README",
|
"homepage": "https://gitlab.com/pushrocks/smartnginx#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/q": "0.x.x",
|
"@pushrocks/smartfile": "^6.0.6",
|
||||||
"@types/shelljs": "0.x.x",
|
"@pushrocks/smartlog": "^2.0.1",
|
||||||
"beautylog": "^5.0.20",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
"cert": "^1.0.7",
|
"@pushrocks/smartshell": "^2.0.6",
|
||||||
"q": "^1.4.1",
|
"@pushrocks/smartstring": "^3.0.0"
|
||||||
"shelljs": "^0.7.3",
|
|
||||||
"smartfile": "^4.0.13",
|
|
||||||
"smartstring": "^2.0.15"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"npmts-g": "^5.2.8",
|
"@gitzone/tsbuild": "^2.0.22",
|
||||||
"qenv": "^1.0.8",
|
"@gitzone/tsrun": "^1.1.12",
|
||||||
"should": "^10.0.0",
|
"@gitzone/tstest": "^1.0.13",
|
||||||
"typings-test": "^1.0.1"
|
"@pushrocks/tapbundle": "^3.0.5",
|
||||||
}
|
"qenv": "^1.1.7"
|
||||||
|
},
|
||||||
|
"private": true
|
||||||
}
|
}
|
||||||
|
2
test/test.d.ts
vendored
2
test/test.d.ts
vendored
@ -1,2 +0,0 @@
|
|||||||
import "typings-test";
|
|
||||||
import "should";
|
|
61
test/test.js
61
test/test.js
@ -1,61 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
const path = require("path");
|
|
||||||
require("should");
|
|
||||||
const qenv_1 = require("qenv");
|
|
||||||
const smartnginx = require("../dist/index");
|
|
||||||
// setup environment
|
|
||||||
let testQenv = new qenv_1.Qenv(process.cwd(), path.join(process.cwd(), ".nogit"));
|
|
||||||
describe("smartnginx", function () {
|
|
||||||
let testNginxConfig;
|
|
||||||
let testNginxZone01;
|
|
||||||
let testNginxZone02;
|
|
||||||
describe("NginxZone", function () {
|
|
||||||
it(`"new NginxZone()" should produce an instance of NginxConfig`, function () {
|
|
||||||
testNginxZone01 = new smartnginx.NginxHost({
|
|
||||||
hostName: "test100.bleu.de",
|
|
||||||
type: smartnginx.hostTypes.reverseProxy,
|
|
||||||
destination: "192.192.192.191"
|
|
||||||
});
|
|
||||||
testNginxZone02 = new smartnginx.NginxHost({
|
|
||||||
hostName: "test102.bleu.de",
|
|
||||||
type: smartnginx.hostTypes.reverseProxy,
|
|
||||||
destination: "192.192.192.192"
|
|
||||||
});
|
|
||||||
testNginxZone01.should.be.instanceof(smartnginx.NginxHost);
|
|
||||||
console.log(testNginxZone01.configString);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("NginxConfig", function () {
|
|
||||||
this.timeout(10000);
|
|
||||||
it(`"new NginxConfig()" should produce an instance of NginxConfig`, function () {
|
|
||||||
testNginxConfig = new smartnginx.NginxConfig({
|
|
||||||
cfEmail: process.env.CF_EMAIL,
|
|
||||||
cfKey: process.env.CF_KEY,
|
|
||||||
testMode: true
|
|
||||||
});
|
|
||||||
testNginxConfig.should.be.instanceof(smartnginx.NginxConfig);
|
|
||||||
});
|
|
||||||
describe(".addZone()", function () {
|
|
||||||
it("should add a zone to NginxConfig Object", function () {
|
|
||||||
testNginxConfig.addHost(testNginxZone01);
|
|
||||||
testNginxConfig.addHost(testNginxZone02);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe(".deploy()", function () {
|
|
||||||
this.timeout(600000);
|
|
||||||
it("should deploy a config from an instance", function (done) {
|
|
||||||
testNginxConfig.deploy()
|
|
||||||
.then(() => {
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe(".stop", function () {
|
|
||||||
it("should end the process", function () {
|
|
||||||
testNginxConfig.nginxProcess.stop();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxJQUFJLFdBQVcsTUFBTSxDQUFDLENBQUM7QUFDOUI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UFDTixDQUFDLENBQUMsQ0FBQztRQUNILFFBQVEsQ0FBQyxPQUFPLEVBQUM7WUFDYixFQUFFLENBQUMsd0JBQXdCLEVBQUM7Z0JBQ3hCLGVBQWUsQ0FBQyxZQUFZLENBQUMsSUFBSSxFQUFFLENBQUM7WUFDeEMsQ0FBQyxDQUFDLENBQUE7UUFDTixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFDO0FBQ1AsQ0FBQyxDQUFDLENBQUMifQ==
|
|
85
test/test.ts
85
test/test.ts
@ -1,60 +1,45 @@
|
|||||||
import "typings-test";
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import path = require("path");
|
import path = require('path');
|
||||||
import "should";
|
|
||||||
import {Qenv} from "qenv";
|
|
||||||
import * as smartnginx from "../dist/index";
|
|
||||||
|
|
||||||
// setup environment
|
import { Qenv } from 'qenv';
|
||||||
let testQenv = new Qenv(process.cwd(),path.join(process.cwd(),".nogit"));
|
let testQenv = new Qenv(process.cwd(), path.join(process.cwd(), '.nogit'));
|
||||||
describe("smartnginx",function(){
|
|
||||||
let testNginxConfig:smartnginx.NginxConfig;
|
|
||||||
|
import * as smartnginx from '../ts';
|
||||||
|
|
||||||
|
let testSmartNginx: smartnginx.SmartNginx;
|
||||||
let testNginxZone01: smartnginx.NginxHost;
|
let testNginxZone01: smartnginx.NginxHost;
|
||||||
let testNginxZone02: smartnginx.NginxHost;
|
let testNginxZone02: smartnginx.NginxHost;
|
||||||
describe("NginxZone",function(){
|
|
||||||
it(`"new NginxZone()" should produce an instance of NginxConfig`,function(){
|
tap.test('should create a valid instance of SmartNginx', async () => {
|
||||||
testNginxZone01 = new smartnginx.NginxHost({
|
testSmartNginx = new smartnginx.SmartNginx();
|
||||||
hostName:"test100.bleu.de",
|
expect(testSmartNginx).to.be.instanceof(smartnginx.SmartNginx);
|
||||||
type:smartnginx.hostTypes.reverseProxy,
|
|
||||||
destination:"192.192.192.191"
|
|
||||||
});
|
});
|
||||||
testNginxZone02 = new smartnginx.NginxHost({
|
|
||||||
hostName:"test102.bleu.de",
|
tap.test(`should produce an instance of NginxConfig`, async () => {
|
||||||
type:smartnginx.hostTypes.reverseProxy,
|
testNginxZone01 = new smartnginx.NginxHost(testSmartNginx, {
|
||||||
destination:"192.192.192.192"
|
hostName: 'test100.bleu.de',
|
||||||
|
destination: '192.192.192.191'
|
||||||
});
|
});
|
||||||
testNginxZone01.should.be.instanceof(smartnginx.NginxHost);
|
testNginxZone02 = new smartnginx.NginxHost(testSmartNginx, {
|
||||||
|
hostName: 'test102.bleu.de',
|
||||||
|
destination: '192.192.192.192'
|
||||||
|
});
|
||||||
|
expect(testNginxZone01).to.be.instanceof(smartnginx.NginxHost);
|
||||||
console.log(testNginxZone01.configString);
|
console.log(testNginxZone01.configString);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('.addZone() should add a zone to NginxConfig Object', async () => {
|
||||||
|
testSmartNginx.addHost(testNginxZone01);
|
||||||
|
testSmartNginx.addHost(testNginxZone02);
|
||||||
});
|
});
|
||||||
describe("NginxConfig",function(){
|
|
||||||
this.timeout(10000);
|
tap.test('.deploy() should deploy a config from an instance', async () => {
|
||||||
it(`"new NginxConfig()" should produce an instance of NginxConfig`,function(){
|
await testSmartNginx.deploy();
|
||||||
testNginxConfig = new smartnginx.NginxConfig({
|
|
||||||
cfEmail: process.env.CF_EMAIL,
|
|
||||||
cfKey: process.env.CF_KEY,
|
|
||||||
testMode:true
|
|
||||||
});
|
|
||||||
testNginxConfig.should.be.instanceof(smartnginx.NginxConfig);
|
|
||||||
});
|
|
||||||
describe(".addZone()",function(){
|
|
||||||
it("should add a zone to NginxConfig Object",function(){
|
|
||||||
testNginxConfig.addHost(testNginxZone01);
|
|
||||||
testNginxConfig.addHost(testNginxZone02);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
describe(".deploy()",function(){
|
|
||||||
this.timeout(600000);
|
|
||||||
it("should deploy a config from an instance",function(done){
|
|
||||||
testNginxConfig.deploy()
|
|
||||||
.then(() => {
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
});
|
|
||||||
describe(".stop",function(){
|
|
||||||
it("should end the process",function(){
|
|
||||||
testNginxConfig.nginxProcess.stop();
|
|
||||||
})
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('.stop() should end the process', async () => {
|
||||||
|
testSmartNginx.nginxProcess.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.start();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import * as plugins from "./smartnginx.plugins";
|
import * as plugins from './smartnginx.plugins';
|
||||||
|
|
||||||
// classes
|
// classes
|
||||||
export * from "./smartnginx.classes.nginxconfig";
|
export * from './smartnginx.classes.smartnginx';
|
||||||
export * from "./smartnginx.classes.nginxprocess";
|
export * from './smartnginx.classes.nginxprocess';
|
||||||
export * from "./smartnginx.classes.nginxhost";
|
export * from './smartnginx.classes.nginxhost';
|
||||||
|
5
ts/smartnginx.classes.certhandler.ts
Normal file
5
ts/smartnginx.classes.certhandler.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import * as plugins from './smartnginx.plugins';
|
||||||
|
|
||||||
|
export class CertHandler {
|
||||||
|
|
||||||
|
}
|
@ -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;
|
|
||||||
};
|
|
||||||
};
|
|
@ -1,43 +1,47 @@
|
|||||||
import * as plugins from "./smartnginx.plugins";
|
import * as plugins from './smartnginx.plugins';
|
||||||
import * as paths from "./smartnginx.paths";
|
import * as paths from './smartnginx.paths';
|
||||||
import * as snippets from "./smartnginx.snippets"
|
import * as snippets from './smartnginx.snippets';
|
||||||
|
|
||||||
|
import { SmartNginx } from './smartnginx.classes.smartnginx';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the host config data that NginxHost needs to create a valid instance
|
* the host config data that NginxHost needs to create a valid instance
|
||||||
*/
|
*/
|
||||||
export interface IHostConfigData {
|
export interface IHostConfigData {
|
||||||
hostName: string,
|
hostName: string;
|
||||||
type: hostTypes,
|
destination: string;
|
||||||
destination: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum hostTypes {
|
export enum hostTypes {
|
||||||
reverseProxy,
|
reverseProxy
|
||||||
static
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* manages a single nginx host
|
* manages a single nginx host
|
||||||
*/
|
*/
|
||||||
export class NginxHost {
|
export class NginxHost {
|
||||||
|
/**
|
||||||
|
* smartnginxInstance this NginHost belongs to
|
||||||
|
*/
|
||||||
|
smartnginxInstance: SmartNginx
|
||||||
|
|
||||||
hostName: string; // the host name e.g. domain name
|
hostName: string; // the host name e.g. domain name
|
||||||
type: hostTypes;
|
|
||||||
destination: string;
|
destination: string;
|
||||||
configString: string; // the actual host config file as string
|
configString: string; // the actual host config file as string
|
||||||
constructor(optionsArg:IHostConfigData) {
|
constructor(smartnginxInstanceArg: SmartNginx, optionsArg: IHostConfigData) {
|
||||||
|
this.smartnginxInstance = smartnginxInstanceArg;
|
||||||
this.hostName = optionsArg.hostName;
|
this.hostName = optionsArg.hostName;
|
||||||
this.type = optionsArg.type;
|
|
||||||
this.destination = optionsArg.destination;
|
this.destination = optionsArg.destination;
|
||||||
this.configString = snippets.getHostConfigString(optionsArg.hostName, 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");
|
*
|
||||||
|
* @param certInstanceArg
|
||||||
|
*/
|
||||||
|
async deploy() {
|
||||||
|
let filePath = plugins.path.join(paths.nginxHostFileBase, this.hostName + '.conf');
|
||||||
// writeConfig
|
// writeConfig
|
||||||
plugins.smartfile.memory.toFsSync(this.configString, filePath);
|
plugins.smartfile.memory.toFsSync(this.configString, filePath);
|
||||||
// get cert
|
}
|
||||||
certInstanceArg.getDomainCert(this.hostName)
|
}
|
||||||
.then(done.resolve);
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
@ -1,74 +1,82 @@
|
|||||||
import * as plugins from "./smartnginx.plugins";
|
import * as plugins from './smartnginx.plugins';
|
||||||
import * as paths from "./smartnginx.paths";
|
import * as paths from './smartnginx.paths';
|
||||||
import { NginxConfig } from "./smartnginx.classes.nginxconfig";
|
import { SmartNginx } from './smartnginx.classes.smartnginx';
|
||||||
import { NginxHost } from "./smartnginx.classes.nginxhost";
|
import { NginxHost } from './smartnginx.classes.nginxhost';
|
||||||
|
|
||||||
|
import { Smartshell } from '@pushrocks/smartshell';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* manages a nginxprocess for an NginxConfig
|
* manages a nginxprocess for an NginxConfig
|
||||||
*/
|
*/
|
||||||
export class NginxProcess {
|
export class NginxProcess {
|
||||||
started: boolean = false;
|
started: boolean = false;
|
||||||
nginxConfig:NginxConfig;
|
nginxConfig: SmartNginx;
|
||||||
nginxChildProcess: plugins.childProcess.ChildProcess;
|
nginxChildProcess: plugins.childProcess.ChildProcess;
|
||||||
|
smartshellInstance = new Smartshell({
|
||||||
|
executor: 'bash'
|
||||||
|
});
|
||||||
constructor(nginxConfigArg) {
|
constructor(nginxConfigArg) {
|
||||||
this.nginxConfig = nginxConfigArg;
|
this.nginxConfig = nginxConfigArg;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start nginx
|
* start nginx
|
||||||
*/
|
*/
|
||||||
start() {
|
start() {
|
||||||
let done = plugins.q.defer();
|
let done = plugins.smartpromise.defer();
|
||||||
if (typeof this.nginxChildProcess == "undefined"){
|
if (typeof this.nginxChildProcess == 'undefined') {
|
||||||
this.nginxChildProcess = plugins.childProcess.exec(`nginx -c ${paths.nginxConfFile}`, function (error, stdout, stderr) {
|
this.nginxChildProcess = plugins.childProcess.exec(
|
||||||
|
`nginx -c ${paths.nginxConfFile}`,
|
||||||
|
function(error, stdout, stderr) {
|
||||||
console.log(`stdout: ${stdout}`);
|
console.log(`stdout: ${stdout}`);
|
||||||
console.log(`stderr: ${stderr}`);
|
console.log(`stderr: ${stderr}`);
|
||||||
if (error !== null) {
|
if (error !== null) {
|
||||||
console.log(`exec error: ${error}`);
|
console.log(`exec error: ${error}`);
|
||||||
};
|
}
|
||||||
});
|
}
|
||||||
};
|
);
|
||||||
|
}
|
||||||
this.started = true;
|
this.started = true;
|
||||||
plugins.beautylog.info("started Nginx!");
|
plugins.smartlog.defaultLogger.info('started Nginx!');
|
||||||
done.resolve();
|
done.resolve();
|
||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reload config
|
* reload config
|
||||||
*/
|
*/
|
||||||
reloadConfig() {
|
reloadConfig() {
|
||||||
let done = plugins.q.defer();
|
let done = plugins.smartpromise.defer();
|
||||||
if (this.started == false) {
|
if (this.started == false) {
|
||||||
this.start();
|
this.start();
|
||||||
} else {
|
} else {
|
||||||
plugins.shelljs.exec("nginx -s reload");
|
this.smartshellInstance.exec('nginx -s reload');
|
||||||
};
|
}
|
||||||
plugins.beautylog.ok("NginxProcess has loaded the new config!")
|
plugins.smartlog.defaultLogger.info('NginxProcess has loaded the new config!');
|
||||||
done.resolve();
|
done.resolve();
|
||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stop the nginx instance
|
* stop the nginx instance
|
||||||
*/
|
*/
|
||||||
stop() {
|
stop() {
|
||||||
let done = plugins.q.defer();
|
let done = plugins.smartpromise.defer();
|
||||||
if (typeof this.nginxChildProcess != "undefined") {
|
if (typeof this.nginxChildProcess != 'undefined') {
|
||||||
plugins.shelljs.exec("nginx -s quit");
|
this.smartshellInstance.exec('nginx -s quit');
|
||||||
this.started = false;
|
this.started = false;
|
||||||
plugins.beautylog.info("stopped Nginx!");
|
plugins.smartlog.defaultLogger.info('stopped Nginx!');
|
||||||
} else {
|
} else {
|
||||||
plugins.beautylog.log("nginx already stopped!");
|
plugins.smartlog.defaultLogger.info('nginx already stopped!');
|
||||||
};
|
}
|
||||||
done.resolve();
|
done.resolve();
|
||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks if nginx is in path
|
* checks if nginx is in path
|
||||||
*/
|
*/
|
||||||
check(): boolean {
|
check(): boolean {
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
74
ts/smartnginx.classes.smartnginx.ts
Normal file
74
ts/smartnginx.classes.smartnginx.ts
Normal 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();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,11 @@
|
|||||||
import * as plugins from "./smartnginx.plugins"
|
import * as plugins from './smartnginx.plugins';
|
||||||
|
|
||||||
// directories
|
// directories
|
||||||
export let packageBase = plugins.path.join(__dirname,"../");
|
export let packageBase = plugins.path.join(__dirname, '../');
|
||||||
export let nginxConfigBase = plugins.path.join(packageBase,"nginxconfig");
|
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 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 let nginxConfFile = plugins.path.join(nginxConfigBase, 'nginx.conf');
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
import "typings-global";
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
export import beautylog = require("beautylog");
|
import * as childProcess from 'child_process';
|
||||||
export import cert = require("cert");
|
import * as path from 'path';
|
||||||
export import childProcess = require("child_process");
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
export import path = require("path");
|
import * as smartshell from '@pushrocks/smartshell';
|
||||||
export import q = require("q");
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
export import shelljs = require("shelljs");
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
export import smartfile = require("smartfile");
|
|
||||||
export import smartstring = require("smartstring");
|
export {
|
||||||
|
smartlog,
|
||||||
|
childProcess,
|
||||||
|
path,
|
||||||
|
smartpromise,
|
||||||
|
smartshell,
|
||||||
|
smartfile,
|
||||||
|
smartstring
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
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(`
|
let baseConfig = plugins.smartstring.indent.normalize(`
|
||||||
user www-data;
|
user www-data;
|
||||||
@ -69,8 +69,7 @@ export let getBaseConfigString = () => {
|
|||||||
daemon off;
|
daemon off;
|
||||||
`);
|
`);
|
||||||
return baseConfig;
|
return baseConfig;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string) => {
|
export let getHostConfigString = (hostNameArg: string, destinationIpArg: string) => {
|
||||||
let hostConfig = plugins.smartstring.indent.normalize(`
|
let hostConfig = plugins.smartstring.indent.normalize(`
|
||||||
@ -101,4 +100,3 @@ export let getHostConfigString = (hostNameArg:string,destinationIpArg:string) =>
|
|||||||
`);
|
`);
|
||||||
return hostConfig;
|
return hostConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user