You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by da...@apache.org on 2017/05/09 18:30:50 UTC

[15/27] incubator-trafficcontrol git commit: successfully demo'd postinstall after lots of testing

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/0351e1b1/traffic_ops/install/lib/Database.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/install/lib/Database.pm b/traffic_ops/install/lib/Database.pm
new file mode 100644
index 0000000..9dbbc31
--- /dev/null
+++ b/traffic_ops/install/lib/Database.pm
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+package Database;
+
+use warnings;
+use strict;
+
+use base qw{ Exporter };
+our @EXPORT_OK = qw{ connect };
+our %EXPORT_TAGS = ( all => \@EXPORT_OK );
+
+#------------------------------------
+sub connect {
+    my $databaseConfFile = shift;
+    my $todbconf = shift;
+
+    my $dbconf = InstallUtils::readJson($databaseConfFile);
+
+    # Check if the Postgres db is used and set the admin database to be "postgres"
+    my $dbName = $dbconf->{type};
+    if ( $dbconf->{type} eq "Pg" ) {
+        $dbName = "traffic_ops";
+    }
+
+    $ENV{PGUSER}     = $todbconf->{"pgUser"};
+    $ENV{PGPASSWORD} = $todbconf->{"pgPassword"};
+
+    my $dsn = sprintf( "DBI:%s:db=%s;host=%s;port=%d", $dbconf->{type}, $dbName, $dbconf->{hostname}, $dbconf->{port} );
+    my $dbh = DBI->connect( $dsn, $todbconf->{"pgUser"}, $todbconf->{"pgPassword"} );
+    if ($dbh) {
+        InstallUtils::logger( "Database connection succeeded", "info" );
+    }
+    else {
+        InstallUtils::logger( "Error connecting to database", "error" );
+        exit(-1);
+    }
+   return $dbh;
+}
+
+
+1;