edit-config 8.7 KB

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