Leveraging WP-CLI Aliases in Your WordPress Development Workflow

Actually another pull request by @guilro is in line for automating database and file backups using Stouts.backup Ansible role with Duplicity and is mentioned here: https://github.com/roots/trellis/pull/650 . I think that would be really good to have and can be configured in many different ways.

2 Likes

I use the --export option of wp search-replace for this and also add gzip. No wp-aliases though, but not much different. It’s basically like:

ssh $srv1 "cd $dir1; wp search-replace 'http://$srv1' 'http://$srv2'  --export | gzip" \
| ssh $srv2 "cd $dir2; gunzip | wp db import -"
1 Like

Just did a run to move stuff from a dev site to a production site. Latter is a demo so worries there. But I ran into an error

./sync-all.sh development production
Would you really like to reset the production database and sync up from development? [y/N] y
mysqldump: Can't create/write to file 'filathlos_imagewize_com_production-a822007.sql' (Errcode: 13 "Permission denied")

Must be a permissions issue. But should did not work using user web?

Well when I used the user web in the wp-cli.yml file it all worked

./sync-all.sh development production
Would you really like to reset the production database and sync up from development? [y/N] y
Success: Exported to 'filathlos_imagewize_com_production-a9ab231.sql'.
Success: Imported from '-'.

I only had a Sage error:

Error: Sage â€ș ErrorAutoloader not found.. You must run composer install from the Sage directory.

, but when I commented out build-before.yml and ran deployment again I solved that too

Thank you for this very helpful script!

In my case, I ran sync.sh from within my Trellis-provisioned development vagrant vm and it only worked when I changed the first line from #!/bin/sh to #!/bin/bash. Otherwise it will throw the following error on line 25 of the script:

./sync.sh: 25: ./sync.sh: Syntax error: "(" unexpected (expecting "then")

I’m far from being a Unix shell expert though, so I’m wondering if anyone has encountered this?

2 Likes

Yeah, others encountered this common Bash issue too. See https://github.com/jasperf/trellis-sync/issues/3#issuecomment-291927603 where I use @ben and Raquelle’s scripts and another person @redstartinternet encountered the same POSIX issue.

I opened up a pull request to fix the upload and db syncing scripts.

2 Likes

@seraphim I’m also getting that error. Did anyone experience this and resolve the issue?

Did you try changing #!/bin/sh to #!/bin/bash at the top of the script, as he suggested?

Yes. But that didn’t change my result.

Just reading this here now, but did deal with the pull request at Github earlier on :wink:

If your wp @prod alias is not working, you need to do the same thing for your prod server. Something like adding the below to your ~/.ssh/config file:

Host example.com
HostName XXX.XXX.XXX.XXX (IP ADDRESS)
User admin
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
ForwardAgent yes

It looks like the vagrant-triggers plugin has been merged into Vagrant in recent versions, and this vagrant-triggers script (which I :heart:) is not working as is (at least with Vagrant 2.1.0). Anyone else experienced this?

If you want to use this automatic SSH configuration after updating Vagrant > 2.1.0, you can uninstall vagrant-triggers and replace this code in your Vagrantfile:

# Run script to update local SSH config for .dev hostname after `vagrant up`
if Vagrant.has_plugin?('vagrant-triggers')
  config.trigger.after :up do
    run "./bin/ssh-config-development.sh"
  end
else
  fail_with_message "vagrant-triggers missing, please install the plugin with this command:\nvagrant plugin install vagrant-triggers"
end

with

config.trigger.after :up do |trigger|
  trigger.run = { path: "./bin/ssh-config-development.sh"}
end

I made a few modifications to this, including the change noted by @nickkeenan to automate both the script itself, while removing the hardcoded host name in the script, and creating a wp-cli.local.yml file in the project root so that you can use the @dev alias with wp-cli from anywhere in the project :slight_smile:

Put this block in Vagrantfile after config.vm.provision provisioner do |ansible|:

    # Run script to update local SSH config for .dev hostname after `vagrant up`
    config.trigger.after :up do |trigger|
      trigger.run = {
        path: "./bin/ssh-config-development.sh",
        args: [main_hostname, "#{ANSIBLE_PATH}", trellis_config.wordpress_sites.keys.first]
      }
    end

And the updated script:

# bin/ssh-config-development.sh
#!/bin/bash
host=$1
path=$2
site=$3

# add ssh-config for vagrant to ~/.ssh/config
sed "/^$/d;s/Host /$NL&/" ~/.ssh/config | sed "/^Host $host$/,/^$/d;"> config &&
cat config > ~/.ssh/config &&
rm config &&
vagrant ssh-config --host $host >> ~/.ssh/config

# add wp-cli.local.yml to project root if it doesn't exist with alias for @dev
cd $path/.. &&
if [ ! -f wp-cli.local.yml ]
then
  touch wp-cli.local.yml
fi

if ! grep -Fxq "@dev" wp-cli.local.yml
then

cat << EOF > wp-cli.local.yml
@dev:
  ssh: vagrant@$host/srv/www/$site/current
  path: web/wp
EOF

fi

A few caveats:

  • if you have more than one site in your wordpress_sites.yml it only creates an alias for the first one
  • the script assumes your ssh config is located at ~/.ssh/config
  • it only checks for the existence of a @dev alias in wp-cli.local.yml so if one already exists, it won’t do anything. this might mean the alias won’t work if you’ve run vagrant up then changed your site name or URL and run vagrant up again
  • by default, the alias will not work in the site/ directory
    – to fix, either add the alias to site/wp-cli.yml, delete site/wp-cli.yml, or add this to the configuration in site/wp-cli.yml:
_:
  inherit: ../wp-cli.local.yml
1 Like

I have this working but some problems.

  • This is a fresh install of trellis/bedrock. (as of 11/22/2018, Happy Thanksgiving!)
  • I have my wp-cli aliases in wp-cli.yml in “site”
    Example:
    @staging:
    ssh: web@staging.example.com/srv/www/example.com/current
  • The script is in “site/scripts/sync-db.sh”
  • I have this at the top of my script “#!/bin/bash”
  • I have sshd_permit_root_login: false in my security.yml
  • I’m using the new and improved script by benword (above) with the conditional uploads added by runofthemill.

My problems/lack of understanding:

  1. When I run the script I have to be inside “scripts” “./sync-db.sh development staging” Previously, I was able to the script in “site” “scripts/sync-db.sh”
    (Update: I understand this now. In the script cds into the folder up cd ../ && so that’s why it couldn’t find my aliases.)

  2. The script runs fine, but then when I go to staging to see the result I have a white screen. I can access the admin, after I login and logout I can see the page.
    (Update: this is the behavior on other devices. For example, first I see a white screen on my iPhone, then after log in/out on laptop, site works fine on iPhone. Is this normal? why is it doing this?)