From aa963100ba764676a96149d3475694b04302915a Mon Sep 17 00:00:00 2001 From: Reyk Floeter Date: Sun, 2 Jun 2019 02:41:36 +0200 Subject: [PATCH] Fall back to 169.254.169.254 if DHCP endpoint does not work (unbreak OpenStack) --- agent/cloudinit.c | 51 +++++++++++++++++++++++++++++++++++++---------- agent/main.h | 4 ++++ agent/openstack.c | 14 ++----------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/agent/cloudinit.c b/agent/cloudinit.c index 807d784..aff28d6 100644 --- a/agent/cloudinit.c +++ b/agent/cloudinit.c @@ -31,31 +31,60 @@ static int cloudinit_fetch(struct system_config *); +int +tryendpoint(struct system_config *sc, + int (fetch)(struct system_config *), + int (next)(struct system_config *)) +{ + free(sc->sc_endpoint); + sc->sc_endpoint = NULL; + + switch (sc->sc_dhcpendpoint) { + case 0: + sc->sc_dhcpendpoint = 1; + if (dhcp_getendpoint(sc) == -1) + return tryendpoint(sc, fetch, next); + break; + case 1: + sc->sc_dhcpendpoint = 2; + if ((sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) { + log_warnx("failed to set defaults"); + return (-1); + } + break; + default: + if (next == NULL) + return (-1); + sc->sc_dhcpendpoint = 0; + return (*next)(sc); + } + + if ((*fetch)(sc) != 0) + return tryendpoint(sc, fetch, next); + + return (0); +} + int ec2(struct system_config *sc) { free(sc->sc_username); - if ((sc->sc_username = strdup("ec2-user")) == NULL || - (sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) { - log_warnx("failed to set defaults"); + if ((sc->sc_username = strdup("ec2-user")) == NULL) { + log_warnx("failed to set default user"); return (-1); } sc->sc_stack = "ec2"; - return (cloudinit_fetch(sc)); + sc->sc_dhcpendpoint = 1; + return tryendpoint(sc, cloudinit_fetch, NULL); } int cloudinit(struct system_config *sc) { - if ((dhcp_getendpoint(sc) == -1) && - (sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) { - log_warnx("failed to set defaults"); - return (-1); - } - sc->sc_stack = "cloudinit"; - return (cloudinit_fetch(sc)); + sc->sc_dhcpendpoint = 0; + return tryendpoint(sc, cloudinit_fetch, NULL); } static int diff --git a/agent/main.h b/agent/main.h index 29a59ec..3ae86f7 100644 --- a/agent/main.h +++ b/agent/main.h @@ -76,6 +76,7 @@ struct system_config { char *sc_pubkey; char *sc_userdata; char *sc_endpoint; + int sc_dhcpendpoint; char *sc_instance; int sc_timeout; @@ -123,6 +124,9 @@ int azure(struct system_config *); /* cloudinit.c */ int ec2(struct system_config *); int cloudinit(struct system_config *); +int tryendpoint(struct system_config *, + int (fetch)(struct system_config *), + int (next)(struct system_config *)); /* opennebula.c */ int opennebula(struct system_config *); diff --git a/agent/openstack.c b/agent/openstack.c index 9c84d34..c60acd1 100644 --- a/agent/openstack.c +++ b/agent/openstack.c @@ -31,21 +31,11 @@ static int openstack_fetch(struct system_config *); + int openstack(struct system_config *sc) { - if ((dhcp_getendpoint(sc) == -1) && - (sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) { - log_warnx("failed to set defaults"); - return (-1); - } - - if (openstack_fetch(sc) != 0) { - free(sc->sc_endpoint); - return (cloudinit(sc)); - } - - return (0); + return tryendpoint(sc, openstack_fetch, cloudinit); } static int