|
| 1 | +#!/usr/bin/env perl |
| 2 | +# Copyright SUSE LLC |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +use Mojo::Base -strict, -signatures; |
| 6 | +use Mojo::File qw(path); |
| 7 | +use autodie ':all'; |
| 8 | + |
| 9 | +sub configure_firewall ($firewall, $bridge) { |
| 10 | + path($firewall)->spurt(qq( |
| 11 | + <?xml version="1.0" encoding="utf-8"?> |
| 12 | + <zone target="ACCEPT"> |
| 13 | + <short>Trusted</short> |
| 14 | + <description>All network connections are accepted.</description> |
| 15 | + <interface name="br$bridge"/> |
| 16 | + <interface name="ovs-system"/> |
| 17 | + <interface name="eth0"/> |
| 18 | + <masquerade/> |
| 19 | + </zone> |
| 20 | + )); |
| 21 | +} |
| 22 | + |
| 23 | +sub provision_services () { |
| 24 | + # bind-utils contains dig |
| 25 | + system("zypper in -y bind-utils os-autoinst-openvswitch"); |
| 26 | + system("systemctl enable --now openvswitch os-autoinst-openvswitch"); |
| 27 | + system("systemctl reload firewalld"); |
| 28 | +} |
| 29 | + |
| 30 | +sub generate_bridge_configs ($etc, $bridge) { |
| 31 | + path("$etc/sysconfig/os-autoinst-openvswitch")->spurt("OS_AUTOINST_USE_BRIDGE=br$bridge"); |
| 32 | + # Manage services only if writing to system-wide files |
| 33 | + system("ovs-vsctl add-br br$bridge") if $etc =~ /^\/etc/; |
| 34 | + |
| 35 | + my $bridge_file = "$etc/sysconfig/network/ifcfg-br$bridge"; |
| 36 | + my $ip = "10.0.2.2/15"; |
| 37 | + my $config = " |
| 38 | + BOOTPROTO=static |
| 39 | + IPADDR=$ip |
| 40 | + STARTMODE=auto |
| 41 | + ZONE=trusted |
| 42 | + OVS_BRIDGE=yes |
| 43 | + PRE_UP_SCRIPT=wicked:gre_tunnel_preup.sh |
| 44 | + "; |
| 45 | + |
| 46 | + for my $i (0..147) { |
| 47 | + $config .= "OVS_BRIDGE_PORT_DEVICE_$i='tap$i'\n"; |
| 48 | + |
| 49 | + path("$etc/sysconfig/network/ifcfg-tap$i")->spurt(" |
| 50 | + BOOTPROTO='none' |
| 51 | + IPADDR='' |
| 52 | + NETMASK='' |
| 53 | + PREFIXLEN='' |
| 54 | + STARTMODE='auto' |
| 55 | + TUNNEL='tap' |
| 56 | + TUNNEL_SET_GROUP='nogroup' |
| 57 | + TUNNEL_SET_OWNER='_openqa-worker' |
| 58 | + ZONE=trusted |
| 59 | + "); |
| 60 | + } |
| 61 | + |
| 62 | + path($bridge_file)->spurt($config); |
| 63 | +} |
| 64 | + |
| 65 | +sub generate_preup ($gre, $bridge) { |
| 66 | + my $gre_config = ' |
| 67 | + #!/bin/sh |
| 68 | + action="$1" |
| 69 | + bridge="$2" |
| 70 | + ovs-vsctl set bridge $bridge stp_enable=true |
| 71 | + '; |
| 72 | + |
| 73 | + my @workers = qw(openqaworker1 openqaworker4 openqaworker7 openqaworker19); |
| 74 | + my $device = 0; |
| 75 | + my $this_worker = qx(hostname -i); |
| 76 | + chomp $this_worker; |
| 77 | + for my $worker (@workers) { |
| 78 | + my $ip = qx"dig +short $worker"; |
| 79 | + next if $ip eq $this_worker; # Don't put the machine itself here |
| 80 | + $device++; |
| 81 | + $gre_config .= " |
| 82 | + # $worker |
| 83 | + ovs-vsctl --may-exist add-port $bridge gre$device -- set interface gre$device type=gre options:remote_ip=$ip"; |
| 84 | + } |
| 85 | + |
| 86 | + path($gre)->spurt($gre_config); |
| 87 | + system("chmod +x $gre"); |
| 88 | +} |
| 89 | + |
| 90 | +my ($etc, $bridge) = @ARGV; |
| 91 | +$etc //= '/etc'; |
| 92 | +$bridge //= 1; |
| 93 | + |
| 94 | +configure_firewall("$etc/firewalld/zones/trusted.xml", $bridge); |
| 95 | +# Manage services only if writing to system-wide files |
| 96 | +provision_services if $etc =~ /^\/etc/; |
| 97 | +generate_bridge_configs($etc, $bridge); |
| 98 | +generate_preup("$etc/wicked/scripts/gre_tunnel_preup.sh", $bridge); |
0 commit comments