Support for openstack meta_data.json, connect timeout, other fixes.

This commit is contained in:
Reyk Floeter 2018-01-10 14:13:49 +01:00
commit ac66f160e0
7 changed files with 279 additions and 55 deletions

View file

@ -30,12 +30,11 @@
#include "xml.h"
static int cloudinit_fetch(struct system_config *);
static char *cloudinit_get(struct system_config *, const char *,
enum strtype);
int
ec2(struct system_config *sc)
{
free(sc->sc_username);
if ((sc->sc_username = strdup("ec2-user")) == NULL ||
(sc->sc_endpoint = strdup("169.254.169.254")) == NULL) {
log_warnx("failed to set defaults");
@ -49,8 +48,7 @@ int
cloudinit(struct system_config *sc)
{
/* XXX get endpoint from DHCP lease file */
if ((sc->sc_username = strdup("puffy")) == NULL ||
(sc->sc_endpoint = strdup("169.254.169.254")) == NULL) {
if ((sc->sc_endpoint = strdup("169.254.169.254")) == NULL) {
log_warnx("failed to set defaults");
return (-1);
}
@ -58,35 +56,6 @@ cloudinit(struct system_config *sc)
return (cloudinit_fetch(sc));
}
static char *
cloudinit_get(struct system_config *sc, const char *path, enum strtype type)
{
struct httpget *g = NULL;
char *str = NULL;
log_debug("%s: %s", __func__, path);
g = http_get(&sc->sc_addr, 1,
sc->sc_endpoint, 80, path, NULL, 0, NULL);
if (g != NULL && g->code == 200 && g->bodypartsz > 0) {
switch (type) {
case TEXT:
/* multi-line string, always printable */
str = get_string(g->bodypart, g->bodypartsz);
break;
case LINE:
str = get_line(g->bodypart, g->bodypartsz);
break;
case WORD:
str = get_word(g->bodypart, g->bodypartsz);
break;
}
}
http_get_free(g);
return (str);
}
static int
cloudinit_fetch(struct system_config *sc)
{
@ -96,37 +65,32 @@ cloudinit_fetch(struct system_config *sc)
sc->sc_addr.ip = sc->sc_endpoint;
sc->sc_addr.family = 4;
if (sc->sc_dryrun)
return (0);
/* instance-id */
if ((sc->sc_instance = cloudinit_get(sc,
if ((sc->sc_instance = metadata(sc,
"/latest/meta-data/instance-id", WORD)) == NULL)
goto fail;
/* hostname */
if ((sc->sc_hostname = cloudinit_get(sc,
if ((sc->sc_hostname = metadata(sc,
"/latest/meta-data/local-hostname", WORD)) == NULL)
goto fail;
/* pubkey */
if ((str = cloudinit_get(sc,
if ((str = metadata(sc,
"/latest/meta-data/public-keys/0/openssh-key", LINE)) == NULL)
goto fail;
if (agent_addpubkey(sc, str, NULL) != 0)
goto fail;
/* optional username - this is an extension by meta-data(8) */
if ((str = cloudinit_get(sc,
"/latest/meta-data/username", WORD)) != NULL) {
if ((str = metadata(sc, "/latest/meta-data/username", WORD)) != NULL) {
free(sc->sc_username);
sc->sc_username = str;
str = NULL;
}
/* userdata */
if ((sc->sc_userdata = cloudinit_get(sc,
"/latest/user-data", TEXT)) == NULL)
if ((sc->sc_userdata = metadata(sc, "/latest/user-data", TEXT)) == NULL)
goto fail;
ret = 0;