Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically set options, aliases #3063

Closed
grasmash opened this issue Oct 13, 2017 · 8 comments
Closed

Dynamically set options, aliases #3063

grasmash opened this issue Oct 13, 2017 · 8 comments

Comments

@grasmash
Copy link
Contributor

In Drush 8, I could conditionally set configuration options:

if (getenv('PROBO_ENVIRONMENT') && getenv('BUILD_DOMAIN')) {
  $options['uri'] = $_ENV['BUILD_DOMAIN'];
}
if (getenv('TUGBOAT_URL')) {
  $options['uri'] = $_ENV['TUGBOAT_URL'];
}
// Include current directory. Will add policy.drush.inc.
$options['include'][] = __DIR__;
// If we are on Acquia Cloud, add Acquia specific commands.
if (file_exists('/usr/local/drush8/commands')) {
  $options['include'][] = '/usr/local/drush8/commands';
}

Now that Drush 9 uses drush.yml in place of drushrc.php, how can this be accomplished?

Similarly, how might one dynamically set aliases?

// Local environment.
$aliases['${project.machine_name}.local'] = array(
  'root' => '/var/www/${project.machine_name}/docroot',
  'uri' => '${project.local.uri}',
  );
// Add remote connection options when alias is used outside VM.
if ('vagrant' != $_SERVER['USER']) {
  $aliases['${project.machine_name}.local'] += array(
    'remote-host' => '${project.local.hostname}',
    'remote-user' => 'vagrant',
    'ssh-options' => '-o PasswordAuthentication=no -i ' . drush_server_home() . '/.vagrant.d/insecure_private_key'
  );
}

@grasmash
Copy link
Contributor Author

greg.1.anderson [3:54 PM] 
Well, it depends on when you want to set the variable.


[3:55] 
`uri` is hard to set, because the Drupal root and URI are calculated in preflight, prior to loading any commandfiles.


[3:56] 
For any config value foo_bar.baz, you can set it via an environment variable `DRUSH_FOO_BAR_BAZ`.  So if you set an environment variable `DRUSH_URI`, that should set the URI.  Maybe.  URI is special, I haven't tested it.


[3:57] 
Nope, didn't work. :disappointed:


[3:59] 
`DRUSH_COMMAND_CORE_STATUS_OPTIONS_FORMAT=yaml ./sut status` worked, though.


[4:00] 
For other command-specific or other sorts of options, you can use a `@hook init` to provide new values for options. Any commandfile may provide this hook.

@greg-1-anderson
Copy link
Member

Other options:

  • Use the Drush launcher to select your site and set options. May need enhancements here.
  • Make site aliases for each of your sites and use site-set (aka use) to select the alias to use

Also, regarding:

if (file_exists('/usr/local/drush8/commands')) {
  $options['include'][] = '/usr/local/drush8/commands';
}

For this example, you can simply set the extra location invariantly in your drush.yml configuration file. Drush will not complain if the location does not exist.

@weitzman
Copy link
Member

Documenting this would be nice. Maybe in example.drush.yml.

@weitzman
Copy link
Member

Not yet tested, but it should be possible in a commandfile to call the AliasManager, get and change aliases, etc.

@weitzman
Copy link
Member

Posted an example showing alias and general config alteration at #3115. I didn't try to alter uri.

@weitzman
Copy link
Member

I have tried to alter uri using the above technique and was unsuccessful. But I did learn that uri and any other drush config may be set via environment variable. see https://github.com/drush-ops/drush/blob/master/examples/example.drush.yml#L26-L35

@HnLn
Copy link

HnLn commented Jan 24, 2019

Not sure what the best way is, we have people running drush on vagrant and others on docker, so the 'root' part in the alias differs from person to person. What's the recommended way ?

@mariacha
Copy link

I'm not sure of the best way either, so what we're doing is having an example.drush.yml file in our sites/all/drush folder. It's super basic and includes instructions to users to rename the file drush.yml and alter the values. That file (sites/all/drush/drush.yml) is just gitignored then. It's much like having a settings.local.php or some such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants