zulip-ec2-configure-interfaces: Remove.

Our current EC2 systems don’t have an interface named ‘eth0’, and if
they did, this script would do nothing but crash with ImportError
because we have never installed boto.utils for Python 3.

(The message of commit 2a4d851a7c made
an effort to document for future researchers why this script should
not have been blindly converted to Python 3.  However, commit
2dc6d09c2a (#14278) was evidently
unresearched and untested.)

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-05-02 20:43:50 -07:00 committed by Alex Vandiver
parent 7acb642fa5
commit e9ba9b0e0d
5 changed files with 2 additions and 209 deletions

View File

@ -33,10 +33,6 @@ Copyright: 2005 Francesc Guasch
License: GPL-2.0 License: GPL-2.0
Comment: Not linked. Comment: Not linked.
Files: puppet/zulip_ops/files/zulip-ec2-configure-interfaces
Copyright: 2013-2017, Dropbox, Inc., Kandra Labs, Inc., and contributors
License: Expat
Files: scripts/setup/generate-self-signed-cert Files: scripts/setup/generate-self-signed-cert
Copyright: 2003-2006 Thom May Copyright: 2003-2006 Thom May
2006 Fabio M. Di Nitto 2006 Fabio M. Di Nitto

View File

@ -1,179 +0,0 @@
#!/usr/bin/env python3
#
# Copyright © 2013 Zulip, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Original author: Luke Faraone
"""Configure a host for EC2-VPC dynamically assigned network interfaces
Amazon VPC gives us a good deal of flexibility compared to classic EC2.
However there are limitations; you can assign multiple IPs to a host
yet only the first IP per interface will be DHCP assigned, and you are
limited in the total number of interfaces you have, so doing one-IP-per-
interface is also untenable.
This script grabs the metadata provided by AWS and uses it to correctly
configure all available network interfaces.
It is suitable to be hooked in to system boot and network
reconfiguration scripts.
Note that it currently does not handle the deconfiguration of
interfaces.
"""
import logging
import logging.handlers
import subprocess
import sys
from typing import Optional
import boto.utils
import netifaces
def address_of(device_id: int) -> Optional[str]:
try:
return netifaces.ifaddresses(f"ens{device_id}")[netifaces.AF_INET][0]["addr"]
except KeyError:
return None
def guess_gateway(device_id: int) -> Optional[str]:
# This will not work if the default gateway isn't n.n.n.1.
address = address_of(device_id)
if address is None:
return None
gateway = address.split(".")
gateway[3] = "1"
return ".".join(gateway)
log = logging.getLogger("configure-cloud-interfaces")
log.setLevel(logging.DEBUG)
log.addHandler(logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON))
log.addHandler(logging.StreamHandler())
log.info("Starting.")
metadata = boto.utils.get_instance_metadata()
if metadata is None:
log.error("Could not get instance metadata!")
sys.exit(1)
macs = metadata["network"]["interfaces"]["macs"]
ids = [int(macdata["device-number"]) for macdata in macs.values()]
ifaces = [iface for iface in netifaces.interfaces() if ":" not in iface and iface != "lo"]
# Number of IDs should equal number of interfaces
if len(ids) != len(ifaces):
log.error(f"Metadata indicated {len(ids)} interfaces but we have {len(ifaces)}!")
sys.exit(1)
for device in macs.values():
# There's an annoying API inconsistency here:
# If you have multiple IPs, local-ipv4s is a list.
# If you only have one, local-ipv4s is a string.
# Who knew?
if isinstance(device["local-ipv4s"], str):
# Only do dhcp, don't try to assign addresses
to_configure = [device["local-ipv4s"]]
else:
to_configure = list(device["local-ipv4s"])
device_number = int(device["device-number"])
address = address_of(device_number)
if address is None:
# If the device was not autoconfigured, do so now.
log.info(f"Device ens{device_number} not configured, starting dhcpd")
subprocess.check_call(["/sbin/dhcpcd", f"ens{device_number}"])
dev_num = str(device_number)
address = address_of(device_number)
gateway = guess_gateway(device_number)
assert address is not None
assert gateway is not None
# Horrible hack to route return packets on the correct interface
# See https://unix.stackexchange.com/a/4421/933
subprocess.check_call(["/sbin/ip", "rule", "add", "fwmark", dev_num, "table", dev_num])
subprocess.check_call(
[
"/sbin/ip",
"route",
"add",
"0.0.0.0/0",
"table",
dev_num,
"dev",
f"ens{device_number}",
"via",
gateway,
]
)
subprocess.check_call(
[
"/sbin/iptables",
"-t",
"mangle",
"-A",
"OUTPUT",
"-m",
"conntrack",
"--ctorigdst",
address,
"-j",
"MARK",
"--set-mark",
dev_num,
]
)
to_configure.remove(address)
for (count, ip) in enumerate(to_configure):
# Configure the IP via a virtual interface
device = f"ens{device_number}:{count}"
log.info(f"Configuring {device} with IP {ip}")
subprocess.check_call(["/sbin/ifconfig", device, ip])
subprocess.check_call(
[
"/sbin/iptables",
"-t",
"mangle",
"-A",
"OUTPUT",
"-m",
"conntrack",
"--ctorigdst",
ip,
"-j",
"MARK",
"--set-mark",
str(device_number),
]
)
for throwaway in range(2):
# Don't freak out if this doesn't work.
subprocess.call(["/sbin/ip", "route", "del", "10.0.0.0/8"])
log.info("Finished.")

