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.

1632 lines
48 KiB

  1. #!/bin/bash
  2. . /etc/shlib
  3. include common
  4. include parse
  5. include cmdline
  6. include config
  7. include cache
  8. include fn
  9. include docker
  10. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  11. version=0.1
  12. desc='Install backup'
  13. help=""
  14. docker:running-container-projects() {
  15. :cache: scope=session
  16. docker ps --format '{{.Label "com.docker.compose.project"}}' | sort | uniq
  17. }
  18. decorator._mangle_fn docker:running-container-projects
  19. ssh:mk-private-key() {
  20. local host="$1" service_name="$2"
  21. (
  22. settmpdir VPS_TMPDIR
  23. ssh-keygen -t rsa -N "" -f "$VPS_TMPDIR/rsync_rsa" -C "$service_name@$host" >/dev/null
  24. cat "$VPS_TMPDIR/rsync_rsa"
  25. )
  26. }
  27. mailcow:has-images-running() {
  28. local images
  29. images=$(docker ps --format '{{.Image}}' | sort | uniq)
  30. [[ $'\n'"$images" == *$'\n'"mailcow/"* ]]
  31. }
  32. mailcow:has-container-project-mentionning-mailcow() {
  33. local projects
  34. projects=$(docker:running-container-projects) || return 1
  35. [[ $'\n'"$projects"$'\n' == *mailcow* ]]
  36. }
  37. mailcow:has-running-containers() {
  38. mailcow:has-images-running ||
  39. mailcow:has-container-project-mentionning-mailcow
  40. }
  41. mailcow:get-root() {
  42. :cache: scope=session
  43. local dir
  44. for dir in {/opt{,/apps},/root}/mailcow-dockerized; do
  45. [ -d "$dir" ] || continue
  46. [ -r "$dir/mailcow.conf" ] || continue
  47. echo "$dir"
  48. return 0
  49. done
  50. return 1
  51. }
  52. decorator._mangle_fn mailcow:get-root
  53. compose:get-compose-yml() {
  54. :cache: scope=session
  55. local path
  56. [ -e "/etc/compose/local.conf" ] && . "/etc/compose/local.conf"
  57. path=${DEFAULT_COMPOSE_FILE:-/etc/compose/compose.yml}
  58. [ -e "$path" ] || return 1
  59. echo "$path"
  60. }
  61. decorator._mangle_fn compose:get-compose-yml
  62. export -f compose:get-compose-yml
  63. compose:has-container-project-myc() {
  64. local projects
  65. projects=$(docker:running-container-projects) || return 1
  66. [[ $'\n'"$projects"$'\n' == *$'\n'"myc"$'\n'* ]]
  67. }
  68. compose:file:value-change() {
  69. local key="$1" value="$2"
  70. yaml:file:value-change "$(compose:get-compose-yml)" "$key" "$value" || exit 1
  71. }
  72. export -f compose:file:value-change
  73. yaml:file:value-change() {
  74. local file="$1" key="$2" value="$3" first=1 count=0 diff=""
  75. (
  76. cd "${file%/*}"
  77. while read-0 hunk; do
  78. if [ -n "$first" ]; then
  79. diff+="$hunk"
  80. first=
  81. continue
  82. fi
  83. if [[ "$hunk" =~ $'\n'"+"[[:space:]]+"${key##*.}:" ]]; then
  84. ((count++))
  85. diff+="$hunk" >&2
  86. else
  87. :
  88. # echo "discarding:" >&2
  89. # e "$hunk" | prefix " | " >&2
  90. fi
  91. done < <(
  92. export DEBUG=
  93. settmpdir YQ_TEMP
  94. cp "${file}" "$YQ_TEMP/compose.yml" &&
  95. yq -i ".${key} = \"${value}\"" "$YQ_TEMP/compose.yml" &&
  96. sed -ri 's/^([^# ])/\n\0/g' "$YQ_TEMP/compose.yml" &&
  97. diff -u0 -Z "${file}" "$YQ_TEMP/compose.yml" |
  98. sed -r "s/^(@@.*)$/\x00\1/g;s%^(\+\+\+) [^\t]+%\1 ${file}%g"
  99. printf "\0"
  100. )
  101. if [[ "$count" == 0 ]]; then
  102. err "No change made to '$file'."
  103. return 1
  104. fi
  105. if [[ "$count" != 1 ]]; then
  106. err "compose file change request seems dubious and was refused:"
  107. e "$diff" | prefix " | " >&2
  108. return 1
  109. fi
  110. echo Applying: >&2
  111. e "$diff" | prefix " | " >&2
  112. patch <<<"$diff"
  113. ) || exit 1
  114. }
  115. export -f yaml:file:value-change
  116. type:is-mailcow() {
  117. mailcow:get-root >/dev/null ||
  118. mailcow:has-running-containers
  119. }
  120. type:is-compose() {
  121. compose:get-compose-yml >/dev/null &&
  122. compose:has-container-project-myc
  123. }
  124. vps:get-type() {
  125. :cache: scope=session
  126. local fn
  127. for fn in $(declare -F | cut -f 3 -d " " | egrep "^type:is-"); do
  128. "$fn" && {
  129. echo "${fn#type:is-}"
  130. return 0
  131. }
  132. done
  133. return 1
  134. }
  135. decorator._mangle_fn vps:get-type
  136. mirror-dir:sources() {
  137. :cache: scope=session
  138. if ! shyaml get-values default.sources < /etc/mirror-dir/config.yml; then
  139. err "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'."
  140. return 1
  141. fi
  142. }
  143. decorator._mangle_fn mirror-dir:sources
  144. mirror-dir:check-add() {
  145. local elt="$1" sources
  146. sources=$(mirror-dir:sources) || return 1
  147. if [[ $'\n'"$sources"$'\n' == *$'\n'"$elt"$'\n'* ]]; then
  148. info "Volume $elt already in sources"
  149. else
  150. Elt "Adding directory $elt"
  151. sed -i "/sources:/a\ - \"${elt}\"" \
  152. /etc/mirror-dir/config.yml
  153. Feedback || return 1
  154. fi
  155. }
  156. mirror-dir:check-add-vol() {
  157. local elt="$1"
  158. mirror-dir:check-add "/var/lib/docker/volumes/*_${elt}-*/_data"
  159. }
  160. ## The first colon is to prevent auto-export of function from shlib
  161. : ; bash-bug-5() { { cat; } < <(e) >/dev/null; ! cat "$1"; } && bash-bug-5 <(e) 2>/dev/null &&
  162. export BASH_BUG_5=1 && unset -f bash_bug_5
  163. wrap() {
  164. local label="$1" code="$2"
  165. shift 2
  166. export VERBOSE=1
  167. interpreter=/bin/bash
  168. if [ -n "$BASH_BUG_5" ]; then
  169. (
  170. settmpdir tmpdir
  171. fname=${label##*/}
  172. e "$code" > "$tmpdir/$fname" &&
  173. chmod +x "$tmpdir/$fname" &&
  174. Wrap -vsd "$label" -- "$interpreter" "$tmpdir/$fname" "$@"
  175. )
  176. else
  177. Wrap -vsd "$label" -- "$interpreter" <(e "$code") "$@"
  178. fi
  179. }
  180. ping_check() {
  181. #global ignore_ping_check
  182. local host="$1"
  183. ip=$(getent ahosts "$host" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+" |
  184. head -n 1 | cut -f 1 -d " ") || return 1
  185. my_ip=$(curl -s myip.kal.fr)
  186. if [ "$ip" != "$my_ip" ]; then
  187. if [ -n "$ignore_ping_check" ]; then
  188. warn "IP of '$host' ($ip) doesn't match mine ($my_ip). Ignoring due to ``--ignore-ping-check`` option."
  189. else
  190. err "IP of '$host' ($ip) doesn't match mine ($my_ip). Use ``--ignore-ping-check`` to ignore check."
  191. return 1
  192. fi
  193. fi
  194. }
  195. mailcow:install-backup() {
  196. local BACKUP_SERVER="$1" ignore_ping_check="$2" mailcow_root DOMAIN
  197. ## find installation
  198. mailcow_root=$(mailcow:get-root) || {
  199. err "Couldn't find a valid mailcow root directory."
  200. return 1
  201. }
  202. ## check ok
  203. DOMAIN=$(cat "$mailcow_root/.env" | grep ^MAILCOW_HOSTNAME= | cut -f 2 -d =) || {
  204. err "Couldn't find MAILCOW_HOSTNAME in file \"$mailcow_root/.env\"."
  205. return 1
  206. }
  207. ping_check "$DOMAIN" || return 1
  208. MYSQL_ROOT_PASSWORD=$(cat "$mailcow_root/.env" | grep ^DBROOT= | cut -f 2 -d =) || {
  209. err "Couldn't find DBROOT in file \"$mailcow_root/.env\"."
  210. return 1
  211. }
  212. MYSQL_CONTAINER=${MYSQL_CONTAINER:-mailcowdockerized_mysql-mailcow_1}
  213. container_id=$(docker ps -f name="$MYSQL_CONTAINER" --format "{{.ID}}")
  214. if [ -z "$container_id" ]; then
  215. err "Couldn't find docker container named '$MYSQL_CONTAINER'."
  216. return 1
  217. fi
  218. export KEY_BACKUP_ID="mailcow"
  219. export MYSQL_ROOT_PASSWORD
  220. export MYSQL_CONTAINER
  221. export BACKUP_SERVER
  222. export DOMAIN
  223. wrap "Install rsync-backup on host" "
  224. cd /srv/charm-store/rsync-backup
  225. bash ./hooks/install.d/60-install.sh
  226. " || return 1
  227. wrap "Mysql dump install" "
  228. cd /srv/charm-store/mariadb
  229. bash ./hooks/install.d/60-backup.sh
  230. " || return 1
  231. ## Using https://github.com/mailcow/mailcow-dockerized/blob/master/helper-scripts/backup_and_restore.sh
  232. for elt in "vmail{,-attachments-vol}" crypt redis rspamd postfix; do
  233. mirror-dir:check-add-vol "$elt" || return 1
  234. done
  235. mirror-dir:check-add "$mailcow_root" || return 1
  236. mirror-dir:check-add "/var/backups/mysql" || return 1
  237. mirror-dir:check-add "/etc" || return 1
  238. dest="$BACKUP_SERVER"
  239. dest="${dest%/*}"
  240. ssh_options=()
  241. if [[ "$dest" == *":"* ]]; then
  242. port="${dest##*:}"
  243. dest="${dest%%:*}"
  244. ssh_options=(-p "$port")
  245. else
  246. port=""
  247. dest="${dest%%:*}"
  248. fi
  249. info "You can run this following command from an host having admin access to $dest:"
  250. echo " (Or send it to a backup admin of $dest)" >&2
  251. echo "ssh ${ssh_options[@]} myadmin@$dest ssh-key add '$(cat /var/lib/rsync/.ssh/id_rsa.pub)'"
  252. }
  253. compose:has_domain() {
  254. local compose_file="$1" host="$2" name conf relation relation_value domain server_aliases
  255. while read-0 name conf ; do
  256. name=$(e "$name" | shyaml get-value)
  257. if [[ "$name" =~ ^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+ ]]; then
  258. [ "$host" == "$name" ] && return 0
  259. fi
  260. rel=$(e "$conf" | shyaml -y get-value relations 2>/dev/null) || continue
  261. for relation in web-proxy publish-dir; do
  262. relation_value=$(e "$rel" | shyaml -y get-value "$relation" 2>/dev/null) || continue
  263. while read-0 label conf_relation; do
  264. domain=$(e "$conf_relation" | shyaml get-value "domain" 2>/dev/null) && {
  265. [ "$host" == "$domain" ] && return 0
  266. }
  267. server_aliases=$(e "$conf_relation" | shyaml get-values "server-aliases" 2>/dev/null) && {
  268. [[ $'\n'"$server_aliases" == *$'\n'"$host"$'\n'* ]] && return 0
  269. }
  270. done < <(e "$relation_value" | shyaml -y key-values-0)
  271. done
  272. done < <(shyaml -y key-values-0 < "$compose_file")
  273. return 1
  274. }
  275. compose:install-backup() {
  276. local BACKUP_SERVER="$1" service_name="$2" compose_file="$3" ignore_ping_check="$4" ignore_domain_check="$5"
  277. ## XXXvlab: far from perfect as it mimics and depends internal
  278. ## logic of current default way to get a domain in compose-core
  279. host=$(hostname)
  280. if ! compose:has_domain "$compose_file" "$host"; then
  281. if [ -n "$ignore_domain_check" ]; then
  282. warn "domain of '$host' not found in compose file '$compose_file'. Ignoring due to ``--ignore-domain-check`` option."
  283. else
  284. err "domain of '$host' not found in compose file '$compose_file'. Use ``--ignore-domain-check`` to ignore check."
  285. return 1
  286. fi
  287. fi
  288. ping_check "$host" || return 1
  289. if [ -e "/root/.ssh/rsync_rsa" ]; then
  290. warn "deleting private key in /root/.ssh/rsync_rsa, has we are not using it anymore."
  291. rm -fv /root/.ssh/rsync_rsa
  292. fi
  293. if [ -e "/root/.ssh/rsync_rsa.pub" ]; then
  294. warn "deleting public key in /root/.ssh/rsync_rsa.pub, has we are not using it anymore."
  295. rm -fv /root/.ssh/rsync_rsa.pub
  296. fi
  297. if service_cfg=$(cat "$compose_file" |
  298. shyaml get-value -y "$service_name" 2>/dev/null); then
  299. info "Entry for service ${DARKYELLOW}$service_name${NORMAL}" \
  300. "is already present in '$compose_file'."
  301. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  302. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  303. "entry in '$compose_file'."
  304. return 1
  305. }
  306. private_key=$(e "$cfg" | shyaml get-value private-key) || return 1
  307. target=$(e "$cfg" | shyaml get-value target) || return 1
  308. if [ "$target" != "$BACKUP_SERVER" ]; then
  309. err "Existing backup target '$target' is different" \
  310. "from specified '$BACKUP_SERVER'"
  311. return 1
  312. fi
  313. else
  314. private_key=$(ssh:mk-private-key "$host" "$service_name")
  315. cat <<EOF >> "$compose_file"
  316. $service_name:
  317. options:
  318. ident: $host
  319. target: $BACKUP_SERVER
  320. private-key: |
  321. $(e "$private_key" | sed -r 's/^/ /g')
  322. EOF
  323. fi
  324. dest="$BACKUP_SERVER"
  325. dest="${dest%/*}"
  326. ssh_options=()
  327. if [[ "$dest" == *":"* ]]; then
  328. port="${dest##*:}"
  329. dest="${dest%%:*}"
  330. ssh_options=(-p "$port")
  331. else
  332. port=""
  333. dest="${dest%%:*}"
  334. fi
  335. info "You can run this following command from an host having admin access to $dest:"
  336. echo " (Or send it to a backup admin of $dest)" >&2
  337. public_key=$(ssh-keygen -y -f <(e "$private_key"$'\n'))
  338. echo "ssh ${ssh_options[@]} myadmin@$dest ssh-key add '$public_key compose@$host'"
  339. }
  340. backup-action() {
  341. local action="$1"
  342. shift
  343. vps_type=$(vps:get-type) || {
  344. err "Failed to get type of installation."
  345. return 1
  346. }
  347. if ! fn.exists "${vps_type}:${action}"; then
  348. err "type '${vps_type}' has no ${vps_type}:${action} implemented yet."
  349. return 1
  350. fi
  351. "${vps_type}:${action}" "$@"
  352. }
  353. compose:get_default_backup_host_ident() {
  354. local service_name="$1" ## Optional
  355. local compose_file service_cfg cfg target
  356. compose_file=$(compose:get-compose-yml)
  357. service_name="${service_name:-rsync-backup}"
  358. if ! service_cfg=$(cat "$compose_file" |
  359. shyaml get-value -y "$service_name" 2>/dev/null); then
  360. err "No service named '$service_name' found in 'compose.yml'."
  361. return 1
  362. fi
  363. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  364. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  365. "entry in '$compose_file'."
  366. return 1
  367. }
  368. if ! target=$(e "$cfg" | shyaml get-value target); then
  369. err "No ${WHITE}options.target${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  370. "entry in '$compose_file'."
  371. fi
  372. if ! target=$(e "$cfg" | shyaml get-value target); then
  373. err "No ${WHITE}options.target${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  374. "entry in '$compose_file'."
  375. fi
  376. if ! ident=$(e "$cfg" | shyaml get-value ident); then
  377. err "No ${WHITE}options.ident${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  378. "entry in '$compose_file'."
  379. fi
  380. echo "$target $ident"
  381. }
  382. mailcow:get_default_backup_host_ident() {
  383. local content cron_line ident found dest cmd_line
  384. if ! [ -e "/etc/cron.d/mirror-dir" ]; then
  385. err "No '/etc/cron.d/mirror-dir' found."
  386. return 1
  387. fi
  388. content=$(cat /etc/cron.d/mirror-dir) || {
  389. err "Can't read '/etc/cron.d/mirror-dir'."
  390. return 1
  391. }
  392. if ! cron_line=$(e "$content" | grep "mirror-dir backup"); then
  393. err "Can't find 'mirror-dir backup' line in '/etc/cron.d/mirror-dir'."
  394. return 1
  395. fi
  396. cron_line=${cron_line%|*}
  397. cmd_line=(${cron_line#*root})
  398. found=
  399. dest=
  400. for arg in "${cmd_line[@]}"; do
  401. [ -n "$found" ] && {
  402. dest="$arg"
  403. break
  404. }
  405. [ "$arg" == "-d" ] && {
  406. found=1
  407. }
  408. done
  409. if ! [[ "$dest" =~ ^[\'\"a-zA-Z0-9:/.-]+$ ]]; then
  410. err "Can't find valid destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  411. return 1
  412. fi
  413. if [[ "$dest" == \"*\" ]] || [[ "$dest" == \'*\' ]]; then
  414. ## unquoting, the eval should be safe because of previous check
  415. dest=$(eval e "$dest")
  416. fi
  417. if [ -z "$dest" ]; then
  418. err "Can't find destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  419. return 1
  420. fi
  421. ## looking for ident
  422. found=
  423. ident=
  424. for arg in "${cmd_line[@]}"; do
  425. [ -n "$found" ] && {
  426. ident="$arg"
  427. break
  428. }
  429. [ "$arg" == "-h" ] && {
  430. found=1
  431. }
  432. done
  433. if ! [[ "$ident" =~ ^[\'\"a-zA-Z0-9.-]+$ ]]; then
  434. err "Can't find valid identifier in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  435. return 1
  436. fi
  437. if [[ "$ident" == \"*\" ]] || [[ "$ident" == \'*\' ]]; then
  438. ## unquoting, the eval should be safe because of previous check
  439. ident=$(eval e "$ident")
  440. fi
  441. if [ -z "$ident" ]; then
  442. err "Can't find destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  443. return 1
  444. fi
  445. echo "$dest $ident"
  446. }
  447. compose:service:containers() {
  448. local project="$1" service="$2"
  449. docker ps \
  450. --filter label="com.docker.compose.project=$project" \
  451. --filter label="compose.master-service=$service" \
  452. --format="{{.ID}}"
  453. }
  454. export -f compose:service:containers
  455. compose:service:container_one() {
  456. local project="$1" service="$2" container_id
  457. {
  458. read-0a container_id || {
  459. err "service ${DARKYELLOW}$service${NORMAL} has no running container."
  460. return 1
  461. }
  462. if read-0a _; then
  463. err "service ${DARKYELLOW}$service${NORMAL} has more than one running container."
  464. return 1
  465. fi
  466. } < <(compose:service:containers "$project" "$service")
  467. echo "$container_id"
  468. }
  469. export -f compose:service:container_one
  470. compose:service:container_first() {
  471. local project="$1" service="$2" container_id
  472. {
  473. read-0a container_id || {
  474. err "service ${DARKYELLOW}$service${NORMAL} has no running container."
  475. return 1
  476. }
  477. if read-0a _; then
  478. warn "service ${DARKYELLOW}$service${NORMAL} has more than one running container."
  479. fi
  480. } < <(compose:service:containers "$project" "$service")
  481. echo "$container_id"
  482. }
  483. export -f compose:service:container_first
  484. compose:project:containers() {
  485. local project="$1" opts
  486. opts+=(--filter label="com.docker.compose.project=$project")
  487. docker ps "${opts[@]}" \
  488. --format="{{.ID}}"
  489. }
  490. export -f compose:project:containers
  491. compose:charm:containers() {
  492. local project="$1" charm="$2"
  493. docker ps \
  494. --filter label="com.docker.compose.project=$project" \
  495. --filter label="compose.charm=$charm" \
  496. --format="{{.ID}}"
  497. }
  498. export -f compose:charm:containers
  499. compose:charm:container_one() {
  500. local project="$1" charm="$2" container_id
  501. {
  502. read-0a container_id || {
  503. err "charm ${DARKPINK}$charm${NORMAL} has no running container in project '$project'."
  504. return 1
  505. }
  506. if read-0a _; then
  507. err "charm ${DARKPINK}$charm${NORMAL} has more than one running container."
  508. return 1
  509. fi
  510. } < <(compose:charm:containers "$project" "$charm")
  511. echo "$container_id"
  512. }
  513. export -f compose:charm:container_one
  514. compose:charm:container_first() {
  515. local project="$1" charm="$2" container_id
  516. {
  517. read-0a container_id || {
  518. warn "charm ${DARKYELLOW}$charm${NORMAL} has no running container in project '$project'."
  519. }
  520. if read-0a _; then
  521. warn "charm ${DARKYELLOW}$charm${NORMAL} has more than one running container."
  522. fi
  523. } < <(compose:charm:containers "$project" "$charm")
  524. echo "$container_id"
  525. }
  526. export -f compose:charm:container_first
  527. compose:get_url() {
  528. local project_name="$1" service="$2" data_file network ip
  529. data_file="/var/lib/compose/relations/${project_name}/${service}-frontend/web-proxy/data"
  530. if [ -e "$data_file" ]; then
  531. (
  532. set -o pipefail
  533. cat "$data_file" | shyaml get-value url
  534. )
  535. else
  536. ## Assume there are no frontend relation here, the url is direct IP
  537. container_id=$(compose:service:container_one "${project_name}" "${service}") || return 1
  538. network_ip=$(docker:container:network_ip_one "${container_id}") || return 1
  539. IFS=":" read -r network ip <<<"$network_ip"
  540. tcp_port=
  541. for port in $(docker:exposed_ports "$container_id"); do
  542. IFS="/" read port type <<<"$port"
  543. [ "$type" == "tcp" ] || continue
  544. tcp_port="$port"
  545. break
  546. done
  547. echo -n "http://$ip"
  548. [ -n "$tcp_port" ] && echo ":$tcp_port"
  549. fi || {
  550. err "Failed querying ${service} to frontend relation to get url."
  551. return 1
  552. }
  553. }
  554. export -f compose:get_url
  555. compose:container:service() {
  556. local container="$1" service
  557. if ! service=$(docker:container:label "$container" "compose.service"); then
  558. err "Failed to get service name from container ${container}."
  559. return 1
  560. fi
  561. if [ -z "$service" ]; then
  562. err "No service found for container ${container}."
  563. return 1
  564. fi
  565. echo "$service"
  566. }
  567. export -f compose:container:service
  568. compose:psql() {
  569. local project_name="$1" dbname="$2" container_id
  570. container_id=$(compose:charm:container_one "$project_name" "postgres") || return 1
  571. docker exec -i "${container_id}" psql -U postgres "$dbname"
  572. }
  573. export -f compose:psql
  574. compose:mongo() {
  575. local project_name="$1" dbname="$2" container_id
  576. container_id=$(compose:charm:container_one "$project_name" "mongo") || return 1
  577. docker exec -i "${container_id}" mongo --quiet "$dbname"
  578. }
  579. export -f compose:mongo
  580. compose:pgm() {
  581. local project_name="$1" container_network_ip container_ip container_network
  582. shift
  583. container_id=$(compose:charm:container_one "$project_name" "postgres") || return 1
  584. service_name=$(compose:container:service "$container_id") || return 1
  585. image_id=$(docker:container:image "$container_id") || return 1
  586. container_network_ip=$(docker:container:network_ip_one "$container_id") || return 1
  587. IFS=":" read -r container_network container_ip <<<"$container_network_ip"
  588. pgpass="/srv/datastore/data/${service_name}/var/lib/postgresql/data/pgpass"
  589. local final_pgm_docker_run_opts+=(
  590. -u 0 -e prefix_pg_local_command=" "
  591. --network "${container_network}"
  592. -e PGHOST="$container_ip"
  593. -e PGUSER=postgres
  594. -v "$pgpass:/root/.pgpass"
  595. "${pgm_docker_run_opts[@]}"
  596. )
  597. cmd=(docker run --rm \
  598. "${final_pgm_docker_run_opts[@]}" \
  599. "${image_id}" pgm "$@"
  600. )
  601. echo "${cmd[@]}"
  602. "${cmd[@]}"
  603. }
  604. export -f compose:pgm
  605. postgres:dump() {
  606. local project_name="$1" src="$2" dst="$3"
  607. (
  608. settmpdir PGM_TMP_LOCATION
  609. pgm_docker_run_opts=('-v' "${PGM_TMP_LOCATION}:/tmp/dump")
  610. compose:pgm "$project_name" cp -f "$src" "/tmp/dump/dump.gz" &&
  611. mv "$PGM_TMP_LOCATION/dump.gz" "$dst"
  612. ) || return 1
  613. }
  614. export -f postgres:dump
  615. postgres:restore() {
  616. local project_name="$1" src="$2" dst="$3"
  617. full_src_path=$(readlink -e "$src") || exit 1
  618. (
  619. pgm_docker_run_opts=('-v' "${full_src_path}:/tmp/dump.gz")
  620. compose:pgm "$project_name" cp -f "/tmp/dump.gz" "$dst"
  621. ) || return 1
  622. }
  623. export -f postgres:restore
  624. cyclos:set_root_url() {
  625. local project_name="$1" dbname="$2" url="$3"
  626. echo "UPDATE configurations SET root_url = '$url';" |
  627. compose:psql "$project_name" "$dbname" || {
  628. err "Failed to set cyclos url value in '$dbname' database."
  629. return 1
  630. }
  631. }
  632. export -f cyclos:set_root_url
  633. cyclos:unlock() {
  634. local project_name="$1" dbname="$2"
  635. echo "delete from database_lock;" |
  636. compose:psql "${project_name}" "${dbname}"
  637. }
  638. export -f cyclos:unlock
  639. rocketchat:drop-indexes() {
  640. local project_name="$1" dbname="$2"
  641. echo "db.users.dropIndexes()" |
  642. compose:mongo "${project_name}" "${dbname}"
  643. }
  644. export -f rocketchat:drop-indexes
  645. compose:project_name() {
  646. if [ -z "$PROJECT_NAME" ]; then
  647. PROJECT_NAME=$(compose --get-project-name) || {
  648. err "Couldn't get project name."
  649. return 1
  650. }
  651. if [ -z "$PROJECT_NAME" -o "$PROJECT_NAME" == "orphan" ]; then
  652. err "Couldn't get project name, probably because 'compose.yml' wasn't found."
  653. echo " Please ensure to either configure a global 'compose.yml' or run this command" >&2
  654. echo " in a compose project (with 'compose.yml' on the top level directory)." >&2
  655. return 1
  656. fi
  657. export PROJECT_NAME
  658. fi
  659. echo "$PROJECT_NAME"
  660. }
  661. export -f compose:project_name
  662. compose:get_cron_docker_cmd() {
  663. local cron_line cmd_line docker_cmd
  664. project_name=$(compose:project_name) || return 1
  665. if ! cron_line=$(docker exec "${project_name}"_cron_1 cat /etc/cron.d/rsync-backup | grep "\* \* \*"); then
  666. err "Can't find cron_line in cron container."
  667. echo " Have you forgotten to run 'compose up' ?" >&2
  668. return 1
  669. fi
  670. cron_line=${cron_line%|*}
  671. cron_line=${cron_line%"2>&1"*}
  672. cmd_line="${cron_line#*root}"
  673. eval "args=($cmd_line)"
  674. ## should be last argument
  675. docker_cmd=$(echo ${args[@]: -1})
  676. if ! [[ "$docker_cmd" == "docker run --rm -e "* ]]; then
  677. echo "docker command found should start with 'docker run'." >&2
  678. echo "Here's command:" >&2
  679. echo " $docker_cmd" >&2
  680. return 1
  681. fi
  682. e "$docker_cmd"
  683. }
  684. compose:recover-target() {
  685. local backup_host="$1" ident="$2" src="$3" dst="$4" service_name="${5:-rsync-backup}" project_name
  686. project_name=$(compose:project_name) || return 1
  687. docker_image="${project_name}_${service_name}"
  688. if ! docker_has_image "$docker_image"; then
  689. compose build "${service_name}" || {
  690. err "Couldn't find nor build image for service '$service_name'."
  691. return 1
  692. }
  693. fi
  694. dst="${dst%/}" ## remove final slash
  695. ssh_options=(-o StrictHostKeyChecking=no)
  696. if [[ "$backup_host" == *":"* ]]; then
  697. port="${backup_host##*:}"
  698. backup_host="${backup_host%%:*}"
  699. ssh_options+=(-p "$port")
  700. else
  701. port=""
  702. backup_host="${backup_host%%:*}"
  703. fi
  704. rsync_opts=(
  705. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  706. -azvArH --delete --delete-excluded
  707. --partial --partial-dir .rsync-partial
  708. --numeric-ids
  709. )
  710. if [ "$DRY_RUN" ]; then
  711. rsync_opts+=("-n")
  712. fi
  713. cmd=(
  714. docker run --rm --entrypoint rsync \
  715. -v "/srv/datastore/config/${service_name}/var/lib/rsync":/var/lib/rsync \
  716. -v "${dst%/*}":/mnt/dest \
  717. "$docker_image" \
  718. "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "/mnt/dest/${dst##*/}"
  719. )
  720. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  721. "${cmd[@]}"
  722. }
  723. mailcow:recover-target() {
  724. local backup_host="$1" ident="$2" src="$3" dst="$4"
  725. dst="${dst%/}" ## remove final slash
  726. ssh_options=(-o StrictHostKeyChecking=no)
  727. if [[ "$backup_host" == *":"* ]]; then
  728. port="${backup_host##*:}"
  729. backup_host="${backup_host%%:*}"
  730. ssh_options+=(-p "$port")
  731. else
  732. port=""
  733. backup_host="${backup_host%%:*}"
  734. fi
  735. rsync_opts=(
  736. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  737. -azvArH --delete --delete-excluded
  738. --partial --partial-dir .rsync-partial
  739. --numeric-ids
  740. )
  741. if [ "$DRY_RUN" ]; then
  742. rsync_opts+=("-n")
  743. fi
  744. cmd=(
  745. rsync "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "${dst}"
  746. )
  747. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  748. "${cmd[@]}"
  749. }
  750. nextcloud:src:version() {
  751. local version
  752. if ! version=$(cat "/srv/datastore/data/${nextcloud_service}/var/www/html/version.php" 2>/dev/null); then
  753. err "Can't find version.php file to get last version installed."
  754. exit 1
  755. fi
  756. version=$(e "$version" | grep 'VersionString =' | cut -f 3 -d ' ' | cut -f 2 -d "'")
  757. if [ -z "$version" ]; then
  758. err "Can't figure out version from version.php content."
  759. exit 1
  760. fi
  761. echo "$version"
  762. }
  763. container:health:check-fix() {
  764. local container_id="$1"
  765. timeout 5s docker inspect "$container_id" >/dev/null 2>&1
  766. errlvl=$?
  767. if [ "$errlvl" == 124 ]; then
  768. service_name=$(docker ps --filter id="$container_id" --format '{{.Label "com.docker.compose.service"}}')
  769. container_name=($(docker ps --filter id="$container_id" --format '{{.Names}}'))
  770. pid=$(ps ax -o pid,command -ww | grep docker-containerd-shim | grep "/$container_id" |
  771. cut -f 1 -d " ")
  772. if [ -z "$pid" ]; then
  773. err "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} doesn't answer to 'inspect' command. Can't find its PID neither."
  774. return 1
  775. fi
  776. echo "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} doesn't answer to 'inspect' command (pid: $pid)."
  777. Wrap -d "kill pid $pid and restart" <<EOF
  778. kill "$pid"
  779. sleep 2
  780. docker restart "$container_id"
  781. EOF
  782. fi
  783. return $errlvl
  784. }
  785. [ "$SOURCED" ] && return 0
  786. ##
  787. ## Command line processing
  788. ##
  789. cmdline.spec.gnu
  790. cmdline.spec.reporting
  791. cmdline.spec.gnu install
  792. cmdline.spec::cmd:install:run() {
  793. :
  794. }
  795. cmdline.spec.gnu get-type
  796. cmdline.spec::cmd:get-type:run() {
  797. vps:get-type
  798. }
  799. cmdline.spec:install:cmd:backup:run() {
  800. : :posarg: BACKUP_SERVER 'Target backup server'
  801. : :optfla: --ignore-domain-check \
  802. "Allow to bypass the domain check in
  803. compose file (only used in compose
  804. installation)."
  805. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  806. local vps_type
  807. vps_type=$(vps:get-type) || {
  808. err "Failed to get type of installation."
  809. return 1
  810. }
  811. if ! fn.exists "${vps_type}:install-backup"; then
  812. err "type '${vps_type}' has no backup installation implemented yet."
  813. return 1
  814. fi
  815. opts=()
  816. [ "$opt_ignore_ping_check" ] &&
  817. opts+=("--ignore-ping-check")
  818. if [ "$vps_type" == "compose" ]; then
  819. [ "$opt_ignore_domain_check" ] &&
  820. opts+=("--ignore-domain-check")
  821. fi
  822. "cmdline.spec:install:cmd:${vps_type}-backup:run" "${opts[@]}" "$BACKUP_SERVER"
  823. }
  824. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  825. cmdline.spec.gnu compose-backup
  826. cmdline.spec:install:cmd:compose-backup:run() {
  827. : :posarg: BACKUP_SERVER 'Target backup server'
  828. : :optval: --service-name,-s "YAML service name in compose
  829. file to check for existence of key.
  830. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  831. : :optval: --compose-file,-f "Compose file location. Defaults to
  832. the value of '\$DEFAULT_COMPOSE_FILE'"
  833. : :optfla: --ignore-domain-check \
  834. "Allow to bypass the domain check in
  835. compose file."
  836. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  837. local service_name compose_file
  838. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  839. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  840. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  841. if ! [ -e "$compose_file" ]; then
  842. err "Compose file not found in '$compose_file'."
  843. return 1
  844. fi
  845. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" \
  846. "$opt_ignore_ping_check" "$opt_ignore_domain_check"
  847. }
  848. cmdline.spec:install:cmd:mailcow-backup:run() {
  849. : :posarg: BACKUP_SERVER 'Target backup server'
  850. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  851. "mailcow:install-backup" "$BACKUP_SERVER" "$opt_ignore_ping_check"
  852. }
  853. cmdline.spec.gnu backup
  854. cmdline.spec::cmd:backup:run() {
  855. local vps_type
  856. vps_type=$(vps:get-type) || {
  857. err "Failed to get type of installation."
  858. return 1
  859. }
  860. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  861. err "type '${vps_type}' has no backup process implemented yet."
  862. return 1
  863. fi
  864. "cmdline.spec:backup:cmd:${vps_type}:run"
  865. }
  866. cmdline.spec:backup:cmd:mailcow:run() {
  867. local cmd_line cron_line cmd
  868. for f in mysql-backup mirror-dir; do
  869. [ -e "/etc/cron.d/$f" ] || {
  870. err "Can't find '/etc/cron.d/$f'."
  871. echo " Have you forgotten to run 'vps install backup BACKUP_HOST' ?" >&2
  872. return 1
  873. }
  874. if ! cron_line=$(cat "/etc/cron.d/$f" |
  875. grep -v "^#" | grep "\* \* \*"); then
  876. err "Can't find cron_line in '/etc/cron.d/$f'." \
  877. "Have you modified it ?"
  878. return 1
  879. fi
  880. cron_line=${cron_line%|*}
  881. cmd_line=(${cron_line#*root})
  882. if [ "$f" == "mirror-dir" ]; then
  883. cmd=()
  884. for arg in "${cmd_line[@]}"; do
  885. [ "$arg" != "-q" ] && cmd+=("$arg")
  886. done
  887. else
  888. cmd=("${cmd_line[@]}")
  889. fi
  890. code="${cmd[*]}"
  891. echo "${WHITE}Launching:${NORMAL} ${code}"
  892. {
  893. {
  894. (
  895. ## Some commands are using colors that are already
  896. ## set by this current program and will trickle
  897. ## down unwantedly
  898. ansi_color no
  899. eval "${code}"
  900. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  901. set_errlvl "${PIPESTATUS[0]}"
  902. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  903. set_errlvl "${PIPESTATUS[0]}"
  904. } 3>&1 1>&2 2>&3
  905. if [ "$?" != "0" ]; then
  906. err "Failed."
  907. return 1
  908. fi
  909. done
  910. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  911. }
  912. set_errlvl() { return "${1:-1}"; }
  913. cmdline.spec:backup:cmd:compose:run() {
  914. local cron_line args
  915. project_name=$(compose:project_name) || return 1
  916. docker_cmd=$(compose:get_cron_docker_cmd) || return 1
  917. echo "${WHITE}Launching:${NORMAL} docker exec -i "${project_name}_cron_1" $docker_cmd"
  918. {
  919. {
  920. eval "docker exec -i \"${project_name}_cron_1\" $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  921. set_errlvl "${PIPESTATUS[0]}"
  922. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  923. set_errlvl "${PIPESTATUS[0]}"
  924. } 3>&1 1>&2 2>&3
  925. if [ "$?" != "0" ]; then
  926. err "Failed."
  927. return 1
  928. fi
  929. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  930. }
  931. cmdline.spec.gnu recover-target
  932. cmdline.spec::cmd:recover-target:run() {
  933. : :posarg: BACKUP_DIR 'Source directory on backup side'
  934. : :posarg: HOST_DIR 'Target directory on host side'
  935. : :optval: --backup-host,-B "The backup host"
  936. : :optfla: --dry-run,-n "Don't do anything, instead tell what it
  937. would do."
  938. ## if no backup host take the one by default
  939. backup_host="$opt_backup_host"
  940. if [ -z "$backup_host" ]; then
  941. backup_host_ident=$(backup-action get_default_backup_host_ident) || return 1
  942. read -r backup_host ident <<<"$backup_host_ident"
  943. fi
  944. if [[ "$BACKUP_DIR" == /* ]]; then
  945. err "BACKUP_DIR must be a relative path from the root of your backup."
  946. return 1
  947. fi
  948. REAL_HOST_DIR=$(realpath "$HOST_DIR") || {
  949. err "Can't find HOST_DIR '$HOST_DIR'."
  950. return 1
  951. }
  952. export DRY_RUN="${opt_dry_run}"
  953. backup-action recover-target "$backup_host" "$ident" "$BACKUP_DIR" "$REAL_HOST_DIR"
  954. }
  955. cmdline.spec.gnu odoo
  956. cmdline.spec::cmd:odoo:run() {
  957. :
  958. }
  959. cmdline.spec.gnu restart
  960. cmdline.spec:odoo:cmd:restart:run() {
  961. : :optval: --service,-s "The service (defaults to 'odoo')"
  962. local out odoo_service
  963. odoo_service="${opt_service:-odoo}"
  964. project_name=$(compose:project_name) || return 1
  965. if ! out=$(docker restart "${project_name}_${odoo_service}_1" 2>&1); then
  966. if [[ "$out" == *"no matching entries in passwd file" ]]; then
  967. warn "Catched docker bug. Restarting once more."
  968. if ! out=$(docker restart "${project_name}_${odoo_service}_1"); then
  969. err "Can't restart container ${project_name}_${odoo_service}_1 (restarted twice)."
  970. echo " output:" >&2
  971. echo "$out" | prefix " ${GRAY}|${NORMAL} " >&2
  972. exit 1
  973. fi
  974. else
  975. err "Couldn't restart container ${project_name}_${odoo_service}_1 (and no restart bug detected)."
  976. exit 1
  977. fi
  978. fi
  979. info "Container ${project_name}_${odoo_service}_1 was ${DARKGREEN}successfully${NORMAL} restarted."
  980. }
  981. cmdline.spec.gnu restore
  982. cmdline.spec:odoo:cmd:restore:run() {
  983. : :posarg: ZIP_DUMP_LOCATION 'Source odoo dump file to restore
  984. (can be a local file or an url)'
  985. : :optval: --service,-s "The service (defaults to 'odoo')"
  986. : :optval: --database,-d 'Target database (default if not specified)'
  987. local out
  988. odoo_service="${opt_service:-odoo}"
  989. if [[ "$ZIP_DUMP_LOCATION" == "http://"* ]] ||
  990. [[ "$ZIP_DUMP_LOCATION" == "https://"* ]]; then
  991. settmpdir ZIP_TMP_LOCATION
  992. tmp_location="$ZIP_TMP_LOCATION/dump.zip"
  993. curl -k -s -L "$ZIP_DUMP_LOCATION" > "$tmp_location" || {
  994. err "Couldn't get '$ZIP_DUMP_LOCATION'."
  995. exit 1
  996. }
  997. if [[ "$(dd if="$tmp_location" count=2 bs=1 2>/dev/null)" != "PK" ]]; then
  998. err "Download doesn't seem to be a zip file."
  999. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1000. exit 1
  1001. fi
  1002. info "Successfully downloaded '$ZIP_DUMP_LOCATION'"
  1003. echo " in '$tmp_location'." >&2
  1004. ZIP_DUMP_LOCATION="$tmp_location"
  1005. fi
  1006. [ -e "$ZIP_DUMP_LOCATION" ] || {
  1007. err "No file '$ZIP_DUMP_LOCATION' found." >&2
  1008. exit 1
  1009. }
  1010. #cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  1011. msg_dbname=default
  1012. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1013. compose --no-hooks drop "$odoo_service" $opt_database || {
  1014. err "Error dropping $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}."
  1015. exit 1
  1016. }
  1017. compose --no-hooks load "$odoo_service" $opt_database < "$ZIP_DUMP_LOCATION" || {
  1018. err "Error restoring service ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  1019. exit 1
  1020. }
  1021. info "Successfully restored ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  1022. ## Restart odoo, ensure there is no bugs lingering on it.
  1023. cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  1024. }
  1025. cmdline.spec.gnu dump
  1026. cmdline.spec:odoo:cmd:dump:run() {
  1027. : :posarg: DUMP_ZIPFILE 'Target path to store odoo dump zip file.'
  1028. : :optval: --database,-d 'Target database (default if not specified)'
  1029. : :optval: --service,-s "The service (defaults to 'odoo')"
  1030. odoo_service="${opt_service:-odoo}"
  1031. msg_dbname=default
  1032. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1033. compose --no-hooks save "$odoo_service" $opt_database > "$DUMP_ZIPFILE" || {
  1034. err "Error dumping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1035. exit 1
  1036. }
  1037. info "Successfully dumped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1038. }
  1039. cmdline.spec.gnu drop
  1040. cmdline.spec:odoo:cmd:drop:run() {
  1041. : :optval: --database,-d 'Target database (default if not specified)'
  1042. : :optval: --service,-s "The service (defaults to 'odoo')"
  1043. odoo_service="${opt_service:-odoo}"
  1044. msg_dbname=default
  1045. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1046. compose --no-hooks drop "$odoo_service" $opt_database || {
  1047. err "Error dropping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1048. exit 1
  1049. }
  1050. info "Successfully dropped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1051. }
  1052. cmdline.spec.gnu set-cyclos-url
  1053. cmdline.spec:odoo:cmd:set-cyclos-url:run() {
  1054. : :optval: --database,-d "Target database ('odoo' if not specified)"
  1055. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1056. local URL
  1057. dbname=${opt_database:-odoo}
  1058. cyclos_service="${opt_service:-cyclos}"
  1059. project_name=$(compose:project_name) || exit 1
  1060. URL=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1061. Wrap -d "set cyclos url to '$URL'" <<EOF || exit 1
  1062. echo "UPDATE res_company SET cyclos_server_url = '$URL/api' WHERE id=1;" |
  1063. compose:psql "$project_name" "$dbname" || {
  1064. err "Failed to set cyclos url value in '$dbname' database."
  1065. exit 1
  1066. }
  1067. EOF
  1068. }
  1069. cmdline.spec.gnu cyclos
  1070. cmdline.spec::cmd:cyclos:run() {
  1071. :
  1072. }
  1073. cmdline.spec:cyclos:cmd:dump:run() {
  1074. : :posarg: DUMP_GZFILE 'Target path to store odoo dump gz file.'
  1075. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1076. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1077. cyclos_service="${opt_service:-cyclos}"
  1078. cyclos_database="${opt_database:-cyclos}"
  1079. project_name=$(compose:project_name) || exit 1
  1080. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1081. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1082. docker stop "$container_id" || exit 1
  1083. Wrap -d "Dump postgres database '${cyclos_database}'." -- \
  1084. postgres:dump "${project_name}" "$cyclos_database" "$DUMP_GZFILE" || exit 1
  1085. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1086. docker start "${container_id}" || exit 1
  1087. }
  1088. cmdline.spec.gnu restore
  1089. cmdline.spec:cyclos:cmd:restore:run() {
  1090. : :posarg: GZ_DUMP_LOCATION 'Source cyclos dump file to restore
  1091. (can be a local file or an url)'
  1092. : :optval: --service,-s "The service (defaults to 'cyclos')"
  1093. : :optval: --database,-d 'Target database (default if not specified)'
  1094. local out
  1095. cyclos_service="${opt_service:-cyclos}"
  1096. cyclos_database="${opt_database:-cyclos}"
  1097. project_name=$(compose:project_name) || exit 1
  1098. url=$(compose:get_url "${project_name}" "${cyclos_service}") || return 1
  1099. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1100. if [[ "$GZ_DUMP_LOCATION" == "http://"* ]] ||
  1101. [[ "$GZ_DUMP_LOCATION" == "https://"* ]]; then
  1102. settmpdir GZ_TMP_LOCATION
  1103. tmp_location="$GZ_TMP_LOCATION/dump.gz"
  1104. Wrap -d "get '$GZ_DUMP_LOCATION'" <<EOF || exit 1
  1105. ## Note that curll version before 7.76.0 do not have
  1106. curl -k -s -L "$GZ_DUMP_LOCATION" --fail \\
  1107. > "$tmp_location" || {
  1108. echo "Error fetching ressource. Is url correct ?" >&2
  1109. exit 1
  1110. }
  1111. if [[ "\$(dd if="$tmp_location" count=2 bs=1 2>/dev/null |
  1112. hexdump -v -e "/1 \"%02x\"")" != "1f8b" ]]; then
  1113. err "Download doesn't seem to be a gzip file."
  1114. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1115. exit 1
  1116. fi
  1117. EOF
  1118. GZ_DUMP_LOCATION="$tmp_location"
  1119. fi
  1120. [ -e "$GZ_DUMP_LOCATION" ] || {
  1121. err "No file '$GZ_DUMP_LOCATION' found." >&2
  1122. exit 1
  1123. }
  1124. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1125. docker stop "$container_id" || exit 1
  1126. ## XXXvlab: making the assumption that the postgres username should
  1127. ## be the same as the cyclos service selected (which is the default,
  1128. ## but not always the case).
  1129. Wrap -d "restore postgres database '${cyclos_database}'." -- \
  1130. postgres:restore "$project_name" "$GZ_DUMP_LOCATION" "${cyclos_service}@${cyclos_database}" || exit 1
  1131. ## ensure that the database is not locked
  1132. Wrap -d "check and remove database lock if any" -- \
  1133. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1134. Wrap -d "set root url to '$url'" -- \
  1135. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1136. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1137. docker start "${container_id}" || exit 1
  1138. }
  1139. cmdline.spec.gnu set-root-url
  1140. cmdline.spec:cyclos:cmd:set-root-url:run() {
  1141. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1142. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1143. local URL
  1144. cyclos_database=${opt_database:-cyclos}
  1145. cyclos_service="${opt_service:-cyclos}"
  1146. project_name=$(compose:project_name) || exit 1
  1147. url=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1148. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1149. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1150. docker stop "$container_id" || exit 1
  1151. Wrap -d "set root url to '$url'" -- \
  1152. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1153. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1154. docker start "${container_id}" || exit 1
  1155. }
  1156. cmdline.spec.gnu unlock
  1157. cmdline.spec:cyclos:cmd:unlock:run() {
  1158. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1159. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1160. local URL
  1161. cyclos_database=${opt_database:-cyclos}
  1162. cyclos_service="${opt_service:-cyclos}"
  1163. project_name=$(compose:project_name) || exit 1
  1164. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1165. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1166. docker stop "$container_id" || exit 1
  1167. Wrap -d "check and remove database lock if any" -- \
  1168. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1169. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1170. docker start "${container_id}" || exit 1
  1171. }
  1172. cmdline.spec.gnu rocketchat
  1173. cmdline.spec::cmd:rocketchat:run() {
  1174. :
  1175. }
  1176. cmdline.spec.gnu drop-indexes
  1177. cmdline.spec:rocketchat:cmd:drop-indexes:run() {
  1178. : :optval: --database,-d "Target database ('rocketchat' if not specified)"
  1179. : :optval: --service,-s "The rocketchat service name (defaults to 'rocketchat')"
  1180. local URL
  1181. rocketchat_database=${opt_database:-rocketchat}
  1182. rocketchat_service="${opt_service:-rocketchat}"
  1183. project_name=$(compose:project_name) || exit 1
  1184. container_id=$(compose:service:container_one "${project_name}" "${rocketchat_service}") || exit 1
  1185. Wrap -d "stop ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1186. docker stop "$container_id" || exit 1
  1187. errlvl=0
  1188. Wrap -d "drop indexes" -- \
  1189. rocketchat:drop-indexes "${project_name}" "${rocketchat_database}" || {
  1190. errlvl=1
  1191. errmsg="Failed to drop indexes"
  1192. }
  1193. Wrap -d "start ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1194. docker start "${container_id}" || exit 1
  1195. if [ "$errlvl" != 0 ]; then
  1196. err "$errmsg"
  1197. fi
  1198. exit "$errlvl"
  1199. }
  1200. cmdline.spec.gnu nextcloud
  1201. cmdline.spec::cmd:nextcloud:run() {
  1202. :
  1203. }
  1204. cmdline.spec.gnu upgrade
  1205. cmdline.spec:nextcloud:cmd:upgrade:run() {
  1206. : :posarg: [TARGET_VERSION] "Source cyclos dump file to restore"
  1207. : :optval: --service,-s "The nexcloud service name (defaults to 'nextcloud')"
  1208. local URL
  1209. nextcloud_service="${opt_service:-nextcloud}"
  1210. project_name=$(compose:project_name) || exit 1
  1211. containers=$(compose:service:containers "${project_name}" "${nextcloud_service}") || exit 1
  1212. container_stopped=()
  1213. if [ -n "$containers" ]; then
  1214. for container in $containers; do
  1215. Wrap -d "stop ${DARKYELLOW}${nextcloud_service}${NORMAL}'s container" -- \
  1216. docker stop "$container" || {
  1217. err "Failed to stop container '$container'."
  1218. exit 1
  1219. }
  1220. container_stopped+=("$container")
  1221. done
  1222. fi
  1223. before_version=$(nextcloud:src:version) || exit 1
  1224. ## -q to remove the display of ``compose`` related information
  1225. ## like relation resolution.
  1226. ## --no-hint to remove the final hint about modifying your
  1227. ## ``compose.yml``.
  1228. compose -q upgrade "$nextcloud_service" --no-hint
  1229. errlvl="$?"
  1230. after_version=$(nextcloud:src:version)
  1231. if [ "$after_version" != "$before_version" ]; then
  1232. desc="update \`compose.yml\` to set ${DARKYELLOW}$nextcloud_service${NORMAL}'s "
  1233. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  1234. Wrap -d "$desc" -- \
  1235. compose:file:value-change \
  1236. "${nextcloud_service}.docker-compose.image" \
  1237. "docker.0k.io/nextcloud:${after_version}-myc" || exit 1
  1238. fi
  1239. if [ "$errlvl" == 0 ]; then
  1240. echo "${WHITE}Launching final compose${NORMAL}"
  1241. compose up || exit 1
  1242. fi
  1243. exit "$errlvl"
  1244. }
  1245. cmdline.spec.gnu check-fix
  1246. cmdline.spec::cmd:check-fix:run() {
  1247. :
  1248. }
  1249. cmdline.spec.gnu upgrade
  1250. cmdline.spec:check-fix:cmd:container-aliveness:run() {
  1251. local project_name containers container
  1252. project_name=$(compose:project_name) || exit 1
  1253. containers=($(compose:project:containers "${project_name}")) || exit 1
  1254. ## XXXvlab: could make it parallel
  1255. for container in "${containers[@]}"; do
  1256. container:health:check-fix "$container"
  1257. done
  1258. }
  1259. cmdline::parse "$@"