Skip to content

Commit

Permalink
Merge pull request #58 from bwarden/add-hostname-support
Browse files Browse the repository at this point in the history
Add hostname support
Fixes #57
  • Loading branch information
bwarden committed Nov 4, 2022
2 parents 2de5b52 + f9541b6 commit befb158
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ micro-config-drive-*/
tests/Makefile
tests/.libs/
tests/*_test
tests/test_user_data
libtool
ltmain.sh
m4/
Expand Down
78 changes: 74 additions & 4 deletions src/ucd-data-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct cloud_struct {
char *ip;
uint16_t port;
char *request_sshkey_path;
char *request_hostname_path;
char *request_userdata_path;
char *cloud_config_header;
};
Expand All @@ -74,6 +75,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
"169.254.169.254",
80,
"/latest/meta-data/public-keys/0/openssh-key",
"/latest/meta-data/hostname",
"/latest/user-data",
"#cloud-config\n" \
"users:\n" \
Expand All @@ -87,6 +89,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
80,
"/opc/v1/instance/metadata/ssh_authorized_keys",
NULL,
NULL,
"#cloud-config\n" \
"users:\n" \
" - name: opc\n" \
Expand All @@ -100,6 +103,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
80,
"/latest/meta-data/public-keys/0/openssh-key",
NULL,
NULL,
"#cloud-config\n" \
"users:\n" \
" - name: tencent\n" \
Expand All @@ -112,6 +116,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
80,
"/latest/meta-data/public-keys/0/openssh-key",
NULL,
NULL,
"#cloud-config\n" \
"users:\n" \
" - name: aliyun\n" \
Expand All @@ -123,6 +128,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
"metadata.platformequinix.com",
80,
"/2009-04-04/meta-data/public-keys",
"/2009-04-04/meta-data/hostname",
"/userdata",
"#cloud-config\n" \
"users:\n" \
Expand All @@ -135,6 +141,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
"127.0.0.254",
8123,
"/public-keys",
"/hostname",
"/user-data",
"#cloud-config\n" \
"users:\n" \
Expand Down Expand Up @@ -226,7 +233,7 @@ static int write_lines(int out, FILE *f, size_t cl, const char *prefix)
int main(int argc, char *argv[]) {
int conf = -1;
int sockfd;
char *request, *request2;
char *request, *request2, *request3;
char *outpath;
int n = 0;

Expand Down Expand Up @@ -407,17 +414,80 @@ int main(int argc, char *argv[]) {
}
}

/* next, get hostname */
if (!config[conf].request_hostname_path)
goto user_data;

if (asprintf(&request2, "GET %s HTTP/1.1\r\nhost: %s \r\nConnection: keep-alive\r\n\r\n",
config[conf].request_hostname_path, config[conf].ip) < 0) {
FAIL("asprintf");
}
len = strlen(request2);

if (write(sockfd, request2, len) < (ssize_t)len) {
close(sockfd);
FAIL("write()");
}

f = fdopen(sockfd, "r");
if (!f) {
close(sockfd);
FAIL("fdopen()");
}

/* parse/discard the header and body */
result = parse_headers(f, &cl);
if (result == 0) {
/* error - exit */
fclose(f);
close(out);
FAIL("parse_headers()");
}

/* don't write part #2 if 404 or some non-error */
if ((result != 2) && (write_lines(out, f, cl, "hostname: ") != 0)) {
close(out);
fclose(f);
unlink(outpath);
FAIL("write_lines()");
}

/* cleanup */
fclose(f);

/* reopen socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
FAIL("socket()");
}

n = 0;
for (;;) {
int r = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
if (r == 0) {
break;
}
if ((errno != EAGAIN) && (errno != ENETUNREACH) && (errno != ETIMEDOUT)) {
FAIL("connect()");
}
nanosleep(&ts, NULL);
if (++n > 200) { /* 10 secs */
FAIL("timeout in connect()");
}
}

user_data:
/* next, get user-data */
if (!config[conf].request_userdata_path)
goto finish;

if (asprintf(&request2, "GET %s HTTP/1.1\r\nhost: %s \r\nConnection: keep-alive\r\n\r\n",
if (asprintf(&request3, "GET %s HTTP/1.1\r\nhost: %s \r\nConnection: keep-alive\r\n\r\n",
config[conf].request_userdata_path, config[conf].ip) < 0) {
FAIL("asprintf");
}
len = strlen(request2);
len = strlen(request3);

if (write(sockfd, request2, len) < (ssize_t)len) {
if (write(sockfd, request3, len) < (ssize_t)len) {
close(sockfd);
FAIL("write()");
}
Expand Down
1 change: 1 addition & 0 deletions tests/fetch_data/expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ssh_authorized_keys:
- SSH_TEST_KEY_STRING_2
- SSH_TEST_KEY_STRING_3

hostname: myhostname
#cloud-config

users:
Expand Down
1 change: 1 addition & 0 deletions tests/fetch_data/hostname
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myhostname
2 changes: 1 addition & 1 deletion tests/fetch_test
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sleep 2
../ucd-data-fetch test

# Compare what we got/generated with what we expect
cmp -b fetch_data/expected test-user-data
diff -y fetch_data/expected test-user-data

# Cleanup the test data file
rm test-user-data

0 comments on commit befb158

Please sign in to comment.