1package App::Netdisco::Worker::Plugin::Psql; 2 3use Dancer ':syntax'; 4use App::Netdisco::Worker::Plugin; 5use aliased 'App::Netdisco::Worker::Status'; 6 7register_worker({ phase => 'main' }, sub { 8 my ($job, $workerconf) = @_; 9 my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; 10 11 my $name = setting('database')->{name}; 12 my $host = setting('database')->{host}; 13 my $user = setting('database')->{user}; 14 my $pass = setting('database')->{pass}; 15 16 my $portnum = undef; 17 if ($host and $host =~ m/([^;]+);(.+)/) { 18 $host = $1; 19 my $extra = $2; 20 my @opts = split(/;/, $extra); 21 debug sprintf("Host: %s, extra: %s\n", $host, $extra); 22 foreach my $opt (@opts) { 23 if ($opt =~ m/port=(\d+)/) { 24 $portnum = $1; 25 } else { 26 # Unhandled connection option, ignore for now 27 } 28 } 29 } 30 31 $ENV{PGHOST} = $host if $host; 32 $ENV{PGPORT} = $portnum if defined $portnum; 33 $ENV{PGDATABASE} = $name; 34 $ENV{PGUSER} = $user; 35 $ENV{PGPASSWORD} = $pass; 36 $ENV{PGCLIENTENCODING} = 'UTF8'; 37 38 if ($extra) { 39 system('psql', '-c', $extra); 40 } 41 else { 42 system('psql'); 43 } 44 45 return Status->done('psql session closed.'); 46}); 47 48true; 49