Add wait_for_network: <boolean>.

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=<hostname>` configure flag. You shouldn't put an
IP address in here, since that fully disables any network testing.
This commit is contained in:
Auke Kok
2019-10-29 10:25:37 -07:00
parent 4364c943e1
commit 9644cbdc5d
10 changed files with 169 additions and 4 deletions

View File

@@ -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 \

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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;

View File

@@ -0,0 +1,107 @@
/***
Copyright © 2019 Intel Corporation
Author: Auke-jan H. Kok <auke-jan.h.kok@intel.com>
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 <http://www.gnu.org/licenses/>.
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 <stdbool.h>
#include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <glib.h>
#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
};

View File

@@ -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:
-

View File

@@ -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

View File

@@ -42,6 +42,8 @@
#include "userdata.h"
extern void wait_for_network(void);
START_TEST(test_userdata_process_file)
{
int fd_script;