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.

1574 lines
46 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:charm:containers() {
  485. local project="$1" charm="$2"
  486. docker ps \
  487. --filter label="com.docker.compose.project=$project" \
  488. --filter label="compose.charm=$charm" \
  489. --format="{{.ID}}"
  490. }
  491. export -f compose:charm:containers
  492. compose:charm:container_one() {
  493. local project="$1" charm="$2" container_id
  494. {
  495. read-0a container_id || {
  496. err "charm ${DARKPINK}$charm${NORMAL} has no running container in project '$project'."
  497. return 1
  498. }
  499. if read-0a _; then
  500. err "charm ${DARKPINK}$charm${NORMAL} has more than one running container."
  501. return 1
  502. fi
  503. } < <(compose:charm:containers "$project" "$charm")
  504. echo "$container_id"
  505. }
  506. export -f compose:charm:container_one
  507. compose:charm:container_first() {
  508. local project="$1" charm="$2" container_id
  509. {
  510. read-0a container_id || {
  511. warn "charm ${DARKYELLOW}$charm${NORMAL} has no running container in project '$project'."
  512. }
  513. if read-0a _; then
  514. warn "charm ${DARKYELLOW}$charm${NORMAL} has more than one running container."
  515. fi
  516. } < <(compose:charm:containers "$project" "$charm")
  517. echo "$container_id"
  518. }
  519. export -f compose:charm:container_first
  520. compose:get_url() {
  521. local project_name="$1" service="$2" data_file network ip
  522. data_file="/var/lib/compose/relations/${project_name}/${service}-frontend/web-proxy/data"
  523. if [ -e "$data_file" ]; then
  524. (
  525. set -o pipefail
  526. cat "$data_file" | shyaml get-value url
  527. )
  528. else
  529. ## Assume there are no frontend relation here, the url is direct IP
  530. container_id=$(compose:service:container_one "${project_name}" "${service}") || return 1
  531. network_ip=$(docker:container:network_ip_one "${container_id}") || return 1
  532. IFS=":" read -r network ip <<<"$network_ip"
  533. tcp_port=
  534. for port in $(docker:exposed_ports "$container_id"); do
  535. IFS="/" read port type <<<"$port"
  536. [ "$type" == "tcp" ] || continue
  537. tcp_port="$port"
  538. break
  539. done
  540. echo -n "http://$ip"
  541. [ -n "$tcp_port" ] && echo ":$tcp_port"
  542. fi || {
  543. err "Failed querying ${service} to frontend relation to get url."
  544. return 1
  545. }
  546. }
  547. export -f compose:get_url
  548. compose:container:service() {
  549. local container="$1" service
  550. if ! service=$(docker:container:label "$container" "compose.service"); then
  551. err "Failed to get service name from container ${container}."
  552. return 1
  553. fi
  554. if [ -z "$service" ]; then
  555. err "No service found for container ${container}."
  556. return 1
  557. fi
  558. echo "$service"
  559. }
  560. export -f compose:container:service
  561. compose:psql() {
  562. local project_name="$1" dbname="$2" container_id
  563. container_id=$(compose:charm:container_one "$project_name" "postgres") || return 1
  564. docker exec -i "${container_id}" psql -U postgres "$dbname"
  565. }
  566. export -f compose:psql
  567. compose:mongo() {
  568. local project_name="$1" dbname="$2" container_id
  569. container_id=$(compose:charm:container_one "$project_name" "mongo") || return 1
  570. docker exec -i "${container_id}" mongo --quiet "$dbname"
  571. }
  572. export -f compose:mongo
  573. compose:pgm() {
  574. local project_name="$1" container_network_ip container_ip container_network
  575. shift
  576. container_id=$(compose:charm:container_one "$project_name" "postgres") || return 1
  577. service_name=$(compose:container:service "$container_id") || return 1
  578. image_id=$(docker:container:image "$container_id") || return 1
  579. container_network_ip=$(docker:container:network_ip_one "$container_id") || return 1
  580. IFS=":" read -r container_network container_ip <<<"$container_network_ip"
  581. pgpass="/srv/datastore/data/${service_name}/var/lib/postgresql/data/pgpass"
  582. local final_pgm_docker_run_opts+=(
  583. -u 0 -e prefix_pg_local_command=" "
  584. --network "${container_network}"
  585. -e PGHOST="$container_ip"
  586. -e PGUSER=postgres
  587. -v "$pgpass:/root/.pgpass"
  588. "${pgm_docker_run_opts[@]}"
  589. )
  590. cmd=(docker run --rm \
  591. "${final_pgm_docker_run_opts[@]}" \
  592. "${image_id}" pgm "$@"
  593. )
  594. echo "${cmd[@]}"
  595. "${cmd[@]}"
  596. }
  597. export -f compose:pgm
  598. postgres:dump() {
  599. local project_name="$1" src="$2" dst="$3"
  600. (
  601. settmpdir PGM_TMP_LOCATION
  602. pgm_docker_run_opts=('-v' "${PGM_TMP_LOCATION}:/tmp/dump")
  603. compose:pgm "$project_name" cp -f "$src" "/tmp/dump/dump.gz" &&
  604. mv "$PGM_TMP_LOCATION/dump.gz" "$dst"
  605. ) || return 1
  606. }
  607. export -f postgres:dump
  608. postgres:restore() {
  609. local project_name="$1" src="$2" dst="$3"
  610. full_src_path=$(readlink -e "$src") || exit 1
  611. (
  612. pgm_docker_run_opts=('-v' "${full_src_path}:/tmp/dump.gz")
  613. compose:pgm "$project_name" cp -f "/tmp/dump.gz" "$dst"
  614. ) || return 1
  615. }
  616. export -f postgres:restore
  617. cyclos:set_root_url() {
  618. local project_name="$1" dbname="$2" url="$3"
  619. echo "UPDATE configurations SET root_url = '$url';" |
  620. compose:psql "$project_name" "$dbname" || {
  621. err "Failed to set cyclos url value in '$dbname' database."
  622. return 1
  623. }
  624. }
  625. export -f cyclos:set_root_url
  626. cyclos:unlock() {
  627. local project_name="$1" dbname="$2"
  628. echo "delete from database_lock;" |
  629. compose:psql "${project_name}" "${dbname}"
  630. }
  631. export -f cyclos:unlock
  632. rocketchat:drop-indexes() {
  633. local project_name="$1" dbname="$2"
  634. echo "db.users.dropIndexes()" |
  635. compose:mongo "${project_name}" "${dbname}"
  636. }
  637. export -f rocketchat:drop-indexes
  638. compose:project_name() {
  639. if [ -z "$PROJECT_NAME" ]; then
  640. PROJECT_NAME=$(compose --get-project-name) || {
  641. err "Couldn't get project name."
  642. return 1
  643. }
  644. if [ -z "$PROJECT_NAME" -o "$PROJECT_NAME" == "orphan" ]; then
  645. err "Couldn't get project name, probably because 'compose.yml' wasn't found."
  646. echo " Please ensure to either configure a global 'compose.yml' or run this command" >&2
  647. echo " in a compose project (with 'compose.yml' on the top level directory)." >&2
  648. return 1
  649. fi
  650. export PROJECT_NAME
  651. fi
  652. echo "$PROJECT_NAME"
  653. }
  654. export -f compose:project_name
  655. compose:get_cron_docker_cmd() {
  656. local cron_line cmd_line docker_cmd
  657. project_name=$(compose:project_name) || return 1
  658. if ! cron_line=$(docker exec "${project_name}"_cron_1 cat /etc/cron.d/rsync-backup | grep "\* \* \*"); then
  659. err "Can't find cron_line in cron container."
  660. echo " Have you forgotten to run 'compose up' ?" >&2
  661. return 1
  662. fi
  663. cron_line=${cron_line%|*}
  664. cron_line=${cron_line%"2>&1"*}
  665. cmd_line="${cron_line#*root}"
  666. eval "args=($cmd_line)"
  667. ## should be last argument
  668. docker_cmd=$(echo ${args[@]: -1})
  669. if ! [[ "$docker_cmd" == "docker run --rm -e "* ]]; then
  670. echo "docker command found should start with 'docker run'." >&2
  671. echo "Here's command:" >&2
  672. echo " $docker_cmd" >&2
  673. return 1
  674. fi
  675. e "$docker_cmd"
  676. }
  677. compose:recover-target() {
  678. local backup_host="$1" ident="$2" src="$3" dst="$4" service_name="${5:-rsync-backup}" project_name
  679. project_name=$(compose:project_name) || return 1
  680. docker_image="${project_name}_${service_name}"
  681. if ! docker_has_image "$docker_image"; then
  682. compose build "${service_name}" || {
  683. err "Couldn't find nor build image for service '$service_name'."
  684. return 1
  685. }
  686. fi
  687. dst="${dst%/}" ## remove final slash
  688. ssh_options=(-o StrictHostKeyChecking=no)
  689. if [[ "$backup_host" == *":"* ]]; then
  690. port="${backup_host##*:}"
  691. backup_host="${backup_host%%:*}"
  692. ssh_options+=(-p "$port")
  693. else
  694. port=""
  695. backup_host="${backup_host%%:*}"
  696. fi
  697. rsync_opts=(
  698. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  699. -azvArH --delete --delete-excluded
  700. --partial --partial-dir .rsync-partial
  701. --numeric-ids
  702. )
  703. if [ "$DRY_RUN" ]; then
  704. rsync_opts+=("-n")
  705. fi
  706. cmd=(
  707. docker run --rm --entrypoint rsync \
  708. -v "/srv/datastore/config/${service_name}/var/lib/rsync":/var/lib/rsync \
  709. -v "${dst%/*}":/mnt/dest \
  710. "$docker_image" \
  711. "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "/mnt/dest/${dst##*/}"
  712. )
  713. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  714. "${cmd[@]}"
  715. }
  716. mailcow:recover-target() {
  717. local backup_host="$1" ident="$2" src="$3" dst="$4"
  718. dst="${dst%/}" ## remove final slash
  719. ssh_options=(-o StrictHostKeyChecking=no)
  720. if [[ "$backup_host" == *":"* ]]; then
  721. port="${backup_host##*:}"
  722. backup_host="${backup_host%%:*}"
  723. ssh_options+=(-p "$port")
  724. else
  725. port=""
  726. backup_host="${backup_host%%:*}"
  727. fi
  728. rsync_opts=(
  729. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  730. -azvArH --delete --delete-excluded
  731. --partial --partial-dir .rsync-partial
  732. --numeric-ids
  733. )
  734. if [ "$DRY_RUN" ]; then
  735. rsync_opts+=("-n")
  736. fi
  737. cmd=(
  738. rsync "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "${dst}"
  739. )
  740. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  741. "${cmd[@]}"
  742. }
  743. nextcloud:src:version() {
  744. local version
  745. if ! version=$(cat "/srv/datastore/data/${nextcloud_service}/var/www/html/version.php" 2>/dev/null); then
  746. err "Can't find version.php file to get last version installed."
  747. exit 1
  748. fi
  749. version=$(e "$version" | grep 'VersionString =' | cut -f 3 -d ' ' | cut -f 2 -d "'")
  750. if [ -z "$version" ]; then
  751. err "Can't figure out version from version.php content."
  752. exit 1
  753. fi
  754. echo "$version"
  755. }
  756. [ "$SOURCED" ] && return 0
  757. ##
  758. ## Command line processing
  759. ##
  760. cmdline.spec.gnu
  761. cmdline.spec.reporting
  762. cmdline.spec.gnu install
  763. cmdline.spec::cmd:install:run() {
  764. :
  765. }
  766. cmdline.spec.gnu get-type
  767. cmdline.spec::cmd:get-type:run() {
  768. vps:get-type
  769. }
  770. cmdline.spec:install:cmd:backup:run() {
  771. : :posarg: BACKUP_SERVER 'Target backup server'
  772. : :optfla: --ignore-domain-check \
  773. "Allow to bypass the domain check in
  774. compose file (only used in compose
  775. installation)."
  776. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  777. local vps_type
  778. vps_type=$(vps:get-type) || {
  779. err "Failed to get type of installation."
  780. return 1
  781. }
  782. if ! fn.exists "${vps_type}:install-backup"; then
  783. err "type '${vps_type}' has no backup installation implemented yet."
  784. return 1
  785. fi
  786. opts=()
  787. [ "$opt_ignore_ping_check" ] &&
  788. opts+=("--ignore-ping-check")
  789. if [ "$vps_type" == "compose" ]; then
  790. [ "$opt_ignore_domain_check" ] &&
  791. opts+=("--ignore-domain-check")
  792. fi
  793. "cmdline.spec:install:cmd:${vps_type}-backup:run" "${opts[@]}" "$BACKUP_SERVER"
  794. }
  795. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  796. cmdline.spec.gnu compose-backup
  797. cmdline.spec:install:cmd:compose-backup:run() {
  798. : :posarg: BACKUP_SERVER 'Target backup server'
  799. : :optval: --service-name,-s "YAML service name in compose
  800. file to check for existence of key.
  801. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  802. : :optval: --compose-file,-f "Compose file location. Defaults to
  803. the value of '\$DEFAULT_COMPOSE_FILE'"
  804. : :optfla: --ignore-domain-check \
  805. "Allow to bypass the domain check in
  806. compose file."
  807. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  808. local service_name compose_file
  809. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  810. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  811. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  812. if ! [ -e "$compose_file" ]; then
  813. err "Compose file not found in '$compose_file'."
  814. return 1
  815. fi
  816. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" \
  817. "$opt_ignore_ping_check" "$opt_ignore_domain_check"
  818. }
  819. cmdline.spec:install:cmd:mailcow-backup:run() {
  820. : :posarg: BACKUP_SERVER 'Target backup server'
  821. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  822. "mailcow:install-backup" "$BACKUP_SERVER" "$opt_ignore_ping_check"
  823. }
  824. cmdline.spec.gnu backup
  825. cmdline.spec::cmd:backup:run() {
  826. local vps_type
  827. vps_type=$(vps:get-type) || {
  828. err "Failed to get type of installation."
  829. return 1
  830. }
  831. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  832. err "type '${vps_type}' has no backup process implemented yet."
  833. return 1
  834. fi
  835. "cmdline.spec:backup:cmd:${vps_type}:run"
  836. }
  837. cmdline.spec:backup:cmd:mailcow:run() {
  838. local cmd_line cron_line cmd
  839. for f in mysql-backup mirror-dir; do
  840. [ -e "/etc/cron.d/$f" ] || {
  841. err "Can't find '/etc/cron.d/$f'."
  842. echo " Have you forgotten to run 'vps install backup BACKUP_HOST' ?" >&2
  843. return 1
  844. }
  845. if ! cron_line=$(cat "/etc/cron.d/$f" |
  846. grep -v "^#" | grep "\* \* \*"); then
  847. err "Can't find cron_line in '/etc/cron.d/$f'." \
  848. "Have you modified it ?"
  849. return 1
  850. fi
  851. cron_line=${cron_line%|*}
  852. cmd_line=(${cron_line#*root})
  853. if [ "$f" == "mirror-dir" ]; then
  854. cmd=()
  855. for arg in "${cmd_line[@]}"; do
  856. [ "$arg" != "-q" ] && cmd+=("$arg")
  857. done
  858. else
  859. cmd=("${cmd_line[@]}")
  860. fi
  861. code="${cmd[*]}"
  862. echo "${WHITE}Launching:${NORMAL} ${code}"
  863. {
  864. {
  865. (
  866. ## Some commands are using colors that are already
  867. ## set by this current program and will trickle
  868. ## down unwantedly
  869. ansi_color no
  870. eval "${code}"
  871. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  872. set_errlvl "${PIPESTATUS[0]}"
  873. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  874. set_errlvl "${PIPESTATUS[0]}"
  875. } 3>&1 1>&2 2>&3
  876. if [ "$?" != "0" ]; then
  877. err "Failed."
  878. return 1
  879. fi
  880. done
  881. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  882. }
  883. set_errlvl() { return "${1:-1}"; }
  884. cmdline.spec:backup:cmd:compose:run() {
  885. local cron_line args
  886. project_name=$(compose:project_name) || return 1
  887. docker_cmd=$(compose:get_cron_docker_cmd) || return 1
  888. echo "${WHITE}Launching:${NORMAL} docker exec -i "${project_name}_cron_1" $docker_cmd"
  889. {
  890. {
  891. eval "docker exec -i \"${project_name}_cron_1\" $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  892. set_errlvl "${PIPESTATUS[0]}"
  893. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  894. set_errlvl "${PIPESTATUS[0]}"
  895. } 3>&1 1>&2 2>&3
  896. if [ "$?" != "0" ]; then
  897. err "Failed."
  898. return 1
  899. fi
  900. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  901. }
  902. cmdline.spec.gnu recover-target
  903. cmdline.spec::cmd:recover-target:run() {
  904. : :posarg: BACKUP_DIR 'Source directory on backup side'
  905. : :posarg: HOST_DIR 'Target directory on host side'
  906. : :optval: --backup-host,-B "The backup host"
  907. : :optfla: --dry-run,-n "Don't do anything, instead tell what it
  908. would do."
  909. ## if no backup host take the one by default
  910. backup_host="$opt_backup_host"
  911. if [ -z "$backup_host" ]; then
  912. backup_host_ident=$(backup-action get_default_backup_host_ident) || return 1
  913. read -r backup_host ident <<<"$backup_host_ident"
  914. fi
  915. if [[ "$BACKUP_DIR" == /* ]]; then
  916. err "BACKUP_DIR must be a relative path from the root of your backup."
  917. return 1
  918. fi
  919. REAL_HOST_DIR=$(realpath "$HOST_DIR") || {
  920. err "Can't find HOST_DIR '$HOST_DIR'."
  921. return 1
  922. }
  923. export DRY_RUN="${opt_dry_run}"
  924. backup-action recover-target "$backup_host" "$ident" "$BACKUP_DIR" "$REAL_HOST_DIR"
  925. }
  926. cmdline.spec.gnu odoo
  927. cmdline.spec::cmd:odoo:run() {
  928. :
  929. }
  930. cmdline.spec.gnu restart
  931. cmdline.spec:odoo:cmd:restart:run() {
  932. : :optval: --service,-s "The service (defaults to 'odoo')"
  933. local out odoo_service
  934. odoo_service="${opt_service:-odoo}"
  935. project_name=$(compose:project_name) || return 1
  936. if ! out=$(docker restart "${project_name}_${odoo_service}_1" 2>&1); then
  937. if [[ "$out" == *"no matching entries in passwd file" ]]; then
  938. warn "Catched docker bug. Restarting once more."
  939. if ! out=$(docker restart "${project_name}_${odoo_service}_1"); then
  940. err "Can't restart container ${project_name}_${odoo_service}_1 (restarted twice)."
  941. echo " output:" >&2
  942. echo "$out" | prefix " ${GRAY}|${NORMAL} " >&2
  943. exit 1
  944. fi
  945. else
  946. err "Couldn't restart container ${project_name}_${odoo_service}_1 (and no restart bug detected)."
  947. exit 1
  948. fi
  949. fi
  950. info "Container ${project_name}_${odoo_service}_1 was ${DARKGREEN}successfully${NORMAL} restarted."
  951. }
  952. cmdline.spec.gnu restore
  953. cmdline.spec:odoo:cmd:restore:run() {
  954. : :posarg: ZIP_DUMP_LOCATION 'Source odoo dump file to restore
  955. (can be a local file or an url)'
  956. : :optval: --service,-s "The service (defaults to 'odoo')"
  957. : :optval: --database,-d 'Target database (default if not specified)'
  958. local out
  959. odoo_service="${opt_service:-odoo}"
  960. if [[ "$ZIP_DUMP_LOCATION" == "http://"* ]] ||
  961. [[ "$ZIP_DUMP_LOCATION" == "https://"* ]]; then
  962. settmpdir ZIP_TMP_LOCATION
  963. tmp_location="$ZIP_TMP_LOCATION/dump.zip"
  964. curl -k -s -L "$ZIP_DUMP_LOCATION" > "$tmp_location" || {
  965. err "Couldn't get '$ZIP_DUMP_LOCATION'."
  966. exit 1
  967. }
  968. if [[ "$(dd if="$tmp_location" count=2 bs=1 2>/dev/null)" != "PK" ]]; then
  969. err "Download doesn't seem to be a zip file."
  970. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  971. exit 1
  972. fi
  973. info "Successfully downloaded '$ZIP_DUMP_LOCATION'"
  974. echo " in '$tmp_location'." >&2
  975. ZIP_DUMP_LOCATION="$tmp_location"
  976. fi
  977. [ -e "$ZIP_DUMP_LOCATION" ] || {
  978. err "No file '$ZIP_DUMP_LOCATION' found." >&2
  979. exit 1
  980. }
  981. #cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  982. msg_dbname=default
  983. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  984. compose --no-hooks drop "$odoo_service" $opt_database || {
  985. err "Error dropping $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}."
  986. exit 1
  987. }
  988. compose --no-hooks load "$odoo_service" $opt_database < "$ZIP_DUMP_LOCATION" || {
  989. err "Error restoring service ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  990. exit 1
  991. }
  992. info "Successfully restored ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  993. ## Restart odoo, ensure there is no bugs lingering on it.
  994. cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  995. }
  996. cmdline.spec.gnu dump
  997. cmdline.spec:odoo:cmd:dump:run() {
  998. : :posarg: DUMP_ZIPFILE 'Target path to store odoo dump zip file.'
  999. : :optval: --database,-d 'Target database (default if not specified)'
  1000. : :optval: --service,-s "The service (defaults to 'odoo')"
  1001. odoo_service="${opt_service:-odoo}"
  1002. msg_dbname=default
  1003. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1004. compose --no-hooks save "$odoo_service" $opt_database > "$DUMP_ZIPFILE" || {
  1005. err "Error dumping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1006. exit 1
  1007. }
  1008. info "Successfully dumped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1009. }
  1010. cmdline.spec.gnu drop
  1011. cmdline.spec:odoo:cmd:drop:run() {
  1012. : :optval: --database,-d 'Target database (default if not specified)'
  1013. : :optval: --service,-s "The service (defaults to 'odoo')"
  1014. odoo_service="${opt_service:-odoo}"
  1015. msg_dbname=default
  1016. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1017. compose --no-hooks drop "$odoo_service" $opt_database || {
  1018. err "Error dropping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1019. exit 1
  1020. }
  1021. info "Successfully dropped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1022. }
  1023. cmdline.spec.gnu set-cyclos-url
  1024. cmdline.spec:odoo:cmd:set-cyclos-url:run() {
  1025. : :optval: --database,-d "Target database ('odoo' if not specified)"
  1026. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1027. local URL
  1028. dbname=${opt_database:-odoo}
  1029. cyclos_service="${opt_service:-cyclos}"
  1030. project_name=$(compose:project_name) || exit 1
  1031. URL=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1032. Wrap -d "set cyclos url to '$URL'" <<EOF || exit 1
  1033. echo "UPDATE res_company SET cyclos_server_url = '$URL/api' WHERE id=1;" |
  1034. compose:psql "$project_name" "$dbname" || {
  1035. err "Failed to set cyclos url value in '$dbname' database."
  1036. exit 1
  1037. }
  1038. EOF
  1039. }
  1040. cmdline.spec.gnu cyclos
  1041. cmdline.spec::cmd:cyclos:run() {
  1042. :
  1043. }
  1044. cmdline.spec:cyclos:cmd:dump:run() {
  1045. : :posarg: DUMP_GZFILE 'Target path to store odoo dump gz file.'
  1046. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1047. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1048. cyclos_service="${opt_service:-cyclos}"
  1049. cyclos_database="${opt_database:-cyclos}"
  1050. project_name=$(compose:project_name) || exit 1
  1051. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1052. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1053. docker stop "$container_id" || exit 1
  1054. Wrap -d "Dump postgres database '${cyclos_database}'." -- \
  1055. postgres:dump "${project_name}" "$cyclos_database" "$DUMP_GZFILE" || exit 1
  1056. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1057. docker start "${container_id}" || exit 1
  1058. }
  1059. cmdline.spec.gnu restore
  1060. cmdline.spec:cyclos:cmd:restore:run() {
  1061. : :posarg: GZ_DUMP_LOCATION 'Source cyclos dump file to restore
  1062. (can be a local file or an url)'
  1063. : :optval: --service,-s "The service (defaults to 'cyclos')"
  1064. : :optval: --database,-d 'Target database (default if not specified)'
  1065. local out
  1066. cyclos_service="${opt_service:-cyclos}"
  1067. cyclos_database="${opt_database:-cyclos}"
  1068. project_name=$(compose:project_name) || exit 1
  1069. url=$(compose:get_url "${project_name}" "${cyclos_service}") || return 1
  1070. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1071. if [[ "$GZ_DUMP_LOCATION" == "http://"* ]] ||
  1072. [[ "$GZ_DUMP_LOCATION" == "https://"* ]]; then
  1073. settmpdir GZ_TMP_LOCATION
  1074. tmp_location="$GZ_TMP_LOCATION/dump.gz"
  1075. Wrap -d "get '$GZ_DUMP_LOCATION'" <<EOF || exit 1
  1076. ## Note that curll version before 7.76.0 do not have
  1077. curl -k -s -L "$GZ_DUMP_LOCATION" --fail \\
  1078. > "$tmp_location" || {
  1079. echo "Error fetching ressource. Is url correct ?" >&2
  1080. exit 1
  1081. }
  1082. if [[ "\$(dd if="$tmp_location" count=2 bs=1 2>/dev/null |
  1083. hexdump -v -e "/1 \"%02x\"")" != "1f8b" ]]; then
  1084. err "Download doesn't seem to be a gzip file."
  1085. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1086. exit 1
  1087. fi
  1088. EOF
  1089. GZ_DUMP_LOCATION="$tmp_location"
  1090. fi
  1091. [ -e "$GZ_DUMP_LOCATION" ] || {
  1092. err "No file '$GZ_DUMP_LOCATION' found." >&2
  1093. exit 1
  1094. }
  1095. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1096. docker stop "$container_id" || exit 1
  1097. ## XXXvlab: making the assumption that the postgres username should
  1098. ## be the same as the cyclos service selected (which is the default,
  1099. ## but not always the case).
  1100. Wrap -d "restore postgres database '${cyclos_database}'." -- \
  1101. postgres:restore "$project_name" "$GZ_DUMP_LOCATION" "${cyclos_service}@${cyclos_database}" || exit 1
  1102. ## ensure that the database is not locked
  1103. Wrap -d "check and remove database lock if any" -- \
  1104. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1105. Wrap -d "set root url to '$url'" -- \
  1106. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1107. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1108. docker start "${container_id}" || exit 1
  1109. }
  1110. cmdline.spec.gnu set-root-url
  1111. cmdline.spec:cyclos:cmd:set-root-url:run() {
  1112. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1113. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1114. local URL
  1115. cyclos_database=${opt_database:-cyclos}
  1116. cyclos_service="${opt_service:-cyclos}"
  1117. project_name=$(compose:project_name) || exit 1
  1118. url=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1119. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1120. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1121. docker stop "$container_id" || exit 1
  1122. Wrap -d "set root url to '$url'" -- \
  1123. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1124. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1125. docker start "${container_id}" || exit 1
  1126. }
  1127. cmdline.spec.gnu unlock
  1128. cmdline.spec:cyclos:cmd:unlock:run() {
  1129. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1130. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1131. local URL
  1132. cyclos_database=${opt_database:-cyclos}
  1133. cyclos_service="${opt_service:-cyclos}"
  1134. project_name=$(compose:project_name) || exit 1
  1135. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1136. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1137. docker stop "$container_id" || exit 1
  1138. Wrap -d "check and remove database lock if any" -- \
  1139. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1140. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1141. docker start "${container_id}" || exit 1
  1142. }
  1143. cmdline.spec.gnu rocketchat
  1144. cmdline.spec::cmd:rocketchat:run() {
  1145. :
  1146. }
  1147. cmdline.spec.gnu drop-indexes
  1148. cmdline.spec:rocketchat:cmd:drop-indexes:run() {
  1149. : :optval: --database,-d "Target database ('rocketchat' if not specified)"
  1150. : :optval: --service,-s "The rocketchat service name (defaults to 'rocketchat')"
  1151. local URL
  1152. rocketchat_database=${opt_database:-rocketchat}
  1153. rocketchat_service="${opt_service:-rocketchat}"
  1154. project_name=$(compose:project_name) || exit 1
  1155. container_id=$(compose:service:container_one "${project_name}" "${rocketchat_service}") || exit 1
  1156. Wrap -d "stop ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1157. docker stop "$container_id" || exit 1
  1158. errlvl=0
  1159. Wrap -d "drop indexes" -- \
  1160. rocketchat:drop-indexes "${project_name}" "${rocketchat_database}" || {
  1161. errlvl=1
  1162. errmsg="Failed to drop indexes"
  1163. }
  1164. Wrap -d "start ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1165. docker start "${container_id}" || exit 1
  1166. if [ "$errlvl" != 0 ]; then
  1167. err "$errmsg"
  1168. fi
  1169. exit "$errlvl"
  1170. }
  1171. cmdline.spec.gnu nextcloud
  1172. cmdline.spec::cmd:nextcloud:run() {
  1173. :
  1174. }
  1175. cmdline.spec.gnu upgrade
  1176. cmdline.spec:nextcloud:cmd:upgrade:run() {
  1177. : :posarg: [TARGET_VERSION] "Source cyclos dump file to restore"
  1178. : :optval: --service,-s "The nexcloud service name (defaults to 'nextcloud')"
  1179. local URL
  1180. nextcloud_service="${opt_service:-nextcloud}"
  1181. project_name=$(compose:project_name) || exit 1
  1182. containers=$(compose:service:containers "${project_name}" "${nextcloud_service}") || exit 1
  1183. container_stopped=()
  1184. if [ -n "$containers" ]; then
  1185. for container in $containers; do
  1186. Wrap -d "stop ${DARKYELLOW}${nextcloud_service}${NORMAL}'s container" -- \
  1187. docker stop "$container" || {
  1188. err "Failed to stop container '$container'."
  1189. exit 1
  1190. }
  1191. container_stopped+=("$container")
  1192. done
  1193. fi
  1194. before_version=$(nextcloud:src:version) || exit 1
  1195. ## -q to remove the display of ``compose`` related information
  1196. ## like relation resolution.
  1197. ## --no-hint to remove the final hint about modifying your
  1198. ## ``compose.yml``.
  1199. compose -q upgrade "$nextcloud_service" --no-hint
  1200. errlvl="$?"
  1201. after_version=$(nextcloud:src:version)
  1202. if [ "$after_version" != "$before_version" ]; then
  1203. desc="update \`compose.yml\` to set ${DARKYELLOW}$nextcloud_service${NORMAL}'s "
  1204. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  1205. Wrap -d "$desc" -- \
  1206. compose:file:value-change \
  1207. "${nextcloud_service}.docker-compose.image" \
  1208. "docker.0k.io/nextcloud:${after_version}-myc" || exit 1
  1209. fi
  1210. if [ "$errlvl" == 0 ]; then
  1211. echo "${WHITE}Launching final compose${NORMAL}"
  1212. compose up || exit 1
  1213. fi
  1214. exit "$errlvl"
  1215. }
  1216. cmdline::parse "$@"