summaryrefslogtreecommitdiffstats
path: root/system/edit-config
blob: 3944810d23a0572b4c6e4fa6f47ccf36460e22c8 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env sh

# shellcheck disable=SC1091
[ -f /etc/profile ] && . /etc/profile

set -e

script_dir="$(CDPATH="" cd -- "$(dirname -- "$0")" && pwd -P)"

usage() {
  check_directories
  cat <<EOF
USAGE:
  ${0} [options] FILENAME

  Copy and edit the stock config file named: FILENAME
  if FILENAME is already copied, it will be edited as-is.

  Stock config files at: '${NETDATA_STOCK_CONFIG_DIR}'
  User  config files at: '${NETDATA_USER_CONFIG_DIR}'

  The editor to use can be specified either by setting the EDITOR
  environment variable, or by using the --editor option.

  The file to edit can also be specified using the --file option.

  For a list of known config files, run '${0} --list'
EOF
  exit 0
}

error() {
  echo >&2 "ERROR: ${1}"
}

abspath() {
  if [ -d "${1}/" ]; then
    echo "$(cd "${1}" && /usr/bin/env PWD= pwd -P)/"
  elif [ -f "${1}" ]; then
    echo "$(cd "$(dirname "${1}")" && /usr/bin/env PWD= pwd -P)/$(basename "${1}")"
  elif echo "${1}" | grep -q '/'; then
    if echo "${1}" | grep -q '^/'; then
      mkdir -p "$(dirname "${1}")"
      echo "$(cd "$(dirname "${1}")" && /usr/bin/env PWD= pwd -P)/$(basename "${1}")"
    else
      mkdir -p "${script_dir}/$(dirname "${1}")"
      echo "${script_dir}/${1}"
    fi
  else
    echo "${script_dir}/${1}"
  fi
}

is_prefix() {
  echo "${2}" | grep -qE "^${1}"
  return $?
}

