elitak / nixos-infect

[GPLv3+] install nixos over the existing OS in a DigitalOcean droplet (and others with minor modifications)
GNU General Public License v3.0
1.24k stars 210 forks source link

Use droplet metadata instead of parsing commands output #16

Open BlessJah opened 7 years ago

BlessJah commented 7 years ago

Parsing ip output is error prone and provides lot of unnecessary duplication. All the data that is necessary to configure should be (and in fact is) provided by means of droplet metadata, exposed via convenient REST API.

eth0_ip4s=$(ip address show dev "$eth0_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
eth0_ip6s=$(ip address show dev "$eth0_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
eth0_ip4s=$(curl http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
eth0_ip6s=$(curl http://169.254.169.254/metadata/v1/interfaces/public/0/ipv6/address)

Existence and lack of specific keys gives one great inspection abilities with minimal effort.

eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}')||true
if [ -n "$eth1_name" ];then
if curl --fail http://169.254.169.254/metadata/v1/interfaces/public/1 ; then

The interfaces endpoint contains precise information on amount and intention of interfaces:

# curl http://169.254.169.254/metadata/v1/interfaces/
public/
private/
# curl http://169.254.169.254/metadata/v1/interfaces/private/
0/
# curl http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address
10.135.53.177
elitak commented 7 years ago

I'd accept a PR for this, as long as it falls back to parsing ip address output, so that non-DigitialOcean systems still work, although I'm not really convinced the parsing is any more error-prone than using a ReST API, my reasoning being that ip address output is always going to be there and probably never going to change format, whereas a hardcoded host address and path could very well change or fail in some novel way.

The lines could otherwise be beautified by using a few bash functions that call each other.