From 9644cbdc5d35919d8e9e54f272a2cfd17ff4e5c2 Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Tue, 29 Oct 2019 10:25:37 -0700 Subject: [PATCH] Add `wait_for_network: `. This option allows users to suspend execution of the cloud-init provisioning until a valid network is detected. This detection is currently done through requesting a DNS lookup for one of the Clear Linux NTP service IP addresses (this is a RR record, we don't actually look at the result). This lookup is not infinite. After 5 minutes, the wait exits no matter what even if there is no network detected. The option can be provided manually. `packages` and `package_upgrade` options *imply* this option, but one can explicitly disable the wait by providing it early in the cloud-config file with a value of `false`. The wait routing is active - it will retry relatively quickly to detect an active connection. Any failure will result in another retry. In a fully private network without public DNS, this will not work. The DNS hostname used for testing can be manipulated through the `-with-dnstestaddr=` configure flag. You shouldn't put an IP address in here, since that fully disables any network testing. --- Makefile.am | 1 + configure.ac | 6 ++ docs/cloud-config.5 | 24 ++++++- docs/cloud-config.5.md | 22 ++++++- src/ccmodules/package_upgrade.c | 3 + src/ccmodules/packages.c | 3 + src/ccmodules/wait-for-network.c | 107 +++++++++++++++++++++++++++++++ test.yaml | 2 + tests/Makefile.am | 3 +- tests/userdata_test.c | 2 + 10 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 src/ccmodules/wait-for-network.c diff --git a/Makefile.am b/Makefile.am index aa02458..2a4b961 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,6 +45,7 @@ ucd_SOURCES = \ src/ccmodules/runcmd.c \ src/ccmodules/envar.c \ src/ccmodules/fbootcmd.c \ + src/ccmodules/wait-for-network.c \ src/datasources.h \ src/datasources/openstack.c \ src/datasources/openstack.h \ diff --git a/configure.ac b/configure.ac index cae324c..903ecfe 100644 --- a/configure.ac +++ b/configure.ac @@ -159,6 +159,12 @@ else AC_MSG_ERROR([Please provide a valid package manager (swupd, yum, dnf, tdnf, or apt).]) fi +AC_ARG_WITH([dnstestaddr],[AC_HELP_STRING([--with-dnstestaddr=hostname], + [Default DNS test host name (0.clearlinux.pool.ntp.org)])], [dnstestaddr=${withval}], + [dnstestaddr=0.clearlinux.pool.ntp.org]) +AC_SUBST([DNSTESTADDR], ["$dnstestaddr"], [Default DNS test host name]) +AC_DEFINE_UNQUOTED([DNSTESTADDR], ["$DNSTESTADDR"], [Default DNS test host name]) + # Checks for library functions. AC_OUTPUT diff --git a/docs/cloud-config.5 b/docs/cloud-config.5 index db934b0..f22bde4 100644 --- a/docs/cloud-config.5 +++ b/docs/cloud-config.5 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "CLOUD\-CONFIG" "5" "November 2018" "" "" +.TH "CLOUD\-CONFIG" "5" "October 2019" "" "" . .SH "NAME" \fBcloud\-config\fR \- User data format used by \fBucd(1)\fR @@ -47,6 +47,7 @@ write_files |Write content to arbitrary files |yes |yes hostname |Define the system\'s hostname |yes |yes envar |Set environment variables |no |no bootcmd |Execute system commands on first boot|no |no +wait_for_network |Halt execution until network is up |no |no . .fi . @@ -100,6 +101,9 @@ Option |Type |Required |Function . .fi . +.P +This option implies the \fBwait_for_network\fR option\. +. .SS "packages" . .nf @@ -110,6 +114,9 @@ name |string[] |no |Enables installation of software bundles . .fi . +.P +This option implies the \fBwait_for_network\fR option\. +. .SS "runcmd" . .nf @@ -213,7 +220,7 @@ permissions|octal |no |Octal value describing the file permissions . .fi . -.SH "envar" +.SS "envar" . .nf @@ -234,6 +241,19 @@ Option |Type |Required |Function . .fi . +.SS "wait_for_network" +. +.nf + +Option |Type |Required |Function +\-\-\-\-\-\-\-\-\-\-|\-\-\-\-\-\-\-\-\-|\-\-\-\-\-\-\-\-\-\-\-\-|\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- +* |boolean |no |Enable to force waiting for a functional network\. +. +.fi +. +.P +This option makes the program wait until an active network is detected\. To prevent waiting, ensure that this option is disabled explicitly disabled, before other options are used\. This may be needed when using the \fBpackages\fR or \fBpackage_upgrade\fR options, which imply that this option is used\. +. .SH "COPYRIGHT" . .IP "\(bu" 4 diff --git a/docs/cloud-config.5.md b/docs/cloud-config.5.md index 049b5a5..10587b6 100644 --- a/docs/cloud-config.5.md +++ b/docs/cloud-config.5.md @@ -54,6 +54,7 @@ write_files |Write content to arbitrary files |yes |yes hostname |Define the system's hostname |yes |yes envar |Set environment variables |no |no bootcmd |Execute system commands on first boot|no |no +wait_for_network |Halt execution until network is up |no |no ``` ## OPTIONS @@ -100,6 +101,9 @@ Option |Type |Required |Function | | |update is performed ``` +This option implies the `wait_for_network` option. + + ### packages ``` @@ -108,6 +112,8 @@ Option |Type |Required |Function name |string[] |no |Enables installation of software bundles ``` +This option implies the `wait_for_network` option. + ### runcmd ``` @@ -199,7 +205,7 @@ permissions|octal |no |Octal value describing the file permissions | | |`umask` ``` -## envar +### envar ``` Option |Type |Required |Function @@ -216,6 +222,20 @@ Option |Type |Required |Function * |string[] |no |Similar to runcmd but bootcmd will run only on first boot ``` +### wait_for_network + +``` +Option |Type |Required |Function +----------|---------|------------|----------------------------------- +* |boolean |no |Enable to force waiting for a functional network. +``` + +This option makes the program wait until an active network is detected. +To prevent waiting, ensure that this option is disabled explicitly +disabled, before other options are used. This may be needed when +using the `packages` or `package_upgrade` options, which imply that +this option is used. + ## COPYRIGHT * Copyright (C) 2017 Intel Corporation, License: CC-BY-SA-3.0 diff --git a/src/ccmodules/package_upgrade.c b/src/ccmodules/package_upgrade.c index 7d8863e..f979d0e 100644 --- a/src/ccmodules/package_upgrade.c +++ b/src/ccmodules/package_upgrade.c @@ -41,6 +41,8 @@ #include "cloud_config.h" #include "lib.h" +extern void wait_for_network(void); + #define MOD "package_upgrade: " void package_upgrade_handler(GNode *node) { @@ -57,6 +59,7 @@ void package_upgrade_handler(GNode *node) { } if (do_upgrade) { LOG(MOD "Performing system software update.\n"); + wait_for_network(); #if defined(PACKAGE_MANAGER_SWUPD) exec_task("/usr/bin/swupd update"); #elif defined(PACKAGE_MANAGER_YUM) diff --git a/src/ccmodules/packages.c b/src/ccmodules/packages.c index 9435954..02f03f3 100644 --- a/src/ccmodules/packages.c +++ b/src/ccmodules/packages.c @@ -41,6 +41,8 @@ #include "cloud_config.h" #include "lib.h" +extern void wait_for_network(void); + #define MOD "packages: " #define COMMAND_SIZE 256 @@ -59,6 +61,7 @@ static gboolean packages_item(GNode* node, __unused__ gpointer data) { "/usr/bin/tdnf --assumeyes install %s", #endif (char*)node->data); + wait_for_network(); LOG(MOD "Installing %s..\n", (char*)node->data); exec_task(command); return false; diff --git a/src/ccmodules/wait-for-network.c b/src/ccmodules/wait-for-network.c new file mode 100644 index 0000000..871dfb7 --- /dev/null +++ b/src/ccmodules/wait-for-network.c @@ -0,0 +1,107 @@ +/*** + Copyright © 2019 Intel Corporation + + Author: Auke-jan H. Kok + + This file is part of micro-config-drive. + + micro-config-drive is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + micro-config-drive is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with micro-config-drive. If not, see . + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. +***/ + +#include +#include +#include +#include + +#include + +#include "handlers.h" +#include "cloud_config.h" +#include "lib.h" + +#define MOD "package_upgrade: " + +static int do_network_wait = -1; +// -1: default: unset +// 0: don't wait +// 1: wait +// 2: wait already happened + +void wait_for_network(void) { + if (do_network_wait == 1) { + struct hostent *he = NULL; + useconds_t slept = 0; + useconds_t times = 0; + + // don't re-enter + do_network_wait = 2; + + memset(he, 0, sizeof(struct hostent)); + LOG(MOD "Waiting for an active network connection.\n"); + for (;;) { + he = gethostbyname(DNSTESTADDR); + if (he) { + LOG(MOD "Network appears active, waiting completed.\n"); + break; + } + times = times < 10 ? times + 1 : times; + slept += times; + if (slept > 3000) { + LOG(MOD "Waited for network for 5 minutes, no answer - giving up.\n"); + break; + } + usleep(times * 100000); + } + } +} + +void wait_for_network_handler(GNode *node) { + bool do_wait; + + LOG(MOD "Wait System Software Update Handler running...\n"); + GNode* val = g_node_first_child(node); + if (!val) { + LOG(MOD "Corrupt userdata!\n"); + return; + } + if (!cloud_config_bool(val, &do_wait)) { + return; + } + + if (do_wait) { + do_network_wait = 1; + wait_for_network(); + } else { + do_network_wait = 0; + LOG(MOD "Disabling network wait.\n"); + } +} + +struct cc_module_handler_struct wait_for_network_cc_module = { + .name = "wait_for_network", + .handler = &wait_for_network_handler +}; diff --git a/test.yaml b/test.yaml index 8877029..96349f5 100644 --- a/test.yaml +++ b/test.yaml @@ -1,6 +1,8 @@ #cloud-config package_upgrade: true package_upgrade: false +wait_for_network: false +wait_for_network: true apt_upgrade: false write_files: - diff --git a/tests/Makefile.am b/tests/Makefile.am index 4e36bc3..a49c8c9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -31,7 +31,8 @@ libtest_la_SOURCES = \ ../src/ccmodules/ssh_authorized_keys.c \ ../src/ccmodules/users.c \ ../src/ccmodules/write_files.c \ - ../src/ccmodules/fbootcmd.c + ../src/ccmodules/fbootcmd.c \ + ../src/ccmodules/wait-for-network.c if DEBUG libtest_la_SOURCES += ../src/debug.c diff --git a/tests/userdata_test.c b/tests/userdata_test.c index 5bbedf7..9909629 100644 --- a/tests/userdata_test.c +++ b/tests/userdata_test.c @@ -42,6 +42,8 @@ #include "userdata.h" +extern void wait_for_network(void); + START_TEST(test_userdata_process_file) { int fd_script;