Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network configuration using netplan for Ubuntu versions greater than 16.04 #141

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 83 additions & 41 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,66 @@ setup_networking() {
if ! is_network_configured; then
cidr=$(echo $TINKERBELL_NETWORK | grep -Eo "\/[[:digit:]]+" | grep -v "^$" | tr -d "/")
case "$1" in
ubuntu)
if [ ! -f /etc/network/interfaces ]; then
echo "$ERR file /etc/network/interfaces not found"
exit 1
fi

if grep -q $TINKERBELL_HOST_IP /etc/network/interfaces; then
echo "$INFO tinkerbell network interface is already configured"
else
# plumb IP and restart to tinkerbell network interface
if grep -q $TINKERBELL_NETWORK_INTERFACE /etc/network/interfaces; then
echo "" >>/etc/network/interfaces
write_iface_config
ubuntu)
v16=16.04
version=$(. /etc/os-release && echo "$VERSION_ID")
if (( $(echo "$version > $v16" | bc -l) )); then
echo "$INFO found Ubuntu $version, using 'netplan' for network configuration"
Comment on lines +236 to +239
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like 17.10 is the first release to use netplan by default. Maybe we should change this to >= 17.10?


# this is required to fix the bug described here
# https://bugs.launchpad.net/netplan/+bug/1810043
apt-get install -y --no-install-recommends netplan.io >> /dev/null
cat >/etc/netplan/${TINKERBELL_NETWORK_INTERFACE}.yaml <<EOF
---
network:
version: 2
renderer: networkd
ethernets:
$TINKERBELL_NETWORK_INTERFACE:
addresses:
- $TINKERBELL_HOST_IP/$cidr
- $TINKERBELL_NGINX_IP/$cidr
EOF
Comment on lines +244 to +254
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generating YAML on the fly with bash feels a bit scary. In particular, magic values can turn in to other things like "no" being a bool, and 1.10 being just 1.1. What would you think about using (and installing if it isn't yet available) jq for this? Using jq guarantees the output will be correct and that we didn't make an error in indentation, whitespace, and also makes sure that all of the values are properly quoted.

Here is an example of what that would look like:

TINKERBELL_NETWORK_INTERFACE=10.5.3.1
TINKERBELL_HOST_IP=10.5.3.2
TINKERBELL_NGINX_IP=10.5.3.3
cidr=24

jq -n \
   --arg interface "$TINKERBELL_NETWORK_INTERFACE" \
   --arg cidr "$cidr" \
   --arg host_ip "$TINKERBELL_HOST_IP" \
   --arg nginx_ip "$TINKERBELL_NGINX_IP" \
   '{
  network: "hi",
  renderer: "networkd",
  ethernets: {
    ($interface): {
      addresses: [
        "\($host_ip)/\($cidr)",
        "\($nginx_ip)/\($cidr)"
      ]
    }
  }
}'

This prints:

{
  "network": "hi",
  "renderer": "networkd",
  "ethernets": {
    "10.5.3.1": {
      "addresses": [
        "10.5.3.2/24",
        "10.5.3.3/24"
      ]
    }
  }
}

since JSON is valid YAML, this output works directly with netplan.

ip link set $TINKERBELL_NETWORK_INTERFACE nomaster
netplan apply
echo "$INFO waiting for the network configuration to be applied by systemd-networkd"
sleep 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if netplan creates a systemd unit we could wait for, like this:

while ! systemctl is-active netplan-something-or-other; do
  sleep 1
done

else
echo "$INFO found Ubuntu $version, using '/etc/network/interfaces' for network configuration"
if [ ! -f /etc/network/interfaces ] ; then
echo "$ERR file /etc/network/interfaces not found"
exit 1
fi

if grep -q $TINKERBELL_HOST_IP /etc/network/interfaces ; then
echo "$INFO tinkerbell network interface is already configured"
else
if command_exists ifdown; then
echo "$BLANK- ifupdown already installed"
else
echo "$BLANK- installing ifupdown"
apt-get install -y ifupdown >> /dev/null && echo "$BLANK- ifupdown installed successfully"
fi

# plumb IP and restart to tinkerbell network interface
if grep -q $TINKERBELL_NETWORK_INTERFACE /etc/network/interfaces ; then
echo "" >> /etc/network/interfaces
write_iface_config
else
echo -e "\nauto $TINKERBELL_NETWORK_INTERFACE\n" >> /etc/network/interfaces
write_iface_config
fi
ip link set $TINKERBELL_NETWORK_INTERFACE nomaster
ifdown "$TINKERBELL_NETWORK_INTERFACE:0"
ifdown "$TINKERBELL_NETWORK_INTERFACE:1"
ifup "$TINKERBELL_NETWORK_INTERFACE:0"
ifup "$TINKERBELL_NETWORK_INTERFACE:1"
fi
fi
;;
centos)
if [ -f /etc/sysconfig/network-scripts/ifcfg-$TINKERBELL_NETWORK_INTERFACE ]; then
sed -i '/^ONBOOT.*no$/s/no/yes/; /^BOOTPROTO.*none$/s/none/static/; /^MASTER/d; /^SLAVE/d' /etc/sysconfig/network-scripts/ifcfg-$TINKERBELL_NETWORK_INTERFACE
else
echo -e "\nauto $TINKERBELL_NETWORK_INTERFACE\n" >>/etc/network/interfaces
write_iface_config
Expand Down Expand Up @@ -284,6 +331,7 @@ EOF
echo "$INFO tinkerbell network interface configured successfully"
else
echo "$ERR tinkerbell network interface configuration failed"
exit 1
fi
else
echo "$INFO tinkerbell network interface is already configured"
Expand Down Expand Up @@ -416,34 +464,28 @@ start_components() {
check_prerequisites() {
echo "$INFO verifying prerequisites"
case "$1" in
ubuntu)
if command_exists git; then
echo "$BLANK- git already installed, found $(git --version)"
else
echo "$BLANK- updating packages"
apt-get update >>/dev/null
echo "$BLANK- installing git"
apt-get install -y --no-install-recommends git >>/dev/null
echo "$BLANK- $(git --version) installed successfully"
fi
if command_exists ifdown; then
echo "$BLANK- ifupdown already installed"
else
echo "$BLANK- installing ifupdown"
apt-get install -y ifupdown >>/dev/null && echo "$BLANK- ifupdown installed successfully"
fi
;;
centos)
if command_exists git; then
echo "$BLANK- git already installed, found $(git --version)"
else
echo "$BLANK- updating packages"
yum update -y >>/dev/null
echo "$BLANK- installing git"
yum install -y git >>/dev/null
echo "$BLANK- $(git --version) installed successfully"
fi
;;
ubuntu)
if command_exists git; then
echo "$BLANK- git already installed, found $(git --version)"
else
echo "$BLANK- updating packages"
apt-get update >> /dev/null
echo "$BLANK- installing git"
apt-get install -y --no-install-recommends git >> /dev/null
echo "$BLANK- $(git --version) installed successfully"
fi
;;
centos)
if command_exists git; then
echo "$BLANK- git already installed, found $(git --version)"
else
echo "$BLANK- updating packages"
yum update -y >> /dev/null
echo "$BLANK- installing git"
yum install -y git >> /dev/null
echo "$BLANK- $(git --version) installed successfully"
fi
;;
esac

setup_docker
Expand Down