Use -n to 'rescue' ovf-env.xml file and exit

This commit is contained in:
Reyk Floeter 2017-08-14 13:40:12 +02:00
parent 6f6e63f99b
commit 63d46cd6f1
4 changed files with 48 additions and 37 deletions

View file

@ -72,13 +72,19 @@ azure(struct system_config *sc)
sc->sc_ovfenv = "/var/db/azure-ovf-env.xml"; sc->sc_ovfenv = "/var/db/azure-ovf-env.xml";
sc->sc_priv = &az_config; sc->sc_priv = &az_config;
if (azure_getendpoint(sc) != 0) { if (azure_getovfenv(sc) != 0) {
log_warnx("failed to get endpoint"); log_warnx("failed to get ovf-env.xml");
goto done; goto done;
} }
if (azure_getovfenv(sc) != 0) { if (sc->sc_dryrun) {
log_warnx("failed to get ovf-env.xml"); /* Return after backing up the ovf-env.xml file */
ret = 0;
goto done;
}
if (azure_getendpoint(sc) != 0) {
log_warnx("failed to get endpoint");
goto done; goto done;
} }

View file

@ -96,6 +96,9 @@ cloudinit_fetch(struct system_config *sc)
sc->sc_addr.ip = sc->sc_endpoint; sc->sc_addr.ip = sc->sc_endpoint;
sc->sc_addr.family = 4; sc->sc_addr.family = 4;
if (sc->sc_dryrun)
return (0);
/* instance-id */ /* instance-id */
if ((sc->sc_instance = cloudinit_get(sc, if ((sc->sc_instance = cloudinit_get(sc,
"/latest/meta-data/instance-id", WORD)) == NULL) "/latest/meta-data/instance-id", WORD)) == NULL)

View file