check_directories() {
  if [ -f "${script_dir}/.container-hostname" ]; then
    NETDATA_USER_CONFIG_DIR="${script_dir}"
    NETDATA_STOCK_CONFIG_DIR="/usr/lib/netdata/conf.d"
    return
  fi

  if [ -e "${script_dir}/.environment" ]; then
    OLDPATH="${PATH}"
    # shellcheck disable=SC1091
    . "${script_dir}/.environment"
    PATH="${OLDPATH}"
  fi

  if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}/usr/lib/netdata/conf.d" ]; then
    stock_dir="${NETDATA_PREFIX}/usr/lib/netdata/conf.d"
  elif [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}/lib/netdata/conf.d" ]; then
    stock_dir="${NETDATA_PREFIX}/lib/netdata/conf.d"
  elif [ -d "${script_dir}/../../usr/lib/netdata/conf.d" ]; then
    stock_dir="${script_dir}/../../usr/lib/netdata/conf.d"
  elif [ -d "${script_dir}/../../lib/netdata/conf.d" ]; then
    stock_dir="${script_dir}/../../lib/netdata/conf.d"
  elif [ -d "/usr/lib/netdata/conf.d" ]; then
    stock_dir="/usr/lib/netdata/conf.d"
  fi

  [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="${script_dir}"
  [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="${stock_dir}"

  if [ -z "${NETDATA_STOCK_CONFIG_DIR}" ]; then
    error "Unable to find stock config directory."
    exit 1
  fi
}

check_editor() {
  if [ -z "${editor}" ]; then
    if [ -n "${EDITOR}" ] && command -v "${EDITOR}" >/dev/null 2>&1; then
      editor="${EDITOR}"
    elif command -v editor >/dev/null 2>&1; then
      editor="editor"
    elif command -v vi >/dev/null 2>&1; then
      editor="vi"
    else
      error "Unable to find a usable editor, tried \${EDITOR} (${EDITOR}), editor, and vi."
      exit 1
    fi
  elif ! command -v "${editor}" >/dev/null 2>&1; then
    error "Unable to locate user specified editor ${editor}, is it in your PATH?"
    exit 1
  fi
}

running_in_container() {
  [ -e /.dockerenv ] && return 0
  [ -e /.dockerinit ] && return 0
  [ -e /run/.containerenv ] && return 0
  [ -r /proc/1/environ ] && tr '\000' '\n' </proc/1/environ | grep -Eiq 'container=' && return 0
  grep -qF -e /docker/ -e /libpod- /proc/self/cgroup 2>/dev/null && return 0
  return 1
}

get_docker_command() {
  if [ -x "${docker}" ]; then
    return 0
  elif command -v docker >/dev/null 2>&1; then
    docker="$(command -v docker)"
  elif command -v podman >/dev/null 2>&1; then
    docker="$(command -v podman)"
  else
    error "Unable to find a usable container tool stack. I support Docker and Podman."
    exit 1
  fi
}

run_in_container() {
  ${docker} exec "${1}" /bin/sh -c "${2}" || return 1
  return 0
}

check_for_container() {
  get_docker_command
  ${docker} container inspect "${1}" >/dev/null 2>&1 || return 1
  run_in_container "${1}" "[ -d \"${NETDATA_STOCK_CONFIG_DIR}\" ]" >/dev/null 2>&1 || return 1
  return 0
}

handle_container() {
  if running_in_container; then
    return 0
  elif [ -z "${container}" ] && [ -f "${script_dir}/.container-hostname" ]; then
    echo >&2 "Autodetected containerized Netdata instance. Attempting to autodetect container ID."
    possible_container="$(cat "${script_dir}/.container-hostname")"
    if check_for_container "${possible_container}"; then
      container="${possible_container}"
    elif check_for_container netdata; then
      container="netdata"
    else
      error "Could not autodetect container ID. It must be supplied on the command line with the --container option."
      exit 1
    fi

    echo >&2 "Found Netdata container with ID or name ${container}"
  elif [ -n "${container}" ]; then
    if ! check_for_container "${container}"; then
      error "No container with ID or name ${container} exists."
      exit 1
    fi
  fi
}

list_files() {
  check_directories
  handle_container

  if test -t && command -v tput > /dev/null 2>&1; then
    width="$(tput cols)"
  fi

  if [ -z "${container}" ]; then
    if [ "$(uname -s)" = "Linux" ]; then
      # shellcheck disable=SC2046,SC2086
      files="$(cd "${NETDATA_STOCK_CONFIG_DIR}" && ls ${width:+-C} ${width:+-w ${width}} $(find . -type f | cut -d '/' -f 2-))"
    elif [ "$(uname -s)" = "FreeBSD" ]; then
      if [ -n "${width}" ]; then
        export COLUMNS="${width}"
      fi

      # shellcheck disable=SC2046
      files="$(cd "${NETDATA_STOCK_CONFIG_DIR}" && ls ${width:+-C} $(find . -type f | cut -d '/' -f 2-))"
    else
      # shellcheck disable=SC2046
      files="$(cd "${NETDATA_STOCK_CONFIG_DIR}" && ls $(find . -type f | cut -d '/' -f 2-))"
    fi
  else
    files="$(run_in_container "${container}" "cd /usr/lib/netdata/conf.d && ls ${width:+-C} ${width:+-w ${width}} \$(find . -type f | cut -d '/' -f 2-)")"
  fi

  if [ -z "${files}" ]; then
    error "Failed to find any configuration files."
    exit 1
  fi

  cat <<EOF
The following configuration files are known to this script:

${files}
EOF
  exit 0
}

parse_args() {
  while [ -n "${1}" ]; do
    case "${1}" in
      "--help") usage ;;
      "--list") list_files ;;
      "--file")
        if [ -n "${2}" ]; then
          file="${2}"
          shift 1
        else
          error "No file specified to edit."
          exit 1
        fi
        ;;
      "--container")
        if [ -n "${2}" ]; then
          container="${2}"
          shift 1
        else
          error "No container ID or name specified with the --container option."
          exit 1
        fi
        ;;
      "--editor")
        if [ -n "${2}" ]; then
          editor="${2}"
          shift 1
        else
          error "No editor specified with the --editor option."
          exit 1
        fi
        ;;
      *)
        if [ -z "${2}" ]; then
          file="${1}"
        else
          error "Unrecognized option ${1}."
          exit 1
        fi
        ;;
    esac
    shift 1
  done

  [ -z "${file}" ] && usage

  absfile="$(abspath "${file}")"
  if ! is_prefix "${script_dir}" "${absfile}"; then
    error "${file} is not located under ${script_dir}"
    exit 1
  fi

  file="${absfile##"${script_dir}"}"
}

copy_native() {
  if [ ! -w "${NETDATA_USER_CONFIG_DIR}" ]; then
    error "Cannot write to ${NETDATA_USER_CONFIG_DIR}!"
    exit 1
  fi

  if [ -f "${NETDATA_STOCK_CONFIG_DIR}/${1}" ]; then
    echo >&2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
    cp -p "${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  else
    echo >&2 "Creating empty '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
    touch "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  fi
}

copy_container() {
  if [ ! -w "${NETDATA_USER_CONFIG_DIR}" ]; then
    error "Cannot write to ${NETDATA_USER_CONFIG_DIR}!"
    exit 1
  fi

  if run_in_container "${container}" "[ -f \"${NETDATA_STOCK_CONFIG_DIR}/${1}\" ]"; then
    echo >&2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
    ${docker} cp -a "${container}:${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  else
    echo >&2 "Creating empty '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
    touch "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  fi
}

copy() {
  if [ -f "${NETDATA_USER_CONFIG_DIR}/${1}" ]; then
    return 0
  elif [ -n "${container}" ]; then
    copy_container "${1}"
  else
    copy_native "${1}"
  fi
}

edit() {
  echo >&2 "Editing '${1}' ..."

  # check we can edit
  if [ ! -w "${1}" ]; then
    error "Cannot write to ${1}!"
    exit 1
  fi

  "${editor}" "${1}"
  exit $?
}

main() {
  parse_args "${@}"
  check_directories
  check_editor
  handle_container
  copy "${file}"
  edit "${absfile}"
}

main "${@}"