Skip to content

Commit

Permalink
Add wait_for_network: <boolean>.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ahkok committed Oct 29, 2019
1 parent 4364c94 commit 9644cbd
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 22 additions & 2 deletions docs/cloud-config.5
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
.
Expand Down Expand Up @@ -100,6 +101,9 @@ Option |Type |Required |Function
.
.fi
.
.P
This option implies the \fBwait_for_network\fR option\.
.
.SS "packages"
.
.nf
Expand All @@ -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
Expand Down Expand Up @@ -213,7 +220,7 @@ permissions|octal |no |Octal value describing the file permissions
.
.fi
.
.SH "envar"
.SS "envar"
.
.nf

Expand All @@ -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
Expand Down
22 changes: 21 additions & 1 deletion docs/cloud-config.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +101,9 @@ Option |Type |Required |Function
| | |update is performed
```

This option implies the `wait_for_network` option.


### packages

```
Expand All @@ -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

```
Expand Down Expand Up @@ -199,7 +205,7 @@ permissions|octal |no |Octal value describing the file permissions
| | |`umask`
```

## envar
### envar

```
Option |Type |Required |Function
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/ccmodules/package_upgrade.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/ccmodules/packages.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "cloud_config.h"
#include "lib.h"

extern void wait_for_network(void);

#define MOD "packages: "
#define COMMAND_SIZE 256

Expand All @@ -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;
Expand Down
107 changes: 107 additions & 0 deletions src/ccmodules/wait-for-network.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/***
Copyright © 2019 Intel Corporation
Author: Auke-jan H. Kok <[email protected]>
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
};
2 changes: 2 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -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:
-
Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/userdata_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

#include "userdata.h"

extern void wait_for_network(void);

START_TEST(test_userdata_process_file)
{
int fd_script;
Expand Down

0 comments on commit 9644cbd

Please sign in to comment.