Skip to content

Instantly share code, notes, and snippets.

@giovannicandido
Created April 3, 2019 16:29
Show Gist options
  • Save giovannicandido/b3cc1875205c45a977c20ba062fccd8e to your computer and use it in GitHub Desktop.
Save giovannicandido/b3cc1875205c45a977c20ba062fccd8e to your computer and use it in GitHub Desktop.
#!/bin/bash
upgrade() {
# Initial updates
apt update
apt upgrade -y
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt install -y jq
apt install -y ipcalc
snap install yq
}
calculate_mask() {
MASK=`ipcalc -n 1.1.1.1 $1 | sed -n '/^Netm/{s#.*= #/#;s/ .*//p;q}'`
}
configure_private_ip() {
curl -o /tmp/metadata.json http://169.254.169.254/v1.json
PRIVATE_IP=`cat /tmp/metadata.json | jq '.interfaces[1].ipv4.address' -r`
NET_MASK=`cat /tmp/metadata.json | jq '.interfaces[1].ipv4.netmask' -r`
calculate_mask $NET_MASK
jq -n --arg ip_cidr "$PRIVATE_IP$MASK" '{"network": {"version": 2, "renderer": "networkd", "ethernets": {"ens7": {"dhcp4": "no", "mtu": 1450, "addresses": [$ip_cidr]}}}}' > /tmp/01-netcfg.json
cat /tmp/01-netcfg.json | yq r - > /etc/netplan/01-netcfg.yaml
netplan apply
}
upgrade
configure_private_ip
@giovannicandido
Copy link
Author

Upgrade the system and configures private network with the vultr api. You can see the ip in the settings tab of your server.
It may take a few minutes to finish the upgrade

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment