Commit 5fed9a8c authored by nanahira's avatar nanahira

add gateway group

parent 748e53ae
......@@ -32,11 +32,13 @@ protocol kernel {
{% for plan in routePlans %}
ipv4 table {{plan.name}};
{% if plan.addressesString %}
protocol pipe {
table master4;
peer table {{plan.name}};
export where ospf_router_id ~ {{plan.addressesString}};
}
{% endif %}
protocol kernel {
ipv4 {
table {{plan.name}};
......
......@@ -56,13 +56,13 @@
vars:
conn: '{{item}}'
with_items: '{{ connections }}'
when: "item.protocol == 'null'"
when: "not noUpdateLinks and item.protocol == 'null'"
- name: 'loop through list from a variable'
include_tasks: 'protocols/{{item.protocol}}/configure.yaml'
vars:
conn: '{{item}}'
with_items: '{{ connections }}'
when: "item.protocol != 'null'"
when: "not noUpdateLinks and item.protocol != 'null'"
# end
- name: services conf
copy:
......
......@@ -3,15 +3,26 @@ import util from 'util';
import fs from 'fs';
import path from 'path';
import YAML from 'yaml';
import _ from 'lodash';
import _, { add } from 'lodash';
import child_process from 'child_process';
import assert from 'assert';
import ip from "ip";
import { promises as dns } from "dns";
interface GatewayGroup {
id: number;
name: string;
locationPrefix: string;
includeRouters: string;
excludeRouters: string;
children: string;
destMark: number;
}
class InventoryBuilder {
hosts: { [key: string]: any };
gateways: any;
gatewayGroups: GatewayGroup[];
connections: string[];
routeLists: any;
resolveCache: Map<string, string>;
......@@ -45,7 +56,7 @@ class InventoryBuilder {
constructor() {
this.resolveCache = new Map();
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) {
......@@ -71,6 +82,7 @@ class InventoryBuilder {
async main() {
this.hosts = _.keyBy(await this.load('nextgen2'), 'name');
this.gateways = _.mapValues(_.groupBy(await this.loadGateways(), 'router'), g => _.keyBy(g, 'isp'));
this.gatewayGroups = await this.load('gateway groups');
//console.log(this.gateways);
this.connections = _.intersection(Object.keys(this.hosts), Object.keys(_.find(this.hosts)));
......@@ -111,6 +123,7 @@ class InventoryBuilder {
const vars = {
routeLists: this.routeLists,
routeListNames: Object.keys(this.routeLists),
noUpdateLinks: !!process.env.NO_LINKS
};
for (let col in raw_utility) {
vars[col] = raw_utility[col].value;
......@@ -119,8 +132,45 @@ class InventoryBuilder {
return vars;
}
getRoutePlanAddressesString(addresses: string[]) {
if (!addresses.length) {
return null;
}
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) {
const connections = [];
host.dockerServices = {
......@@ -138,7 +188,7 @@ class InventoryBuilder {
host.frpsNeeded = false;
const null_connection = "10000,null";
const lanInterfaces = host.lanInterfaces.length > 0 ? host.lanInterfaces.split(",") : [];
const routePlans = [];
const routePlans = this.getRoutePlansFromGatewayGroups(host);
for (const h of this.connections) {
if (h != host.name) {
const to = host[h]; // 当前主机的条目
......@@ -161,11 +211,13 @@ class InventoryBuilder {
routePlans.push({
name: h.replace(/-/g, "_"),
destMark: targetHost.destMark,
addresses: targetHost.address,
addresses: [targetHost.address],
addressesString: this.getRoutePlanAddressesString([targetHost.address])
});
}
}
return {
ansible_ssh_host: host.host,
......
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