You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2958 lines
91 KiB

  1. #!/bin/bash
  2. . /etc/shlib >/dev/null 2>&1 || {
  3. echo "Error: you don't have kal-shlib-core installed."
  4. echo ""
  5. echo " You might want to add `deb.kalysto.org` deb repository, you'll need root access,"
  6. echo " so you might want to run these command after a \`sudo -i\` for instance..."
  7. echo ""
  8. echo " echo deb https://deb.kalysto.org no-dist kal-alpha kal-beta kal-main \\"
  9. echo " > /etc/apt/sources.list.d/kalysto.org.list"
  10. echo " wget -O - https://deb.kalysto.org/conf/public-key.gpg | apt-key add -"
  11. echo " apt-get update -o Dir::Etc::sourcelist=sources.list.d/kalysto.org.list \\"
  12. echo " -o Dir::Etc::sourceparts=- -o APT::Get::List-Cleanup=0"
  13. echo ""
  14. echo " Then install package kal-shlib-*:"
  15. echo ""
  16. echo " apt install kal-shlib-{common,cmdline,config,cache,docker,pretty}"
  17. echo ""
  18. exit 1
  19. } >&2
  20. include common
  21. include parse
  22. include cmdline
  23. include config
  24. include cache
  25. include fn
  26. include docker
  27. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  28. version=0.1
  29. desc='Install backup'
  30. help=""
  31. version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  32. read-0a-err() {
  33. local ret="$1" eof="" idx=0 last=
  34. read -r -- "${ret?}" <<<"0"
  35. shift
  36. while [ "$1" ]; do
  37. last=$idx
  38. read -r -- "$1" || {
  39. ## Put this last value in ${!ret}
  40. eof="$1"
  41. read -r -- "$ret" <<<"${!eof}"
  42. break
  43. }
  44. ((idx++))
  45. shift
  46. done
  47. [ -z "$eof" ] || {
  48. if [ "$last" != 0 ]; then
  49. echo "Error: read-0a-err couldn't fill all value" >&2
  50. read -r -- "$ret" <<<"127"
  51. else
  52. if [ -z "${!ret}" ]; then
  53. echo "Error: last value is not a number, did you finish with an errorlevel ?" >&2
  54. read -r -- "$ret" <<<"126"
  55. fi
  56. fi
  57. false
  58. }
  59. }
  60. p-0a-err() {
  61. "$@"
  62. echo -n "$?"
  63. }
  64. docker:running-container-projects() {
  65. :cache: scope=session
  66. docker ps --format '{{.Label "com.docker.compose.project"}}' | sort | uniq
  67. }
  68. decorator._mangle_fn docker:running-container-projects
  69. ssh:mk-private-key() {
  70. local host="$1" service_name="$2"
  71. (
  72. settmpdir VPS_TMPDIR
  73. ssh-keygen -t rsa -N "" -f "$VPS_TMPDIR/rsync_rsa" -C "$service_name@$host" >/dev/null
  74. cat "$VPS_TMPDIR/rsync_rsa"
  75. )
  76. }
  77. mailcow:has-images-running() {
  78. local images
  79. images=$(docker ps --format '{{.Image}}' | sort | uniq)
  80. [[ $'\n'"$images" == *$'\n'"mailcow/"* ]]
  81. }
  82. mailcow:has-container-project-mentionning-mailcow() {
  83. local projects
  84. projects=$(docker:running-container-projects) || return 1
  85. [[ $'\n'"$projects"$'\n' == *mailcow* ]]
  86. }
  87. mailcow:has-running-containers() {
  88. mailcow:has-images-running ||
  89. mailcow:has-container-project-mentionning-mailcow
  90. }
  91. mailcow:get-root() {
  92. :cache: scope=session
  93. local dir
  94. for dir in {/opt{,/apps},/root}/mailcow-dockerized; do
  95. [ -d "$dir" ] || continue
  96. [ -r "$dir/mailcow.conf" ] || continue
  97. echo "$dir"
  98. return 0
  99. done
  100. return 1
  101. }
  102. decorator._mangle_fn mailcow:get-root
  103. compose:get-compose-yml() {
  104. :cache: scope=session
  105. local path
  106. path=$(DEBUG=1 DRY_RUN=1 compose 2>&1 | egrep '^\s+-e HOST_COMPOSE_YML_FILE=' | cut -f 2- -d "=" | cut -f 1 -d " ")
  107. [ -e "$path" ] || return 1
  108. echo "$path"
  109. }
  110. decorator._mangle_fn compose:get-compose-yml
  111. export -f compose:get-compose-yml
  112. compose:has-container-project-myc() {
  113. local projects
  114. projects=$(docker:running-container-projects) || return 1
  115. [[ $'\n'"$projects"$'\n' == *$'\n'"myc"$'\n'* ]]
  116. }
  117. compose:service:exists() {
  118. local project="$1" service="$2" service_cfg
  119. service_cfg=$(cat "$(compose:get-compose-yml)" |
  120. shyaml get-value -y "$service" 2>/dev/null) || return 1
  121. [ -n "$service_cfg" ]
  122. }
  123. docker:container:name() {
  124. local container_id="$1"
  125. docker inspect --format '{{.Name}}' "$container_id" | cut -f 2 -d "/"
  126. }
  127. compose:service:container:get() {
  128. local project="$1" service="$2" container_id
  129. container_id=$(compose:service:container_one "$project" "$service") || return 1
  130. container_name=$(docker:container:name "$container_id") || return 1
  131. echo "$container_name"
  132. }
  133. compose:file:value-change() {
  134. local key="$1" value="$2"
  135. local compose_yml
  136. if ! compose_yml=$(compose:get-compose-yml); then
  137. err "Couldn't locate your 'compose.yml' file."
  138. return 1
  139. fi
  140. yaml:file:value-change "$compose_yml" "$key" "$value" || return 1
  141. }
  142. export -f compose:file:value-change
  143. yaml:file:value-change() {
  144. local file="$1" key="$2" value="$3" first=1 count=0 diff=""
  145. (
  146. cd "${file%/*}"
  147. while read-0 hunk; do
  148. if [ -n "$first" ]; then
  149. diff+="$hunk"
  150. first=
  151. continue
  152. fi
  153. if [[ "$hunk" =~ $'\n'"+"[[:space:]]+"${key##*.}:" ]]; then
  154. ((count++))
  155. diff+="$hunk" >&2
  156. else
  157. :
  158. # echo "discarding:" >&2
  159. # e "$hunk" | prefix " | " >&2
  160. fi
  161. done < <(
  162. export DEBUG=
  163. settmpdir YQ_TEMP
  164. cp "${file}" "$YQ_TEMP/compose.yml" &&
  165. yq -i ".${key} = \"${value}\"" "$YQ_TEMP/compose.yml" &&
  166. sed -ri 's/^([^# ])/\n\0/g' "$YQ_TEMP/compose.yml" &&
  167. diff -u0 -Z "${file}" "$YQ_TEMP/compose.yml" |
  168. sed -r "s/^(@@.*)$/\x00\1/g;s%^(\+\+\+) [^\t]+%\1 ${file}%g"
  169. printf "\0"
  170. )
  171. if [[ "$count" == 0 ]]; then
  172. err "No change made to '$file'."
  173. return 1
  174. fi
  175. if [[ "$count" != 1 ]]; then
  176. err "compose file change request seems dubious and was refused:"
  177. e "$diff" | prefix " | " >&2
  178. return 1
  179. fi
  180. echo Applying: >&2
  181. e "$diff" | prefix " | " >&2
  182. patch <<<"$diff"
  183. ) || exit 1
  184. }
  185. export -f yaml:file:value-change
  186. type:is-mailcow() {
  187. mailcow:get-root >/dev/null ||
  188. mailcow:has-running-containers
  189. }
  190. type:is-compose() {
  191. compose:get-compose-yml >/dev/null &&
  192. compose:has-container-project-myc
  193. }
  194. vps:get-type() {
  195. :cache: scope=session
  196. local fn
  197. for fn in $(declare -F | cut -f 3 -d " " | egrep "^type:is-"); do
  198. "$fn" && {
  199. echo "${fn#type:is-}"
  200. return 0
  201. }
  202. done
  203. return 1
  204. }
  205. decorator._mangle_fn vps:get-type
  206. mirror-dir:sources() {
  207. :cache: scope=session
  208. if ! shyaml get-values default.sources < /etc/mirror-dir/config.yml; then
  209. err "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'."
  210. return 1
  211. fi
  212. }
  213. decorator._mangle_fn mirror-dir:sources
  214. mirror-dir:check-add() {
  215. local elt="$1" sources
  216. sources=$(mirror-dir:sources) || return 1
  217. if [[ $'\n'"$sources"$'\n' == *$'\n'"$elt"$'\n'* ]]; then
  218. info "Volume $elt already in sources"
  219. else
  220. Elt "Adding directory $elt"
  221. sed -i "/sources:/a\ - \"${elt}\"" \
  222. /etc/mirror-dir/config.yml
  223. Feedback || return 1
  224. fi
  225. }
  226. mirror-dir:check-add-vol() {
  227. local elt="$1"
  228. mirror-dir:check-add "/var/lib/docker/volumes/*_${elt}-*/_data"
  229. }
  230. ## The first colon is to prevent auto-export of function from shlib
  231. : ; bash-bug-5() { { cat; } < <(e) >/dev/null; ! cat "$1"; } && bash-bug-5 <(e) 2>/dev/null &&
  232. export BASH_BUG_5=1 && unset -f bash_bug_5
  233. wrap() {
  234. local label="$1" code="$2"
  235. shift 2
  236. export VERBOSE=1
  237. interpreter=/bin/bash
  238. if [ -n "$BASH_BUG_5" ]; then
  239. (
  240. settmpdir tmpdir
  241. fname=${label##*/}
  242. e "$code" > "$tmpdir/$fname" &&
  243. chmod +x "$tmpdir/$fname" &&
  244. Wrap -vsd "$label" -- "$interpreter" "$tmpdir/$fname" "$@"
  245. )
  246. else
  247. Wrap -vsd "$label" -- "$interpreter" <(e "$code") "$@"
  248. fi
  249. }
  250. ping_check() {
  251. #global ignore_ping_check
  252. local host="$1"
  253. ip=$(getent ahosts "$host" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+" |
  254. head -n 1 | cut -f 1 -d " ") || return 1
  255. my_ip=$(curl -s myip.kal.fr)
  256. if [ "$ip" != "$my_ip" ]; then
  257. if [ -n "$ignore_ping_check" ]; then
  258. warn "IP of '$host' ($ip) doesn't match mine ($my_ip). Ignoring due to ``--ignore-ping-check`` option."
  259. else
  260. err "IP of '$host' ($ip) doesn't match mine ($my_ip). Use ``--ignore-ping-check`` to ignore check."
  261. return 1
  262. fi
  263. fi
  264. }
  265. mailcow:install-backup() {
  266. local BACKUP_SERVER="$1" ignore_ping_check="$2" mailcow_root DOMAIN
  267. ## find installation
  268. mailcow_root=$(mailcow:get-root) || {
  269. err "Couldn't find a valid mailcow root directory."
  270. return 1
  271. }
  272. ## check ok
  273. DOMAIN=$(cat "$mailcow_root/.env" | grep ^MAILCOW_HOSTNAME= | cut -f 2 -d =) || {
  274. err "Couldn't find MAILCOW_HOSTNAME in file \"$mailcow_root/.env\"."
  275. return 1
  276. }
  277. ping_check "$DOMAIN" || return 1
  278. MYSQL_ROOT_PASSWORD=$(cat "$mailcow_root/.env" | grep ^DBROOT= | cut -f 2 -d =) || {
  279. err "Couldn't find DBROOT in file \"$mailcow_root/.env\"."
  280. return 1
  281. }
  282. if docker compose >/dev/null 2>&1; then
  283. MYSQL_CONTAINER=${MYSQL_CONTAINER:-mailcowdockerized-mysql-mailcow-1}
  284. else
  285. MYSQL_CONTAINER=${MYSQL_CONTAINER:-mailcowdockerized_mysql-mailcow_1}
  286. fi
  287. container_id=$(docker ps -f name="$MYSQL_CONTAINER" --format "{{.ID}}")
  288. if [ -z "$container_id" ]; then
  289. err "Couldn't find docker container named '$MYSQL_CONTAINER'."
  290. return 1
  291. fi
  292. export KEY_BACKUP_ID="mailcow"
  293. export MYSQL_ROOT_PASSWORD
  294. export MYSQL_CONTAINER
  295. export BACKUP_SERVER
  296. export DOMAIN
  297. wrap "Install rsync-backup on host" "
  298. cd /srv/charm-store/rsync-backup
  299. bash ./hooks/install.d/60-install.sh
  300. " || return 1
  301. wrap "Mysql dump install" "
  302. cd /srv/charm-store/mariadb
  303. bash ./hooks/install.d/60-backup.sh
  304. " || return 1
  305. ## Using https://github.com/mailcow/mailcow-dockerized/blob/master/helper-scripts/backup_and_restore.sh
  306. for elt in "vmail{,-attachments-vol}" crypt redis rspamd postfix; do
  307. mirror-dir:check-add-vol "$elt" || return 1
  308. done
  309. mirror-dir:check-add "$mailcow_root" || return 1
  310. mirror-dir:check-add "/var/backups/mysql" || return 1
  311. mirror-dir:check-add "/etc" || return 1
  312. dest="$BACKUP_SERVER"
  313. dest="${dest%/*}"
  314. ssh_options=()
  315. if [[ "$dest" == *":"* ]]; then
  316. port="${dest##*:}"
  317. dest="${dest%%:*}"
  318. ssh_options=(-p "$port")
  319. else
  320. port=""
  321. dest="${dest%%:*}"
  322. fi
  323. info "You can run this following command from an host having admin access to $dest:"
  324. echo " (Or send it to a backup admin of $dest)" >&2
  325. echo "ssh ${ssh_options[@]} myadmin@$dest ssh-key add '$(cat /var/lib/rsync/.ssh/id_rsa.pub)'"
  326. }
  327. compose:has_domain() {
  328. local compose_file="$1" host="$2" name conf relation relation_value domain server_aliases
  329. while read-0 name conf ; do
  330. name=$(e "$name" | shyaml get-value)
  331. if [[ "$name" =~ ^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+ ]]; then
  332. [ "$host" == "$name" ] && return 0
  333. fi
  334. rel=$(e "$conf" | shyaml -y get-value relations 2>/dev/null) || continue
  335. for relation in web-proxy publish-dir; do
  336. relation_value=$(e "$rel" | shyaml -y get-value "$relation" 2>/dev/null) || continue
  337. while read-0 label conf_relation; do
  338. domain=$(e "$conf_relation" | shyaml get-value "domain" 2>/dev/null) && {
  339. [ "$host" == "$domain" ] && return 0
  340. }
  341. server_aliases=$(e "$conf_relation" | shyaml get-values "server-aliases" 2>/dev/null) && {
  342. [[ $'\n'"$server_aliases" == *$'\n'"$host"$'\n'* ]] && return 0
  343. }
  344. done < <(e "$relation_value" | shyaml -y key-values-0)
  345. done
  346. done < <(shyaml -y key-values-0 < "$compose_file")
  347. return 1
  348. }
  349. compose:install-backup() {
  350. local BACKUP_SERVER="$1" service_name="$2" compose_file="$3" ignore_ping_check="$4" ignore_domain_check="$5"
  351. ## XXXvlab: far from perfect as it mimics and depends internal
  352. ## logic of current default way to get a domain in compose-core
  353. host=$(hostname)
  354. if ! compose:has_domain "$compose_file" "$host"; then
  355. if [ -n "$ignore_domain_check" ]; then
  356. warn "domain of '$host' not found in compose file '$compose_file'. Ignoring due to ``--ignore-domain-check`` option."
  357. else
  358. err "domain of '$host' not found in compose file '$compose_file'. Use ``--ignore-domain-check`` to ignore check."
  359. return 1
  360. fi
  361. fi
  362. ping_check "$host" || return 1
  363. if [ -e "/root/.ssh/rsync_rsa" ]; then
  364. warn "deleting private key in /root/.ssh/rsync_rsa, as we are not using it anymore."
  365. rm -fv /root/.ssh/rsync_rsa
  366. fi
  367. if [ -e "/root/.ssh/rsync_rsa.pub" ]; then
  368. warn "deleting public key in /root/.ssh/rsync_rsa.pub, as we are not using it anymore."
  369. rm -fv /root/.ssh/rsync_rsa.pub
  370. fi
  371. if service_cfg=$(cat "$compose_file" |
  372. shyaml get-value -y "$service_name" 2>/dev/null); then
  373. info "Entry for service ${DARKYELLOW}$service_name${NORMAL}" \
  374. "is already present in '$compose_file'."
  375. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  376. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  377. "entry in '$compose_file'."
  378. return 1
  379. }
  380. private_key=$(e "$cfg" | shyaml get-value private-key) || return 1
  381. target=$(e "$cfg" | shyaml get-value target) || return 1
  382. if [ "$target" != "$BACKUP_SERVER" ]; then
  383. err "Existing backup target '$target' is different" \
  384. "from specified '$BACKUP_SERVER'"
  385. return 1
  386. fi
  387. else
  388. private_key=$(ssh:mk-private-key "$host" "$service_name")
  389. cat <<EOF >> "$compose_file"
  390. $service_name:
  391. options:
  392. ident: $host
  393. target: $BACKUP_SERVER
  394. private-key: |
  395. $(e "$private_key" | sed -r 's/^/ /g')
  396. EOF
  397. fi
  398. dest="$BACKUP_SERVER"
  399. dest="${dest%/*}"
  400. ssh_options=()
  401. if [[ "$dest" == *":"* ]]; then
  402. port="${dest##*:}"
  403. dest="${dest%%:*}"
  404. ssh_options=(-p "$port")
  405. else
  406. port=""
  407. dest="${dest%%:*}"
  408. fi
  409. info "You can run this following command from an host having admin access to $dest:"
  410. echo " (Or send it to a backup admin of $dest)" >&2
  411. ## We remove ending label (label will be added or not in the
  412. ## private key, and thus here, depending on the version of
  413. ## openssh-client)
  414. public_key=$(ssh-keygen -y -f <(e "$private_key"$'\n') | sed -r 's/ [^ ]+@[^ ]+$//')
  415. echo "ssh ${ssh_options[@]} myadmin@$dest ssh-key add '$public_key compose@$host'"
  416. }
  417. backup-action() {
  418. local action="$1"
  419. shift
  420. vps_type=$(vps:get-type) || {
  421. err "Failed to get type of installation."
  422. return 1
  423. }
  424. if ! fn.exists "${vps_type}:${action}"; then
  425. err "type '${vps_type}' has no ${vps_type}:${action} implemented yet."
  426. return 1
  427. fi
  428. "${vps_type}:${action}" "$@"
  429. }
  430. compose:get_default_backup_host_ident() {
  431. local service_name="$1" ## Optional
  432. local compose_file service_cfg cfg target
  433. compose_file=$(compose:get-compose-yml)
  434. service_name="${service_name:-rsync-backup}"
  435. if ! service_cfg=$(cat "$compose_file" |
  436. shyaml get-value -y "$service_name" 2>/dev/null); then
  437. err "No service named '$service_name' found in 'compose.yml'."
  438. return 1
  439. fi
  440. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  441. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  442. "entry in '$compose_file'."
  443. return 1
  444. }
  445. if ! target=$(e "$cfg" | shyaml get-value target); then
  446. err "No ${WHITE}options.target${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  447. "entry in '$compose_file'."
  448. fi
  449. if ! target=$(e "$cfg" | shyaml get-value target); then
  450. err "No ${WHITE}options.target${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  451. "entry in '$compose_file'."
  452. fi
  453. if ! ident=$(e "$cfg" | shyaml get-value ident); then
  454. err "No ${WHITE}options.ident${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  455. "entry in '$compose_file'."
  456. fi
  457. echo "$target $ident"
  458. }
  459. mailcow:get_default_backup_host_ident() {
  460. local content cron_line ident found dest cmd_line
  461. if ! [ -e "/etc/cron.d/mirror-dir" ]; then
  462. err "No '/etc/cron.d/mirror-dir' found."
  463. return 1
  464. fi
  465. content=$(cat /etc/cron.d/mirror-dir) || {
  466. err "Can't read '/etc/cron.d/mirror-dir'."
  467. return 1
  468. }
  469. if ! cron_line=$(e "$content" | grep "mirror-dir backup"); then
  470. err "Can't find 'mirror-dir backup' line in '/etc/cron.d/mirror-dir'."
  471. return 1
  472. fi
  473. cron_line=${cron_line%|*}
  474. cmd_line=(${cron_line#*root})
  475. found=
  476. dest=
  477. for arg in "${cmd_line[@]}"; do
  478. [ -n "$found" ] && {
  479. dest="$arg"
  480. break
  481. }
  482. [ "$arg" == "-d" ] && {
  483. found=1
  484. }
  485. done
  486. if ! [[ "$dest" =~ ^[\'\"a-zA-Z0-9:/.-]+$ ]]; then
  487. err "Can't find valid destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  488. return 1
  489. fi
  490. if [[ "$dest" == \"*\" ]] || [[ "$dest" == \'*\' ]]; then
  491. ## unquoting, the eval should be safe because of previous check
  492. dest=$(eval e "$dest")
  493. fi
  494. if [ -z "$dest" ]; then
  495. err "Can't find destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  496. return 1
  497. fi
  498. ## looking for ident
  499. found=
  500. ident=
  501. for arg in "${cmd_line[@]}"; do
  502. [ -n "$found" ] && {
  503. ident="$arg"
  504. break
  505. }
  506. [ "$arg" == "-h" ] && {
  507. found=1
  508. }
  509. done
  510. if ! [[ "$ident" =~ ^[\'\"a-zA-Z0-9.-]+$ ]]; then
  511. err "Can't find valid identifier in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  512. return 1
  513. fi
  514. if [[ "$ident" == \"*\" ]] || [[ "$ident" == \'*\' ]]; then
  515. ## unquoting, the eval should be safe because of previous check
  516. ident=$(eval e "$ident")
  517. fi
  518. if [ -z "$ident" ]; then
  519. err "Can't find destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  520. return 1
  521. fi
  522. echo "$dest $ident"
  523. }
  524. compose:service:containers() {
  525. local project="$1" service="$2"
  526. docker ps \
  527. --filter label="com.docker.compose.project=$project" \
  528. --filter label="compose.master-service=$service" \
  529. --format="{{.ID}}"
  530. }
  531. export -f compose:service:containers
  532. compose:service:container_one() {
  533. local project="$1" service="$2" container_id
  534. {
  535. read-0a container_id || {
  536. err "service ${DARKYELLOW}$service${NORMAL} has no running container."
  537. return 1
  538. }
  539. if read-0a _; then
  540. err "service ${DARKYELLOW}$service${NORMAL} has more than one running container."
  541. return 1
  542. fi
  543. } < <(compose:service:containers "$project" "$service")
  544. echo "$container_id"
  545. }
  546. export -f compose:service:container_one
  547. compose:service:container_first() {
  548. local project="$1" service="$2" container_id
  549. {
  550. read-0a container_id || {
  551. err "service ${DARKYELLOW}$service${NORMAL} has no running container."
  552. return 1
  553. }
  554. if read-0a _; then
  555. warn "service ${DARKYELLOW}$service${NORMAL} has more than one running container."
  556. fi
  557. } < <(compose:service:containers "$project" "$service")
  558. echo "$container_id"
  559. }
  560. export -f compose:service:container_first
  561. docker:running_containers() {
  562. :cache: scope=session
  563. docker ps --format="{{.ID}}"
  564. }
  565. decorator._mangle_fn docker:running_containers
  566. export -f docker:running_containers
  567. compose:project:containers() {
  568. local project="$1" opts
  569. opts+=(--filter label="com.docker.compose.project=$project")
  570. docker ps "${opts[@]}" \
  571. --format="{{.ID}}"
  572. }
  573. export -f compose:project:containers
  574. compose:charm:containers() {
  575. local project="$1" charm="$2"
  576. docker ps \
  577. --filter label="com.docker.compose.project=$project" \
  578. --filter label="compose.charm=$charm" \
  579. --format="{{.ID}}"
  580. }
  581. export -f compose:charm:containers
  582. compose:charm:container_one() {
  583. local project="$1" charm="$2" container_id
  584. {
  585. read-0a container_id || {
  586. err "charm ${DARKPINK}$charm${NORMAL} has no running container in project '$project'."
  587. return 1
  588. }
  589. if read-0a _; then
  590. err "charm ${DARKPINK}$charm${NORMAL} has more than one running container."
  591. return 1
  592. fi
  593. } < <(compose:charm:containers "$project" "$charm")
  594. echo "$container_id"
  595. }
  596. export -f compose:charm:container_one
  597. compose:charm:container_first() {
  598. local project="$1" charm="$2" container_id
  599. {
  600. read-0a container_id || {
  601. warn "charm ${DARKYELLOW}$charm${NORMAL} has no running container in project '$project'."
  602. }
  603. if read-0a _; then
  604. warn "charm ${DARKYELLOW}$charm${NORMAL} has more than one running container."
  605. fi
  606. } < <(compose:charm:containers "$project" "$charm")
  607. echo "$container_id"
  608. }
  609. export -f compose:charm:container_first
  610. compose:get_url() {
  611. local project_name="$1" service="$2" data_file network ip
  612. data_dir=("/var/lib/compose/relations/${project_name}/${service}-"*"/web-proxy")
  613. if [ "${#data_dir[@]}" -gt 1 ]; then
  614. err "More than one web-proxy relation." \
  615. "Current 'vps' algorithm is insufficient" \
  616. "to figure out which relation is concerned"
  617. return 1
  618. fi
  619. data_file="${data_dir[0]}/data"
  620. if [ -d "${data_file%/*}" ]; then
  621. (
  622. set -o pipefail
  623. ## users can't cat directly the content
  624. docker run --rm \
  625. -v "${data_file%/*}":/tmp/dummy alpine \
  626. cat "/tmp/dummy/${data_file##*/}" |
  627. shyaml get-value url
  628. )
  629. else
  630. ## Assume there are no frontend relation here, the url is direct IP
  631. container_id=$(compose:service:container_one "${project_name}" "${service}") || return 1
  632. network_ip=$(docker:container:network_ip_one "${container_id}") || return 1
  633. IFS=":" read -r network ip <<<"$network_ip"
  634. tcp_port=
  635. for port in $(docker:exposed_ports "$container_id"); do
  636. IFS="/" read port type <<<"$port"
  637. [ "$type" == "tcp" ] || continue
  638. tcp_port="$port"
  639. break
  640. done
  641. echo -n "http://$ip"
  642. [ -n "$tcp_port" ] && echo ":$tcp_port"
  643. fi || {
  644. err "Failed querying ${service} to frontend relation to get url."
  645. return 1
  646. }
  647. }
  648. export -f compose:get_url
  649. compose:container:service() {
  650. local container="$1" service
  651. if ! service=$(docker:container:label "$container" "compose.service"); then
  652. err "Failed to get service name from container ${container}."
  653. return 1
  654. fi
  655. if [ -z "$service" ]; then
  656. err "No service found for container ${container}."
  657. return 1
  658. fi
  659. echo "$service"
  660. }
  661. export -f compose:container:service
  662. compose:psql() {
  663. local project_name="$1" dbname="$2" container_id
  664. shift 2
  665. container_id=$(compose:charm:container_one "$project_name" "postgres") || return 1
  666. docker exec -i "${container_id}" psql -U postgres "$dbname" "$@"
  667. }
  668. export -f compose:psql
  669. compose:mongo() {
  670. local project_name="$1" dbname="$2" container_id
  671. container_id=$(compose:charm:container_one "$project_name" "mongo") || return 1
  672. docker exec -i "${container_id}" mongo --quiet "$dbname"
  673. }
  674. export -f compose:mongo
  675. compose:pgm() {
  676. local project_name="$1" container_network_ip container_ip container_network
  677. shift
  678. container_id=$(compose:charm:container_one "$project_name" "postgres") || return 1
  679. service_name=$(compose:container:service "$container_id") || return 1
  680. image_id=$(docker:container:image "$container_id") || return 1
  681. container_network_ip=$(docker:container:network_ip_one "$container_id") || return 1
  682. IFS=":" read -r container_network container_ip <<<"$container_network_ip"
  683. pgpass="/srv/datastore/data/${service_name}/var/lib/postgresql/data/pgpass"
  684. local final_pgm_docker_run_opts+=(
  685. -u 0 -e prefix_pg_local_command=" "
  686. --network "${container_network}"
  687. -e PGHOST="$container_ip"
  688. -e PGUSER=postgres
  689. -v "$pgpass:/root/.pgpass"
  690. "${pgm_docker_run_opts[@]}"
  691. )
  692. cmd=(docker run --rm \
  693. "${final_pgm_docker_run_opts[@]}" \
  694. "${image_id}" pgm "$@"
  695. )
  696. echo "${cmd[@]}"
  697. "${cmd[@]}"
  698. }
  699. export -f compose:pgm
  700. postgres:dump() {
  701. local project_name="$1" src="$2" dst="$3"
  702. (
  703. settmpdir PGM_TMP_LOCATION
  704. pgm_docker_run_opts=('-v' "${PGM_TMP_LOCATION}:/tmp/dump")
  705. compose:pgm "$project_name" cp -f "$src" "/tmp/dump/dump.gz" &&
  706. mv "$PGM_TMP_LOCATION/dump.gz" "$dst"
  707. ) || return 1
  708. }
  709. export -f postgres:dump
  710. postgres:restore() {
  711. local project_name="$1" src="$2" dst="$3"
  712. full_src_path=$(readlink -e "$src") || exit 1
  713. (
  714. pgm_docker_run_opts=('-v' "${full_src_path}:/tmp/dump.gz")
  715. compose:pgm "$project_name" cp -f "/tmp/dump.gz" "$dst"
  716. ) || return 1
  717. }
  718. export -f postgres:restore
  719. odoo:get_public_user_id() {
  720. local project_name="$1" dbname="$2"
  721. echo "select res_id from ir_model_data where model = 'res.users' and name = 'public_user';" |
  722. compose:psql "$project_name" "$dbname" -qAt
  723. }
  724. cyclos:set_root_url() {
  725. local project_name="$1" dbname="$2" url="$3"
  726. echo "UPDATE configurations SET root_url = '$url';" |
  727. compose:psql "$project_name" "$dbname" || {
  728. err "Failed to set cyclos url value in '$dbname' database."
  729. return 1
  730. }
  731. }
  732. export -f cyclos:set_root_url
  733. cyclos:unlock() {
  734. local project_name="$1" dbname="$2"
  735. echo "delete from database_lock;" |
  736. compose:psql "${project_name}" "${dbname}"
  737. }
  738. export -f cyclos:unlock
  739. rocketchat:drop-indexes() {
  740. local project_name="$1" dbname="$2"
  741. compose:mongo "${project_name}" "${dbname}" <<'EOF'
  742. db.users.dropIndexes();
  743. // Check if the 'rocketchat_uploads' collection exists
  744. var collections = db.getCollectionNames();
  745. if (collections.indexOf('rocketchat_uploads') !== -1) {
  746. db.rocketchat_uploads.dropIndexes();
  747. }
  748. if (collections.indexOf('rocketchat_read_receipts') !== -1) {
  749. db.rocketchat_read_receipts.dropIndexes();
  750. var duplicates = [];
  751. db.getCollection("rocketchat_read_receipts").aggregate([
  752. {
  753. "$group": {
  754. "_id": { "roomId": "$roomId", "userId": "$userId", "messageId": "$messageId" },
  755. "uniqueIds": { "$addToSet": "$_id" },
  756. "count": { "$sum": 1 }
  757. }
  758. },
  759. { "$match": { "count": { "$gt": 1 } } }
  760. ],
  761. { allowDiskUse: true }
  762. ).forEach(function (doc) {
  763. // remove 1st element
  764. doc.uniqueIds.shift();
  765. doc.uniqueIds.forEach(function (dupId) {
  766. duplicates.push(dupId);
  767. }
  768. )
  769. })
  770. // printjson(duplicates);
  771. db.getCollection("rocketchat_read_receipts").remove({ _id: { $in: duplicates } });
  772. }
  773. EOF
  774. }
  775. export -f rocketchat:drop-indexes
  776. compose:project_name() {
  777. if [ -z "$PROJECT_NAME" ]; then
  778. PROJECT_NAME=$(compose --get-project-name) || {
  779. err "Couldn't get project name."
  780. return 1
  781. }
  782. if [ -z "$PROJECT_NAME" -o "$PROJECT_NAME" == "orphan" ]; then
  783. err "Couldn't get project name, probably because 'compose.yml' wasn't found."
  784. echo " Please ensure to either configure a global 'compose.yml' or run this command" >&2
  785. echo " in a compose project (with 'compose.yml' on the top level directory)." >&2
  786. return 1
  787. fi
  788. export PROJECT_NAME
  789. fi
  790. echo "$PROJECT_NAME"
  791. }
  792. export -f compose:project_name
  793. compose:get_cron_docker_cmd() {
  794. local cron_line cmd_line docker_cmd
  795. project_name=$(compose:project_name) || return 1
  796. container=$(compose:service:containers "${project_name}" "cron") || {
  797. err "Can't find service 'cron' in project ${project_name}."
  798. return 1
  799. }
  800. if docker exec "$container" test -e /etc/cron.d/rsync-backup; then
  801. if ! cron_line=$(docker exec "${project_name}"_cron_1 cat /etc/cron.d/rsync-backup | grep "\* \* \*"); then
  802. err "Can't find cron line in legacy cron container."
  803. return 1
  804. fi
  805. elif docker exec "$container" test -e /etc/crontabs/root; then
  806. if ! cron_line=$(docker exec "$container" cat /etc/crontabs/root | grep " launch-rsync-backup " | grep "\* \* \*"); then
  807. err "Can't find cron line in cron container."
  808. return 1
  809. fi
  810. else
  811. err "Unrecognized cron container:"
  812. echo " Can't find neither:" >&2
  813. echo " - /etc/cron.d/rsync-backup for old-style cron services" >&2
  814. echo " - nor /etc/crontabs/root for new-style cron services." >&2
  815. return 1
  816. fi
  817. cron_line=${cron_line%|*}
  818. cron_line=${cron_line%"2>&1"*}
  819. cmd_line="${cron_line#*root}"
  820. eval "args=($cmd_line)"
  821. ## should be last argument
  822. docker_cmd=$(echo ${args[@]: -1})
  823. if ! [[ "$docker_cmd" == "docker run --rm -e "* ]]; then
  824. echo "docker command found should start with 'docker run'." >&2
  825. echo "Here's command:" >&2
  826. echo " $docker_cmd" >&2
  827. return 1
  828. fi
  829. e "$docker_cmd"
  830. }
  831. compose:recover-target() {
  832. local backup_host="$1" ident="$2" src="$3" dst="$4" service_name="${5:-rsync-backup}" project_name
  833. project_name=$(compose:project_name) || return 1
  834. docker_image="${project_name}_${service_name}"
  835. if ! docker_has_image "$docker_image"; then
  836. compose build "${service_name}" || {
  837. err "Couldn't find nor build image for service '$service_name'."
  838. return 1
  839. }
  840. fi
  841. dst="${dst%/}" ## remove final slash
  842. ssh_options=(-o StrictHostKeyChecking=no)
  843. if [[ "$backup_host" == *":"* ]]; then
  844. port="${backup_host##*:}"
  845. backup_host="${backup_host%%:*}"
  846. ssh_options+=(-p "$port")
  847. else
  848. port=""
  849. backup_host="${backup_host%%:*}"
  850. fi
  851. rsync_opts=(
  852. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  853. -azvArH --delete --delete-excluded
  854. --partial --partial-dir .rsync-partial
  855. --numeric-ids
  856. )
  857. if [ "$DRY_RUN" ]; then
  858. rsync_opts+=("-n")
  859. fi
  860. cmd=(
  861. docker run --rm --entrypoint rsync \
  862. -v "/srv/datastore/config/${service_name}/var/lib/rsync":/var/lib/rsync \
  863. -v "${dst%/*}":/mnt/dest \
  864. "$docker_image" \
  865. "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "/mnt/dest/${dst##*/}"
  866. )
  867. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  868. "${cmd[@]}"
  869. }
  870. mailcow:recover-target() {
  871. local backup_host="$1" ident="$2" src="$3" dst="$4"
  872. dst="${dst%/}" ## remove final slash
  873. ssh_options=(-o StrictHostKeyChecking=no)
  874. if [[ "$backup_host" == *":"* ]]; then
  875. port="${backup_host##*:}"
  876. backup_host="${backup_host%%:*}"
  877. ssh_options+=(-p "$port")
  878. else
  879. port=""
  880. backup_host="${backup_host%%:*}"
  881. fi
  882. rsync_opts=(
  883. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  884. -azvArH --delete --delete-excluded
  885. --partial --partial-dir .rsync-partial
  886. --numeric-ids
  887. )
  888. if [ "$DRY_RUN" ]; then
  889. rsync_opts+=("-n")
  890. fi
  891. cmd=(
  892. rsync "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "${dst}"
  893. )
  894. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  895. "${cmd[@]}"
  896. }
  897. nextcloud:src:version() {
  898. local version
  899. if ! version=$(cat "/srv/datastore/data/${nextcloud_service}/var/www/html/version.php" 2>/dev/null); then
  900. err "Can't find version.php file to get last version installed."
  901. exit 1
  902. fi
  903. version=$(e "$version" | grep 'VersionString =' | cut -f 3 -d ' ' | cut -f 2 -d "'")
  904. if [ -z "$version" ]; then
  905. err "Can't figure out version from version.php content."
  906. exit 1
  907. fi
  908. echo "$version"
  909. }
  910. container:health:check-fix:container-aliveness() {
  911. local container_id="$1"
  912. timeout 5s docker inspect "$container_id" >/dev/null 2>&1
  913. errlvl=$?
  914. if [ "$errlvl" == 124 ]; then
  915. service_name=$(docker ps --filter id="$container_id" --format '{{.Label "com.docker.compose.service"}}')
  916. container_name=($(docker ps --filter id="$container_id" --format '{{.Names}}'))
  917. pid=$(ps ax -o pid,command -ww | grep docker-containerd-shim |
  918. grep "/$container_id" |
  919. sed -r 's/^ *//g' |
  920. cut -f 1 -d " ")
  921. if [ -z "$pid" ]; then
  922. err "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} doesn't answer to 'inspect' command. Can't find its PID neither."
  923. return 1
  924. fi
  925. echo "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} doesn't answer to 'inspect' command (pid: $pid)."
  926. Wrap -d "kill pid $pid and restart" <<EOF
  927. kill "$pid"
  928. sleep 2
  929. docker restart "$container_id"
  930. EOF
  931. fi
  932. return $errlvl
  933. }
  934. container:health:check-fix:no-matching-entries() {
  935. local container_id="$1"
  936. out=$(docker exec -u root "$container_id" echo 2>&1)
  937. errlvl=$?
  938. [ "$errlvl" == 0 ] && return 0
  939. service_name=$(docker ps --filter id="$container_id" --format '{{.Label "com.docker.compose.service"}}')
  940. container_name=($(docker ps --filter id="$container_id" --format '{{.Names}}'))
  941. if [ "$errlvl" == 126 ] && [[ "$out" == *"no matching entries in passwd file"* ]]; then
  942. echo "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} has ${DARKRED}no-matching-entries${NORMAL} bug." >&2
  943. Wrap -d "restarting container of ${DARKYELLOW}$service_name${NORMAL} twice" <<EOF
  944. docker restart "$container_id"
  945. sleep 2
  946. docker restart "$container_id"
  947. EOF
  948. return 2
  949. fi
  950. warn "Unknown issue with ${DARKYELLOW}$service_name${NORMAL}'s container:"
  951. echo " ${WHITE}cmd:${NORMAL} docker exec -ti $container_id echo" >&2
  952. echo "$out" | prefix " ${DARKGRAY}|${NORMAL} " >&2
  953. echo " ${DARKGRAY}..${NORMAL} leaving this as-is."
  954. return 1
  955. }
  956. docker:api() {
  957. local endpoint="$1"
  958. curl -sS --unix-socket /var/run/docker.sock "http://localhost$endpoint"
  959. }
  960. docker:containers:id() {
  961. docker:api /containers/json | jq -r ".[] | .Id"
  962. }
  963. docker:containers:names() {
  964. docker:api /containers/json | jq -r '.[] | .Names[0] | ltrimstr("/")'
  965. }
  966. docker:container:stats() {
  967. container="$1"
  968. docker:api "/containers/$container/stats?stream=false"
  969. }
  970. docker:containers:stats() {
  971. :cache: scope=session
  972. local jobs='' line container id_names sha names name data service project
  973. local DC="com.docker.compose"
  974. local PSF_values=(
  975. ".ID" ".Names" ".Label \"$DC.project\"" ".Label \"$DC.service\"" ".Image"
  976. )
  977. local PSF="$(printf "{{%s}} " "${PSF_values[@]}")"
  978. id_names=$(docker ps -a --format="$PSF") || return 1
  979. ## Create a docker container table from name/sha to service, project, image_name
  980. declare -A resolve
  981. while read-0a line; do
  982. sha=${line%% *}; line=${line#* }
  983. names=${line%% *}; line=${line#* }
  984. names=(${names//,/ })
  985. for name in "${names[@]}"; do
  986. resolve["$name"]="$line"
  987. done
  988. resolve["$sha"]="$line"
  989. done < <(printf "%s\n" "$id_names")
  990. declare -A data
  991. while read-0a line; do
  992. name=${line%% *}; line=${line#* }
  993. ts=${line%% *}; line=${line#* }
  994. resolved="${resolve["$name"]}"
  995. project=${resolved%% *}; resolved=${resolved#* }
  996. service=${resolved%% *}; resolved=${resolved#* }
  997. image_name="$resolved"
  998. if [ -z "$service" ]; then
  999. project="@"
  1000. service=$(docker inspect "$image_name" | jq -r '.[0].RepoTags[0]')
  1001. service=${service//\//_}
  1002. fi
  1003. if [ -n "${data["$project/$service"]}" ]; then
  1004. previous=(${data["$project/$service"]})
  1005. previous=(${previous[@]:1})
  1006. current=($line)
  1007. sum=()
  1008. i=0; max=${#previous[@]}
  1009. while (( i < max )); do
  1010. sum+=($((${previous[$i]} + ${current[$i]})))
  1011. ((i++))
  1012. done
  1013. data["$project/$service"]="$ts ${sum[*]}"
  1014. else
  1015. data["$project/$service"]="$ts $line"
  1016. fi
  1017. done < <(
  1018. for container in "$@"; do
  1019. (
  1020. docker:container:stats "${container}" |
  1021. jq -r '
  1022. (.name | ltrimstr("/"))
  1023. + " " + (.read | sub("\\.[0-9]+Z"; "Z") | fromdate | tostring)
  1024. + " " + (.memory_stats.usage | tostring)
  1025. + " " + (.memory_stats.stats.inactive_file | tostring)
  1026. + " " + ((.memory_stats.usage - .memory_stats.stats.inactive_file) | tostring)
  1027. + " " + (.memory_stats.limit | tostring)
  1028. + " " + (.networks.eth0.rx_bytes | tostring)
  1029. + " " + (.networks.eth0.rx_packets | tostring)
  1030. + " " + (.networks.eth0.rx_errors | tostring)
  1031. + " " + (.networks.eth0.rx_dropped | tostring)
  1032. + " " + (.networks.eth0.tx_bytes | tostring)
  1033. + " " + (.networks.eth0.tx_packets | tostring)
  1034. + " " + (.networks.eth0.tx_errors | tostring)
  1035. + " " + (.networks.eth0.tx_dropped | tostring)
  1036. '
  1037. ) &
  1038. jobs=1
  1039. done
  1040. [ -n "$jobs" ] && wait
  1041. )
  1042. for label in "${!data[@]}"; do
  1043. echo "$label ${data[$label]}"
  1044. done
  1045. }
  1046. decorator._mangle_fn docker:containers:stats
  1047. export -f docker:containers:stats
  1048. col:normalize:size() {
  1049. local alignment=$1
  1050. awk -v alignment="$alignment" '{
  1051. # Store the entire line in the lines array.
  1052. lines[NR] = $0;
  1053. # Split the line into fields.
  1054. split($0, fields);
  1055. # Update max for each field.
  1056. for (i = 1; i <= length(fields); i++) {
  1057. if (length(fields[i]) > max[i]) {
  1058. max[i] = length(fields[i]);
  1059. }
  1060. }
  1061. }
  1062. END {
  1063. # Print lines with fields padded to max.
  1064. for (i = 1; i <= NR; i++) {
  1065. split(lines[i], fields);
  1066. line = "";
  1067. for (j = 1; j <= length(fields); j++) {
  1068. # Get alignment for the current field.
  1069. align = substr(alignment, j, 1);
  1070. if (align != "+") {
  1071. align = "-"; # Default to left alignment if not "+".
  1072. }
  1073. line = line sprintf("%" align max[j] "s ", fields[j]);
  1074. }
  1075. print line;
  1076. }
  1077. }'
  1078. }
  1079. rrd:create() {
  1080. local prefix="$1"
  1081. shift
  1082. local label="$1" step="300" src_def
  1083. shift
  1084. if [ -z "$VAR_DIR" ]; then
  1085. err "Unset \$VAR_DIR, can't create rrd graph"
  1086. return 1
  1087. fi
  1088. mkdir -p "$VAR_DIR"
  1089. if ! [ -d "$VAR_DIR" ]; then
  1090. err "Invalid \$VAR_DIR: '$VAR_DIR' is not a directory"
  1091. return 1
  1092. fi
  1093. if ! type -p rrdtool >/dev/null 2>&1; then
  1094. apt-get install rrdtool -y --force-yes </dev/null
  1095. if ! type -p rrdtool 2>/dev/null 2>&1; then
  1096. err "Couldn't find nor install 'rrdtool'."
  1097. return 1
  1098. fi
  1099. fi
  1100. local RRD_PATH="$VAR_DIR/rrd"
  1101. local RRD_FILE="$RRD_PATH/$prefix/$label.rrd"
  1102. mkdir -p "${RRD_FILE%/*}"
  1103. if [ -f "$RRD_FILE" ]; then
  1104. err "File '$RRD_FILE' already exists, use a different label."
  1105. return 1
  1106. fi
  1107. local rrd_ds_opts=()
  1108. for src_def in "$@"; do
  1109. IFS=":" read -r name type min max rra_types <<<"$src_def"
  1110. rra_types=${rra_types:-average,max,min}
  1111. rrd_ds_opts+=("DS:$name:$type:900:$min:$max")
  1112. done
  1113. local step=120
  1114. local times=( ## with steps 120 is 2mn datapoint
  1115. 2m:1w
  1116. 6m:3w
  1117. 30m:12w
  1118. 3h:1y
  1119. 1d:10y
  1120. 1w:2080w
  1121. )
  1122. rrd_rra_opts=()
  1123. for time in "${times[@]}"; do
  1124. rrd_rra_opts+=("RRA:"{AVERAGE,MIN,MAX}":0.5:$time")
  1125. done
  1126. cmd=(
  1127. rrdtool create "$RRD_FILE" \
  1128. --step "$step" \
  1129. "${rrd_ds_opts[@]}" \
  1130. "${rrd_rra_opts[@]}"
  1131. )
  1132. "${cmd[@]}" || {
  1133. err "Failed command: ${cmd[@]}"
  1134. return 1
  1135. }
  1136. }
  1137. rrd:update() {
  1138. local prefix="$1"
  1139. shift
  1140. while read-0a data; do
  1141. [ -z "$data" ] && continue
  1142. IFS="~" read -ra data <<<"${data// /\~}"
  1143. label="${data[0]}"
  1144. ts="${data[1]}"
  1145. for arg in "$@"; do
  1146. IFS="|" read -r name arg <<<"$arg"
  1147. rrd_label="${label}/${name}"
  1148. rrd_create_opt=()
  1149. rrd_update_opt="$ts"
  1150. for col_def in ${arg//,/ }; do
  1151. col=${col_def%%:*}; create_def=${col_def#*:}
  1152. rrd_update_opt="${rrd_update_opt}:${data[$col]}"
  1153. rrd_create_opt+=("$create_def")
  1154. done
  1155. local RRD_ROOT_PATH="$VAR_DIR/rrd"
  1156. local RRD_PATH="$RRD_ROOT_PATH/${prefix%/}"
  1157. local RRD_FILE="${RRD_PATH%/}/${rrd_label#/}.rrd"
  1158. if ! [ -f "$RRD_FILE" ]; then
  1159. info "Creating new RRD file '${RRD_FILE#$RRD_ROOT_PATH/}'"
  1160. if ! rrd:create "$prefix" "${rrd_label}" "${rrd_create_opt[@]}" </dev/null ; then
  1161. err "Couldn't create new RRD file ${rrd_label} with options: '${rrd_create_opt[*]}'"
  1162. return 1
  1163. fi
  1164. fi
  1165. rrdtool update "$RRD_FILE" "$rrd_update_opt" || {
  1166. err "update failed with options: '$rrd_update_opt'"
  1167. return 1
  1168. }
  1169. done
  1170. done
  1171. }
  1172. [ "$SOURCED" ] && return 0
  1173. ##
  1174. ## Command line processing
  1175. ##
  1176. cmdline.spec.gnu
  1177. cmdline.spec.reporting
  1178. cmdline.spec.gnu install
  1179. cmdline.spec::cmd:install:run() {
  1180. :
  1181. }
  1182. cmdline.spec.gnu get-type
  1183. cmdline.spec::cmd:get-type:run() {
  1184. vps:get-type
  1185. }
  1186. cmdline.spec:install:cmd:backup:run() {
  1187. : :posarg: BACKUP_SERVER 'Target backup server'
  1188. : :optfla: --ignore-domain-check \
  1189. "Allow to bypass the domain check in
  1190. compose file (only used in compose
  1191. installation)."
  1192. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  1193. local vps_type
  1194. vps_type=$(vps:get-type) || {
  1195. err "Failed to get type of installation."
  1196. return 1
  1197. }
  1198. if ! fn.exists "${vps_type}:install-backup"; then
  1199. err "type '${vps_type}' has no backup installation implemented yet."
  1200. return 1
  1201. fi
  1202. opts=()
  1203. [ "$opt_ignore_ping_check" ] &&
  1204. opts+=("--ignore-ping-check")
  1205. if [ "$vps_type" == "compose" ]; then
  1206. [ "$opt_ignore_domain_check" ] &&
  1207. opts+=("--ignore-domain-check")
  1208. fi
  1209. "cmdline.spec:install:cmd:${vps_type}-backup:run" "${opts[@]}" "$BACKUP_SERVER"
  1210. }
  1211. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  1212. cmdline.spec.gnu compose-backup
  1213. cmdline.spec:install:cmd:compose-backup:run() {
  1214. : :posarg: BACKUP_SERVER 'Target backup server'
  1215. : :optval: --service-name,-s "YAML service name in compose
  1216. file to check for existence of key.
  1217. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  1218. : :optfla: --ignore-domain-check \
  1219. "Allow to bypass the domain check in
  1220. compose file."
  1221. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  1222. local service_name compose_file
  1223. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  1224. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  1225. compose_file=$(compose:get-compose-yml) || return 1
  1226. if ! [ -e "$compose_file" ]; then
  1227. err "Compose file not found in '$compose_file'."
  1228. return 1
  1229. fi
  1230. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" \
  1231. "$opt_ignore_ping_check" "$opt_ignore_domain_check"
  1232. }
  1233. cmdline.spec:install:cmd:mailcow-backup:run() {
  1234. : :posarg: BACKUP_SERVER 'Target backup server'
  1235. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  1236. "mailcow:install-backup" "$BACKUP_SERVER" "$opt_ignore_ping_check"
  1237. }
  1238. cmdline.spec.gnu backup
  1239. cmdline.spec::cmd:backup:run() {
  1240. local vps_type
  1241. vps_type=$(vps:get-type) || {
  1242. err "Failed to get type of installation."
  1243. return 1
  1244. }
  1245. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  1246. err "type '${vps_type}' has no backup process implemented yet."
  1247. return 1
  1248. fi
  1249. "cmdline.spec:backup:cmd:${vps_type}:run"
  1250. }
  1251. cmdline.spec:backup:cmd:mailcow:run() {
  1252. local cmd_line cron_line cmd
  1253. for f in mysql-backup mirror-dir; do
  1254. [ -e "/etc/cron.d/$f" ] || {
  1255. err "Can't find '/etc/cron.d/$f'."
  1256. echo " Have you forgotten to run 'vps install backup BACKUP_HOST' ?" >&2
  1257. return 1
  1258. }
  1259. if ! cron_line=$(cat "/etc/cron.d/$f" |
  1260. grep -v "^#" | grep "\* \* \*"); then
  1261. err "Can't find cron_line in '/etc/cron.d/$f'." \
  1262. "Have you modified it ?"
  1263. return 1
  1264. fi
  1265. cron_line=${cron_line%|*}
  1266. cmd_line=(${cron_line#*root})
  1267. if [ "$f" == "mirror-dir" ]; then
  1268. cmd=()
  1269. for arg in "${cmd_line[@]}"; do
  1270. [ "$arg" != "-q" ] && cmd+=("$arg")
  1271. done
  1272. else
  1273. cmd=("${cmd_line[@]}")
  1274. fi
  1275. code="${cmd[*]}"
  1276. echo "${WHITE}Launching:${NORMAL} ${code}"
  1277. {
  1278. {
  1279. (
  1280. ## Some commands are using colors that are already
  1281. ## set by this current program and will trickle
  1282. ## down unwantedly
  1283. ansi_color no
  1284. eval "${code}"
  1285. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  1286. set_errlvl "${PIPESTATUS[0]}"
  1287. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  1288. set_errlvl "${PIPESTATUS[0]}"
  1289. } 3>&1 1>&2 2>&3
  1290. if [ "$?" != "0" ]; then
  1291. err "Failed."
  1292. return 1
  1293. fi
  1294. done
  1295. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  1296. }
  1297. set_errlvl() { return "${1:-1}"; }
  1298. cmdline.spec:backup:cmd:compose:run() {
  1299. local cron_line args
  1300. project_name=$(compose:project_name) || return 1
  1301. docker_cmd=$(compose:get_cron_docker_cmd) || return 1
  1302. echo "${WHITE}Launching:${NORMAL} docker exec -i "${project_name}_cron_1" $docker_cmd"
  1303. {
  1304. {
  1305. eval "docker exec -i \"${project_name}_cron_1\" $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  1306. set_errlvl "${PIPESTATUS[0]}"
  1307. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  1308. set_errlvl "${PIPESTATUS[0]}"
  1309. } 3>&1 1>&2 2>&3
  1310. if [ "$?" != "0" ]; then
  1311. err "Failed."
  1312. return 1
  1313. fi
  1314. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  1315. }
  1316. cmdline.spec.gnu recover-target
  1317. cmdline.spec::cmd:recover-target:run() {
  1318. : :posarg: BACKUP_DIR 'Source directory on backup side'
  1319. : :posarg: HOST_DIR 'Target directory on host side'
  1320. : :optval: --backup-host,-B "The backup host"
  1321. : :optfla: --dry-run,-n "Don't do anything, instead tell what it
  1322. would do."
  1323. ## if no backup host take the one by default
  1324. backup_host="$opt_backup_host"
  1325. if [ -z "$backup_host" ]; then
  1326. backup_host_ident=$(backup-action get_default_backup_host_ident) || return 1
  1327. read -r backup_host ident <<<"$backup_host_ident"
  1328. fi
  1329. if [[ "$BACKUP_DIR" == /* ]]; then
  1330. err "BACKUP_DIR must be a relative path from the root of your backup."
  1331. return 1
  1332. fi
  1333. REAL_HOST_DIR=$(realpath "$HOST_DIR") || {
  1334. err "Can't find HOST_DIR '$HOST_DIR'."
  1335. return 1
  1336. }
  1337. export DRY_RUN="${opt_dry_run}"
  1338. backup-action recover-target "$backup_host" "$ident" "$BACKUP_DIR" "$REAL_HOST_DIR"
  1339. }
  1340. cmdline.spec.gnu odoo
  1341. cmdline.spec::cmd:odoo:run() {
  1342. :
  1343. }
  1344. cmdline.spec.gnu restart
  1345. cmdline.spec:odoo:cmd:restart:run() {
  1346. : :optval: --service,-s "The service (defaults to 'odoo')"
  1347. local out odoo_service
  1348. odoo_service="${opt_service:-odoo}"
  1349. project_name=$(compose:project_name) || return 1
  1350. container_name=$(compose:service:container:get "${project_name}" "${odoo_service}") || return 1
  1351. if ! out=$(docker restart "${container_name}" 2>/dev/null); then
  1352. if [[ "$out" == *"no matching entries in passwd file" ]]; then
  1353. warn "Catched docker bug. Restarting once more."
  1354. if ! out=$(docker restart "${container_name}" 2>&1); then
  1355. err "Can't restart container ${container_name}."
  1356. echo " output:" >&2
  1357. echo "$out" | prefix " ${GRAY}|${NORMAL} " >&2
  1358. exit 1
  1359. fi
  1360. else
  1361. err "Couldn't restart container ${container_name} (and no restart bug detected)."
  1362. exit 1
  1363. fi
  1364. fi
  1365. info "Container ${CYAN}${container_name}${NORMAL} was ${DARKGREEN}successfully${NORMAL} restarted."
  1366. if [[ "${container_name}" == "${project_name}_${odoo_service}_run_"* ]]; then
  1367. echo " ${WHITE}Note:${NORMAL} You can re-attach to your container with:" >&2
  1368. echo " docker container attach ${container_name}" >&2
  1369. fi
  1370. }
  1371. cmdline.spec.gnu restore
  1372. cmdline.spec:odoo:cmd:restore:run() {
  1373. : :posarg: ZIP_DUMP_LOCATION 'Source odoo dump file to restore
  1374. (can be a local file or an url)'
  1375. : :optval: --service,-s "The service (defaults to 'odoo')"
  1376. : :optval: --database,-D 'Target database (default if not specified)'
  1377. : :optfla: --neutralize,-n "Restore database in neutralized state."
  1378. : :optfla: --debug,-d "Display more information."
  1379. local out
  1380. unzip:require "6.0" || return 1
  1381. odoo_service="${opt_service:-odoo}"
  1382. if [[ "$ZIP_DUMP_LOCATION" == "http://"* ]] ||
  1383. [[ "$ZIP_DUMP_LOCATION" == "https://"* ]]; then
  1384. settmpdir ZIP_TMP_LOCATION
  1385. tmp_location="$ZIP_TMP_LOCATION/dump.zip"
  1386. curl -k -s -L "$ZIP_DUMP_LOCATION" > "$tmp_location" || {
  1387. err "Couldn't get '$ZIP_DUMP_LOCATION'."
  1388. exit 1
  1389. }
  1390. if [[ "$(dd if="$tmp_location" count=2 bs=1 2>/dev/null)" != "PK" ]]; then
  1391. err "Download doesn't seem to be a zip file."
  1392. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1393. exit 1
  1394. fi
  1395. info "Successfully downloaded '$ZIP_DUMP_LOCATION'"
  1396. echo " in '$tmp_location'." >&2
  1397. ZIP_DUMP_LOCATION="$tmp_location"
  1398. fi
  1399. [ -e "$ZIP_DUMP_LOCATION" ] || {
  1400. err "No file '$ZIP_DUMP_LOCATION' found." >&2
  1401. exit 1
  1402. }
  1403. opts_compose=()
  1404. [ -t 1 ] && opts_compose+=("--color")
  1405. [ "$opt_debug" ] && {
  1406. VERBOSE=1
  1407. opts_compose+=("--debug")
  1408. }
  1409. opts_load=()
  1410. [ "$opt_neutralize" ] && opts_load+=("--neutralize")
  1411. unzip:check-integrity "$ZIP_DUMP_LOCATION" || {
  1412. if [ -z "$opt_debug" ]; then
  1413. echo " Use \`\`--debug\`\` (or \`\`-d\`\`) to get more information." >&2
  1414. fi
  1415. return 1
  1416. }
  1417. project_name=$(compose:project_name) || exit 1
  1418. container_name=$(compose:service:container:get "${project_name}" "${odoo_service}") || exit 1
  1419. info "Found container ${CYAN}${container_name}${NORMAL} for service ${DARKYELLOW}$odoo_service${NORMAL}."
  1420. container:health:check-fix:no-matching-entries "${container_name}"
  1421. case "$?" in
  1422. 0)
  1423. debug "Container ${CYAN}${container_name}${NORMAL} is ${DARKGREEN}healthy${NORMAL}."
  1424. ;;
  1425. 1) err "Container ${CYAN}${container_name}${NORMAL} is not healthy."
  1426. exit 1
  1427. ;;
  1428. 2) info "Container ${CYAN}${container_name}${NORMAL} was ${DARKYELLOW}fixed${NORMAL}."
  1429. ;;
  1430. esac
  1431. msg_dbname=default
  1432. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1433. Wrap -vsd "drop $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}" -- \
  1434. compose --no-hooks "${opts_compose[@]}" drop "$odoo_service" $opt_database || {
  1435. err "Error dropping $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}:"
  1436. [ -z "$opt_debug" ] && {
  1437. echo " Use \`\`--debug\`\` (or \`\`-d\`\`) to get more information." >&2
  1438. }
  1439. exit 1
  1440. }
  1441. Wrap -vsd "restore $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}" -- \
  1442. compose --no-hooks "${opts_compose[@]}" \
  1443. load "$odoo_service" $opt_database "${opts_load[@]}" < "$ZIP_DUMP_LOCATION" || {
  1444. err "Error restoring service ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  1445. [ -z "$opt_debug" ] && {
  1446. echo " Use \`\`--debug\`\` (or \`\`-d\`\`) to get more information." >&2
  1447. }
  1448. exit 1
  1449. }
  1450. ## Restart odoo, ensure there is no bugs lingering on it.
  1451. cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  1452. }
  1453. cmdline.spec.gnu dump
  1454. cmdline.spec:odoo:cmd:dump:run() {
  1455. : :posarg: DUMP_ZIPFILE 'Target path to store odoo dump zip file.'
  1456. : :optval: --database,-D 'Target database (default if not specified)'
  1457. : :optval: --service,-s "The service (defaults to 'odoo')"
  1458. : :optfla: --debug,-d "Display debugging information"
  1459. : :optfla: --force,-f "Force the dump even if the file already exists"
  1460. odoo_service="${opt_service:-odoo}"
  1461. unzip:require "6.0" || return 1
  1462. msg_dbname=default
  1463. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1464. opts_compose=()
  1465. [ -t 1 ] && opts_compose+=("--color")
  1466. [ "$opt_debug" ] && {
  1467. VERBOSE=1
  1468. opts_compose+=("--debug")
  1469. }
  1470. if [ -e "$DUMP_ZIPFILE" ]; then
  1471. if [ -z "$opt_force" ]; then
  1472. err "File '$DUMP_ZIPFILE' already exists."
  1473. echo " Use \`\`--force\`\` (or \`\`-f\`\`) to overwrite it." >&2
  1474. exit 1
  1475. else
  1476. info "Removing previous file '$DUMP_ZIPFILE'."
  1477. rm -f "$DUMP_ZIPFILE" || return 1
  1478. fi
  1479. fi
  1480. max_retry=5
  1481. retry=1
  1482. success=
  1483. while [ "$retry" -le "$max_retry" ]; do
  1484. if [ "$retry" -gt 1 ]; then
  1485. info "Retry $retry/$max_retry to dump ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1486. fi
  1487. compose --no-hooks "${opts_compose[@]}" save "$odoo_service" $opt_database > "$DUMP_ZIPFILE" || {
  1488. err "Failed dumping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1489. return 1
  1490. }
  1491. unzip:check-integrity "$DUMP_ZIPFILE" || {
  1492. err "Dump produced an invalid zip file '$DUMP_ZIPFILE'. Deleting it."
  1493. if [ -z "$opt_debug" ]; then
  1494. echo " Use \`\`--debug\`\` (or \`\`-d\`\`) to get more information." >&2
  1495. fi
  1496. rm -f "$DUMP_ZIPFILE"
  1497. ((retry++))
  1498. continue
  1499. }
  1500. success=1
  1501. break
  1502. done
  1503. if [ -z "$success" ]; then
  1504. err "Failed to dump ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1505. exit 1
  1506. fi
  1507. info "Successfully dumped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1508. }
  1509. cmdline.spec.gnu drop
  1510. cmdline.spec:odoo:cmd:drop:run() {
  1511. : :optval: --database,-d 'Target database (default if not specified)'
  1512. : :optval: --service,-s "The service (defaults to 'odoo')"
  1513. odoo_service="${opt_service:-odoo}"
  1514. msg_dbname=default
  1515. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1516. compose --no-hooks drop "$odoo_service" $opt_database || {
  1517. err "Error dropping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1518. exit 1
  1519. }
  1520. info "Successfully dropped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1521. }
  1522. cmdline.spec.gnu set-cyclos-url
  1523. cmdline.spec:odoo:cmd:set-cyclos-url:run() {
  1524. : :optval: --database,-d "Target database ('odoo' if not specified)"
  1525. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1526. local URL
  1527. dbname=${opt_database:-odoo}
  1528. cyclos_service="${opt_service:-cyclos}"
  1529. project_name=$(compose:project_name) || exit 1
  1530. URL=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1531. Wrap -d "set cyclos url to '$URL'" <<EOF || exit 1
  1532. echo "UPDATE res_company SET cyclos_server_url = '$URL/api' WHERE id=1;" |
  1533. compose:psql "$project_name" "$dbname" || {
  1534. err "Failed to set cyclos url value in '$dbname' database."
  1535. exit 1
  1536. }
  1537. EOF
  1538. }
  1539. cmdline.spec.gnu fix-sso
  1540. cmdline.spec:odoo:cmd:fix-sso:run() {
  1541. : :optval: --database,-d "Target database ('odoo' if not specified)"
  1542. local public_user_id project_name dbname
  1543. dbname=${opt_database:-odoo}
  1544. project_name=$(compose:project_name) || exit 1
  1545. public_user_id=$(odoo:get_public_user_id "${project_name}" "${dbname}") || exit 1
  1546. Wrap -d "fix website's object to 'public_user' (id=$public_user_id)" <<EOF || exit 1
  1547. echo "UPDATE website SET user_id = $public_user_id;" |
  1548. compose:psql "$project_name" "$dbname" || {
  1549. err "Failed to set website's object user_id to public user's id ($public_user_id) in '$dbname' database."
  1550. exit 1
  1551. }
  1552. EOF
  1553. }
  1554. cmdline.spec.gnu cyclos
  1555. cmdline.spec::cmd:cyclos:run() {
  1556. :
  1557. }
  1558. cmdline.spec:cyclos:cmd:dump:run() {
  1559. : :posarg: DUMP_GZFILE 'Target path to store odoo dump gz file.'
  1560. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1561. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1562. cyclos_service="${opt_service:-cyclos}"
  1563. cyclos_database="${opt_database:-cyclos}"
  1564. project_name=$(compose:project_name) || exit 1
  1565. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1566. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1567. docker stop "$container_id" || exit 1
  1568. Wrap -d "Dump postgres database '${cyclos_database}'." -- \
  1569. postgres:dump "${project_name}" "$cyclos_database" "$DUMP_GZFILE" || exit 1
  1570. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1571. docker start "${container_id}" || exit 1
  1572. }
  1573. cmdline.spec.gnu restore
  1574. cmdline.spec:cyclos:cmd:restore:run() {
  1575. : :posarg: GZ_DUMP_LOCATION 'Source cyclos dump file to restore
  1576. (can be a local file or an url)'
  1577. : :optval: --service,-s "The service (defaults to 'cyclos')"
  1578. : :optval: --database,-d 'Target database (default if not specified)'
  1579. local out
  1580. cyclos_service="${opt_service:-cyclos}"
  1581. cyclos_database="${opt_database:-cyclos}"
  1582. project_name=$(compose:project_name) || exit 1
  1583. url=$(compose:get_url "${project_name}" "${cyclos_service}") || return 1
  1584. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1585. if [[ "$GZ_DUMP_LOCATION" == "http://"* ]] ||
  1586. [[ "$GZ_DUMP_LOCATION" == "https://"* ]]; then
  1587. settmpdir GZ_TMP_LOCATION
  1588. tmp_location="$GZ_TMP_LOCATION/dump.gz"
  1589. Wrap -d "get '$GZ_DUMP_LOCATION'" <<EOF || exit 1
  1590. ## Note that curll version before 7.76.0 do not have
  1591. curl -k -s -L "$GZ_DUMP_LOCATION" --fail \\
  1592. > "$tmp_location" || {
  1593. echo "Error fetching ressource. Is url correct ?" >&2
  1594. exit 1
  1595. }
  1596. if [[ "\$(dd if="$tmp_location" count=2 bs=1 2>/dev/null |
  1597. hexdump -v -e "/1 \"%02x\"")" != "1f8b" ]]; then
  1598. err "Download doesn't seem to be a gzip file."
  1599. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1600. exit 1
  1601. fi
  1602. EOF
  1603. GZ_DUMP_LOCATION="$tmp_location"
  1604. fi
  1605. [ -e "$GZ_DUMP_LOCATION" ] || {
  1606. err "No file '$GZ_DUMP_LOCATION' found." >&2
  1607. exit 1
  1608. }
  1609. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1610. docker stop "$container_id" || exit 1
  1611. ## XXXvlab: making the assumption that the postgres username should
  1612. ## be the same as the cyclos service selected (which is the default,
  1613. ## but not always the case).
  1614. Wrap -d "restore postgres database '${cyclos_database}'." -- \
  1615. postgres:restore "$project_name" "$GZ_DUMP_LOCATION" "${cyclos_service}@${cyclos_database}" || exit 1
  1616. ## ensure that the database is not locked
  1617. Wrap -d "check and remove database lock if any" -- \
  1618. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1619. Wrap -d "set root url to '$url'" -- \
  1620. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1621. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1622. docker start "${container_id}" || exit 1
  1623. }
  1624. cmdline.spec.gnu set-root-url
  1625. cmdline.spec:cyclos:cmd:set-root-url:run() {
  1626. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1627. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1628. local URL
  1629. cyclos_database=${opt_database:-cyclos}
  1630. cyclos_service="${opt_service:-cyclos}"
  1631. project_name=$(compose:project_name) || exit 1
  1632. url=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1633. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1634. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1635. docker stop "$container_id" || exit 1
  1636. Wrap -d "set root url to '$url'" -- \
  1637. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1638. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1639. docker start "${container_id}" || exit 1
  1640. }
  1641. cmdline.spec.gnu unlock
  1642. cmdline.spec:cyclos:cmd:unlock:run() {
  1643. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1644. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1645. local URL
  1646. cyclos_database=${opt_database:-cyclos}
  1647. cyclos_service="${opt_service:-cyclos}"
  1648. project_name=$(compose:project_name) || exit 1
  1649. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1650. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1651. docker stop "$container_id" || exit 1
  1652. Wrap -d "check and remove database lock if any" -- \
  1653. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1654. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1655. docker start "${container_id}" || exit 1
  1656. }
  1657. cmdline.spec.gnu rocketchat
  1658. cmdline.spec::cmd:rocketchat:run() {
  1659. :
  1660. }
  1661. cmdline.spec.gnu drop-indexes
  1662. cmdline.spec:rocketchat:cmd:drop-indexes:run() {
  1663. : :optval: --database,-d "Target database ('rocketchat' if not specified)"
  1664. : :optval: --service,-s "The rocketchat service name (defaults to 'rocketchat')"
  1665. local URL
  1666. rocketchat_database=${opt_database:-rocketchat}
  1667. rocketchat_service="${opt_service:-rocketchat}"
  1668. project_name=$(compose:project_name) || exit 1
  1669. container_id=$(compose:service:container_one "${project_name}" "${rocketchat_service}") || exit 1
  1670. Wrap -d "stop ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1671. docker stop "$container_id" || exit 1
  1672. errlvl=0
  1673. Wrap -d "drop indexes" -- \
  1674. rocketchat:drop-indexes "${project_name}" "${rocketchat_database}" || {
  1675. errlvl=1
  1676. errmsg="Failed to drop indexes"
  1677. }
  1678. Wrap -d "start ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1679. docker start "${container_id}" || exit 1
  1680. if [ "$errlvl" != 0 ]; then
  1681. err "$errmsg"
  1682. fi
  1683. exit "$errlvl"
  1684. }
  1685. cmdline.spec.gnu nextcloud
  1686. cmdline.spec::cmd:nextcloud:run() {
  1687. :
  1688. }
  1689. cmdline.spec.gnu upgrade
  1690. cmdline.spec:nextcloud:cmd:upgrade:run() {
  1691. : :posarg: [TARGET_VERSION] "Target version to migrate to"
  1692. : :optval: --service,-s "The nexcloud service name (defaults to 'nextcloud')"
  1693. local URL
  1694. nextcloud_service="${opt_service:-nextcloud}"
  1695. project_name=$(compose:project_name) || exit 1
  1696. containers=$(compose:service:containers "${project_name}" "${nextcloud_service}") || exit 1
  1697. container_stopped=()
  1698. if [ -n "$containers" ]; then
  1699. for container in $containers; do
  1700. Wrap -d "stop ${DARKYELLOW}${nextcloud_service}${NORMAL}'s container" -- \
  1701. docker stop "$container" || {
  1702. err "Failed to stop container '$container'."
  1703. exit 1
  1704. }
  1705. container_stopped+=("$container")
  1706. done
  1707. fi
  1708. before_version=$(nextcloud:src:version) || exit 1
  1709. ## -q to remove the display of ``compose`` related information
  1710. ## like relation resolution.
  1711. ## --no-hint to remove the final hint about modifying your
  1712. ## ``compose.yml``.
  1713. compose -q upgrade "$nextcloud_service" --no-hint "$TARGET_VERSION"
  1714. errlvl="$?"
  1715. after_version=$(nextcloud:src:version)
  1716. if [ "$after_version" != "$before_version" ]; then
  1717. desc="update \`compose.yml\` to set ${DARKYELLOW}$nextcloud_service${NORMAL}'s "
  1718. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  1719. Wrap -d "$desc" -- \
  1720. compose:file:value-change \
  1721. "${nextcloud_service}.docker-compose.image" \
  1722. "docker.0k.io/nextcloud:${after_version}-myc" || exit 1
  1723. fi
  1724. if [ "$errlvl" == 0 ]; then
  1725. echo "${WHITE}Launching final compose${NORMAL}"
  1726. compose up || exit 1
  1727. fi
  1728. exit "$errlvl"
  1729. }
  1730. cmdline.spec.gnu check-fix
  1731. cmdline.spec::cmd:check-fix:run() {
  1732. : :posarg: [SERVICES...] "Optional service to check"
  1733. : :optval: --check,-c "Specify a check or a list of checks separated by commas"
  1734. : :optfla: --silent,-s "Don't ouput anything if everything goes well"
  1735. local project_name service_name containers container check
  1736. all_checks=$(declare -F |
  1737. egrep '^declare -fx? container:health:check-fix:[^ ]+$' |
  1738. cut -f 4 -d ":")
  1739. checks=(${opt_check//,/ })
  1740. for check in "${checks[@]}"; do
  1741. fn.exists container:health:check-fix:$check || {
  1742. err "check '$check' not found."
  1743. return 1
  1744. }
  1745. done
  1746. if [ "${#checks[*]}" == 0 ]; then
  1747. checks=($all_checks)
  1748. fi
  1749. ## XXXvlab: could make it parallel
  1750. project_name=$(compose:project_name) || exit 1
  1751. containers=($(compose:project:containers "${project_name}")) || exit 1
  1752. found=
  1753. for container in "${containers[@]}"; do
  1754. service_name=$(docker ps --filter id="$container" --format '{{.Label "com.docker.compose.service"}}')
  1755. if [ "${#SERVICES[@]}" -gt 0 ]; then
  1756. [[ " ${SERVICES[*]} " == *" $service_name "* ]] || continue
  1757. fi
  1758. found=1
  1759. one_bad=
  1760. for check in "${checks[@]}"; do
  1761. if ! container:health:check-fix:"$check" "$container"; then
  1762. one_bad=1
  1763. fi
  1764. done
  1765. if [ -z "$opt_silent" ] && [ -z "$one_bad" ]; then
  1766. Elt "containers have been checked for ${DARKYELLOW}$service_name${NORMAL}"
  1767. Feedback
  1768. fi
  1769. done
  1770. if [ -z "$found" ]; then
  1771. if [ -z "$opt_silent" ]; then
  1772. if [ "${#SERVICES[@]}" -gt 0 ]; then
  1773. warn "No container for given services found in current project '$project_name'."
  1774. else
  1775. warn "No container found for current project '$project_name'."
  1776. fi
  1777. fi
  1778. return 1
  1779. fi
  1780. }
  1781. awk:require() {
  1782. local require_at_least="$1" version already_installed
  1783. while true; do
  1784. if ! version=$(awk --version 2>/dev/null); then
  1785. version=""
  1786. else
  1787. version=${version%%,*}
  1788. version=${version##* }
  1789. fi
  1790. if [ -z "$version" ] || version_gt "$require_at_least" "$version"; then
  1791. if [ -z "$already_installed" ]; then
  1792. if [ -z "$version" ]; then
  1793. info "No 'gawk' available, probably using a clone. Installing 'gawk'..."
  1794. else
  1795. info "Found gawk version '$version'. Updating 'gawk'..."
  1796. fi
  1797. apt-get install gawk -y </dev/null || {
  1798. err "Failed to install 'gawk'."
  1799. return 1
  1800. }
  1801. already_installed=true
  1802. else
  1803. if [ -z "$version" ]; then
  1804. err "No 'gawk' available even after having installed one"
  1805. else
  1806. err "'gawk' version '$version' is lower than required" \
  1807. "'$require_at_least' even after updating 'gawk'."
  1808. fi
  1809. return 1
  1810. fi
  1811. continue
  1812. fi
  1813. return 0
  1814. done
  1815. }
  1816. unzip:require() {
  1817. local require_at_least="$1" version already_installed
  1818. while true; do
  1819. if ! version=$(unzip -v 2>/dev/null); then
  1820. version=""
  1821. else
  1822. ## Version is in the first line, here's an example:
  1823. ##
  1824. ## UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
  1825. version=${version##UnZip }
  1826. version=${version%% of*}
  1827. fi
  1828. if [ -z "$version" ] || version_gt "$require_at_least" "$version"; then
  1829. if [ -z "$already_installed" ]; then
  1830. if [ -z "$version" ]; then
  1831. info "No 'unzip' available, probably using a clone. Installing 'unzip'..."
  1832. else
  1833. info "Found unzip version '$version'. Updating 'unzip'..."
  1834. fi
  1835. apt-get install unzip -y </dev/null || {
  1836. err "Failed to install 'unzip'."
  1837. return 1
  1838. }
  1839. already_installed=true
  1840. else
  1841. if [ -z "$version" ]; then
  1842. err "No 'unzip' available even after having installed one"
  1843. else
  1844. err "'unzip' version '$version' is lower than required" \
  1845. "'$require_at_least' even after updating 'unzip'."
  1846. fi
  1847. return 1
  1848. fi
  1849. continue
  1850. fi
  1851. return 0
  1852. done
  1853. }
  1854. unzip:check-integrity() {
  1855. local file="$1"
  1856. if ! out=$(unzip -t "$file" 2>&1); then
  1857. ## remove OK files
  1858. out=$(printf "%s\n" "$out" | egrep -v "^\s+testing: .*\s+OK$")
  1859. ## if last line is not "No errors detected in compressed data of $file."
  1860. ## then it's an error
  1861. last_line=${out##*$'\n'}
  1862. if [ "$last_line" == "No errors detected in compressed data of $file." ]; then
  1863. return 0
  1864. fi
  1865. err "Zip file '$file' failed integrity check."
  1866. if [ -n "$VERBOSE" ]; then
  1867. printf "%s\n" "$out" | prefix " ${GRAY}|${NORMAL} " >&2
  1868. fi
  1869. return 1
  1870. fi
  1871. return 0
  1872. }
  1873. resource:list() {
  1874. declare -F | egrep -- '-fx? stats:[a-zA-Z0-9_.-]+$' | cut -f 3- -d " " | cut -f 2- -d ":"
  1875. }
  1876. cmdline.spec.gnu stats
  1877. cmdline.spec::cmd:stats:run() {
  1878. : :optval: --format,-f "Either 'silent', 'raw', or 'pretty', default is pretty."
  1879. : :optfla: --silent,-s "Shorthand for '--format silent'"
  1880. : :optval: --resource,-r "resource(s) separated with a comma
  1881. (available resources: $(resource:list))"
  1882. local project_name service_name containers container check
  1883. if [[ -n "${opt_silent}" ]]; then
  1884. if [[ -n "${opt_format}" ]]; then
  1885. err "'--silent' conflict with option '--format'."
  1886. return 1
  1887. fi
  1888. opt_format=s
  1889. fi
  1890. opt_format="${opt_format:-pretty}"
  1891. case "${opt_format}" in
  1892. raw|r)
  1893. opt_format="raw"
  1894. :
  1895. ;;
  1896. silent|s)
  1897. opt_format="silent"
  1898. ;;
  1899. pretty|p)
  1900. opt_format="pretty"
  1901. awk:require 4.1.4 || return 1
  1902. ;;
  1903. *)
  1904. err "Invalid value '$opt_format' for option --format"
  1905. echo " use either 'raw' (shorthand 'r'), 'silent' (shorthand 's') or pretty (shorthand 'p')." >&2
  1906. return 1
  1907. esac
  1908. local resources=(c.{memory,network} load_avg disk-2)
  1909. if [ -n "${opt_resource}" ]; then
  1910. resources=(${opt_resource//,/ })
  1911. fi
  1912. local not_found=()
  1913. for resource in "${resources[@]}"; do
  1914. if ! fn.exists "stats:$resource"; then
  1915. not_found+=("$resource")
  1916. fi
  1917. done
  1918. if [[ "${#not_found[@]}" -gt 0 ]]; then
  1919. not_found_msg=$(printf "%s, " "${not_found[@]}")
  1920. not_found_msg=${not_found_msg%, }
  1921. err "Unsupported resource(s) provided: ${not_found_msg}"
  1922. echo " resource must be one-of:" >&2
  1923. resource:list | prefix " - " >&2
  1924. return 1
  1925. fi
  1926. :state-dir:
  1927. for resource in "${resources[@]}"; do
  1928. if [ "$opt_format" == "pretty" ]; then
  1929. echo "${WHITE}$resource${NORMAL}:"
  1930. stats:"$resource" "$opt_format" 2>&1 | prefix " "
  1931. else
  1932. stats:"$resource" "$opt_format" 2>&1 | prefix "$resource "
  1933. fi
  1934. set_errlvl "${PIPESTATUS[0]}" || return 1
  1935. done
  1936. }
  1937. stats:c.memory() {
  1938. local format="$1"
  1939. local out
  1940. container_to_check=($(docker:running_containers)) || exit 1
  1941. out=$(docker:containers:stats "${container_to_check[@]}")
  1942. printf "%s\n" "$out" | rrd:update "containers" "memory|3:usage:GAUGE:U:U,4:inactive:GAUGE:U:U" || {
  1943. return 1
  1944. }
  1945. case "${format:-p}" in
  1946. raw|r)
  1947. printf "%s\n" "$out" | cut -f 1-5 -d " "
  1948. ;;
  1949. pretty|p)
  1950. awk:require 4.1.4 || return 1
  1951. {
  1952. echo "container" "__total____" "buffered____" "resident____"
  1953. printf "%s\n" "$out" |
  1954. awk '
  1955. {
  1956. offset = strftime("%z", $2);
  1957. print $1, substr($0, index($0,$3));
  1958. }' | cut -f 1-4 -d " " |
  1959. numfmt --field 2-4 --to=iec-i --format=%8.1fB |
  1960. sed -r 's/(\.[0-9])([A-Z]?iB)/\1:\2/g' |
  1961. sort
  1962. } | col:normalize:size -+++ |
  1963. sed -r 's/(\.[0-9]):([A-Z]?iB)/\1 \2/g' |
  1964. header:make
  1965. ;;
  1966. esac
  1967. }
  1968. stats:c.network() {
  1969. local format="$1"
  1970. local out
  1971. container_to_check=($(docker:running_containers)) || exit 1
  1972. out=$(docker:containers:stats "${container_to_check[@]}")
  1973. cols=(
  1974. {rx,tx}_{bytes,packets,errors,dropped}
  1975. )
  1976. idx=5 ## starting column idx for next fields
  1977. defs=()
  1978. for col in "${cols[@]}"; do
  1979. defs+=("$((idx++)):${col}:COUNTER:U:U")
  1980. done
  1981. OLDIFS="$IFS"
  1982. IFS="," defs="${defs[*]}"
  1983. IFS="$OLDIFS"
  1984. printf "%s\n" "$out" |
  1985. rrd:update "containers" \
  1986. "network|${defs}" || {
  1987. return 1
  1988. }
  1989. case "${format:-p}" in
  1990. raw|r)
  1991. printf "%s\n" "$out" | cut -f 1,2,7- -d " "
  1992. ;;
  1993. pretty|p)
  1994. awk:require 4.1.4 || return 1
  1995. {
  1996. echo "container" "_" "_" "_" "RX" "_" "_" "_" "TX"
  1997. echo "_" "__bytes____" "__packets" "__errors" "__dropped" "__bytes____" "__packets" "__errors" "__dropped"
  1998. printf "%s\n" "$out" |
  1999. awk '
  2000. {
  2001. offset = strftime("%z", $2);
  2002. print $1, substr($0, index($0,$7));
  2003. }' |
  2004. numfmt --field 2,6 --to=iec-i --format=%8.1fB |
  2005. numfmt --field 3,4,5,7,8,9 --to=si --format=%8.1f |
  2006. sed -r 's/(\.[0-9])([A-Z]?(iB|B)?)/\1:\2/g' |
  2007. sort
  2008. } | col:normalize:size -++++++++ |
  2009. sed -r '
  2010. s/(\.[0-9]):([A-Z]?iB)/\1 \2/g;
  2011. s/(\.[0-9]):([KMGTPE])/\1 \2/g;
  2012. s/ ([0-9]+)\.0:B/\1 /g;
  2013. s/ ([0-9]+)\.0:/\1 /g;
  2014. ' |
  2015. header:make 2
  2016. ;;
  2017. esac
  2018. }
  2019. header:make() {
  2020. local nb_line="${1:-1}"
  2021. local line
  2022. while ((nb_line-- > 0)); do
  2023. read-0a line
  2024. echo "${GRAY}$(printf "%s" "$line" | sed -r 's/_/ /g')${NORMAL}"
  2025. done
  2026. cat
  2027. }
  2028. stats:load_avg() {
  2029. local format="$1"
  2030. local out
  2031. out=$(host:sys:load_avg)
  2032. printf "%s\n" "$out" | rrd:update "" "load_avg|2:load_avg_1:GAUGE:U:U,3:load_avg_5:GAUGE:U:U,4:load_avg_15:GAUGE:U:U" || {
  2033. return 1
  2034. }
  2035. case "${format:-p}" in
  2036. raw|r)
  2037. printf "%s\n" "$out" | cut -f 2-5 -d " "
  2038. ;;
  2039. pretty|p)
  2040. {
  2041. echo "___1m" "___5m" "__15m"
  2042. printf "%s\n" "$out" | cut -f 3-5 -d " "
  2043. } | col:normalize:size +++ | header:make
  2044. ;;
  2045. esac
  2046. }
  2047. stats:disk() {
  2048. local format="$1"
  2049. local out
  2050. disk_used_size=$(df --output=used,size / | tail -n 1) || return 1
  2051. out=$(printf "%s " "" "$(date +%s)" "${disk_used_size// / }")
  2052. printf "%s\n" "$out" | rrd:update "" "disk|2:used:GAUGE:U:U,3:size:GAUGE:U:U" || {
  2053. return 1
  2054. }
  2055. case "${format:-p}" in
  2056. raw|r)
  2057. printf "%s\n" "$out" | cut -f 2-4 -d " "
  2058. ;;
  2059. pretty|p)
  2060. {
  2061. echo "__used" "__size"
  2062. printf "%s\n" "$out" | cut -f 3-5 -d " " |
  2063. numfmt --field 1-2 --from-unit=1024 --to=iec-i --format=%8.1fB
  2064. } | col:normalize:size ++ | header:make
  2065. ;;
  2066. esac
  2067. }
  2068. stats:disk-2() {
  2069. local format="$1"
  2070. local out
  2071. out=$(disk:usage:stats)
  2072. printf "%s\n" "$out" | rrd:update "disk-2" "size|2:size:GAUGE:U:U" || {
  2073. return 1
  2074. }
  2075. case "${format:-p}" in
  2076. raw|r)
  2077. printf "%s\n" "$out"
  2078. ;;
  2079. pretty|p)
  2080. awk:require 4.1.4 || return 1
  2081. echo "${WHITE}host:${NORMAL}"
  2082. echo " ${WHITE}general:${NORMAL}"
  2083. {
  2084. echo "ident" "size____"
  2085. printf "%s\n" "$out" | grep ^host/ | sed -r 's%^host/%%g' |
  2086. egrep "^(used|available) " |
  2087. cut -f 1,3 -d " " |
  2088. numfmt --field 2-4 --to=iec-i --from-unit=1024 --format=%8.1fB |
  2089. sed -r 's/(\.[0-9])([A-Z]?i?B)/\1:\2/g'
  2090. } | col:normalize:size -+ |
  2091. sed -r '
  2092. s/(\.[0-9]):([A-Z]?iB)/\1 \2/g;
  2093. s/(\.[0-9]):([KMGTPE])/\1 \2/g;
  2094. s/ ([0-9]+)\.0:B/\1 B/g;
  2095. s/ ([0-9]+)\.0:/\1 /g;
  2096. ' |
  2097. header:make |
  2098. prefix " "
  2099. echo " ${WHITE}usage:${NORMAL}"
  2100. {
  2101. echo "ident" "size____"
  2102. printf "%s\n" "$out" | grep ^host/ | sed -r 's%^host/%%g' |
  2103. egrep -v "^(used|available) " |
  2104. sort -rn -k 3 |
  2105. cut -f 1,3 -d " " |
  2106. numfmt --field 2-4 --to=iec-i --from-unit=1024 --format=%8.1fB |
  2107. sed -r 's/(\.[0-9])([A-Z]?i?B)/\1:\2/g'
  2108. } | col:normalize:size -+ |
  2109. sed -r '
  2110. s/(\.[0-9]):([A-Z]?iB)/\1 \2/g;
  2111. s/(\.[0-9]):([KMGTPE])/\1 \2/g;
  2112. s/ ([0-9]+)\.0:B/\1 B/g;
  2113. s/ ([0-9]+)\.0:/\1 /g;
  2114. ' |
  2115. header:make |
  2116. prefix " "
  2117. echo "${WHITE}data:${NORMAL}"
  2118. {
  2119. echo "folder" "size____"
  2120. printf "%s\n" "$out" | grep ^data/ | sed -r 's%^data/%%g' |
  2121. sort -rn -k 3 |
  2122. cut -f 1,3 -d " " |
  2123. numfmt --field 2 --to=iec-i --from-unit=1024 --format=%8.1fB |
  2124. sed -r 's/(\.[0-9])([A-Z]?i?B)/\1:\2/g'
  2125. } | col:normalize:size -+ |
  2126. sed -r '
  2127. s/(\.[0-9]):([A-Z]?iB)/\1 \2/g;
  2128. s/(\.[0-9]):([KMGTPE])/\1 \2/g;
  2129. s/ ([0-9]+)\.0:B/\1 B/g;
  2130. s/ ([0-9]+)\.0:/\1 /g;
  2131. ' |
  2132. header:make |
  2133. prefix " "
  2134. echo "${WHITE}docker:${NORMAL}"
  2135. {
  2136. echo "ident" "size____"
  2137. printf "%s\n" "$out" | grep ^docker/ | sed -r 's%^docker/%%g' |
  2138. egrep -v "^[a-z0-9_]+_r " |
  2139. sort -rn -k 3 |
  2140. cut -f 1,3 -d " " |
  2141. numfmt --field 2-4 --to=iec-i --from-unit=1024 --format=%8.1fB |
  2142. sed -r 's/(\.[0-9])([A-Z]?i?B)/\1:\2/g'
  2143. } | col:normalize:size -+ |
  2144. sed -r '
  2145. s/(\.[0-9]):([A-Z]?iB)/\1 \2/g;
  2146. s/(\.[0-9]):([KMGTPE])/\1 \2/g;
  2147. s/ ([0-9]+)\.0:B/\1 B/g;
  2148. s/ ([0-9]+)\.0:/\1 /g;
  2149. ' |
  2150. header:make |
  2151. prefix " "
  2152. ;;
  2153. esac
  2154. }
  2155. disk:total() {
  2156. local disk_used_size
  2157. disk_used_size=$(df --output=used,size "/") || return 1
  2158. disk_used_size=${disk_used_size##*$'\n'}
  2159. disk_used_size=${disk_used_size// / }
  2160. printf "%s" "$disk_used_size"
  2161. }
  2162. disk:usage:stats() {
  2163. local disk_used_size
  2164. data_sum=0
  2165. while read-0 size_name; do
  2166. name=${size_name#*$'\t'}
  2167. [ -d "$name" ] || continue
  2168. size=${size_name%%$'\t'*}
  2169. name=${name##*/}
  2170. data_sum=$((data_sum + size))
  2171. printf "%s %s %s\n" "data/${name##*/}" "$(date +%s)" "$size"
  2172. done < <(du -s0 /srv/datastore/data/*)
  2173. docker_sum=0
  2174. if ! out=$(du -s /var/lib/docker/containers/*/*-json.log); then
  2175. err "Failed to get disk usage of docker container logs."
  2176. return 1
  2177. fi
  2178. total_usage_logs=$(printf "%s" "$out" | awk '{s+=$1} END {print s}')
  2179. docker_sum=$((docker_sum + total_usage_logs))
  2180. printf "%s %s %s\n" "docker/logs" "$(date +%s)" "$total_usage_logs"
  2181. if ! out=$(docker system df --format '{{.Type}}\t{{.Size}}\t{{.Reclaimable}}'); then
  2182. err "Failed to get disk usage of docker system."
  2183. return 1
  2184. fi
  2185. date=$(date +%s)
  2186. while read-0 ident size reclaimable; do
  2187. docker_sum=$((docker_sum + size))
  2188. printf "%s %s %s\n" "docker/$ident" "$date" "$size"
  2189. printf "%s %s %s\n" "docker/${ident}_r" "$date" "$reclaimable"
  2190. done < <(
  2191. printf "%s" "$out" |
  2192. sed -r 's/^(.*)\t([0-9.]+)([kMGT]?B)\t([0-9.]+)([kMGT]?B).*$/\1\t\2\t\3\t\4\t\5/g' |
  2193. awk -F'\t' '
  2194. # Function to convert sizes to kilobytes
  2195. function convert_to_kb(value, unit) {
  2196. if (unit == "B") {
  2197. return value / 1024;
  2198. } else if (unit == "kB") {
  2199. return value;
  2200. } else if (unit == "MB") {
  2201. return value * 1024;
  2202. } else if (unit == "GB") {
  2203. return value * 1024 * 1024;
  2204. } else if (unit == "TB") {
  2205. return value * 1024 * 1024 * 1024;
  2206. }
  2207. return 0;
  2208. }
  2209. {
  2210. size_value = $2;
  2211. size_unit = $3;
  2212. reclaimable_value = $4;
  2213. reclaimable_unit = $5;
  2214. type = $1;
  2215. # Convert type to lowercase and replace spaces with underscores
  2216. type=tolower(type);
  2217. gsub(/ /, "_", type);
  2218. # Convert size and reclaimable to kilobytes
  2219. size_kb = convert_to_kb(size_value, size_unit);
  2220. reclaimable_kb = convert_to_kb(reclaimable_value, reclaimable_unit);
  2221. printf "%s\0%d\0%d\0", type, size_kb, reclaimable_kb;
  2222. }'
  2223. )
  2224. used_size=$(disk:total)
  2225. used=${used_size%% *}
  2226. available=${used_size##* }
  2227. date=$(date +%s)
  2228. printf "%s %s %s\n" "host/used" "$date" "${used}"
  2229. printf "%s %s %s\n" "host/available" "$date" "${available}"
  2230. printf "%s %s %s\n" "host/docker" "$date" "$docker_sum"
  2231. printf "%s %s %s\n" "host/data" "$date" "$data_sum"
  2232. printf "%s %s %s\n" "host/other" "$date" "$((used - docker_sum - data_sum))"
  2233. }
  2234. host:sys:load_avg() {
  2235. local uptime
  2236. uptime="$(uptime)"
  2237. uptime=${uptime##*: }
  2238. uptime=${uptime//,/}
  2239. printf "%s " "" "$(date +%s)" "$uptime"
  2240. }
  2241. cmdline.spec.gnu mongo
  2242. cmdline.spec::cmd:mongo:run() {
  2243. :
  2244. }
  2245. cmdline.spec.gnu upgrade
  2246. cmdline.spec:mongo:cmd:upgrade:run() {
  2247. : :posarg: [TARGET_VERSION] "Target version to migrate to"
  2248. : :optval: --service,-s "The mongo service name (defaults to 'mongo')"
  2249. : :optfla: --debug,-d "Display debugging information"
  2250. local URL
  2251. mongo_service="${opt_service:-mongo}"
  2252. available_actions=$(compose --get-available-actions) || exit 1
  2253. available_actionable_services=($(e "$available_actions" | yq 'keys().[]'))
  2254. if [[ " ${available_actionable_services[*]} " != *" $mongo_service "* ]]; then
  2255. err "Service '$mongo_service' was not found in current 'compose.yml'."
  2256. exit 1
  2257. fi
  2258. opts_compose=()
  2259. if [ -n "$opt_debug" ]; then
  2260. opts_compose+=("--debug")
  2261. else
  2262. opts_compose+=("-q")
  2263. fi
  2264. project_name=$(compose:project_name) || exit 1
  2265. containers="$(compose:service:containers "${project_name}" "${mongo_service}")" || exit 1
  2266. ## XXXvlab: quick hack, to make more beautiful later
  2267. cron_container=$(compose:service:containers "${project_name}" "cron")
  2268. containers="$containers $cron_container"
  2269. docker stop "$cron_container" >/dev/null 2>&1 || true
  2270. before_version=
  2271. uptodate=
  2272. upgraded=
  2273. msgerr=()
  2274. while read-0a-err errlvl line; do
  2275. echo "$line"
  2276. rline=$(printf "%s" "$line" | sed_compat "s/$__color_sequence_regex//g")
  2277. case "$rline" in
  2278. "II Current mongo version: "*)
  2279. before_version="${rline#II Current mongo version: }"
  2280. ;;
  2281. "II ${mongo_service} is already up-to-date.")
  2282. if [ -z "$before_version" ]; then
  2283. msgerr+=("expected a 'current version' line before the 'up-to-date' one.")
  2284. continue
  2285. fi
  2286. after_version="$before_version"
  2287. uptodate=1
  2288. ;;
  2289. "II Successfully upgraded from ${before_version} to "*)
  2290. after_version="${rline#II Successfully upgraded from ${before_version} to }"
  2291. upgraded=1
  2292. ;;
  2293. *)
  2294. :
  2295. ;;
  2296. esac
  2297. done < <(
  2298. ## -q to remove the display of ``compose`` related information
  2299. ## like relation resolution.
  2300. ## -c on the upgrade action to force color
  2301. ansi_color=yes p-0a-err compose -c "${opts_compose[@]}" upgrade "$mongo_service" --no-hint -c "$TARGET_VERSION"
  2302. )
  2303. if [ "$errlvl" != 0 ]; then
  2304. exit "$errlvl"
  2305. fi
  2306. if [ -n "$uptodate" ]; then
  2307. for container in "${containers[@]}"; do
  2308. [ -n "$container" ] || continue
  2309. Wrap -d "start ${DARKYELLOW}${mongo_service}${NORMAL}'s container" -- \
  2310. docker start "$container" || {
  2311. err "Failed to start container '$container'."
  2312. exit 1
  2313. }
  2314. done
  2315. exit 0
  2316. fi
  2317. if [ -z "$upgraded" ]; then
  2318. err "Unexpected output of 'upgrade' action with errorlevel 0 and without success"
  2319. exit 1
  2320. fi
  2321. desc="update \`compose.yml\` to set ${DARKYELLOW}$mongo_service${NORMAL}'s "
  2322. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  2323. Wrap -d "$desc" -- \
  2324. compose:file:value-change \
  2325. "${mongo_service}.docker-compose.image" \
  2326. "docker.0k.io/mongo:${after_version}-myc" || exit 1
  2327. echo "${WHITE}Launching final compose${NORMAL}"
  2328. compose up || exit 1
  2329. }
  2330. cmdline.spec.gnu postgres
  2331. cmdline.spec::cmd:postgres:run() {
  2332. :
  2333. }
  2334. cmdline.spec.gnu upgrade
  2335. cmdline.spec:postgres:cmd:upgrade:run() {
  2336. : :posarg: [TARGET_VERSION] "Target version to migrate to"
  2337. : :optval: --service,-s "The postgre service name (defaults to 'postgres')"
  2338. : :optfla: --debug,-d "Display debugging information"
  2339. local URL
  2340. depends yq
  2341. postgres_service="${opt_service:-postgres}"
  2342. available_actions=$(compose --get-available-actions) || exit 1
  2343. available_actionable_services=($(e "$available_actions" | yq 'keys().[]'))
  2344. if [[ " ${available_actionable_services[*]} " != *" $postgres_service "* ]]; then
  2345. err "Service '$postgres_service' was not found in current 'compose.yml'."
  2346. exit 1
  2347. fi
  2348. opts_compose=()
  2349. if [ -n "$opt_debug" ]; then
  2350. opts_compose+=("--debug")
  2351. else
  2352. opts_compose+=("-q")
  2353. fi
  2354. project_name=$(compose:project_name) || exit 1
  2355. containers=($(compose:service:containers "${project_name}" "${postgres_service}")) || exit 1
  2356. ## XXXvlab: quick hack, to make more beautiful later
  2357. cron_container=$(compose:service:containers "${project_name}" "cron")
  2358. containers+=("$cron_container")
  2359. docker stop "$cron_container" >/dev/null 2>&1 || true
  2360. before_version=
  2361. uptodate=
  2362. upgraded=
  2363. msgerr=()
  2364. while read-0a-err errlvl line; do
  2365. echo "$line"
  2366. rline=$(printf "%s" "$line" | sed_compat "s/$__color_sequence_regex//g")
  2367. case "$rline" in
  2368. "II Current postgres version: "*)
  2369. before_version="${rline#II Current postgres version: }"
  2370. ;;
  2371. "II ${postgres_service} is already up-to-date.")
  2372. if [ -z "$before_version" ]; then
  2373. msgerr+=("expected a 'current version' line before the 'up-to-date' one.")
  2374. continue
  2375. fi
  2376. after_version="$before_version"
  2377. uptodate=1
  2378. ;;
  2379. "II Successfully upgraded from ${before_version} to "*)
  2380. after_version="${rline#II Successfully upgraded from ${before_version} to }"
  2381. upgraded=1
  2382. ;;
  2383. *)
  2384. :
  2385. ;;
  2386. esac
  2387. done < <(
  2388. ## -q to remove the display of ``compose`` related information
  2389. ## like relation resolution.
  2390. ## -c on the upgrade action to force color
  2391. ansi_color=yes p-0a-err compose -q -c "${opts_compose[@]}" upgrade "$postgres_service" --no-hint -c "$TARGET_VERSION" 2>&1
  2392. )
  2393. if [ "$errlvl" != 0 ]; then
  2394. exit "$errlvl"
  2395. fi
  2396. if [ -n "$uptodate" ]; then
  2397. for container in "${containers[@]}"; do
  2398. [ -n "$container" ] || continue
  2399. Wrap -d "start ${DARKYELLOW}${postgres_service}${NORMAL}'s container" -- \
  2400. docker start "$container" || {
  2401. err "Failed to start container '$container'."
  2402. exit 1
  2403. }
  2404. done
  2405. exit 0
  2406. fi
  2407. if [ -z "$upgraded" ]; then
  2408. err "Unexpected output of 'upgrade' action with errorlevel 0 and without success"
  2409. exit 1
  2410. fi
  2411. desc="update \`compose.yml\` to set ${DARKYELLOW}$postgres_service${NORMAL}'s "
  2412. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  2413. Wrap -d "$desc" -- \
  2414. compose:file:value-change \
  2415. "${postgres_service}.docker-compose.image" \
  2416. "docker.0k.io/postgres:${after_version}-myc" || exit 1
  2417. echo "${WHITE}Launching final compose${NORMAL}"
  2418. compose up || exit 1
  2419. }
  2420. cmdline.spec.gnu bench
  2421. cmdline.spec::cmd:bench:run() {
  2422. depends sysbench
  2423. nbthread=$(lscpu | egrep "^CPU\(s\):" | cut -f 2 -d : | xargs echo)
  2424. single=$(sysbench cpu --cpu-max-prime=20000 run --threads=1 | grep "events per" | cut -f 2 -d : | xargs echo)
  2425. threaded=$(sysbench cpu --cpu-max-prime=20000 run --threads="$nbthread" | grep "events per" | cut -f 2 -d : | xargs echo)
  2426. echo "$threaded / $single / $nbthread"
  2427. }
  2428. cmdline.spec::cmd:monujo:run() {
  2429. :
  2430. }
  2431. cmdline.spec.gnu monujo
  2432. cmdline.spec:monujo:cmd:set-version:run() {
  2433. : :posarg: TARGET_VERSION "Target version to put in options"
  2434. : :optval: --service,-s "The monujo service name (defaults to 'monujo')"
  2435. local URL
  2436. monujo_service="${opt_service:-monujo}"
  2437. project_name=$(compose:project_name) || exit 1
  2438. ## check if service exists in compose.yml
  2439. compose:service:exists "$project_name" "$monujo_service" || {
  2440. err "Service '$monujo_service' was not found in current 'compose.yml'."
  2441. exit 1
  2442. }
  2443. Wrap -d "Changing ${DARKYELLOW}$monujo_service${NORMAL} version" -- \
  2444. compose:file:value-change \
  2445. "${monujo_service}.options.version" \
  2446. "${TARGET_VERSION}" || exit 1
  2447. }
  2448. cmdline::parse "$@"