A few questions regarding staging and production

I have been deploying to my staging environment on digitalocean without problems but after adding production variables and attempting a deploy, both staging and production environments are broken.

The deploys fail at

TASK [deploy : WordPress Installed?] *******************************************
System info:
Ansible 2.2.1.0; Darwin
Trellis at “Allow for per-project packagist.com authentication”

Error: Error establishing a database connection. This either means that the
username and password information in your wp-config.php file is incorrect
or we can’t contact the database server at localhost. This could mean your
host’s database server is down.
fatal: [my.domain]: FAILED! => {“changed”: false, “cmd”: [“wp”, “core”, “is-installed”, “–require=/srv/www/mydomain/shared/tmp_multisite_constants.php”], “delta”: “0:00:00.195600”, “end”: “2017-05-20 09:50:57.623109”, “failed”: true, “failed_when_result”: true, “rc”: 1, “start”: “2017-05-20 09:50:57.427509”, “stderr”: “Error: Error establishing a database connection. This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.”, “stdout”: “”, “stdout_lines”: [], “warnings”: []}

staging is now displaying the standard WordPress ‘error establishing database connection’ when browsing the site.

I am trying to have both the production and staging environments on the same digitalocean droplet (its a small non critical hobby site so I am not concerned about keeping them separate)

My questions are:

  • Is it possible to have both staging and production trellis environments on the same box?

  • During the deploy, does trellis reset the database password both for WordPress and MySql / MariaDB?

  • how is vault_mysql_root_password from vault.yml treated if both environments are on the same machine?

  • is env() trellis’ own function? I am used to getenv()

  • Is it possible to check the actual values being used by application.php i.e

    /**

    • DB settings
      */
      define(‘DB_NAME’, env(‘DB_NAME’));
      define(‘DB_USER’, env(‘DB_USER’));
      define(‘DB_PASSWORD’, env(‘DB_PASSWORD’));
      define(‘DB_HOST’, env(‘DB_HOST’) ?: ‘localhost’);
      define(‘DB_CHARSET’, ‘utf8mb4’);
      define(‘DB_COLLATE’, ‘’);
      $table_prefix = env(‘DB_PREFIX’) ?: ‘wp_’;

When I ssh into the target machine, mysql is running.

printenv shows an entry called ‘OLDPW’ which is pointing at the trellis /config/environments directory

If both staging and production are on the same host/server, you’ll need to use one of the options described here: Production server provision configures Staging envs if hosts are the same - Bug or Intention?

Ansible tries to load all group_vars for the host it is targeting. If your host uses the same name/alias in multiple environments, Ansible loads group_vars for all applicable environments and the load order and resulting precedence for these values would be unpredictable, resulting in messed up variable values.

The PR roots/trellis#562 had a validate_one_env_group_only validation to detect and warn about this problem, but the PR never got traction and needs to be updated. I’ll PR Trellis with a validation for just this issue alone.


Q. Is it possible to have both staging and production trellis environments on the same box?
A. Yes it’s possible, but not recommended. Would require applying one of these options.

Q. During the deploy, does trellis reset the database password both for WordPress and MySql / MariaDB?
A. I’m not sure but I don’t think so. Once you’ve sorted out the host issue above, you should be able to get the answer via testing.

Q. how is vault_mysql_root_password from vault.yml treated if both environments are on the same machine?
A. If both environments are on the same machine, I think this particular password would need to be the same for both environments. But to answer your question, if you don’t apply the fixes mentioned above for multiple envs on same machine, Ansible will load vault_mysql_root_password from both environments in a nondeterministic sequence, so you can’t rely on which would take precedence.

Q. is env() trellis’ own function? I am used to getenv()
A. roots/bedrock#233: Use oscarotero/env instead of getenv

Q. Is it possible to check the actual values being used by application.php
A. SSH into server or VM, navigate to WP, use wp eval

$ cd /srv/www/example.com/current
$ wp eval 'echo DB_NAME . "\n";'
example_com_production

(but maybe that’s not exactly what you wanted)

@fullyint thanks for your detailed answer. I realised I had forgotten the provision step for the staging environment. Things ‘seemed to work’ after that so I guess I got lucky with the ‘resulting precedence for these values would be unpredictable’. I will take a look at those options you linked to. Thanks again.

So for the staging site, did you have to change the keys in the yaml to something like ‘staging.example.com’ to have different prod and staging directories?