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.

1696 lines
50 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:container-aliveness() {
  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 |
  771. grep "/$container_id" |
  772. sed -r 's/^ *//g' |
  773. cut -f 1 -d " ")
  774. if [ -z "$pid" ]; then
  775. err "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} doesn't answer to 'inspect' command. Can't find its PID neither."
  776. return 1
  777. fi
  778. echo "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} doesn't answer to 'inspect' command (pid: $pid)."
  779. Wrap -d "kill pid $pid and restart" <<EOF
  780. kill "$pid"
  781. sleep 2
  782. docker restart "$container_id"
  783. EOF
  784. fi
  785. return $errlvl
  786. }
  787. container:health:check-fix:no-matching-entries() {
  788. local container_id="$1"
  789. out=$(docker exec "$container_id" echo 2>&1)
  790. errlvl=$?
  791. [ "$errlvl" == 0 ] && return 0
  792. service_name=$(docker ps --filter id="$container_id" --format '{{.Label "com.docker.compose.service"}}')
  793. container_name=($(docker ps --filter id="$container_id" --format '{{.Names}}'))
  794. if [ "$errlvl" == 126 ] && [[ "$out" == *"no matching entries in passwd file"* ]]; then
  795. echo "container ${DARKCYAN}${container_name[0]}${NORMAL} for ${DARKYELLOW}$service_name${NORMAL} has ${DARKRED}no-matching-entries${NORMAL} bug." >&2
  796. Wrap -d "restarting container of ${DARKYELLOW}$service_name${NORMAL} twice" <<EOF
  797. docker restart "$container_id"
  798. sleep 2
  799. docker restart "$container_id"
  800. EOF
  801. return $errlvl
  802. fi
  803. warn "Unknown issue with ${DARKYELLOW}$service_name${NORMAL}'s container:"
  804. echo " ${WHITE}cmd:${NORMAL} docker exec -ti $container_id echo" >&2
  805. echo "$out" | prefix " ${DARKGRAY}|${NORMAL} " >&2
  806. echo " ${DARKGRAY}..${NORMAL} leaving this as-is."
  807. return $errlvl
  808. }
  809. [ "$SOURCED" ] && return 0
  810. ##
  811. ## Command line processing
  812. ##
  813. cmdline.spec.gnu
  814. cmdline.spec.reporting
  815. cmdline.spec.gnu install
  816. cmdline.spec::cmd:install:run() {
  817. :
  818. }
  819. cmdline.spec.gnu get-type
  820. cmdline.spec::cmd:get-type:run() {
  821. vps:get-type
  822. }
  823. cmdline.spec:install:cmd:backup:run() {
  824. : :posarg: BACKUP_SERVER 'Target backup server'
  825. : :optfla: --ignore-domain-check \
  826. "Allow to bypass the domain check in
  827. compose file (only used in compose
  828. installation)."
  829. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  830. local vps_type
  831. vps_type=$(vps:get-type) || {
  832. err "Failed to get type of installation."
  833. return 1
  834. }
  835. if ! fn.exists "${vps_type}:install-backup"; then
  836. err "type '${vps_type}' has no backup installation implemented yet."
  837. return 1
  838. fi
  839. opts=()
  840. [ "$opt_ignore_ping_check" ] &&
  841. opts+=("--ignore-ping-check")
  842. if [ "$vps_type" == "compose" ]; then
  843. [ "$opt_ignore_domain_check" ] &&
  844. opts+=("--ignore-domain-check")
  845. fi
  846. "cmdline.spec:install:cmd:${vps_type}-backup:run" "${opts[@]}" "$BACKUP_SERVER"
  847. }
  848. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  849. cmdline.spec.gnu compose-backup
  850. cmdline.spec:install:cmd:compose-backup:run() {
  851. : :posarg: BACKUP_SERVER 'Target backup server'
  852. : :optval: --service-name,-s "YAML service name in compose
  853. file to check for existence of key.
  854. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  855. : :optval: --compose-file,-f "Compose file location. Defaults to
  856. the value of '\$DEFAULT_COMPOSE_FILE'"
  857. : :optfla: --ignore-domain-check \
  858. "Allow to bypass the domain check in
  859. compose file."
  860. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  861. local service_name compose_file
  862. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  863. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  864. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  865. if ! [ -e "$compose_file" ]; then
  866. err "Compose file not found in '$compose_file'."
  867. return 1
  868. fi
  869. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" \
  870. "$opt_ignore_ping_check" "$opt_ignore_domain_check"
  871. }
  872. cmdline.spec:install:cmd:mailcow-backup:run() {
  873. : :posarg: BACKUP_SERVER 'Target backup server'
  874. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  875. "mailcow:install-backup" "$BACKUP_SERVER" "$opt_ignore_ping_check"
  876. }
  877. cmdline.spec.gnu backup
  878. cmdline.spec::cmd:backup:run() {
  879. local vps_type
  880. vps_type=$(vps:get-type) || {
  881. err "Failed to get type of installation."
  882. return 1
  883. }
  884. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  885. err "type '${vps_type}' has no backup process implemented yet."
  886. return 1
  887. fi
  888. "cmdline.spec:backup:cmd:${vps_type}:run"
  889. }
  890. cmdline.spec:backup:cmd:mailcow:run() {
  891. local cmd_line cron_line cmd
  892. for f in mysql-backup mirror-dir; do
  893. [ -e "/etc/cron.d/$f" ] || {
  894. err "Can't find '/etc/cron.d/$f'."
  895. echo " Have you forgotten to run 'vps install backup BACKUP_HOST' ?" >&2
  896. return 1
  897. }
  898. if ! cron_line=$(cat "/etc/cron.d/$f" |
  899. grep -v "^#" | grep "\* \* \*"); then
  900. err "Can't find cron_line in '/etc/cron.d/$f'." \
  901. "Have you modified it ?"
  902. return 1
  903. fi
  904. cron_line=${cron_line%|*}
  905. cmd_line=(${cron_line#*root})
  906. if [ "$f" == "mirror-dir" ]; then
  907. cmd=()
  908. for arg in "${cmd_line[@]}"; do
  909. [ "$arg" != "-q" ] && cmd+=("$arg")
  910. done
  911. else
  912. cmd=("${cmd_line[@]}")
  913. fi
  914. code="${cmd[*]}"
  915. echo "${WHITE}Launching:${NORMAL} ${code}"
  916. {
  917. {
  918. (
  919. ## Some commands are using colors that are already
  920. ## set by this current program and will trickle
  921. ## down unwantedly
  922. ansi_color no
  923. eval "${code}"
  924. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  925. set_errlvl "${PIPESTATUS[0]}"
  926. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  927. set_errlvl "${PIPESTATUS[0]}"
  928. } 3>&1 1>&2 2>&3
  929. if [ "$?" != "0" ]; then
  930. err "Failed."
  931. return 1
  932. fi
  933. done
  934. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  935. }
  936. set_errlvl() { return "${1:-1}"; }
  937. cmdline.spec:backup:cmd:compose:run() {
  938. local cron_line args
  939. project_name=$(compose:project_name) || return 1
  940. docker_cmd=$(compose:get_cron_docker_cmd) || return 1
  941. echo "${WHITE}Launching:${NORMAL} docker exec -i "${project_name}_cron_1" $docker_cmd"
  942. {
  943. {
  944. eval "docker exec -i \"${project_name}_cron_1\" $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  945. set_errlvl "${PIPESTATUS[0]}"
  946. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  947. set_errlvl "${PIPESTATUS[0]}"
  948. } 3>&1 1>&2 2>&3
  949. if [ "$?" != "0" ]; then
  950. err "Failed."
  951. return 1
  952. fi
  953. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  954. }
  955. cmdline.spec.gnu recover-target
  956. cmdline.spec::cmd:recover-target:run() {
  957. : :posarg: BACKUP_DIR 'Source directory on backup side'
  958. : :posarg: HOST_DIR 'Target directory on host side'
  959. : :optval: --backup-host,-B "The backup host"
  960. : :optfla: --dry-run,-n "Don't do anything, instead tell what it
  961. would do."
  962. ## if no backup host take the one by default
  963. backup_host="$opt_backup_host"
  964. if [ -z "$backup_host" ]; then
  965. backup_host_ident=$(backup-action get_default_backup_host_ident) || return 1
  966. read -r backup_host ident <<<"$backup_host_ident"
  967. fi
  968. if [[ "$BACKUP_DIR" == /* ]]; then
  969. err "BACKUP_DIR must be a relative path from the root of your backup."
  970. return 1
  971. fi
  972. REAL_HOST_DIR=$(realpath "$HOST_DIR") || {
  973. err "Can't find HOST_DIR '$HOST_DIR'."
  974. return 1
  975. }
  976. export DRY_RUN="${opt_dry_run}"
  977. backup-action recover-target "$backup_host" "$ident" "$BACKUP_DIR" "$REAL_HOST_DIR"
  978. }
  979. cmdline.spec.gnu odoo
  980. cmdline.spec::cmd:odoo:run() {
  981. :
  982. }
  983. cmdline.spec.gnu restart
  984. cmdline.spec:odoo:cmd:restart:run() {
  985. : :optval: --service,-s "The service (defaults to 'odoo')"
  986. local out odoo_service
  987. odoo_service="${opt_service:-odoo}"
  988. project_name=$(compose:project_name) || return 1
  989. if ! out=$(docker restart "${project_name}_${odoo_service}_1" 2>&1); then
  990. if [[ "$out" == *"no matching entries in passwd file" ]]; then
  991. warn "Catched docker bug. Restarting once more."
  992. if ! out=$(docker restart "${project_name}_${odoo_service}_1"); then
  993. err "Can't restart container ${project_name}_${odoo_service}_1 (restarted twice)."
  994. echo " output:" >&2
  995. echo "$out" | prefix " ${GRAY}|${NORMAL} " >&2
  996. exit 1
  997. fi
  998. else
  999. err "Couldn't restart container ${project_name}_${odoo_service}_1 (and no restart bug detected)."
  1000. exit 1
  1001. fi
  1002. fi
  1003. info "Container ${project_name}_${odoo_service}_1 was ${DARKGREEN}successfully${NORMAL} restarted."
  1004. }
  1005. cmdline.spec.gnu restore
  1006. cmdline.spec:odoo:cmd:restore:run() {
  1007. : :posarg: ZIP_DUMP_LOCATION 'Source odoo dump file to restore
  1008. (can be a local file or an url)'
  1009. : :optval: --service,-s "The service (defaults to 'odoo')"
  1010. : :optval: --database,-d 'Target database (default if not specified)'
  1011. local out
  1012. odoo_service="${opt_service:-odoo}"
  1013. if [[ "$ZIP_DUMP_LOCATION" == "http://"* ]] ||
  1014. [[ "$ZIP_DUMP_LOCATION" == "https://"* ]]; then
  1015. settmpdir ZIP_TMP_LOCATION
  1016. tmp_location="$ZIP_TMP_LOCATION/dump.zip"
  1017. curl -k -s -L "$ZIP_DUMP_LOCATION" > "$tmp_location" || {
  1018. err "Couldn't get '$ZIP_DUMP_LOCATION'."
  1019. exit 1
  1020. }
  1021. if [[ "$(dd if="$tmp_location" count=2 bs=1 2>/dev/null)" != "PK" ]]; then
  1022. err "Download doesn't seem to be a zip file."
  1023. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1024. exit 1
  1025. fi
  1026. info "Successfully downloaded '$ZIP_DUMP_LOCATION'"
  1027. echo " in '$tmp_location'." >&2
  1028. ZIP_DUMP_LOCATION="$tmp_location"
  1029. fi
  1030. [ -e "$ZIP_DUMP_LOCATION" ] || {
  1031. err "No file '$ZIP_DUMP_LOCATION' found." >&2
  1032. exit 1
  1033. }
  1034. #cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  1035. msg_dbname=default
  1036. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1037. compose --no-hooks drop "$odoo_service" $opt_database || {
  1038. err "Error dropping $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}."
  1039. exit 1
  1040. }
  1041. compose --no-hooks load "$odoo_service" $opt_database < "$ZIP_DUMP_LOCATION" || {
  1042. err "Error restoring service ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  1043. exit 1
  1044. }
  1045. info "Successfully restored ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  1046. ## Restart odoo, ensure there is no bugs lingering on it.
  1047. cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  1048. }
  1049. cmdline.spec.gnu dump
  1050. cmdline.spec:odoo:cmd:dump:run() {
  1051. : :posarg: DUMP_ZIPFILE 'Target path to store odoo dump zip file.'
  1052. : :optval: --database,-d 'Target database (default if not specified)'
  1053. : :optval: --service,-s "The service (defaults to 'odoo')"
  1054. odoo_service="${opt_service:-odoo}"
  1055. msg_dbname=default
  1056. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1057. compose --no-hooks save "$odoo_service" $opt_database > "$DUMP_ZIPFILE" || {
  1058. err "Error dumping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1059. exit 1
  1060. }
  1061. info "Successfully dumped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  1062. }
  1063. cmdline.spec.gnu drop
  1064. cmdline.spec:odoo:cmd:drop:run() {
  1065. : :optval: --database,-d 'Target database (default if not specified)'
  1066. : :optval: --service,-s "The service (defaults to 'odoo')"
  1067. odoo_service="${opt_service:-odoo}"
  1068. msg_dbname=default
  1069. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  1070. compose --no-hooks drop "$odoo_service" $opt_database || {
  1071. err "Error dropping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1072. exit 1
  1073. }
  1074. info "Successfully dropped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  1075. }
  1076. cmdline.spec.gnu set-cyclos-url
  1077. cmdline.spec:odoo:cmd:set-cyclos-url:run() {
  1078. : :optval: --database,-d "Target database ('odoo' if not specified)"
  1079. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1080. local URL
  1081. dbname=${opt_database:-odoo}
  1082. cyclos_service="${opt_service:-cyclos}"
  1083. project_name=$(compose:project_name) || exit 1
  1084. URL=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1085. Wrap -d "set cyclos url to '$URL'" <<EOF || exit 1
  1086. echo "UPDATE res_company SET cyclos_server_url = '$URL/api' WHERE id=1;" |
  1087. compose:psql "$project_name" "$dbname" || {
  1088. err "Failed to set cyclos url value in '$dbname' database."
  1089. exit 1
  1090. }
  1091. EOF
  1092. }
  1093. cmdline.spec.gnu cyclos
  1094. cmdline.spec::cmd:cyclos:run() {
  1095. :
  1096. }
  1097. cmdline.spec:cyclos:cmd:dump:run() {
  1098. : :posarg: DUMP_GZFILE 'Target path to store odoo dump gz file.'
  1099. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1100. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1101. cyclos_service="${opt_service:-cyclos}"
  1102. cyclos_database="${opt_database:-cyclos}"
  1103. project_name=$(compose:project_name) || exit 1
  1104. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1105. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1106. docker stop "$container_id" || exit 1
  1107. Wrap -d "Dump postgres database '${cyclos_database}'." -- \
  1108. postgres:dump "${project_name}" "$cyclos_database" "$DUMP_GZFILE" || exit 1
  1109. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1110. docker start "${container_id}" || exit 1
  1111. }
  1112. cmdline.spec.gnu restore
  1113. cmdline.spec:cyclos:cmd:restore:run() {
  1114. : :posarg: GZ_DUMP_LOCATION 'Source cyclos dump file to restore
  1115. (can be a local file or an url)'
  1116. : :optval: --service,-s "The service (defaults to 'cyclos')"
  1117. : :optval: --database,-d 'Target database (default if not specified)'
  1118. local out
  1119. cyclos_service="${opt_service:-cyclos}"
  1120. cyclos_database="${opt_database:-cyclos}"
  1121. project_name=$(compose:project_name) || exit 1
  1122. url=$(compose:get_url "${project_name}" "${cyclos_service}") || return 1
  1123. container_id=$(compose:service:container_one "$project_name" "${cyclos_service}") || exit 1
  1124. if [[ "$GZ_DUMP_LOCATION" == "http://"* ]] ||
  1125. [[ "$GZ_DUMP_LOCATION" == "https://"* ]]; then
  1126. settmpdir GZ_TMP_LOCATION
  1127. tmp_location="$GZ_TMP_LOCATION/dump.gz"
  1128. Wrap -d "get '$GZ_DUMP_LOCATION'" <<EOF || exit 1
  1129. ## Note that curll version before 7.76.0 do not have
  1130. curl -k -s -L "$GZ_DUMP_LOCATION" --fail \\
  1131. > "$tmp_location" || {
  1132. echo "Error fetching ressource. Is url correct ?" >&2
  1133. exit 1
  1134. }
  1135. if [[ "\$(dd if="$tmp_location" count=2 bs=1 2>/dev/null |
  1136. hexdump -v -e "/1 \"%02x\"")" != "1f8b" ]]; then
  1137. err "Download doesn't seem to be a gzip file."
  1138. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  1139. exit 1
  1140. fi
  1141. EOF
  1142. GZ_DUMP_LOCATION="$tmp_location"
  1143. fi
  1144. [ -e "$GZ_DUMP_LOCATION" ] || {
  1145. err "No file '$GZ_DUMP_LOCATION' found." >&2
  1146. exit 1
  1147. }
  1148. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1149. docker stop "$container_id" || exit 1
  1150. ## XXXvlab: making the assumption that the postgres username should
  1151. ## be the same as the cyclos service selected (which is the default,
  1152. ## but not always the case).
  1153. Wrap -d "restore postgres database '${cyclos_database}'." -- \
  1154. postgres:restore "$project_name" "$GZ_DUMP_LOCATION" "${cyclos_service}@${cyclos_database}" || exit 1
  1155. ## ensure that the database is not locked
  1156. Wrap -d "check and remove database lock if any" -- \
  1157. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1158. Wrap -d "set root url to '$url'" -- \
  1159. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1160. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1161. docker start "${container_id}" || exit 1
  1162. }
  1163. cmdline.spec.gnu set-root-url
  1164. cmdline.spec:cyclos:cmd:set-root-url:run() {
  1165. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1166. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1167. local URL
  1168. cyclos_database=${opt_database:-cyclos}
  1169. cyclos_service="${opt_service:-cyclos}"
  1170. project_name=$(compose:project_name) || exit 1
  1171. url=$(compose:get_url "${project_name}" "${cyclos_service}") || exit 1
  1172. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1173. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1174. docker stop "$container_id" || exit 1
  1175. Wrap -d "set root url to '$url'" -- \
  1176. cyclos:set_root_url "${project_name}" "${cyclos_database}" "${url}" || exit 1
  1177. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1178. docker start "${container_id}" || exit 1
  1179. }
  1180. cmdline.spec.gnu unlock
  1181. cmdline.spec:cyclos:cmd:unlock:run() {
  1182. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  1183. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  1184. local URL
  1185. cyclos_database=${opt_database:-cyclos}
  1186. cyclos_service="${opt_service:-cyclos}"
  1187. project_name=$(compose:project_name) || exit 1
  1188. container_id=$(compose:service:container_one "${project_name}" "${cyclos_service}") || exit 1
  1189. Wrap -d "stop ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1190. docker stop "$container_id" || exit 1
  1191. Wrap -d "check and remove database lock if any" -- \
  1192. cyclos:unlock "${project_name}" "${cyclos_database}" || exit 1
  1193. Wrap -d "start ${DARKYELLOW}${cyclos_service}${NORMAL}'s container" -- \
  1194. docker start "${container_id}" || exit 1
  1195. }
  1196. cmdline.spec.gnu rocketchat
  1197. cmdline.spec::cmd:rocketchat:run() {
  1198. :
  1199. }
  1200. cmdline.spec.gnu drop-indexes
  1201. cmdline.spec:rocketchat:cmd:drop-indexes:run() {
  1202. : :optval: --database,-d "Target database ('rocketchat' if not specified)"
  1203. : :optval: --service,-s "The rocketchat service name (defaults to 'rocketchat')"
  1204. local URL
  1205. rocketchat_database=${opt_database:-rocketchat}
  1206. rocketchat_service="${opt_service:-rocketchat}"
  1207. project_name=$(compose:project_name) || exit 1
  1208. container_id=$(compose:service:container_one "${project_name}" "${rocketchat_service}") || exit 1
  1209. Wrap -d "stop ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1210. docker stop "$container_id" || exit 1
  1211. errlvl=0
  1212. Wrap -d "drop indexes" -- \
  1213. rocketchat:drop-indexes "${project_name}" "${rocketchat_database}" || {
  1214. errlvl=1
  1215. errmsg="Failed to drop indexes"
  1216. }
  1217. Wrap -d "start ${DARKYELLOW}${rocketchat_service}${NORMAL}'s container" -- \
  1218. docker start "${container_id}" || exit 1
  1219. if [ "$errlvl" != 0 ]; then
  1220. err "$errmsg"
  1221. fi
  1222. exit "$errlvl"
  1223. }
  1224. cmdline.spec.gnu nextcloud
  1225. cmdline.spec::cmd:nextcloud:run() {
  1226. :
  1227. }
  1228. cmdline.spec.gnu upgrade
  1229. cmdline.spec:nextcloud:cmd:upgrade:run() {
  1230. : :posarg: [TARGET_VERSION] "Source cyclos dump file to restore"
  1231. : :optval: --service,-s "The nexcloud service name (defaults to 'nextcloud')"
  1232. local URL
  1233. nextcloud_service="${opt_service:-nextcloud}"
  1234. project_name=$(compose:project_name) || exit 1
  1235. containers=$(compose:service:containers "${project_name}" "${nextcloud_service}") || exit 1
  1236. container_stopped=()
  1237. if [ -n "$containers" ]; then
  1238. for container in $containers; do
  1239. Wrap -d "stop ${DARKYELLOW}${nextcloud_service}${NORMAL}'s container" -- \
  1240. docker stop "$container" || {
  1241. err "Failed to stop container '$container'."
  1242. exit 1
  1243. }
  1244. container_stopped+=("$container")
  1245. done
  1246. fi
  1247. before_version=$(nextcloud:src:version) || exit 1
  1248. ## -q to remove the display of ``compose`` related information
  1249. ## like relation resolution.
  1250. ## --no-hint to remove the final hint about modifying your
  1251. ## ``compose.yml``.
  1252. compose -q upgrade "$nextcloud_service" --no-hint
  1253. errlvl="$?"
  1254. after_version=$(nextcloud:src:version)
  1255. if [ "$after_version" != "$before_version" ]; then
  1256. desc="update \`compose.yml\` to set ${DARKYELLOW}$nextcloud_service${NORMAL}'s "
  1257. desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}"
  1258. Wrap -d "$desc" -- \
  1259. compose:file:value-change \
  1260. "${nextcloud_service}.docker-compose.image" \
  1261. "docker.0k.io/nextcloud:${after_version}-myc" || exit 1
  1262. fi
  1263. if [ "$errlvl" == 0 ]; then
  1264. echo "${WHITE}Launching final compose${NORMAL}"
  1265. compose up || exit 1
  1266. fi
  1267. exit "$errlvl"
  1268. }
  1269. cmdline.spec.gnu check-fix
  1270. cmdline.spec::cmd:check-fix:run() {
  1271. : :posarg: [SERVICES...] "Optional service to check"
  1272. : :optval: --check,-c "Specify a check or a list of checks separated by commas"
  1273. : :optfla: --silent,-s "Don't ouput anything if everything goes well"
  1274. local project_name service_name containers container check
  1275. all_checks=$(declare -F |
  1276. egrep '^declare -fx? container:health:check-fix:[^ ]+$' |
  1277. cut -f 4 -d ":")
  1278. checks=(${opt_check//,/ })
  1279. for check in "${checks[@]}"; do
  1280. fn.exists container:health:check-fix:$check || {
  1281. err "check '$check' not found."
  1282. return 1
  1283. }
  1284. done
  1285. if [ "${#checks[*]}" == 0 ]; then
  1286. checks=($all_checks)
  1287. fi
  1288. ## XXXvlab: could make it parallel
  1289. project_name=$(compose:project_name) || exit 1
  1290. containers=($(compose:project:containers "${project_name}")) || exit 1
  1291. found=
  1292. for container in "${containers[@]}"; do
  1293. service_name=$(docker ps --filter id="$container" --format '{{.Label "com.docker.compose.service"}}')
  1294. if [ "${#SERVICES[@]}" -gt 0 ]; then
  1295. [[ " ${SERVICES[*]} " == *" $service_name "* ]] || continue
  1296. fi
  1297. found=1
  1298. one_bad=
  1299. for check in "${checks[@]}"; do
  1300. if ! container:health:check-fix:"$check" "$container"; then
  1301. one_bad=1
  1302. fi
  1303. done
  1304. if [ -z "$opt_silent" ] && [ -z "$one_bad" ]; then
  1305. Elt "containers have been checked for ${DARKYELLOW}$service_name${NORMAL}"
  1306. Feedback
  1307. fi
  1308. done
  1309. if [ -z "$found" ]; then
  1310. if [ -z "$opt_silent" ]; then
  1311. if [ "${#SERVICES[@]}" -gt 0 ]; then
  1312. warn "No container for given services found in current project '$project_name'."
  1313. else
  1314. warn "No container found for current project '$project_name'."
  1315. fi
  1316. fi
  1317. return 1
  1318. fi
  1319. }
  1320. cmdline::parse "$@"