2

I've setup a Laravel app on a Fortrabbit server. I can do the following

$ ssh user@server
$ cd htdocs
$ php artisan migrate

Which works perfectly fine.

But I'm trying to use Envoy for tasks like this. So I've made a simple task:

@servers(['test' => 'user@server'])

@task('task:test', ['on' => 'test'])
    cd htdocs
    php artisan migrate
@endtask

However, I encounter the following issue when doing so:

$ envoy run task:test

[user@server]: \(><)/ Welcome to the rabbit hole. \(><)/
[user@server]: [PDOException] SQLSTATE[HY000] [2002] No such file or directory

It might be worth noting that the db connection uses credentials from ENV variables set in the Fortrabbit interface. If i SSH into the server and do env, all the variables are listed as they should be. But when doing

$ ssh user@server env

I only get a small portion of the Fortrabbit server ENV variables.

I can't figure out what could be the cause of the error, and I haven't been able to find anything when asking Google. Could anybody shed a bit of light on this? Thanks alot.

Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
nielsstampe
  • 1,336
  • 1
  • 15
  • 25
  • Presumably this is because if you use `ssh user@host command` you are executing that command rather than a login shell. As such, you may not get certain environment variables set (for example, those set in a bashrc file). As for how to fix this I don't know as I don't use Envoy, but if there's a way to get Envoy to create a login shell and then pipe commands to that rather than executing commands directly that'll probably do what you want. – alexrussell Apr 23 '14 at 12:02
  • How are you currently detecting environments ? – edi9999 Apr 23 '14 at 14:18
  • I have tried to reproduce the error you describe but for me `php artisan env` does work as expected. Can you post the code that does environment detection ? And are you sure you're not setting the environment dynamically somewhere in the code ? That could explain why the second command by forcing --env=test_server doesn't work. – edi9999 Apr 23 '14 at 15:27
  • @edi9999 In bootstrap/start.php i do a closure in detectEnvironment() with the following: `return getenv('APP_ENV') ?: 'development';` – nielsstampe Apr 24 '14 at 08:21

2 Answers2

1

I suggest to check if your environment dedection works as expected.

$ php artisan env

If not, your can force the environment for artisan commands, like so:

$ php artisan migrate --env=test_server
Oliver Stark
  • 186
  • 6
  • Doing `php artisan env` in my Envoy tasks returns `development` which obviously isn't correct. However, I did try to force the stage environment as suggested: `php artisan migrate --env=stage`, which threw the exact same PDOException as earlier. If I, through Envoy, do `cd htdocs` and `env` I only get a small portion of the ENV variables as stated earlier. If I SSH in to the server normally, not through Envoy, and do `env` they are all listed. Furthermore `php artisan env` on the server through SSH results in `stage`. – nielsstampe Apr 23 '14 at 12:18
  • 3
    The difference between (1) logging in to SSH and executing `env` and (2) executing `ssh user@server env` is: When doing (1), you open an new shell session and session scripts like `.bash_profile` are executed. When doing (2) you just execute a remote command and no session scripts are executed As fortrabbit exports the environment variables in the session, you can do this: `ssh user@server '. /etc/profile.envvars && env'` to export the session variables you setup in the dashboard. Mind the `&&`. – ukautz Apr 24 '14 at 07:47
1

As @ukautz said, the session scripts are not executed by envoy, that's why some of your environment variables are missing.

Here's what I did:

@servers(['dev'=>"<<user@server>>"])
@task('list')
    . /etc/profile.envvars
    env
@endtask

That showed all ENV variables, including those set by you on the dashboard !

Your script should work with this:

@servers(['test' => 'user@server'])
@task('task:test', ['on' => 'test'])
    . /etc/profile.envvars
    cd htdocs
    php artisan migrate
@endtask
edi9999
  • 19,701
  • 13
  • 88
  • 127