Move env var forwarding from fatnode to perl interpreter role
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / Connector / PerlInterpreter.pm
index 933924e..20fb59b 100644 (file)
@@ -16,6 +16,7 @@ has module_sender => (is => 'lazy');
 has ulimit => ( is => 'ro');
 has nice => ( is => 'ro');
 has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef });
+has forward_env => (is => 'ro', required => 1, builder => 1);
 has perl_command => (is => 'lazy');
 has pid => (is => 'rwp');
 has connection_id => (is => 'rwp');
@@ -59,6 +60,14 @@ sub _build_perl_command {
   return [ 'bash', '-c', $shell_code ];
 }
 
+sub _build_forward_env {
+  return [qw(
+    OBJECT_REMOTE_PERL_BIN
+    OBJECT_REMOTE_LOG_LEVEL OBJECT_REMOTE_LOG_FORMAT OBJECT_REMOTE_LOG_SELECTIONS
+    OBJECT_REMOTE_LOG_FORWARDING
+  )];
+}
+
 around connect => sub {
   my ($orig, $self) = (shift, shift);
   my $f = $self->$start::start($orig => @_);
@@ -227,6 +236,7 @@ sub fatnode_text {
     $text .= "my \$WATCHDOG_TIMEOUT = undef;\n";
   }
   
+  $text .= $self->_create_env_forward(@{$self->forward_env});
   $text .= '$Object::Remote::FatNode::REMOTE_NODE = "1";' . "\n";
   
   $text .= <<'END';
@@ -244,4 +254,24 @@ END
   return $text;
 }
 
+sub _create_env_forward {
+  my ($self, @env_names) = @_;
+  my $code = '';
+
+  foreach my $name (@env_names) {
+    next unless exists $ENV{$name};
+    my $value = $ENV{$name};
+    $name =~ s/'/\\'/g;
+    if(defined($value)) {
+      $value =~ s/'/\\'/g;
+      $value = "'$value'";
+    } else {
+      $value = 'undef';
+    }
+    $code .= "\$ENV{'$name'} = $value;\n";
+  }
+
+  return $code;
+}
+
 1;