@ -31,7 +31,8 @@
#include "xml.h" #include "xml.h"
__dead void usage(void); __dead void usage(void);
static struct system_config *agent_init(void); static struct system_config *agent_init(const char *, int);
static int agent_configure(struct system_config *);
static void agent_free(struct system_config *); static void agent_free(struct system_config *);
static int agent_pf(struct system_config *, int); static int agent_pf(struct system_config *, int);
static void agent_unconfigure(void); static void agent_unconfigure(void);
@ -288,13 +289,15 @@ get_word(u_int8_t *ptr, size_t len)
} }
static struct system_config * static struct system_config *
agent_init(void) agent_init(const char *ifname, int dryrun)
{ {
struct system_config *sc; struct system_config *sc;
if ((sc = calloc(1, sizeof(*sc))) == NULL) if ((sc = calloc(1, sizeof(*sc))) == NULL)
return (NULL); return (NULL);
sc->sc_interface = ifname;
sc->sc_dryrun = dryrun ? 1 : 0;
TAILQ_INIT(&sc->sc_pubkeys); TAILQ_INIT(&sc->sc_pubkeys);
if ((sc->sc_nullfd = open("/dev/null", O_RDWR)) == -1) { if ((sc->sc_nullfd = open("/dev/null", O_RDWR)) == -1) {
@ -302,6 +305,14 @@ agent_init(void)
return (NULL); return (NULL);
} }
if (sc->sc_dryrun)
return (sc);
if (agent_pf(sc, 1) != 0)
fatalx("pf");
if (http_init() == -1)
fatalx("http_init");
return (sc); return (sc);
} }
@ -460,8 +471,8 @@ agent_pf(struct system_config *sc, int open)
return (ret); return (ret);
} }
int static int
agent_configure(struct system_config *sc, int noaction) agent_configure(struct system_config *sc)
{ {
struct ssh_pubkey *ssh; struct ssh_pubkey *ssh;
char *str1, *str2; char *str1, *str2;
@ -476,25 +487,21 @@ agent_configure(struct system_config *sc, int noaction)
} }
free(str1); free(str1);
if (!noaction && if (fileout(sc->sc_instance, "w", "/var/db/cloud-instance") != 0)
fileout(sc->sc_instance, "w", "/var/db/cloud-instance") != 0)
log_warnx("instance failed"); log_warnx("instance failed");
/* hostname */ /* hostname */
log_debug("%s: hostname %s", __func__, sc->sc_hostname); log_debug("%s: hostname %s", __func__, sc->sc_hostname);
if (!noaction && if (fileout(sc->sc_hostname, "w", "/etc/myname") != 0)
fileout(sc->sc_hostname, "w", "/etc/myname") != 0)
log_warnx("hostname failed"); log_warnx("hostname failed");
else else
(void)shell("hostname", sc->sc_hostname, NULL); (void)shell("hostname", sc->sc_hostname, NULL);
/* username */ /* username */
log_debug("%s: username %s", __func__, sc->sc_username); log_debug("%s: username %s", __func__, sc->sc_username);
if (!noaction && if (shell("useradd", "-L", "staff", "-G", "wheel",
shell("useradd", "-L", "staff", "-G", "wheel",
"-m", sc->sc_username, NULL) != 0) "-m", sc->sc_username, NULL) != 0)
log_warnx("username failed"); log_warnx("username failed");
if (fileout(sc->sc_username, "w", "/root/.forward") != 0) if (fileout(sc->sc_username, "w", "/root/.forward") != 0)
log_warnx(".forward failed"); log_warnx(".forward failed");
@ -506,8 +513,7 @@ agent_configure(struct system_config *sc, int noaction)
"permit keepenv nopass root\n", sc->sc_username) == -1) "permit keepenv nopass root\n", sc->sc_username) == -1)
str2 = NULL; str2 = NULL;
} else { } else {
if (!noaction && if (shell("usermod", "-p", sc->sc_password,
shell("usermod", "-p", sc->sc_password,
sc->sc_username, NULL) != 0) sc->sc_username, NULL) != 0)
log_warnx("password failed"); log_warnx("password failed");
@ -540,8 +546,7 @@ agent_configure(struct system_config *sc, int noaction)
if (ssh->ssh_keyval == NULL) if (ssh->ssh_keyval == NULL)
continue; continue;
log_debug("%s: key %s", __func__, ssh->ssh_keyval); log_debug("%s: key %s", __func__, ssh->ssh_keyval);
if (!noaction && if (fileout(ssh->ssh_keyval, "a",
fileout(ssh->ssh_keyval, "a",
"/home/%s/.ssh/authorized_keys", "/home/%s/.ssh/authorized_keys",
sc->sc_username) != 0) sc->sc_username) != 0)
log_warnx("public key failed"); log_warnx("public key failed");
@ -552,7 +557,7 @@ agent_configure(struct system_config *sc, int noaction)
} }
log_debug("%s: %s", __func__, "/etc/rc.firsttime"); log_debug("%s: %s", __func__, "/etc/rc.firsttime");
if (!noaction && fileout("logger -s -t cloud-agent <<EOF\n" if (fileout("logger -s -t cloud-agent <<EOF\n"
"#############################################################\n" "#############################################################\n"
"-----BEGIN SSH HOST KEY FINGERPRINTS-----\n" "-----BEGIN SSH HOST KEY FINGERPRINTS-----\n"
"$(for _f in /etc/ssh/ssh_host_*_key.pub;" "$(for _f in /etc/ssh/ssh_host_*_key.pub;"
@ -608,13 +613,13 @@ int
main(int argc, char *const *argv) main(int argc, char *const *argv)
{ {
struct system_config *sc; struct system_config *sc;
int verbose = 0, noaction = 0, unconfigure = 0; int verbose = 0, dryrun = 0, unconfigure = 0;
int ch, ret; int ch, ret;
while ((ch = getopt(argc, argv, "nvu")) != -1) { while ((ch = getopt(argc, argv, "nvu")) != -1) {
switch (ch) { switch (ch) {
case 'n': case 'n':
noaction = 1; dryrun = 1;
break; break;
case 'v': case 'v':
verbose += 2; verbose += 2;
@ -645,17 +650,9 @@ main(int argc, char *const *argv)
if (pledge("stdio cpath rpath wpath exec proc dns inet", NULL) == -1) if (pledge("stdio cpath rpath wpath exec proc dns inet", NULL) == -1)
fatal("pledge"); fatal("pledge");
if ((sc = agent_init()) == NULL) if ((sc = agent_init(argv[0], dryrun)) == NULL)
fatalx("agent"); fatalx("agent");
sc->sc_interface = argv[0];
if (agent_pf(sc, 1) != 0)
fatalx("pf");
if (http_init() == -1)
fatalx("http_init");
/* /*
* XXX Detect cloud with help from hostctl and sysctl * XXX Detect cloud with help from hostctl and sysctl
* XXX in addition to the interface name. * XXX in addition to the interface name.
@ -669,13 +666,18 @@ main(int argc, char *const *argv)
else else
fatal("unsupported cloud interface %s", sc->sc_interface); fatal("unsupported cloud interface %s", sc->sc_interface);
if (sc->sc_dryrun) {
agent_free(sc);
return (0);
}
if (agent_pf(sc, 0) != 0) if (agent_pf(sc, 0) != 0)
fatalx("pf"); fatalx("pf");
if (pledge("stdio cpath rpath wpath exec proc", NULL) == -1) if (pledge("stdio cpath rpath wpath exec proc", NULL) == -1)
fatal("pledge"); fatal("pledge");
if (ret == 0 && agent_configure(sc, noaction) != 0) if (ret == 0 && agent_configure(sc) != 0)
fatal("provisioning failed"); fatal("provisioning failed");
agent_free(sc); agent_free(sc);

View file

@ -55,6 +55,7 @@ struct system_config {
struct ssh_pubkeys sc_pubkeys; struct ssh_pubkeys sc_pubkeys;
int sc_nullfd; int sc_nullfd;
int sc_dryrun;
void *sc_priv; void *sc_priv;
}; };
@ -75,7 +76,6 @@ char *get_line(u_int8_t *, size_t);
char *get_word(u_int8_t *, size_t); char *get_word(u_int8_t *, size_t);
int agent_addpubkey(struct system_config *, const char *, const char *); int agent_addpubkey(struct system_config *, const char *, const char *);
int agent_setpubkey(struct system_config *, const char *, const char *); int agent_setpubkey(struct system_config *, const char *, const char *);
int agent_configure(struct system_config *, int);
/* log.c */ /* log.c */
void log_init(int, int); void log_init(int, int);