edit-config 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC1091
  3. [ -f /etc/profile ] && . /etc/profile
  4. set -e
  5. script_dir="$(CDPATH="" cd -- "$(dirname -- "$0")" && pwd -P)"
  6. usage() {
  7. check_directories
  8. cat <<EOF
  9. USAGE:
  10. ${0} [options] FILENAME
  11. Copy and edit the stock config file named: FILENAME
  12. if FILENAME is already copied, it will be edited as-is.
  13. Stock config files at: '${NETDATA_STOCK_CONFIG_DIR}'
  14. User config files at: '${NETDATA_USER_CONFIG_DIR}'
  15. The editor to use can be specified either by setting the EDITOR
  16. environment variable, or by using the --editor option.
  17. The file to edit can also be specified using the --file option.
  18. For a list of known config files, run '${0} --list'
  19. EOF
  20. exit 0
  21. }
  22. error() {
  23. echo >&2 "ERROR: ${1}"
  24. }
  25. abspath() {
  26. if [ -d "${1}" ]; then
  27. echo "$(cd "${1}" && /usr/bin/env PWD= pwd -P)/"
  28. else
  29. echo "$(cd "$(dirname "${1}")" && /usr/bin/env PWD= pwd -P)/$(basename "${1}")"
  30. fi
  31. }
  32. is_prefix() {
  33. echo "${2}" | grep -qE "^${1}"
  34. return $?
  35. }
  36. check_directories() {
  37. if [ -e "${script_dir}/.environment" ]; then
  38. OLDPATH="${PATH}"
  39. # shellcheck disable=SC1091
  40. . "${script_dir}/.environment"
  41. PATH="${OLDPATH}"
  42. fi
  43. if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}/usr/lib/netdata/conf.d" ]; then
  44. stock_dir="${NETDATA_PREFIX}/usr/lib/netdata/conf.d"
  45. elif [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}/lib/netdata/conf.d" ]; then
  46. stock_dir="${NETDATA_PREFIX}/lib/netdata/conf.d"
  47. elif [ -d "${script_dir}/../../usr/lib/netdata/conf.d" ]; then
  48. stock_dir="${script_dir}/../../usr/lib/netdata/conf.d"
  49. elif [ -d "${script_dir}/../../lib/netdata/conf.d" ]; then
  50. stock_dir="${script_dir}/../../lib/netdata/conf.d"
  51. elif [ -d "/usr/lib/netdata/conf.d" ]; then
  52. stock_dir="/usr/lib/netdata/conf.d"
  53. fi
  54. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="${script_dir}"
  55. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="${stock_dir}"
  56. if [ -z "${NETDATA_STOCK_CONFIG_DIR}" ]; then
  57. error "Unable to find stock config directory."
  58. exit 1
  59. fi
  60. }
  61. check_editor() {
  62. if [ -z "${editor}" ]; then
  63. if [ -n "${EDITOR}" ] && command -v "${EDITOR}" >/dev/null 2>&1; then
  64. editor="${EDITOR}"
  65. elif command -v editor >/dev/null 2>&1; then
  66. editor="editor"
  67. elif command -v vi >/dev/null 2>&1; then
  68. editor="vi"
  69. else
  70. error "Unable to find a usable editor, tried \${EDITOR} (${EDITOR}), editor, and vi."
  71. exit 1
  72. fi
  73. elif ! command -v "${editor}" >/dev/null 2>&1; then
  74. error "Unable to locate user specified editor ${editor}, is it in your PATH?"
  75. exit 1
  76. fi
  77. }
  78. running_in_container() {
  79. [ -e /.dockerenv ] && return 0
  80. [ -e /.dockerinit ] && return 0
  81. [ -r /proc/1/environ ] && tr '\000' '\n' </proc/1/environ | grep -Eiq '^container=podman' && return 0
  82. grep -qF -e /docker/ -e /libpod- /proc/self/cgroup 2>/dev/null && return 0
  83. }
  84. get_docker_command() {
  85. if [ -x "${docker}" ]; then
  86. return 0
  87. elif command -v docker >/dev/null 2>&1; then
  88. docker="$(command -v docker)"
  89. elif command -v podman >/dev/null 2>&1; then
  90. docker="$(command -v podman)"
  91. else
  92. error "Unable to find a usable container tool stack. I support Docker and Podman."
  93. exit 1
  94. fi
  95. }
  96. run_in_container() {
  97. ${docker} exec "${1}" /bin/sh -c "${2}" || return 1
  98. return 0
  99. }
  100. check_for_container() {
  101. get_docker_command
  102. ${docker} container inspect "${1}" >/dev/null 2>&1 || return 1
  103. run_in_container "${1}" "[ -d \"${NETDATA_STOCK_CONFIG_DIR}\" ]" >/dev/null 2>&1 || return 1
  104. return 0
  105. }
  106. handle_container() {
  107. if running_in_container; then
  108. return 0
  109. elif [ -z "${container}" ] && [ -f "${script_dir}/.container-hostname" ]; then
  110. echo >&2 "Autodetected containerized Netdata instance. Attempting to autodetect container ID."
  111. possible_container="$(cat "${script_dir}/.container-hostname")"
  112. if check_for_container "${possible_container}"; then
  113. container="${possible_container}"
  114. elif check_for_container netdata; then
  115. container="netdata"
  116. else
  117. error "Could not autodetect container ID. It must be supplied on the command line with the --container option."
  118. exit 1
  119. fi
  120. echo >&2 "Found Netdata container with ID or name ${container}"
  121. elif [ -n "${container}" ]; then
  122. if ! check_for_container "${container}"; then
  123. error "No container with ID or name ${container} exists."
  124. exit 1
  125. fi
  126. fi
  127. }
  128. list_files() {
  129. check_directories
  130. handle_container
  131. if test -t; then
  132. width="$(tput cols)"
  133. fi
  134. if [ -z "${container}" ]; then
  135. if [ "$(uname -s)" = "Linux" ]; then
  136. # shellcheck disable=SC2046,SC2086
  137. files="$(cd "${NETDATA_STOCK_CONFIG_DIR}" && ls ${width:+-C} ${width:+-w ${width}} $(find . -type f | cut -d '/' -f 2-))"
  138. elif [ "$(uname -s)" = "FreeBSD" ]; then
  139. if [ -n "${width}" ]; then
  140. export COLUMNS="${width}"
  141. fi
  142. # shellcheck disable=SC2046
  143. files="$(cd "${NETDATA_STOCK_CONFIG_DIR}" && ls ${width:+-C} $(find . -type f | cut -d '/' -f 2-))"
  144. else
  145. # shellcheck disable=SC2046
  146. files="$(cd "${NETDATA_STOCK_CONFIG_DIR}" && ls $(find . -type f | cut -d '/' -f 2-))"
  147. fi
  148. else
  149. files="$(run_in_container "${container}" "cd /usr/lib/netdata/conf.d && ls ${width:+-C} ${width:+-w ${width}} \$(find . -type f | cut -d '/' -f 2-)")"
  150. fi
  151. if [ -z "${files}" ]; then
  152. error "Failed to find any configuration files."
  153. exit 1
  154. fi
  155. cat <<EOF
  156. The following configuration files are known to this script:
  157. ${files}
  158. EOF
  159. exit 0
  160. }
  161. parse_args() {
  162. while [ -n "${1}" ]; do
  163. case "${1}" in
  164. "--help") usage ;;
  165. "--list") list_files ;;
  166. "--file")
  167. if [ -n "${2}" ]; then
  168. file="${2}"
  169. shift 1
  170. else
  171. error "No file specified to edit."
  172. exit 1
  173. fi
  174. ;;
  175. "--container")
  176. if [ -n "${2}" ]; then
  177. container="${2}"
  178. shift 1
  179. else
  180. error "No container ID or name specified with the --container option."
  181. exit 1
  182. fi
  183. ;;
  184. "--editor")
  185. if [ -n "${2}" ]; then
  186. editor="${2}"
  187. shift 1
  188. else
  189. error "No editor specified with the --editor option."
  190. exit 1
  191. fi
  192. ;;
  193. *)
  194. if [ -z "${2}" ]; then
  195. file="${1}"
  196. else
  197. error "Unrecognized option ${1}."
  198. exit 1
  199. fi
  200. ;;
  201. esac
  202. shift 1
  203. done
  204. [ -z "${file}" ] && usage
  205. absfile="$(abspath "${file}")"
  206. if ! is_prefix "${script_dir}" "${absfile}"; then
  207. error "${file} is not located under ${script_dir}"
  208. exit 1
  209. fi
  210. file="${absfile##"${script_dir}"}"
  211. }
  212. copy_native() {
  213. if [ ! -w "${NETDATA_USER_CONFIG_DIR}" ]; then
  214. error "Cannot write to ${NETDATA_USER_CONFIG_DIR}!"
  215. exit 1
  216. fi
  217. if [ -f "${NETDATA_STOCK_CONFIG_DIR}/${1}" ]; then
  218. echo >&2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
  219. cp -p "${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  220. else
  221. echo >&2 "Creating empty '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
  222. touch "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  223. fi
  224. }
  225. copy_container() {
  226. if [ ! -w "${NETDATA_USER_CONFIG_DIR}" ]; then
  227. error "Cannot write to ${NETDATA_USER_CONFIG_DIR}!"
  228. exit 1
  229. fi
  230. if run_in_container "${container}" "[ -f \"${NETDATA_STOCK_CONFIG_DIR}/${1}\" ]"; then
  231. echo >&2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
  232. ${docker} cp -a "${container}:${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  233. else
  234. echo >&2 "Creating empty '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
  235. touch "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  236. fi
  237. }
  238. copy() {
  239. if [ -f "${NETDATA_USER_CONFIG_DIR}/${1}" ]; then
  240. return 0
  241. elif [ -n "${container}" ]; then
  242. copy_container "${1}"
  243. else
  244. copy_native "${1}"
  245. fi
  246. }
  247. edit() {
  248. echo >&2 "Editing '${1}' ..."
  249. # check we can edit
  250. if [ ! -w "${1}" ]; then
  251. error "Cannot write to ${1}!"
  252. exit 1
  253. fi
  254. "${editor}" "${1}"
  255. exit $?
  256. }
  257. main() {
  258. parse_args "${@}"
  259. check_directories
  260. check_editor
  261. handle_container
  262. copy "${file}"
  263. edit "${absfile}"
  264. }
  265. main "${@}"