Commit c0766b40 authored by nanahira's avatar nanahira

Merge branch 'gwgroup'

parents ce4c9b91 17364bd0
...@@ -9,19 +9,22 @@ protocol device {} ...@@ -9,19 +9,22 @@ protocol device {}
protocol kernel { protocol kernel {
learn; learn;
ipv4 { ipv4 {
import all; import where net = 0.0.0.0/0;
export where net != 0.0.0.0/0;
}; };
#ipv6 { export all; };
} }
{% for plan in routePlans %} {% for plan in routePlans %}
ipv4 table {{plan.name}}; ipv4 table {{plan.name}};
protocol static { {% if plan.addressesString %}
ipv4 { protocol pipe {
table {{plan.name}}; table master4;
}; peer table {{plan.name}};
igp table master4; export where ospf_router_id ~ {{plan.addressesString}};
route 0.0.0.0/0 recursive {{plan.address}};
} }
{% endif %}
protocol kernel { protocol kernel {
ipv4 { ipv4 {
table {{plan.name}}; table {{plan.name}};
......
...@@ -113,18 +113,24 @@ ...@@ -113,18 +113,24 @@
content: '{{dockerServices | to_yaml}}' content: '{{dockerServices | to_yaml}}'
dest: '{{ansible_user_dir}}/nextgen-network/services/docker-compose.yml' dest: '{{ansible_user_dir}}/nextgen-network/services/docker-compose.yml'
when: not noBird when: not noBird
- name: bird conf #- name: bird conf
template: # template:
src: bird.conf.j2 # src: bird.conf.j2
dest: '{{ansible_user_dir}}/nextgen-network/services/bird.conf' # dest: '{{ansible_user_dir}}/nextgen-network/services/bird.conf'
notify: restart_bird # notify: restart_bird
when: not noBird # when: not noBird
- name: babeld conf - name: babeld conf
template: template:
src: babeld.conf.j2 src: babeld.conf.j2
dest: '{{ansible_user_dir}}/nextgen-network/services/babeld.conf' dest: '{{ansible_user_dir}}/nextgen-network/services/babeld.conf'
#notify: restart_babeld #notify: restart_babeld
when: not noBird when: not noBird
- name: monitor route plans
template:
src: route-plans.j2
dest: '{{ansible_user_dir}}/nextgen-network/services/route-plans'
notify: restart_monitor
when: not noBird
- name: babeld reload conf - name: babeld reload conf
template: template:
src: babeld-reload.conf.j2 src: babeld-reload.conf.j2
...@@ -142,56 +148,56 @@ ...@@ -142,56 +148,56 @@
remove_orphans: true remove_orphans: true
# pull: true # pull: true
when: not noBird when: not noBird
- name: systemd bird #- name: systemd bird
become: true # become: true
template: # template:
src: bird.conf.j2 # src: bird.conf.j2
dest: '/etc/bird/bird.conf' # dest: '/etc/bird/bird.conf'
notify: restart_bird_systemd # notify: restart_bird_systemd
when: systemBird # when: systemBird
- name: enable systemd bird #- name: enable systemd bird
become: true # become: true
systemd: # systemd:
name: bird # name: bird
state: started # state: started
enabled: true # enabled: true
masked: false # masked: false
when: systemBird # when: systemBird
- name: systemd babeld conf #- name: systemd babeld conf
become: true # become: true
template: # template:
src: babeld.conf.j2 # src: babeld.conf.j2
dest: '/etc/babeld.conf' # dest: '/etc/babeld.conf'
#notify: restart_babeld_systemd # #notify: restart_babeld_systemd
when: systemBird # when: systemBird
- name: enable systemd babeld #- name: enable systemd babeld
become: true # become: true
systemd: # systemd:
name: babeld # name: babeld
state: started # state: started
enabled: true # enabled: true
masked: false # masked: false
when: systemBird # when: systemBird
handlers: handlers:
- name: reload_switch_rules - name: reload_switch_rules
become: true become: true
shell: '{{ansible_user_dir}}/nextgen-network/scripts/switch-rules-down.sh ; {{ansible_user_dir}}/nextgen-network/scripts/switch-rules-up.sh' shell: '{{ansible_user_dir}}/nextgen-network/scripts/switch-rules-down.sh ; {{ansible_user_dir}}/nextgen-network/scripts/switch-rules-up.sh'
- name: restart_bird - name: restart_babeld # ocserv would be always restarted whenever key changes..
docker_compose: docker_compose:
project_src: '{{ansible_user_dir}}/nextgen-network/services' project_src: '{{ansible_user_dir}}/nextgen-network/services'
restarted: true restarted: true
services: services:
- bird - babeld
when: not noBird when: not noBird
- name: restart_babeld # ocserv would be always restarted whenever key changes.. - name: reload_babeld
shell: cat /tmp/babeld-reload.conf | timeout 1 nc ::1 33123 || true
- name: restart_monitor
docker_compose: docker_compose:
project_src: '{{ansible_user_dir}}/nextgen-network/services' project_src: '{{ansible_user_dir}}/nextgen-network/services'
restarted: true restarted: true
services: services:
- babeld - gateways-monitor
when: not noBird when: not noBird
- name: reload_babeld
shell: cat /tmp/babeld-reload.conf | timeout 1 nc ::1 33123 || true
- name: restart_frps - name: restart_frps
docker_compose: docker_compose:
project_src: '{{ansible_user_dir}}/nextgen-network/services' project_src: '{{ansible_user_dir}}/nextgen-network/services'
...@@ -222,17 +228,17 @@ ...@@ -222,17 +228,17 @@
- openconnect-{{item.name}} - openconnect-{{item.name}}
with_items: '{{connections}}' with_items: '{{connections}}'
when: 'item.protocol == "oc" and item.ocType == "client" and not noBird and not item.noUpdate' when: 'item.protocol == "oc" and item.ocType == "client" and not noBird and not item.noUpdate'
- name: restart_bird_systemd #- name: restart_bird_systemd
become: true # become: true
systemd: # systemd:
name: bird # name: bird
state: restarted # state: restarted
- name: restart_babeld_systemd #- name: restart_babeld_systemd
become: true # become: true
systemd: # systemd:
name: babeld # name: babeld
state: restarted # state: restarted
when: systemBird # when: systemBird
- name: reload_chnroute - name: reload_chnroute
become: true become: true
shell: | shell: |
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
name: '{{item}}' name: '{{item}}'
source: pull source: pull
with_items: with_items:
- fatedier/frps:v0.34.2 #- fatedier/frps:v0.34.2
- fatedier/frpc:v0.34.2 #- fatedier/frpc:v0.34.2
- git-registry.mycard.moe/nanahira/docker-bird - git-registry.mycard.moe/railgun/gateways-monitor
- git-registry.mycard.moe/railgun/babeld - git-registry.mycard.moe/railgun/babeld
- git-registry.mycard.moe/nanahira/docker-ocserv - git-registry.mycard.moe/nanahira/docker-ocserv
- git-registry.mycard.moe/railgun/openconnect - git-registry.mycard.moe/railgun/openconnect
......
declare -A ROUTE_PLANS=(
{% for plan in routePlans %}
[{{plan.destMark}}]="{{plan.addressesString}}"
{% endfor %}
);
#!/usr/bin/env bash #!/usr/bin/env bash
# Forced update 12.24 # Forced update 12.29
source {{ansible_user_dir}}/nextgen-network/scripts/utility.sh source {{ansible_user_dir}}/nextgen-network/scripts/utility.sh
echo "running" > /tmp/mycard_global_postup_done echo "running" > /tmp/mycard_global_postup_done
......
...@@ -3,7 +3,7 @@ import util from 'util'; ...@@ -3,7 +3,7 @@ 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 { promises as dns } from 'dns'; import { promises as dns } from 'dns';
...@@ -17,9 +17,20 @@ async function generateOcpasswdLine(username: string, password: string) { ...@@ -17,9 +17,20 @@ async function generateOcpasswdLine(username: string, password: string) {
return res; return res;
} }
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>;
...@@ -81,6 +92,7 @@ class InventoryBuilder { ...@@ -81,6 +92,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)));
...@@ -140,6 +152,51 @@ class InventoryBuilder { ...@@ -140,6 +152,51 @@ class InventoryBuilder {
return vars; return vars;
} }
getRoutePlanAddressesString(addresses: string[]) {
if (!addresses.length) {
return null;
}
return addresses.join(" ");
}
isGatewayGroupContains(gatewayGroup: GatewayGroup, host: any) {
const locationPrefixes = gatewayGroup.locationPrefix.split(",");
const excludeRouters = gatewayGroup.excludeRouters.split(",");
const includeRouters = gatewayGroup.includeRouters.split(",");
const children = gatewayGroup.children.split(",");
if (excludeRouters.includes(host.name)) {
return false;
}
if (locationPrefixes.some(prefix => prefix !== "" && (host.location as string).startsWith(prefix)) || includeRouters.includes(host.name)) {
return true;
}
for (let childName of children) {
const targetGatewayGroup = this.gatewayGroups.find(g => g.name === childName);
if (!targetGatewayGroup) {
continue;
}
if (this.isGatewayGroupContains(targetGatewayGroup, host)) {
return true;
}
}
return false;
}
getAddressesFromGatewayGroup(gatewayGroup: GatewayGroup, hosts: any[]) {
const suitableHosts = hosts.filter(host => this.isGatewayGroupContains(gatewayGroup, host));
return suitableHosts.map(host => host.address);
}
getRoutePlansFromGatewayGroups(host: any) {
const allOtherHosts = this.connections.filter(h => h !== host.name).map(h => this.hosts[h]);
const routePlans = this.gatewayGroups.filter(group => !this.isGatewayGroupContains(group, host)).map(group => {
const addresses = this.getAddressesFromGatewayGroup(group, allOtherHosts);
return {
name: group.name.replace(/-/g, "_"),
destMark: group.destMark,
addresses,
addressesString: this.getRoutePlanAddressesString(addresses)
}
}).filter(plan => plan.addresses.length > 0);
return routePlans;
}
getHostConnectionInfo(host) { getHostConnectionInfo(host) {
return { return {
...@@ -157,12 +214,13 @@ class InventoryBuilder { ...@@ -157,12 +214,13 @@ class InventoryBuilder {
host.dockerServices = { host.dockerServices = {
version: '2.4', version: '2.4',
services: { services: {
bird: { 'gateways-monitor': {
restart: 'always', restart: 'always',
image: 'git-registry.mycard.moe/nanahira/docker-bird', image: 'git-registry.mycard.moe/railgun/gateways-monitor',
network_mode: 'host', network_mode: 'host',
cap_add: ['NET_ADMIN'], cap_add: ['NET_ADMIN'],
volumes: ['./bird.conf:/etc/bird/bird.conf:ro'] volumes: ['./route-plans:/usr/src/app/route-plans:ro'],
command: '/usr/src/app/gateway-monitor.sh'
}, },
babeld: { babeld: {
restart: 'always', restart: 'always',
...@@ -180,7 +238,7 @@ class InventoryBuilder { ...@@ -180,7 +238,7 @@ class InventoryBuilder {
const localSubnets = host.subnets; const localSubnets = host.subnets;
//console.log(localSubnets); //console.log(localSubnets);
const masqInterfaces = host.masqInterfaces.length > 0 ? host.masqInterfaces.split(',') : []; const masqInterfaces = host.masqInterfaces.length > 0 ? host.masqInterfaces.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) {
...@@ -200,14 +258,18 @@ class InventoryBuilder { ...@@ -200,14 +258,18 @@ class InventoryBuilder {
connections.push(await this.parse_connection(host, this.hosts[h], null_connection, true, false, false)); connections.push(await this.parse_connection(host, this.hosts[h], null_connection, true, false, false));
connections.push(await this.parse_connection(host, this.hosts[h], null_connection, false, true, false)); connections.push(await this.parse_connection(host, this.hosts[h], null_connection, false, true, false));
} }
const targetHost = this.hosts[h];
routePlans.push({ routePlans.push({
name: h.replace(/-/g, '_'), name: h.replace(/-/g, "_"),
destMark: this.hosts[h].destMark, destMark: targetHost.destMark,
address: this.hosts[h].address addresses: [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