Move env var forwarding from fatnode to perl interpreter role
Tyler Riddle [Mon, 21 Jan 2013 23:43:47 +0000 (15:43 -0800)]
The forward env var list is now configurable and escapes ' chars if they are present in the env value.

lib/Object/Remote/FatNode.pm
lib/Object/Remote/Role/Connector/PerlInterpreter.pm

index b5f3437..c840cf2 100644 (file)
@@ -68,32 +68,6 @@ my @core_non_arch = grep +(
     or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
 ), keys %mods;
 
-#FIXME - encode the env vars in YAML or something similiar and make the
-#env vars that will be forwarded driven by a list
-my $env_pass = '';
-if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
-  my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
-  $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
-}
-if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
-  my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
-  $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
-}
-if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
-  my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
-  $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
-}
-if (defined($ENV{OBJECT_REMOTE_LOG_FORWARDING})) {
-  my $forwarding = $ENV{OBJECT_REMOTE_LOG_FORWARDING};
-  $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORWARDING} = "' . $forwarding . "\";\n";
-}
-if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) {
-  my $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
-  $env_pass .= '$ENV{OBJECT_REMOTE_PERL_BIN} = "' . $perl_bin . "\";\n";
-}
-
-
-
 my $start = stripspace <<'END_START';
   # This chunk of stuff was generated by Object::Remote::FatNode. To find
   # the original file's code, look for the end of this BEGIN block or the
@@ -158,6 +132,7 @@ my @segments = (
   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
 );
 
-our $DATA = join "\n", $start, $env_pass, @segments, $end;
+#print STDERR Dumper(\@segments);
+our $DATA = join "\n", $start, @segments, $end;
 
 1;
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;