edit-config 8.7 KB

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