2

我刚刚完成了 DDEV 设置,并且通过手动运行进行了多站点工作ddev import-db --target-db=[db-name]。它工作得很好,但我想弄清楚如何从 Acquia 获取数据库拉取,以便在我可以指定要从中拉取的站点的地方工作。

我有这个脚本工作,但有没有办法用 DDEV 命令来做到这一点会更干净一点?

首先,我将 acquia.yaml 修改为:

environment_variables:
  project_id: mysite.dev
  uri: mysite.com
  db_name: mysite_us

#uri: mysite.ca
#db_name: mysite_canada

#uri: mysite.co.uk
#db_name: mysite_unitedkingdom

# etc etc

db_pull_command:
  command: |
    # set -x   # You can enable bash debugging output by uncommenting
    ls /var/www/html/.ddev >/dev/null # This just refreshes stale NFS if possible
    pushd /var/www/html/.ddev/.downloads >/dev/null
    acli remote:drush -n ${project_id} -- sql-dump --extra-dump=--no-tablespaces --uri=${uri}  >${db_name}.sql

然后我编写了以下脚本,我称之为:

./ddev-refresh-db.sh mysite_us mysite.com
#!/bin/bash

site="$1"
uri="$2"

ddev pull acquia
ddev import-db --target-db=${site} --src=.ddev/.downloads/${site}.sql
ddev drush --uri=${uri} cr

但是,这仍然需要我们在运行此命令之前更改 acquia.yaml 文件中的站点和 URI。

有没有办法将变量传递给ddev pull acquia?还有一种方法可以模仿这个脚本使用真正的 DDEV 命令做什么?

4

3 回答 3

2

这是 Acquia 多站点拉取的更完整答案,拉取所有站点。从 DDEV v1.18.0 开始,它ddev pull本身确实不够健壮,无法提取多个站点,因为它假定一个数据库和一组文件。这适用于@kelly howard 在https://stackoverflow.com/a/68553116/215713中的回答不足的情况。(在她的示例中,她只提取了一个多站点,它非常适合这种情况。)

但是在这里,我们将把所有逻辑放在一个 DDEV 自定义命令中,并为任何命名站点拉取所有数据库和文件,所以ddev acquiapull <sitename>

将此文件作为.ddev/commands/web/acquiapull

#!/bin/bash

# This DDEV custom command is set up to pull database and files from Acquia for several subsites.
# Usage: `ddev acquiapull [ --skip-db ] [ --skip-files ] <site1> <site2>
# Example: `ddev acquiapull subsite1`

# This assumes that each subsite has its own database (named for the site)
# and that each subsite has its own files in sites/<sitename>/files

# To use it set up the needed ACQUIA_API_KEY and ACQUIA_API_SECRET in global 
# or project config, just as described in 
# https://ddev.readthedocs.io/en/stable/users/providers/acquia/

acquia_project_id=myprojectid.dev
tmpdir=/tmp  #inside web container

set -eu -o pipefail

while :; do
  case ${1:-} in
  -h | -\? | --help)
    show_help
    exit
    ;;
  -y|--yes)
    SKIP_CONFIRMATION=true
    ;;
  --skip-files)
    SKIP_FILES=true
    ;;
  --skip-db)
    SKIP_DB=true
    ;;
  --) # End of all options.
    shift
    break
    ;;
  -?*)
    printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
    ;;
  *) # Default case: No more options, so break out of the loop.
    break ;;
  esac

  shift
done


# Map sitename to database name
function target_db_name() {
  site_name=$1
  echo $site_name
}

# Map sitename to files dir
function target_files_dir() {
  site_name=$1
  echo "sites/${site_name}/files"
}

# Get the files from upstream and load them.
function files_pull() {
  #set -x                           # You can enable bash debugging output by uncommenting
  set -eu -o pipefail
  site_name=$1
  files_dir=$(target_files_dir $1)
  mkdir -p ${DDEV_DOCROOT}/${files_dir}/
  echo "Using drush rsync to update files for ${site_name}..."
  drush rsync --alias-path=~/.drush -q -y -r ${DDEV_DOCROOT} --verbose @${acquia_project_id}:${files_dir}/ ${DDEV_DOCROOT}/${files_dir}/
}

# Get the db from upstream and load it
function db_pull() {
  #set -x   # You can enable bash debugging output by uncommenting
  set -eu -o pipefail
  site_name=$1
  target_db=$(target_db_name ${site_name})
  echo "Downloading ${site_name} database..."
  acli remote:drush -n ${acquia_project_id} -- sql-dump --uri=${site_name} --extra-dump=--no-tablespaces >${tmpdir}/${site_name}.sql
  echo "Loading ${site_name} into database '${target_db}'..."
  mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS ${target_db}; GRANT ALL ON ${target_db}.* TO 'db'@'%'"
  mysql -uroot -proot ${target_db} <${tmpdir}/${site_name}.sql
  drush -r root --uri=${site_name} cr
}

