now working as intended

This commit is contained in:
2016-07-25 01:54:36 +02:00
parent effe9210fc
commit ca2ff3481e
21 changed files with 258 additions and 207 deletions

View File

@ -1,9 +1,6 @@
import * as plugins from "./smartnginx.plugins";
import * as CommandModule from "./smartnginx.command";
// classes
export {NginxConfig} from "./smartnginx.classes.nginxconfig";
export {NginxZone,zoneTypes} from "./smartnginx.classes.nginxzone";
// exports
export let command = CommandModule;
export {NginxProcess} from "./smartnginx.classes.nginxprocess";
export {NginxZone,zoneTypes} from "./smartnginx.classes.nginxzone";

View File

@ -1,67 +1,68 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as command from "./smartnginx.command";
import * as snippets from "./smartnginx.snippets"
import {NginxZone} from "./smartnginx.classes.nginxzone";
let allConfigs:NginxConfig[] = [];
import * as snippets from "./smartnginx.snippets";
import { NginxZone } from "./smartnginx.classes.nginxzone";
import { NginxProcess } from "./smartnginx.classes.nginxprocess";
let allConfigs: NginxConfig[] = [];
export class NginxConfig {
zones:NginxZone[] = [];
cert:plugins.cert.Cert; // the Cert Instance from which the config gets its certificates
isDeployed:boolean = false;
constructor(optionsArg:plugins.cert.ICertConstructorOptions){
zones: NginxZone[] = [];
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
cfEmail: optionsArg.cfEmail,
cfKey: optionsArg.cfKey,
sslDir: paths.nginxCertBase,
gitOriginRepo: optionsArg.gitOriginRepo,
testMode: optionsArg.testMode
});
};
// interact with Zones
addZone(zoneArg:NginxZone){
addZone(zoneArg: NginxZone){
this.zones.push(zoneArg);
}
listZones():NginxZone[] {
listZones(): NginxZone[]{
return this.zones;
};
removeZones(zoneArg:NginxZone){
removeZones(zoneArg: NginxZone) {
}
clean(){
clean() {
this.zones = [];
}
// handle deployment of zones
deploy(nginxRestartArg:boolean = false){
deploy() {
let done = plugins.q.defer();
plugins.smartfile.fs.removeSync(paths.nginxConfigBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxZoneBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxCertBase);
for(let config of allConfigs){
for (let config of allConfigs) {
config.isDeployed = false;
};
this.isDeployed = true;
// write base config
plugins.smartfile.memory.toFsSync(
snippets.getBaseConfigString(),
plugins.path.join(paths.nginxConfigBase,"nginx.conf")
paths.nginxConfFile
);
// deploy zones
let promiseArray = [];
for(let zone of this.zones){
promiseArray.push(zone.deploy(this.cert));
for (let zone of this.zones) {
let zoneDeployedPromise = zone.deploy(this.cert);
zoneDeployedPromise.then(() => {
plugins.beautylog.info(`Zone ${zone.zoneName} deployed!`);
this.nginxProcess.reloadConfig();
});
promiseArray.push(zoneDeployedPromise);
};
plugins.q.all(promiseArray)
.then(() => {
// restart nginx
if(nginxRestartArg){
command.restart(this);
};
done.resolve();
});
return done.promise;
};
};

View File

@ -0,0 +1,70 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import { NginxConfig } from "./smartnginx.classes.nginxconfig";
import { NginxZone } from "./smartnginx.classes.nginxzone";
export class NginxProcess {
started: boolean = false;
nginxConfig:NginxConfig;
nginxChildProcess: plugins.childProcess.ChildProcess;
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;
};
/**
* reload config
*/
reloadConfig(){
let done = plugins.q.defer();
if(this.started == false){
this.start()
} else {
this.nginxChildProcess.kill("SIGHUP");
};
plugins.beautylog.ok("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") {
this.nginxChildProcess.kill();
plugins.beautylog.info("stopped Nginx!");
} else {
plugins.beautylog.log("nginx already stopped!");
};
done.resolve();
return done.promise;
};
/**
* checks if nginx is in path
*/
check(): boolean {
return;
};
}

View File

@ -1,32 +1,31 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as command from "./smartnginx.command";
import * as snippets from "./smartnginx.snippets"
export enum zoneTypes {
reverseProxy,
static
reverseProxy,
static
}
export class NginxZone {
zoneName:string; // the zone name e.g. domain name
type:zoneTypes;
destination:string;
configString:string; // the actual zone config file as string
constructor(optionsArg:{
zoneName:string,
type:zoneTypes,
destination:string
}){
zoneName: string; // the zone name e.g. domain name
type: zoneTypes;
destination: string;
configString: string; // the actual zone config file as string
constructor(optionsArg: {
zoneName: string,
type: zoneTypes,
destination: string
}) {
this.zoneName = optionsArg.zoneName;
this.type = optionsArg.type;
this.destination = optionsArg.destination;
this.configString = snippets.getZoneConfigString(optionsArg.zoneName,optionsArg.destination);
this.configString = snippets.getZoneConfigString(optionsArg.zoneName, optionsArg.destination);
};
deploy(certInstanceArg:plugins.cert.Cert){
deploy(certInstanceArg: plugins.cert.Cert) {
let done = plugins.q.defer();
let filePath = plugins.path.join(paths.nginxZoneBase,this.zoneName + ".conf");
let filePath = plugins.path.join(paths.nginxZoneBase, this.zoneName + ".conf");
// writeConfig
plugins.smartfile.memory.toFsSync(this.configString,filePath);
plugins.smartfile.memory.toFsSync(this.configString, filePath);
// get cert
certInstanceArg.getDomainCert(this.zoneName)
.then(done.resolve);

View File

@ -1,59 +0,0 @@
import * as plugins from "./smartnginx.plugins";
import {NginxConfig} from "./smartnginx.classes.nginxconfig";
import {NginxZone} from "./smartnginx.classes.nginxzone";
let nginxChildProcess = undefined; // points to the nginx child process
/**
* starts nginx
*/
export let start = (configArg:NginxConfig) => {
let done = plugins.q.defer();
if(typeof nginxChildProcess == "undefined"){
nginxChildProcess = plugins.childProcess.exec("nginx",function(error, stdout, stderr){
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
};
plugins.beautylog.info("started Nginx!");
done.resolve();
return done.promise;
};
/**
* restarts nginx
*/
export let restart = (configArg:NginxConfig) => {
return stop().then(
() => {
return start(configArg);
}
);
}
/**
* stops nginx
*/
export let stop = () => {
let done = plugins.q.defer();
if(typeof nginxChildProcess != "undefined"){
nginxChildProcess.kill();
plugins.beautylog.info("stopped Nginx!");
} else {
plugins.beautylog.log("nginx already stopped!");
};
done.resolve();
return done.promise;
};
/**
* checks if nginx is in path
*/
export let check = ():boolean => {
return;
};

View File

@ -1,6 +1,10 @@
import * as plugins from "./smartnginx.plugins"
// directories
export let packageBase = plugins.path.join(__dirname,"../");
export let nginxConfigBase = plugins.path.join(packageBase,"nginxconfig");
export let nginxZoneBase = plugins.path.join(nginxConfigBase,"zones");
export let nginxCertBase = plugins.path.join(nginxConfigBase,"cert");
export let nginxCertBase = plugins.path.join(nginxConfigBase,"cert");
// files
export let nginxConfFile = plugins.path.join(nginxConfigBase,"nginx.conf");