| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | # |
| 4 | # %%NEW_CLAIMING_METHOD%% |
| 5 | |
| 6 | set -e |
| 7 | |
| 8 | warning() { |
| 9 | printf "WARNING: %s\n" "${1}" 1>&2 |
| 10 | } |
| 11 | |
| 12 | error() { |
| 13 | printf "ERROR: %s\n" "${1}" 1>&2 |
| 14 | exit "${2}" |
| 15 | } |
| 16 | |
| 17 | get_templated_value() { |
| 18 | value="$1" |
| 19 | default="$2" |
| 20 | override="$3" |
| 21 | |
| 22 | if [ -n "${override}" ]; then |
| 23 | echo "${override}" |
| 24 | elif [ -z "${value}" ]; then |
| 25 | error "Expected templated value not present" |
| 26 | elif (echo "${value}" | grep -q '@'); then |
| 27 | echo "${default}" |
| 28 | else |
| 29 | echo "${value}" |
| 30 | fi |
| 31 | } |
| 32 | |
| 33 | config_dir="$(get_templated_value "@configdir_POST@" "/etc/netdata" "${NETDATA_CLAIM_CONFIG_DIR}")" |
| 34 | claim_config="${config_dir}/claim.conf" |
| 35 | netdatacli="$(get_templated_value "@sbindir_POST@/netdatacli" "$(command -v netdatacli 2>/dev/null)" "${NETDATA_CLAIM_NETDATACLI_PATH}")" |
| 36 | netdata_group="$(get_templated_value "@netdata_group_POST@" "netdata" "${NETDATA_CLAIM_CONFIG_GROUP}")" |
| 37 | |
| 38 | write_config() { |
| 39 | config="[global]" |
| 40 | config="${config}\n url = ${NETDATA_CLAIM_URL}" |
| 41 | config="${config}\n token = ${NETDATA_CLAIM_TOKEN}" |
| 42 | if [ -n "${NETDATA_CLAIM_ROOMS}" ]; then |
| 43 | config="${config}\n rooms = ${NETDATA_CLAIM_ROOMS}" |
| 44 | fi |
| 45 | if [ -n "${NETDATA_CLAIM_PROXY}" ]; then |
| 46 | config="${config}\n proxy = ${NETDATA_CLAIM_PROXY}" |
| 47 | fi |
| 48 | if [ -n "${NETDATA_CLAIM_INSECURE}" ]; then |
| 49 | config="${config}\n insecure = ${NETDATA_CLAIM_INSECURE}" |
| 50 | fi |
| 51 | |
| 52 | touch "${claim_config}.tmp" |
| 53 | chmod 0660 "${claim_config}.tmp" |
| 54 | chown "root:${netdata_group}" "${claim_config}.tmp" |
| 55 | printf '%b\n' "${config}" > "${claim_config}.tmp" |
| 56 | chmod 0640 "${claim_config}.tmp" |
| 57 | mv -f "${claim_config}.tmp" "${claim_config}" |
| 58 | } |
| 59 | |
| 60 | reload_claiming() { |
| 61 | if [ -z "${NORELOAD}" ]; then |
| 62 | "${netdatacli}" reload-claiming-state |
| 63 | fi |
| 64 | } |
| 65 | |
| 66 | parse_args() { |
| 67 | while [ -n "${1}" ]; do |
| 68 | case "${1}" in |
| 69 | --claim-token) NETDATA_CLAIM_TOKEN="${2}"; shift 1 ;; |
| 70 | -token=*) NETDATA_CLAIM_TOKEN="$(echo "${1}" | sed 's/^-token=//')" ;; |
| 71 | --claim-rooms) NETDATA_CLAIM_ROOMS="${2}"; shift 1 ;; |
| 72 | -rooms=*) NETDATA_CLAIM_ROOMS="$(echo "${1}" | sed 's/^-rooms=//')" ;; |
| 73 | --claim-url) NETDATA_CLAIM_URL="${2}"; shift 1 ;; |
| 74 | -url=*) NETDATA_CLAIM_URL="$(echo "${1}" | sed 's/^-url=//')" ;; |
| 75 | --claim-proxy) NETDATA_CLAIM_PROXY="${2}"; shift 1 ;; |
| 76 | -proxy=*) NETDATA_CLAIM_PROXY="$(echo "${1}" | sed 's/-proxy=//')" ;; |
| 77 | -noproxy|--noproxy) NETDATA_CLAIM_PROXY="none" ;; |
| 78 | -noreload|--noreload) NORELOAD=1 ;; |
| 79 | -insecure|--insecure) NETDATA_CLAIM_INSECURE=yes ;; |
| 80 | -verbose) true ;; |
| 81 | -daemon-not-running) true ;; |
| 82 | -id=*) warning "-id option is no longer supported. Remove the node ID file instead." ;; |
| 83 | -hostname=*) warning "-hostname option is no longer supported. Update the main netdata configuration manually instead." ;; |
| 84 | -user=*) warning "-user option is no longer supported." ;; |
| 85 | *) warning "Ignoring unrecognized option ${1}";; |
| 86 | esac |
| 87 | |
| 88 | shift 1 |
| 89 | done |
| 90 | |
| 91 | if [ -z "${NETDATA_CLAIM_TOKEN}" ]; then |
| 92 | error "Claim token must be specified" 1 |
| 93 | fi |
| 94 | |
| 95 | if [ -z "${NETDATA_CLAIM_URL}" ]; then |
| 96 | NETDATA_CLAIM_URL="https://app.netdata.cloud/" |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | |
| 101 | [ -z "$EUID" ] && EUID="$(id -u)" |
| 102 | if [ "${EUID}" != "0" ] && [ ! -w "${config_dir}" ]; then |
| 103 | error "Script must be run by a user with write access to ${config_dir}." 32 |
| 104 | fi |
| 105 | |
| 106 | warning "This script is deprecated and will be officially unsupported in the near future. Please either use the kickstart script with the appropriate '--claim-*' options, or directly write out the claiming configuration instead." |
| 107 | parse_args "${@}" |
| 108 | write_config |
| 109 | reload_claiming |