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.

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