Commit 5fed9a8c authored by nanahira's avatar nanahira

add gateway group

parent 748e53ae
...@@ -32,11 +32,13 @@ protocol kernel { ...@@ -32,11 +32,13 @@ protocol kernel {
{% for plan in routePlans %} {% for plan in routePlans %}
ipv4 table {{plan.name}}; ipv4 table {{plan.name}};
{% if plan.addressesString %}
protocol pipe { protocol pipe {
table master4; table master4;
peer table {{plan.name}}; peer table {{plan.name}};
export where ospf_router_id ~ {{plan.addressesString}}; export where ospf_router_id ~ {{plan.addressesString}};
} }
{% endif %}
protocol kernel { protocol kernel {
ipv4 { ipv4 {
table {{plan.name}}; table {{plan.name}};
......
...@@ -56,13 +56,13 @@ ...@@ -56,13 +56,13 @@
vars: vars:
conn: '{{item}}' conn: '{{item}}'
with_items: '{{ connections }}' with_items: '{{ connections }}'
when: "item.protocol == 'null'" when: "not noUpdateLinks and item.protocol == 'null'"
- name: 'loop through list from a variable' - name: 'loop through list from a variable'
include_tasks: 'protocols/{{item.protocol}}/configure.yaml' include_tasks: 'protocols/{{item.protocol}}/configure.yaml'
vars: vars:
conn: '{{item}}' conn: '{{item}}'
with_items: '{{ connections }}' with_items: '{{ connections }}'
when: "item.protocol != 'null'" when: "not noUpdateLinks and item.protocol != 'null'"
# end # end
- name: services conf - name: services conf
copy: copy:
......
...@@ -3,15 +3,26 @@ import util from 'util'; ...@@ -3,15 +3,26 @@ import util from 'util';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import YAML from 'yaml'; import YAML from 'yaml';
import _ from 'lodash'; import _, { add } from 'lodash';
import child_process from 'child_process'; import child_process from 'child_process';
import assert from 'assert'; import assert from 'assert';
import ip from "ip"; import ip from "ip";
import { promises as dns } from "dns"; import { promises as dns } from "dns";
interface GatewayGroup {
id: number;
name: string;
locationPrefix: string;
includeRouters: string;
excludeRouters: string;
children: string;
destMark: number;
}
class InventoryBuilder { class InventoryBuilder {
hosts: { [key: string]: any }; hosts: { [key: string]: any };
gateways: any; gateways: any;
gatewayGroups: GatewayGroup[];
connections: string[]; connections: string[];
routeLists: any; routeLists: any;
resolveCache: Map<string, string>; resolveCache: Map<string, string>;
...@@ -45,7 +56,7 @@ class InventoryBuilder { ...@@ -45,7 +56,7 @@ class InventoryBuilder {
constructor() { constructor() {
this.resolveCache = new Map(); this.resolveCache = new Map();
this.resolver = new dns.Resolver(); this.resolver = new dns.Resolver();
this.resolver.setServers(['114.114.114.114', '223.5.5.5']); this.resolver.setServers(process.env.DNS ? [process.env.DNS] : ['114.114.114.114', '223.5.5.5']);
} }
async load(sheetName) { async load(sheetName) {
...@@ -71,6 +82,7 @@ class InventoryBuilder { ...@@ -71,6 +82,7 @@ class InventoryBuilder {
async main() { async main() {
this.hosts = _.keyBy(await this.load('nextgen2'), 'name'); this.hosts = _.keyBy(await this.load('nextgen2'), 'name');
this.gateways = _.mapValues(_.groupBy(await this.loadGateways(), 'router'), g => _.keyBy(g, 'isp')); this.gateways = _.mapValues(_.groupBy(await this.loadGateways(), 'router'), g => _.keyBy(g, 'isp'));
this.gatewayGroups = await this.load('gateway groups');
//console.log(this.gateways); //console.log(this.gateways);
this.connections = _.intersection(Object.keys(this.hosts), Object.keys(_.find(this.hosts))); this.connections = _.intersection(Object.keys(this.hosts), Object.keys(_.find(this.hosts)));
...@@ -111,6 +123,7 @@ class InventoryBuilder { ...@@ -111,6 +123,7 @@ class InventoryBuilder {
const vars = { const vars = {
routeLists: this.routeLists, routeLists: this.routeLists,
routeListNames: Object.keys(this.routeLists), routeListNames: Object.keys(this.routeLists),
noUpdateLinks: !!process.env.NO_LINKS
}; };
for (let col in raw_utility) { for (let col in raw_utility) {
vars[col] = raw_utility[col].value; vars[col] = raw_utility[col].value;
...@@ -119,8 +132,45 @@ class InventoryBuilder { ...@@ -119,8 +132,45 @@ class InventoryBuilder {
return vars; return vars;
} }
getRoutePlanAddressesString(addresses: string[]) { getRoutePlanAddressesString(addresses: string[]) {
if (!addresses.length) {
return null;
}
return `[ ${addresses.join(", ")} ]`; return `[ ${addresses.join(", ")} ]`;
} }
getAddressesFromGatewayGroup(gatewayGroup: GatewayGroup, hosts: any[]) {
const locationPrefixes = gatewayGroup.locationPrefix.split(",");
const excludeRouters = gatewayGroup.excludeRouters.split(",");
const includeRouters = gatewayGroup.includeRouters.split(",");
const children = gatewayGroup.children.split(",");
const suitableHosts = hosts.filter(host => {
if (excludeRouters.includes(host.name)) {
return false;
}
return locationPrefixes.some(prefix => prefix !== "" && (host.location as string).startsWith(prefix)) || includeRouters.includes(host.name);
});
let addresses = suitableHosts.map(host => host.address);
for (let childName of children) {
const targetGatewayGroup = this.gatewayGroups.find(g => g.name === childName);
if (!targetGatewayGroup) {
continue;
}
addresses = addresses.concat(this.getAddressesFromGatewayGroup(targetGatewayGroup, hosts));
}
return addresses;
}
getRoutePlansFromGatewayGroups(host: any) {
const allOtherHosts = this.connections.filter(h => h !== host.name).map(h => this.hosts[h]);
const routePlans = this.gatewayGroups.map(group => {
const addresses = this.getAddressesFromGatewayGroup(group, allOtherHosts);
return {
name: group.name.replace(/-/g, "_"),
destMark: group.destMark,
addresses,
addressesString: this.getRoutePlanAddressesString(addresses)
}
});
return routePlans;
}
async host_vars(host) { async host_vars(host) {
const connections = []; const connections = [];
host.dockerServices = { host.dockerServices = {
...@@ -138,7 +188,7 @@ class InventoryBuilder { ...@@ -138,7 +188,7 @@ class InventoryBuilder {
host.frpsNeeded = false; host.frpsNeeded = false;
const null_connection = "10000,null"; const null_connection = "10000,null";
const lanInterfaces = host.lanInterfaces.length > 0 ? host.lanInterfaces.split(",") : []; const lanInterfaces = host.lanInterfaces.length > 0 ? host.lanInterfaces.split(",") : [];
const routePlans = []; const routePlans = this.getRoutePlansFromGatewayGroups(host);
for (const h of this.connections) { for (const h of this.connections) {
if (h != host.name) { if (h != host.name) {
const to = host[h]; // 当前主机的条目 const to = host[h]; // 当前主机的条目
...@@ -161,12 +211,14 @@ class InventoryBuilder { ...@@ -161,12 +211,14 @@ class InventoryBuilder {
routePlans.push({ routePlans.push({
name: h.replace(/-/g, "_"), name: h.replace(/-/g, "_"),
destMark: targetHost.destMark, destMark: targetHost.destMark,
addresses: targetHost.address, addresses: [targetHost.address],
addressesString: this.getRoutePlanAddressesString([targetHost.address]) addressesString: this.getRoutePlanAddressesString([targetHost.address])
}); });
} }
} }
return { return {
ansible_ssh_host: host.host, ansible_ssh_host: host.host,
ansible_ssh_user: host.user, ansible_ssh_user: host.user,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment