consolidate naming and start exporting interfaces

This commit is contained in:
2016-08-02 15:32:06 +02:00
parent d8a4c3d088
commit c3c2ab776d
23 changed files with 221 additions and 185 deletions

View File

@@ -1,6 +1,6 @@
import * as plugins from "./smartnginx.plugins";
// classes
export {NginxConfig} from "./smartnginx.classes.nginxconfig";
export {NginxProcess} from "./smartnginx.classes.nginxprocess";
export {NginxZone,zoneTypes} from "./smartnginx.classes.nginxzone";
export * from "./smartnginx.classes.nginxconfig";
export * from "./smartnginx.classes.nginxprocess";
export * from "./smartnginx.classes.nginxhost";

View File

@@ -1,12 +1,15 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as snippets from "./smartnginx.snippets";
import { NginxZone } from "./smartnginx.classes.nginxzone";
import { NginxHost } from "./smartnginx.classes.nginxhost";
import { NginxProcess } from "./smartnginx.classes.nginxprocess";
let allConfigs: NginxConfig[] = [];
/**
* main class that manages a NginxInstance
*/
export class NginxConfig {
zones: NginxZone[] = [];
hosts: NginxHost[] = [];
cert: plugins.cert.Cert; // the Cert Instance from which the config gets its certificates
nginxProcess: NginxProcess = new NginxProcess(this);
isDeployed: boolean = false;
@@ -20,24 +23,24 @@ export class NginxConfig {
});
};
// interact with Zones
addZone(zoneArg: NginxZone){
this.zones.push(zoneArg);
// interact with Hosts
addHost(nginxHostArg: NginxHost){
this.hosts.push(nginxHostArg);
}
listZones(): NginxZone[]{
return this.zones;
listHosts(): NginxHost[]{
return this.hosts;
};
removeZones(zoneArg: NginxZone) {
removeHost(nginxHostArg: NginxHost) {
}
clean() {
this.zones = [];
this.hosts = [];
}
// handle deployment of zones
// handle deployment of hosts
deploy() {
let done = plugins.q.defer();
plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxZoneBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxHostFileBase);
plugins.smartfile.fs.ensureDirSync(paths.nginxCertBase);
for (let config of allConfigs) {
config.isDeployed = false;
@@ -48,15 +51,15 @@ export class NginxConfig {
snippets.getBaseConfigString(),
paths.nginxConfFile
);
// deploy zones
// deploy hosts
let promiseArray = [];
for (let zone of this.zones) {
let zoneDeployedPromise = zone.deploy(this.cert);
zoneDeployedPromise.then(() => {
plugins.beautylog.info(`Zone ${zone.zoneName} deployed!`);
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(zoneDeployedPromise);
promiseArray.push(hostDeployedPromise);
};
plugins.q.all(promiseArray)
.then(() => {

View File

@@ -0,0 +1,43 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as snippets from "./smartnginx.snippets"
/**
* the host config data that NginxHost needs to create a valid instance
*/
export interface hostConfigData {
hostName: string,
type: hostTypes,
destination: string
}
export enum hostTypes {
reverseProxy,
static
}
/**
* manages a single nginx host
*/
export class NginxHost {
hostName: string; // the host name e.g. domain name
type: hostTypes;
destination: string;
configString: string; // the actual host config file as string
constructor(optionsArg:hostConfigData) {
this.hostName = optionsArg.hostName;
this.type = optionsArg.type;
this.destination = 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");
// writeConfig
plugins.smartfile.memory.toFsSync(this.configString, filePath);
// get cert
certInstanceArg.getDomainCert(this.hostName)
.then(done.resolve);
return done.promise;
};
};

View File

@@ -1,8 +1,11 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import { NginxConfig } from "./smartnginx.classes.nginxconfig";
import { NginxZone } from "./smartnginx.classes.nginxzone";
import { NginxHost } from "./smartnginx.classes.nginxhost";
/**
* manages a nginxprocess for an NginxConfig
*/
export class NginxProcess {
started: boolean = false;
nginxConfig:NginxConfig;
@@ -37,7 +40,7 @@ export class NginxProcess {
reloadConfig(){
let done = plugins.q.defer();
if(this.started == false){
this.start()
this.start();
} else {
plugins.shelljs.exec("nginx -s reload");
};

View File

@@ -1,34 +0,0 @@
import * as plugins from "./smartnginx.plugins";
import * as paths from "./smartnginx.paths";
import * as snippets from "./smartnginx.snippets"
export enum zoneTypes {
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
}) {
this.zoneName = optionsArg.zoneName;
this.type = optionsArg.type;
this.destination = optionsArg.destination;
this.configString = snippets.getZoneConfigString(optionsArg.zoneName, optionsArg.destination);
};
deploy(certInstanceArg: plugins.cert.Cert) {
let done = plugins.q.defer();
let filePath = plugins.path.join(paths.nginxZoneBase, this.zoneName + ".conf");
// writeConfig
plugins.smartfile.memory.toFsSync(this.configString, filePath);
// get cert
certInstanceArg.getDomainCert(this.zoneName)
.then(done.resolve);
return done.promise;
};
};

View File

@@ -3,7 +3,7 @@ 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 nginxHostFileBase = plugins.path.join(nginxConfigBase,"hosts");
export let nginxCertBase = plugins.path.join(nginxConfigBase,"cert");
// files

View File

@@ -71,26 +71,26 @@ export let getBaseConfigString = () => {
}
export let getZoneConfigString = (zoneNameArg:string,destinationIpArg:string) => {
let zoneConfig = plugins.smartstring.indent.normalize(`
upstream ${zoneNameArg} {
export let getHostConfigString = (hostNameArg:string,destinationIpArg:string) => {
let hostConfig = plugins.smartstring.indent.normalize(`
upstream ${hostNameArg} {
server ${destinationIpArg};
}
server {
listen *:80 ;
server_name ${zoneNameArg};
rewrite ^ https://${zoneNameArg}$request_uri? permanent;
server_name ${hostNameArg};
rewrite ^ https://${hostNameArg}$request_uri? permanent;
}
server {
listen *:443 ssl;
server_name ${zoneNameArg};
ssl_certificate /LE_CERTS/${zoneNameArg}/fullchain.pem;
ssl_certificate_key /LE_CERTS/${zoneNameArg}/privkey.pem;
server_name ${hostNameArg};
ssl_certificate /LE_CERTS/${hostNameArg}/fullchain.pem;
ssl_certificate_key /LE_CERTS/${hostNameArg}/privkey.pem;
location / {
proxy_pass http://${zoneNameArg};
proxy_pass http://${hostNameArg};
include /etc/nginx/proxy_params;
}
location ~ /\.git {
@@ -98,6 +98,6 @@ export let getZoneConfigString = (zoneNameArg:string,destinationIpArg:string) =>
}
}
`);
return zoneConfig;
return hostConfig;
};