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

Use droplet metadata instead of parsing commands output #16

Open
BlessJah opened this issue May 5, 2017 · 1 comment
Open

Use droplet metadata instead of parsing commands output #16

BlessJah opened this issue May 5, 2017 · 1 comment

Comments

@BlessJah
Copy link

BlessJah commented May 5, 2017

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
Copy link
Owner

elitak commented May 6, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants