edit-config 8.5 KB

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