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.

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