View File

@ -1,13 +0,0 @@
#! /bin/sh
# Run zulip-ec2-configure-interfaces when eth0 is brought up
set -e
# Only run from ifup.
if [ "$MODE" != start ]; then
exit 0
fi
if [ "$IFACE" = eth0 ]; then
/usr/local/sbin/zulip-ec2-configure-interfaces
fi

View File

@ -20,9 +20,6 @@ class zulip_ops::profile::base {
'certbot', 'certbot',
# For managing our current Debian packages # For managing our current Debian packages
'debian-goodies', 'debian-goodies',
# Needed for zulip-ec2-configure-network-interfaces
'python3-boto3',
'python3-netifaces',
# Popular editors # Popular editors
'vim', 'vim',
'emacs-nox', 'emacs-nox',
@ -116,9 +113,6 @@ class zulip_ops::profile::base {
if $hosting_provider == 'ec2' { if $hosting_provider == 'ec2' {
# This conditional block is for for whether it's not # This conditional block is for for whether it's not
# chat.zulip.org, which uses a different hosting provider. # chat.zulip.org, which uses a different hosting provider.
package { 'dhcpcd5':
ensure => installed,
}
file { '/root/.ssh/authorized_keys': file { '/root/.ssh/authorized_keys':
ensure => file, ensure => file,
mode => '0600', mode => '0600',
@ -144,15 +138,11 @@ class zulip_ops::profile::base {
} }
file { '/usr/local/sbin/zulip-ec2-configure-interfaces': file { '/usr/local/sbin/zulip-ec2-configure-interfaces':
ensure => file, ensure => absent,
mode => '0755',
source => 'puppet:///modules/zulip_ops/zulip-ec2-configure-interfaces',
} }
file { '/etc/network/if-up.d/zulip-ec2-configure-interfaces_if-up.d.sh': file { '/etc/network/if-up.d/zulip-ec2-configure-interfaces_if-up.d.sh':
ensure => file, ensure => absent,
mode => '0755',
source => 'puppet:///modules/zulip_ops/zulip-ec2-configure-interfaces_if-up.d.sh',
} }
file { '/etc/chrony/chrony.conf': file { '/etc/chrony/chrony.conf':

View File

@ -77,7 +77,6 @@ module = [
"markdown_include.*", "markdown_include.*",
"moto.*", "moto.*",
"natsort.*", "natsort.*",
"netifaces.*",
"onelogin.*", "onelogin.*",
"openapi_core.*", "openapi_core.*",
"premailer.*", "premailer.*",