# Handle initial authentication via Acquia secrets and ssh
function authenticate() {
  if [ -z "${ACQUIA_API_KEY:-}" ] || [ -z "${ACQUIA_API_SECRET:-}" ]; then echo "Please make sure you have set ACQUIA_API_KEY and ACQUIA_API_SECRET in your project or global config" && exit 1; fi
  if ! command -v drush >/dev/null; then echo "Please make sure your project contains drush, ddev composer require drush/drush" && exit 1; fi
  ssh-add -l >/dev/null || (echo "Please 'ddev auth ssh' before running this command." && exit 1)
  acli auth:login -n --key="${ACQUIA_API_KEY}" --secret="${ACQUIA_API_SECRET}"
  acli remote:aliases:download -n >/dev/null
}

# Main script
authenticate || (printf "Failed to authenticate" && exit $?)

if [ $# -eq 0 ]; then
  printf "Usage: ddev acquiapull [ --skip-db ] [ --skip-files ] <sitename>"
  exit 1
fi

if [ ${SKIP_CONFIRMATION:-} != "true" ]; then
  echo "This will overwrite your database and files for sites $*. OK?"
  select yn in "Yes" "No"; do
      case $yn in
          No ) exit;;
      esac
  done
fi

for subsite in $*; do
  echo "Pulling subsite: $subsite"
  if [ "${SKIP_DB:-}" != "true" ]; then
    db_pull ${subsite} || (printf "Failed to pull db for ${subsite}" && exit $?)
  else
    echo "Skipping db pull for ${subsite}"
  fi

  if [ "${SKIP_FILES:-}" != "true" ]; then
    files_pull ${subsite} || (printf "Failed to pull files for ${subsite}" && exit $?)
  else
    echo "Skipping files pull for ${subsite}"
  fi
done
于 2021-09-30T18:19:26.550 回答
1

感谢@rfay 的指导,我在 .ddev/providers 中为每个国家/地区设置了一组文件。每一个的结构如下:

environment_variables:
  uri: mysite.be
  db_name: belgium

auth_command:
  command: |
   <no changes>

db_pull_command:
  command: |
    # set -x   # You can enable bash debugging output by uncommenting
    ls /var/www/html/.ddev >/dev/null # This just refreshes stale NFS if possible
    pushd /var/www/html/.ddev/.downloads >/dev/null
    acli remote:drush -n ${ACQUIA_PROJECT_ID} -- sql-dump --extra-dump=--no-tablespaces --uri=${uri}  >${db_name}.sql

然后我在 .ddev/commands/host 中创建了一个自定义命令,其中包含我的脚本内容。真实剧本中有更多案例,涵盖所有国家。

#!/usr/bin/env bash

## Description: Refresh a database from Acquia and run post-db commands
## Usage: refresh-db [dbname]
## Example: "ddev refresh-db france"

site="$1"

case $site in
  canada)
    uri="mysite.ca"
    ;;
  australia)
    uri="mysite.com.au"
    ;;
  belgium)
    uri="mysite.be"
    ;;
  brazil)
    uri="mysite.com.br"
    ;;
  *)
    site="db"
    uri="mysite.com"
    ;;
esac

ddev pull ${site} -y 2>/dev/null # suppress pull failed message since it really didn't
ddev import-db --target-db=${site} --src=${DDEV_APPROOT}/.ddev/.downloads/${site}.sql
ddev drush --uri=${uri} cr
ddev drush --uri=${uri} -y pmu simplesamlphp_auth
ddev drush --uri=${uri} -y config-set system.performance css.preprocess 0
ddev drush --uri=${uri} -y config-set system.performance js.preprocess 0

我尝试按照建议在 db_pull_command 期间处理 db 导入,但是在导入尚未使用 ddev import-db 导入的数据库时,我无法获得过去的数据库权限错误。但是,使用自定义命令,我还可以合并通常仅在通过 config.yaml 完成时才会针对默认数据库运行的 post-db-import 步骤。

我所做的另一项更改是将项目 ID 移动到 global_config.yaml 文件中的 Web 环境设置中。这样,如果我们想要更改我们想要从中提取的环境,我们只需在此处编辑项目 ID,而不必编辑提供程序文件。

我没有回馈开源项目的经验,但如果这对其他人有帮助,我很乐意与某人合作,在文档或其所属的任何地方执行拉取请求。

于 2021-07-28T00:42:23.517 回答
0

我将继续进行一般性的回答,但是当你解决这个问题时,你可以添加一个完整的答案。(我无权访问 Acquia 多站点。)

你在正确的轨道上,但你可以在拉脚本中完成所有这些。您遇到的问题是 ddev 只是假设一个数据库,而您有多个。

这是您的 acquia.yaml 的策略:

  1. 创建所有数据库。您可以使用mysql -e "CREATE DATABASE IF NOT EXISTS <dbname>;,使用多行或 for 循环。
  2. 拉取所有数据库。您可以使用单独的 acli 行来执行此操作,或使用 for 循环。
  3. 使用 mysql 命令导入不是主数据库的数据库。mysql <dbname> < <dbname.sql同样,这可以是几行代码或一个 for 循环。(您也可以只导入主数据库,它只会由 ddev 重新导入,如果它不大,则不会造成任何伤害。)

感谢您提出的好问题,我希望您能在这里给出完整的答案。您的答案也可以合并到https://ddev.readthedocs.io/en/stable/users/providers/acquia/ - 您可以通过单击右上角的铅笔链接在那里进行 PR。

于 2021-07-24T13:11:41.050 回答