# Capistrano Connection Caching Question

**URL:** <https://www.ruby-forum.com/t/capistrano-connection-caching-question/77793>\
**Category:** Ruby\
**Created:** [November 30, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/capistrano-connection-caching-question/77793 "2006-11-30T16:42:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rooster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rooster](https://www.ruby-forum.com/u/rooster)\
**Post date:** [November 30, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/capistrano-connection-caching-question/77793/1 "2006-11-30T16:42:12Z")

</div>

Hi,

I’m trying to clear up my understanding of how some of the Capistrano  
connection stuff works. In the Capistrano manual, I found the following

“All |run| does is attempt to execute the given command on all  
associated remote hosts. (We’ll talk more later about the available  
helper methods, of which |run| is the most commonly used.) This means  
that as soon as run is invoked, Capistrano inspects the current task and  
determines what roles are active, and then determines which servers  
those roles map to. If no connection has been made a server yet, the  
connection is established and cached, and then the command is executed  
in parallel. /This means that no connections are made to the remote  
hosts until they are actually needed.”/

I’m a bit confused about what it means to “cache” the connection. Let’s  
say I have the following tasks:

* * *

task :wrapper do  
show\_env  
set\_gem\_home  
show\_env  
end

task :show\_env do  
run ‘env’  
end

## task :set\_gem\_home do run ‘export GEM\_HOME=/path/to/ruby/gems’ end

What I’m seeing–when I run the :wrapper task–is that on the second  
call to :show\_env GEM\_HOME is not set on the remote machine’s  
environment. If the connection is being cached, after the first call to  
:set\_gem\_home wouldn’t GEM\_HOME be set for all subsequent tasks for that  
machine?

I realize that the easiest way to set something like GEM\_HOME is to  
configure it in a .ssh/environment file. However, our sys admins have  
disabled the [PermitUserEnvironment] option in the sshd\_config file. As  
things stand right now, I have to prepend all commands like this [export  
GEM\_HOME=/path/to/ruby/gems && gem install super\_gem]. I’d like to avoid  
this if possible.

Regards,  
Steven

---

<div class="post-metadata">

**Author:** ![rooster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rooster](https://www.ruby-forum.com/u/rooster)\
**Post date:** [November 30, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/capistrano-connection-caching-question/77793/2 "2006-11-30T22:29:33Z")

</div>

On Nov 30, 2006, at 7:41 AM, Steven H. wrote:

> current task and determines what roles are active, and then  
> task :wrapper do  
> run ‘export GEM\_HOME=/path/to/ruby/gems’  
> configure it in a .ssh/environment file. However, our sys admins  
> have disabled the [PermitUserEnvironment] option in the sshd\_config  
> file. As things stand right now, I have to prepend all commands  
> like this [export GEM\_HOME=/path/to/ruby/gems && gem install  
> super\_gem]. I’d like to avoid this if possible.
> 
> Regards,  
> Steven

Hey Steven-

```
What you need to know is that calls to run are all new calls. So if

```

you export a env variable in one call to run and then try to use it  
in another call to run it will not be there. Same thing if you cd  
into a directory in one call to run and then try to do something in  
that dir with another call to run. The way around this is to call run  
will a series of commands in the same run call:

run “export GEM\_HOME=/path/to/ruby/gems &&  
ln -s whatever foo &&  
cd /some/path &&  
touch whatever”

```
So the idea is that if commands you call depends on something set in

```

a previous call to run you need to rework them into a chained command  
like above.

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![rooster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rooster](https://www.ruby-forum.com/u/rooster)\
**Post date:** [December 1, 2006, 12:11am UTC](https://www.ruby-forum.com/t/capistrano-connection-caching-question/77793/3 "2006-12-01T00:11:51Z")

</div>

Ezra Z. wrote:

> > ## helper methods, of which |run| is the most commonly used.) This means
> > 
> > task :set\_gem\_home do  
> > I realize that the easiest way to set something like GEM\_HOME is to  
> > Hey Steven-  
> > cd /some/path &&  
> > – (866) 518-YARD (9273)

Cool,  
Thanks!
