| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# run from rc.local by the testing VMs |
| 4 |
import sys |
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import glob |
| 8 |
import shutil |
| 9 |
|
| 10 |
# return True if different |
| 11 |
def copy_if_diff(src, dst): |
| 12 |
cmd = "diff %s %s" % (src, dst) |
| 13 |
(status, output) = subprocess.getstatusoutput(cmd) |
| 14 |
if status: |
| 15 |
shutil.copyfile(src, dst) |
| 16 |
return status |
| 17 |
|
| 18 |
return False |
| 19 |
|
| 20 |
# under KVM the guests use the known MAC address. |
| 21 |
# Use it to figure out who we are |
| 22 |
# we use the eth0 table from baseconfigs/net*sh |
| 23 |
macs = {} |
| 24 |
macs['nic'] = "12:00:00:de:ad:ba" |
| 25 |
macs['beet'] = "12:00:00:de:76:ba" |
| 26 |
macs['carrot'] = "12:00:00:de:76:bb" |
| 27 |
macs['east'] = "12:00:00:dc:bc:ff" |
| 28 |
macs['west'] = "12:00:00:ab:cd:ff" |
| 29 |
macs['north'] = "12:00:00:de:cd:49" |
| 30 |
macs['pole'] = "12:00:00:de:cd:01" |
| 31 |
macs['road'] = "12:00:00:ab:cd:02" |
| 32 |
macs['sunrise'] = "12:00:00:dc:bc:01" |
| 33 |
macs['sunset'] = "12:00:00:ab:cd:01" |
| 34 |
# conflicts with north |
| 35 |
#macs['japan'] = "12:00:00:ab:cd:02" |
| 36 |
|
| 37 |
if os.path.isfile("/etc/redhat-release"): |
| 38 |
GUESTOS = "redhat" |
| 39 |
elif os.path.isfile("/etc/debian_version"): |
| 40 |
GUESTOS = "debian" |
| 41 |
|
| 42 |
|
| 43 |
myname = os.path.basename(sys.argv[0]) |
| 44 |
subprocess.getstatusoutput("rm -f /var/tmp/%s" % (myname)) |
| 45 |
subprocess.getstatusoutput("echo %s > /var/tmp/%s.ran" % (myname, myname)) |
| 46 |
|
| 47 |
(status, eth0) = subprocess.getstatusoutput("cat /sys/class/net/eth0/address") |
| 48 |
print("eth0", status, eth0) |
| 49 |
maclist = None |
| 50 |
if status: |
| 51 |
# may be this host was never initialized. |
| 52 |
(status, maclist) = subprocess.getstatusoutput( |
| 53 |
"cat /sys/class/net/ens*/address") |
| 54 |
print("ens*", maclist) |
| 55 |
eth0 = None |
| 56 |
else: |
| 57 |
maclist = eth0 |
| 58 |
|
| 59 |
output = "" |
| 60 |
is_docker = None |
| 61 |
is_kvm = None |
| 62 |
|
| 63 |
hostname = None |
| 64 |
for h in list(macs.keys()): |
| 65 |
mac = macs[h] |
| 66 |
for m in maclist.split(): |
| 67 |
print(h, mac, "in", m, "?") |
| 68 |
if mac in m: |
| 69 |
print("yes") |
| 70 |
hostname = h |
| 71 |
is_docker = False |
| 72 |
is_kvm = True |
| 73 |
|
| 74 |
if not is_kvm: |
| 75 |
# on docker the hostname is already set. Use hostname to identify ourself |
| 76 |
hostname = subprocess.getoutput("hostname -s") |
| 77 |
print(("this is a docker instance hostname %s" % hostname)) |
| 78 |
is_docker = True |
| 79 |
|
| 80 |
if not hostname: |
| 81 |
sys.exit( |
| 82 |
"ERROR: Failed to find our swan hostname based on the mac match knownlist ") |
| 83 |
|
| 84 |
# print "we are %s"%hostname |
| 85 |
# we seem to have found our identity |
| 86 |
|
| 87 |
# install files common to all GUESTOS types |
| 88 |
if os.path.isfile("/etc/sysconfig/network-scripts/README.libreswan"): |
| 89 |
print("systemd - skipping hostname") |
| 90 |
else: |
| 91 |
output += "\n" + subprocess.getoutput("hostname %s" % hostname) |
| 92 |
|
| 93 |
# fixup /etc/sysctl.conf |
| 94 |
sysctl = "/testing/baseconfigs/%s/etc/sysctl.conf" % hostname |
| 95 |
if not os.path.isfile(sysctl): |
| 96 |
sysctl = "/testing/baseconfigs/all/etc/sysctl.conf" |
| 97 |
shutil.copyfile(sysctl, "/etc/sysctl.conf") |
| 98 |
output += "\n" + subprocess.getoutput("sysctl -q -p") |
| 99 |
|
| 100 |
# and resolv.conf |
| 101 |
resolv = "/testing/baseconfigs/%s/etc/resolv.conf" % hostname |
| 102 |
if os.path.isfile(resolv): |
| 103 |
shutil.copyfile(resolv, "/etc/resolv.conf") |
| 104 |
|
| 105 |
# and bind config - can be run on all hosts (to prevent network DNS |
| 106 |
# packets) as well as on nic |
| 107 |
if not os.path.isdir("/etc/bind"): |
| 108 |
os.mkdir("/etc/bind") |
| 109 |
output += "\n" + \ |
| 110 |
subprocess.getoutput("cp -a /testing/baseconfigs/all/etc/bind/* /etc/bind/") |
| 111 |
|
| 112 |
if hostname == "nic": |
| 113 |
output += "\n" + \ |
| 114 |
subprocess.getoutput("cp -a /testing/baseconfigs/nic/etc/unbound /etc/") |
| 115 |
output += "\n" + \ |
| 116 |
subprocess.getoutput("cp -a /testing/baseconfigs/nic/etc/nsd /etc/") |
| 117 |
output += "\n" + \ |
| 118 |
subprocess.getoutput("cp -a /testing/baseconfigs/nic/etc/systemd/system/unbound.service /etc/systemd/system/") |
| 119 |
|
| 120 |
|
| 121 |
if not os.path.isdir("/etc/ssh"): |
| 122 |
os.makedirs("/etc/ssh", 0o755) |
| 123 |
if not os.path.isdir("/root/.ssh"): |
| 124 |
os.makedirs("/root/.ssh", 0o700) |
| 125 |
output += "\n" + \ |
| 126 |
subprocess.getoutput("cp /testing/baseconfigs/all/etc/ssh/*key* /etc/ssh/") |
| 127 |
output += "\n" + \ |
| 128 |
subprocess.getoutput("cp /testing/baseconfigs/all/root/.ssh/* /root/.ssh/") |
| 129 |
output += "\n" + subprocess.getoutput("chmod 600 /etc/ssh/*key* /root/.ssh/*") |
| 130 |
|
| 131 |
|
| 132 |
if os.path.isfile("/etc/sysconfig/network-scripts/README.libreswan"): |
| 133 |
print("systemd - skipping hostname") |
| 134 |
else: |
| 135 |
hostname_file = "/testing/baseconfigs/%s/etc/hostname" % hostname |
| 136 |
if os.path.isfile(hostname_file): |
| 137 |
shutil.copyfile(hostname_file, "/etc/hostname") |
| 138 |
|
| 139 |
if GUESTOS == "redhat": |
| 140 |
|
| 141 |
# these files are needed for systemd-networkd too |
| 142 |
fnames = glob.glob("/testing/baseconfigs/all/etc/sysconfig/*") |
| 143 |
for fname in fnames: |
| 144 |
if os.path.isfile(fname): |
| 145 |
dst="/etc/sysconfig/%s" % os.path.basename(fname) |
| 146 |
copy_if_diff(fname, dst) |
| 147 |
|
| 148 |
if os.path.isfile("/etc/sysconfig/network-scripts/README.libreswan"): |
| 149 |
print("systemd - no network files to copy") |
| 150 |
elif os.path.isdir("/testing/baseconfigs/%s" % hostname): |
| 151 |
print("sysconfg - and files to copy") |
| 152 |
src = "/testing/baseconfigs/%s/etc/sysconfig/network" % hostname |
| 153 |
dst = "/etc/sysconfig/network" |
| 154 |
copy_if_diff(src, dst) |
| 155 |
ifaces = glob.glob("/testing/baseconfigs/%s/etc/sysconfig/network-scripts/ifcfg*" % hostname) |
| 156 |
routes = glob.glob("/testing/baseconfigs/%s/etc/sysconfig/network-scripts/route*" % hostname) |
| 157 |
dst = "/etc/sysconfig/network-scripts/" |
| 158 |
|
| 159 |
for entry in (ifaces + routes): |
| 160 |
dst_local = dst + '/' + os.path.basename(entry) |
| 161 |
copy_if_diff(entry, dst_local) |
| 162 |
|
| 163 |
if is_docker: |
| 164 |
iffile = os.path.basename(entry) |
| 165 |
cmd = "sed -i /HWADDR/d /etc/sysconfig/network-scripts/%s" % ( |
| 166 |
iffile) |
| 167 |
print(("cmd %s" % cmd)) |
| 168 |
subprocess.getoutput(cmd) |
| 169 |
|
| 170 |
# SElinux fixup |
| 171 |
output += "\n" + subprocess.getoutput("restorecon -R /etc/ /root/.ssh") |
| 172 |
output += "\n" + \ |
| 173 |
subprocess.getoutput("chcon -R --reference /var/log /testing/pluto") |
| 174 |
|
| 175 |
if os.path.isfile("/etc/sysconfig/network-scripts/README.libreswan"): |
| 176 |
print("systemd - no network to restart") |
| 177 |
elif os.path.isdir("/testing/baseconfigs/%s" % hostname): |
| 178 |
if os.path.isfile("/usr/bin/systemctl"): |
| 179 |
output += "\n" + subprocess.getoutput("systemctl restart network.service") |
| 180 |
else: |
| 181 |
output += "\n" + subprocess.getoutput("service network restart") |
| 182 |
|
| 183 |
# get rid of damn cp/mv/rm aliases for root |
| 184 |
|
| 185 |
if not "# alias" in open("/root/.bashrc", "r").read(): |
| 186 |
output += "\n" + \ |
| 187 |
subprocess.getoutput("sed -i 's/alias rm/# alias rm/g' /root/.bashrc") |
| 188 |
output += "\n" + \ |
| 189 |
subprocess.getoutput("sed -i 's/alias cp/# alias cp/g' /root/.bashrc") |
| 190 |
output += "\n" + \ |
| 191 |
subprocess.getoutput("sed -i 's/alias mv/# alias mv/g' /root/.bashrc") |
| 192 |
|
| 193 |
elif GUESTOS == "debian": |
| 194 |
shutil.copyfile("/testing/baseconfigs/%s/etc/network/debian.interfaces" % |
| 195 |
hostname, "/etc/network/interfaces") |
| 196 |
output += "\n" + subprocess.getoutput("/etc/init.d/networking restart") |
| 197 |
|
| 198 |
# and some custom ipsec* files, but not directories |
| 199 |
if os.path.isdir("/etc/ipsec.d"): |
| 200 |
if not os.path.isdir("/etc/ipsec.d.stock"): |
| 201 |
os.rename("/etc/ipsec.d", "/etc/ipsec.d.stock") |
| 202 |
else: |
| 203 |
shutil.rmtree("/etc/ipsec.d") |
| 204 |
shutil.copytree("/testing/baseconfigs/all/etc/ipsec.d", "/etc/ipsec.d", |
| 205 |
ignore=shutil.ignore_patterns("ipsec.conf.common")) |
| 206 |
# copytree is not ideal to add to existing dir |
| 207 |
output += "\n" + \ |
| 208 |
subprocess.getoutput( |
| 209 |
"cp -a /testing/baseconfigs/%s/etc/ipsec.* /etc/" % hostname) |
| 210 |
|
| 211 |
# fixup the nss files that are root-only on a real host, but are world-read |
| 212 |
# in our repository so the qemu user can still read it to copy it |
| 213 |
if os.path.isfile("/etc/ipsec.d/pkcs11.txt"): |
| 214 |
os.chmod("/etc/ipsec.d/pkcs11.txt", 0o600) |
| 215 |
for dbfile in glob.glob("/etc/ipsec.d/*.db"): |
| 216 |
os.chmod(dbfile, 0o600) |
| 217 |
|
| 218 |
# SElinux fixup |
| 219 |
if GUESTOS == "redhat": |
| 220 |
output += "\n" + subprocess.getoutput("restorecon -R /etc/") |
| 221 |
|
| 222 |
# selinux does not like our /testing include files |
| 223 |
if hostname == "nic": |
| 224 |
output += "\n" + subprocess.getoutput("setenforce 0") |
| 225 |
|
| 226 |
if len(sys.argv) > 1: |
| 227 |
print(output.strip()) |
| 228 |
sys.exit() |
| 229 |
|
| 230 |
sys.exit("ERROR: Failed to find our swan hostname based on the mac of eth0") |