summaryrefslogtreecommitdiffstats
path: root/builder/modules.d/dnbd3-rootfs/hooks/fetch-config.sh
blob: 2b63f917614b037d4c18e67dcb7e120810571ee4 (plain) (blame)
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
83
84
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# region imports
source '/usr/lib/rebash/core.sh'
core.import exceptions
core.import logging
type emergency_shell >/dev/null 2>&1 || source /lib/dracut-lib.sh
# endregion

exceptions.try
{
logging.set_log_file "${SLX_LOG_FILE_PATH:-/var/log/openslx}"

# NOTE: "getarg" raises an exception so deactivate exceptions for now.
exceptions.deactivate
slx_server="$(getarg slxsrv=)"
slx_server_base="$(getarg slxbase=)"
exceptions.activate

# if no slxsrv was specified, use the server the kernel was
# downloaded from as fallback. Mostly for legacy PXE boot.
if [ -z "$slx_server" ]; then
    slx_server="$(getarg BOOT_IMAGE= | awk -F[/:] '{print $4}')"
fi
if [ -z "$slx_server" ]; then
    # use tftp server from ip parameter
    slx_server="$(getarg ip= | awk -F: '{print $2}' )"
fi
if [ -z "$slx_server" ]; then
    # we have a problem :/
    emergency_shell "Failed to determine SLX server to fetch config from."
fi

# build config_url
config_url="http://${slx_server#@}/${slx_server_base}/config"

# check if system's uuid was set
if [ -s "/run/system-uuid" ]; then
	uuid=$(cat "/run/system-uuid")
	if [ -n "$uuid" ]; then
		config_url="${config_url}?uuid=${uuid}"
	fi
fi

config_path="/etc/openslx.tmp"

logging.info "Downloading '$config_url'..."
slx-tools download_retry -s "$config_url" > "$config_path"

if [ ! -s "$config_path" ] ; then
    logging.warn "Downloading OpenSLX configuration file from '$config_url' failed with: $return_code"
    emergency_shell "CRITICAL: System unusable."
fi

if ! ash -n "$config_path"; then
    logging.warn "Downloaded OpenSLX configuration failed syntax check!"
    emergency_shell "CRITICAL: System unusable."
fi

# remember kcl server and base
{
	echo "SLX_KCL_SERVERS='$slx_server'"
	echo "SLX_BASE_PATH='$slx_server_base'"
    echo "# Config fetched from $config_url"
    echo "CONFIG_DOWNLOAD_TIME=$(sed -r 's/^([0-9]+)\.([0-9]+).*$/\1\2/' /proc/uptime)"
	echo '#_RCONFIG_TAG'
} > /etc/openslx

# finally copy remote config into it
cat "$config_path" >> /etc/openslx

# slxsrv overrides SLX_DNBD3_SERVERS if prefixed with @
[ "${slx_server#@}" != "${slx_server}" ] && sed -i "s/^SLX_DNBD3_SERVERS=.*/SLX_DNBD3_SERVERS='${slx_server#@}'/" "/etc/openslx"

}
exceptions.catch
{
	logging.error "$exceptions_last_traceback"
	emergency_shell "error in ${BASH_SOURCE[0]}"
}
# region vim modline
# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:
# endregion