summaryrefslogtreecommitdiff
path: root/udev/cloud-init-wait
blob: 345333f9532fd6d16b6b8ac56dc50502890cc3dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh

CI_NET_READY="/run/cloud-init/network-config-ready"
LOG="/run/cloud-init/${0##*/}.log"
LOG_INIT=0
DEBUG=0

find_name() {
    local match="" name="" none="_UNSET" pound="#"
    while read match name; do
        [ "${match#${pound}}" = "$match" ] || continue
        case "$match" in
            ID_NET_NAME=${ID_NET_NAME:-$none}) _RET="$name"; return 0;;
            ID_NET_NAME_PATH=${ID_NET_NAME_PATH:-$none}) _RET="$name"; return 0;;
            MAC_ADDRESS=${MAC_ADDRESS:-$none}) _RET="$name"; return 0;;
            INTERFACE=${INTERFACE:-$none}) _RET="$name"; return 0;;
        esac
     done
     return 0
}

block_until_ready() {
    local fname="$1"
    local naplen="$2" max="$3" n=0
    while ! [ -f "$fname" ]; do
        n=$(($n+1))
        [ "$n" -ge "$max" ] && return 1
        sleep $naplen
    done
}

log() {
    [ -n "${LOG}" ] || return
    [ "${DEBUG:-0}" = "0" ] && return

    if [ $LOG_INIT = 0 ]; then
        if [ -d "${LOG%/*}" ] || mkdir -p "${LOG%/*}"; then
            LOG_INIT=1
        else
            echo "${0##*/}: WARN: log init to ${LOG%/*}" 1>&2
            return
        fi
    elif [ "$LOG_INIT" = "-1" ]; then
        return
    fi
    local info="$$ $INTERFACE"
    if [ "$DEBUG" -gt 1 ]; then
       local up idle
       read up idle < /proc/uptime
       info="$$ $INTERFACE $up"
    fi
    echo "[$info]" "$@" >> "$LOG"
}

main() {
    local name="" readyfile="$CI_NET_READY"
    local info="INTERFACE=${INTERFACE} ID_NET_NAME=${ID_NET_NAME}"
    info="$info ID_NET_NAME_PATH=${ID_NET_NAME_PATH}"
    info="$info MAC_ADDRESS=${MAC_ADDRESS}"
    log "$info"

    ## Check to see if cloud-init.target is set.  If cloud-init is 
    ## disabled we do not want to do anything.
    if [ ! -f "/run/cloud-init/enabled" ]; then
        log "cloud-init disabled"
        return 0
    fi

    block_until_ready "$readyfile" .1 600 ||
       { log "failed waiting for ready on $INTERFACE"; return 1; }

    #find_name < "$CI_NET_RULES" && name="$_RET" ||
    #   { log "failed to find match for $INTERFACE"; return 0; }

    log "net config ready"
    #[ -z "$name" ] || echo "CLOUDINIT_NET_NAME=$name"
}

main "$@"
exit

# vi: ts=4 expandtab