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.

2640 lines
80 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. cmdline.spec.gnu stats
  1753. cmdline.spec::cmd:stats:run() {
  1754. : :optval: --format,-f "Either 'silent', 'raw', or 'pretty', default is pretty."
  1755. : :optfla: --silent,-s "Shorthand for '--format silent'"
  1756. : :optval: --resource,-r 'resource(s) separated with a comma'
  1757. local project_name service_name containers container check
  1758. if [[ -n "${opt_silent}" ]]; then
  1759. if [[ -n "${opt_format}" ]]; then
  1760. err "'--silent' conflict with option '--format'."
  1761. return 1
  1762. fi
  1763. opt_format=s
  1764. fi
  1765. opt_format="${opt_format:-pretty}"
  1766. case "${opt_format}" in
  1767. raw|r)
  1768. opt_format="raw"
  1769. :
  1770. ;;
  1771. silent|s)
  1772. opt_format="silent"
  1773. ;;
  1774. pretty|p)
  1775. opt_format="pretty"
  1776. awk:require 4.1.4 || return 1
  1777. ;;
  1778. *)
  1779. err "Invalid value '$opt_format' for option --format"
  1780. echo " use either 'raw' (shorthand 'r'), 'silent' (shorthand 's') or pretty (shorthand 'p')." >&2
  1781. return 1
  1782. esac
  1783. local resources=(c.{memory,network} load_avg disk)
  1784. if [ -n "${opt_resource}" ]; then
  1785. resources=(${opt_resource//,/ })
  1786. fi
  1787. local not_found=()
  1788. for resource in "${resources[@]}"; do
  1789. if ! fn.exists "stats:$resource"; then
  1790. not_found+=("$resource")
  1791. fi
  1792. done
  1793. if [[ "${#not_found[@]}" -gt 0 ]]; then
  1794. not_found_msg=$(printf "%s, " "${not_found[@]}")
  1795. not_found_msg=${not_found_msg%, }
  1796. err "Unsupported resource(s) provided: ${not_found_msg}"
  1797. echo " resource must be one-of:" >&2
  1798. declare -F | egrep -- '-fx? stats:[a-zA-Z0-9_.]+$' | cut -f 3- -d " " | cut -f 2- -d ":" | prefix " - " >&2
  1799. return 1
  1800. fi
  1801. :state-dir:
  1802. for resource in "${resources[@]}"; do
  1803. if [ "$opt_format" == "pretty" ]; then
  1804. echo "${WHITE}$resource${NORMAL}:"
  1805. stats:"$resource" "$opt_format" 2>&1 | prefix " "
  1806. else
  1807. stats:"$resource" "$opt_format" 2>&1 | prefix "$resource "
  1808. fi
  1809. set_errlvl "${PIPESTATUS[0]}" || return 1
  1810. done
  1811. }
  1812. stats:c.memory() {
  1813. local format="$1"
  1814. local out
  1815. container_to_check=($(docker:running_containers)) || exit 1
  1816. out=$(docker:containers:stats "${container_to_check[@]}")
  1817. printf "%s\n" "$out" | rrd:update "containers" "memory|3:usage:GAUGE:U:U,4:inactive:GAUGE:U:U" || {
  1818. return 1
  1819. }
  1820. case "${format:-p}" in
  1821. raw|r)
  1822. printf "%s\n" "$out" | cut -f 1-5 -d " "
  1823. ;;
  1824. pretty|p)
  1825. awk:require 4.1.4 || return 1
  1826. {
  1827. echo "container" "__total____" "buffered____" "resident____"
  1828. printf "%s\n" "$out" |
  1829. awk '
  1830. {
  1831. offset = strftime("%z", $2);
  1832. print $1, substr($0, index($0,$3));
  1833. }' | cut -f 1-4 -d " " |
  1834. numfmt --field 2-4 --to=iec-i --format=%8.1fB |
  1835. sed -r 's/(\.[0-9])([A-Z]?iB)/\1:\2/g' |
  1836. sort
  1837. } | col:normalize:size -+++ |
  1838. sed -r 's/(\.[0-9]):([A-Z]?iB)/\1 \2/g' |
  1839. header:make
  1840. ;;
  1841. esac
  1842. }
  1843. stats:c.network() {
  1844. local format="$1"
  1845. local out
  1846. container_to_check=($(docker:running_containers)) || exit 1
  1847. out=$(docker:containers:stats "${container_to_check[@]}")
  1848. cols=(
  1849. {rx,tx}_{bytes,packets,errors,dropped}
  1850. )
  1851. idx=5 ## starting column idx for next fields
  1852. defs=()
  1853. for col in "${cols[@]}"; do
  1854. defs+=("$((idx++)):${col}:COUNTER:U:U")
  1855. done
  1856. OLDIFS="$IFS"
  1857. IFS="," defs="${defs[*]}"
  1858. IFS="$OLDIFS"
  1859. printf "%s\n" "$out" |
  1860. rrd:update "containers" \
  1861. "network|${defs}" || {
  1862. return 1
  1863. }
  1864. case "${format:-p}" in
  1865. raw|r)
  1866. printf "%s\n" "$out" | cut -f 1,2,7- -d " "
  1867. ;;
  1868. pretty|p)
  1869. awk:require 4.1.4 || return 1
  1870. {
  1871. echo "container" "_" "_" "_" "RX" "_" "_" "_" "TX"
  1872. echo "_" "__bytes____" "__packets" "__errors" "__dropped" "__bytes____" "__packets" "__errors" "__dropped"
  1873. printf "%s\n" "$out" |
  1874. awk '
  1875. {
  1876. offset = strftime("%z", $2);
  1877. print $1, substr($0, index($0,$7));
  1878. }' |
  1879. numfmt --field 2,6 --to=iec-i --format=%8.1fB |
  1880. numfmt --field 3,4,5,7,8,9 --to=si --format=%8.1f |
  1881. sed -r 's/(\.[0-9])([A-Z]?(iB|B)?)/\1:\2/g' |
  1882. sort
  1883. } | col:normalize:size -++++++++ |
  1884. sed -r '
  1885. s/(\.[0-9]):([A-Z]?iB)/\1 \2/g;
  1886. s/(\.[0-9]):([KMGTPE])/\1 \2/g;
  1887. s/ ([0-9]+)\.0:B/\1 /g;
  1888. s/ ([0-9]+)\.0:/\1 /g;
  1889. ' |
  1890. header:make 2
  1891. ;;
  1892. esac
  1893. }
  1894. header:make() {
  1895. local nb_line="${1:-1}"
  1896. local line
  1897. while ((nb_line-- > 0)); do
  1898. read-0a line
  1899. echo "${GRAY}$(printf "%s" "$line" | sed -r 's/_/ /g')${NORMAL}"
  1900. done
  1901. cat
  1902. }
  1903. stats:load_avg() {
  1904. local format="$1"
  1905. local out
  1906. out=$(host:sys:load_avg)
  1907. 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" || {
  1908. return 1
  1909. }
  1910. case "${format:-p}" in
  1911. raw|r)
  1912. printf "%s\n" "$out" | cut -f 2-5 -d " "
  1913. ;;
  1914. pretty|p)
  1915. {
  1916. echo "___1m" "___5m" "__15m"
  1917. printf "%s\n" "$out" | cut -f 3-5 -d " "
  1918. } | col:normalize:size +++ | header:make
  1919. ;;
  1920. esac
  1921. }
  1922. stats:disk() {
  1923. local format="$1"
  1924. local out
  1925. disk_used_size=$(df --output=used,size / | tail -n 1) || return 1
  1926. out=$(printf "%s " "" "$(date +%s)" "${disk_used_size// / }")
  1927. printf "%s\n" "$out" | rrd:update "" "disk|2:used:GAUGE:U:U,3:size:GAUGE:U:U" || {
  1928. return 1
  1929. }
  1930. case "${format:-p}" in
  1931. raw|r)
  1932. printf "%s\n" "$out" | cut -f 2-4 -d " "
  1933. ;;
  1934. pretty|p)
  1935. {
  1936. echo "__used" "__size"
  1937. printf "%s\n" "$out" | cut -f 3-5 -d " " |
  1938. numfmt --field 1-2 --from-unit=1024 --to=iec-i --format=%8.1fB
  1939. } | col:normalize:size ++ | header:make
  1940. ;;
  1941. esac
  1942. }
  1943. host:sys:load_avg() {
  1944. local uptime
  1945. uptime="$(uptime)"
  1946. uptime=${uptime##*: }
  1947. uptime=${uptime//,/}
  1948. printf "%s " "" "$(date +%s)" "$uptime"
  1949. }
  1950. cmdline.spec.gnu mongo
  1951. cmdline.spec::cmd:mongo:run() {
  1952. :
  1953. }
  1954. cmdline.spec.gnu upgrade
  1955. cmdline.spec:mongo:cmd:upgrade:run() {
  1956. : :posarg: [TARGET_VERSION] "Target version to migrate to"
  1957. : :optval: --service,-s "The mongo service name (defaults to 'mongo')"
  1958. : :optfla: --debug,-d "Display debugging information"
  1959. local URL
  1960. mongo_service="${opt_service:-mongo}"
  1961. available_actions=$(compose --get-available-actions) || exit 1
  1962. available_actionable_services=($(e "$available_actions" | yq 'keys().[]'))
  1963. if [[ " ${available_actionable_services[*]} " != *" $mongo_service "* ]]; then
  1964. err "Service '$mongo_service' was not found in current 'compose.yml'."
  1965. exit 1
  1966. fi
  1967. opts_compose=()
  1968. if [ -n "$opt_debug" ]; then
  1969. opts_compose+=("--debug")
  1970. else
  1971. opts_compose+=("-q")
  1972. fi
  1973. project_name=$(compose:project_name) || exit 1
  1974. containers="$(compose:service:containers "${project_name}" "${mongo_service}")" || exit 1
  1975. ## XXXvlab: quick hack, to make more beautiful later
  1976. cron_container=$(compose:service:containers "${project_name}" "cron")
  1977. containers="$containers $cron_container"
  1978. docker stop "$cron_container" >/dev/null 2>&1 || true
  1979. before_version=
  1980. uptodate=
  1981. upgraded=
  1982. msgerr=()
  1983. while read-0a-err errlvl line; do
  1984. echo "$line"
  1985. rline=$(printf "%s" "$line" | sed_compat "s/$__color_sequence_regex//g")
  1986. case "$rline" in
  1987. "II Current mongo version: "*)
  1988. before_version="${rline#II Current mongo version: }"
  1989. ;;
  1990. "II ${mongo_service} is already up-to-date.")
  1991. if [ -z "$before_version" ]; then
  1992. msgerr+=("expected a 'current version' line before the 'up-to-date' one.")
  1993. continue
  1994. fi
  1995. after_version="$before_version"
  1996. uptodate=1
  1997. ;;
  1998. "II Successfully upgraded from ${before_version} to "*)
  1999. after_version="${rline#II Successfully upgraded from ${before_version} to }"
  2000. upgraded=1
  2001. ;;
  2002. *)
  2003. :
  2004. ;;
  2005. esac
  2006. done < <(
  2007. ## -q to remove the display of ``compose`` related information
  2008. ## like relation resolution.
  2009. ## -c on the upgrade action to force color
  2010. ansi_color=yes p-0a-err compose -c "${opts_compose[@]}" upgrade "$mongo_service" --no-hint -c "$TARGET_VERSION"
  2011. )
  2012. if [ "$errlvl" != 0 ]; then
  2013. exit "$errlvl"
  2014. fi
  2015. if [ -n "$uptodate" ]; then
  2016. for container in "${containers[@]}"; do
  2017. [ -n "$container" ] || continue
  2018. Wrap -d "start ${DARKYELLOW}${mongo_service}${NORMAL}'s container" -- \
  2019. docker start "$container" || {
  2020. err "Failed to start container '$container'."
  2021. exit 1
  2022. }
  2023. done
  2024. exit 0
  2025. fi
  2026. if [ -z "$upgraded" ]; then
  2027. err "Unexpected output of 'upgrade' action with errorlevel 0 and without success"
  2028. exit 1
  2029. fi
  2030. desc="update \`compose.yml\` to set ${DARKYELLOW}$mongo_service${NORMAL}'s "
  2031. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  2032. Wrap -d "$desc" -- \
  2033. compose:file:value-change \
  2034. "${mongo_service}.docker-compose.image" \
  2035. "docker.0k.io/mongo:${after_version}-myc" || exit 1
  2036. echo "${WHITE}Launching final compose${NORMAL}"
  2037. compose up || exit 1
  2038. }
  2039. cmdline.spec.gnu postgres
  2040. cmdline.spec::cmd:postgres:run() {
  2041. :
  2042. }
  2043. cmdline.spec.gnu upgrade
  2044. cmdline.spec:postgres:cmd:upgrade:run() {
  2045. : :posarg: [TARGET_VERSION] "Target version to migrate to"
  2046. : :optval: --service,-s "The postgre service name (defaults to 'postgres')"
  2047. : :optfla: --debug,-d "Display debugging information"
  2048. local URL
  2049. depends yq
  2050. postgres_service="${opt_service:-postgres}"
  2051. available_actions=$(compose --get-available-actions) || exit 1
  2052. available_actionable_services=($(e "$available_actions" | yq 'keys().[]'))
  2053. if [[ " ${available_actionable_services[*]} " != *" $postgres_service "* ]]; then
  2054. err "Service '$postgres_service' was not found in current 'compose.yml'."
  2055. exit 1
  2056. fi
  2057. opts_compose=()
  2058. if [ -n "$opt_debug" ]; then
  2059. opts_compose+=("--debug")
  2060. else
  2061. opts_compose+=("-q")
  2062. fi
  2063. project_name=$(compose:project_name) || exit 1
  2064. containers=($(compose:service:containers "${project_name}" "${postgres_service}")) || exit 1
  2065. ## XXXvlab: quick hack, to make more beautiful later
  2066. cron_container=$(compose:service:containers "${project_name}" "cron")
  2067. containers+=("$cron_container")
  2068. docker stop "$cron_container" >/dev/null 2>&1 || true
  2069. before_version=
  2070. uptodate=
  2071. upgraded=
  2072. msgerr=()
  2073. while read-0a-err errlvl line; do
  2074. echo "$line"
  2075. rline=$(printf "%s" "$line" | sed_compat "s/$__color_sequence_regex//g")
  2076. case "$rline" in
  2077. "II Current postgres version: "*)
  2078. before_version="${rline#II Current postgres version: }"
  2079. ;;
  2080. "II ${postgres_service} is already up-to-date.")
  2081. if [ -z "$before_version" ]; then
  2082. msgerr+=("expected a 'current version' line before the 'up-to-date' one.")
  2083. continue
  2084. fi
  2085. after_version="$before_version"
  2086. uptodate=1
  2087. ;;
  2088. "II Successfully upgraded from ${before_version} to "*)
  2089. after_version="${rline#II Successfully upgraded from ${before_version} to }"
  2090. upgraded=1
  2091. ;;
  2092. *)
  2093. :
  2094. ;;
  2095. esac
  2096. done < <(
  2097. ## -q to remove the display of ``compose`` related information
  2098. ## like relation resolution.
  2099. ## -c on the upgrade action to force color
  2100. ansi_color=yes p-0a-err compose -q -c "${opts_compose[@]}" upgrade "$postgres_service" --no-hint -c "$TARGET_VERSION" 2>&1
  2101. )
  2102. if [ "$errlvl" != 0 ]; then
  2103. exit "$errlvl"
  2104. fi
  2105. if [ -n "$uptodate" ]; then
  2106. for container in "${containers[@]}"; do
  2107. [ -n "$container" ] || continue
  2108. Wrap -d "start ${DARKYELLOW}${postgres_service}${NORMAL}'s container" -- \
  2109. docker start "$container" || {
  2110. err "Failed to start container '$container'."
  2111. exit 1
  2112. }
  2113. done
  2114. exit 0
  2115. fi
  2116. if [ -z "$upgraded" ]; then
  2117. err "Unexpected output of 'upgrade' action with errorlevel 0 and without success"
  2118. exit 1
  2119. fi
  2120. desc="update \`compose.yml\` to set ${DARKYELLOW}$postgres_service${NORMAL}'s "
  2121. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  2122. Wrap -d "$desc" -- \
  2123. compose:file:value-change \
  2124. "${postgres_service}.docker-compose.image" \
  2125. "docker.0k.io/postgres:${after_version}-myc" || exit 1
  2126. echo "${WHITE}Launching final compose${NORMAL}"
  2127. compose up || exit 1
  2128. }
  2129. cmdline.spec.gnu bench
  2130. cmdline.spec::cmd:bench:run() {
  2131. depends sysbench
  2132. nbthread=$(lscpu | egrep "^CPU\(s\):" | cut -f 2 -d : | xargs echo)
  2133. single=$(sysbench cpu --cpu-max-prime=20000 run --threads=1 | grep "events per" | cut -f 2 -d : | xargs echo)
  2134. threaded=$(sysbench cpu --cpu-max-prime=20000 run --threads="$nbthread" | grep "events per" | cut -f 2 -d : | xargs echo)
  2135. echo "$threaded / $single / $nbthread"
  2136. }
  2137. cmdline.spec::cmd:monujo:run() {
  2138. :
  2139. }
  2140. cmdline.spec.gnu monujo
  2141. cmdline.spec:monujo:cmd:set-version:run() {
  2142. : :posarg: TARGET_VERSION "Target version to put in options"
  2143. : :optval: --service,-s "The monujo service name (defaults to 'monujo')"
  2144. local URL
  2145. monujo_service="${opt_service:-monujo}"
  2146. project_name=$(compose:project_name) || exit 1
  2147. ## check if service exists in compose.yml
  2148. compose:service:exists "$project_name" "$monujo_service" || {
  2149. err "Service '$monujo_service' was not found in current 'compose.yml'."
  2150. exit 1
  2151. }
  2152. Wrap -d "Changing ${DARKYELLOW}$monujo_service${NORMAL} version" -- \
  2153. compose:file:value-change \
  2154. "${monujo_service}.options.version" \
  2155. "${TARGET_VERSION}" || exit 1
  2156. }
  2157. cmdline::parse "$@"