root/honeyclient/tags/exp/DOWN1-mbriggs-db/bin/install_honeyclient_db.pl

Revision 500, 2.4 kB (checked in by kindlund, 2 years ago)

DB integration testing.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl -w
2 use strict;
3
4 use DBI;
5 use HoneyClient::Util::Config qw(getVar);
6
7 # Retrieve values from HoneyClient config file
8 my $host = getVar(name => "host", namespace => "HoneyClient::DB");
9 my $user = getVar(name => "user", namespace => "HoneyClient::DB");
10 my $pass = getVar(name => "pass", namespace => "HoneyClient::DB");
11 my $database_name = getVar(name => "dbname", namespace => "HoneyClient::DB");
12
13 print "Attempting to Create a HoneyClient database with the name: ".
14     "${database_name}\n";
15 my $r_pass = retrieve_pw();
16
17 my $dsn = "DBI:mysql:database=mysql;host=".$host;
18
19 eval {
20     # Connect and Create Database
21     my $dbh = DBI->connect($dsn,'root',$r_pass,{'RaiseError' => 1});
22     Carp::croak "Connect Failed: $DBI::errstr" if ($dbh eq '');
23    
24     $dbh->do("CREATE DATABASE ".$database_name);
25    
26     my $mgr_address = (get_mgr_addr() or '127.0.0.1');
27    
28     # Create A User Account to Access and Manage Database
29     print "Attempting to Create database user: ${user}\n";
30     $dbh->do("GRANT ALL PRIVILEGES ON ".$database_name.".* TO '".$user."\'@\'".
31         $mgr_address."' IDENTIFIED BY '".$pass."'");
32        
33     $dbh->disconnect() if $dbh;
34 };
35 if ($@) {
36     die "Failed to initialize Database Connection:\n\t$@";
37 }
38
39 print "Database and user installed successfully.\n";
40
41 # Retrieve a password from the user while hiding it from the display
42 sub retrieve_pw {
43     my $p;
44     print "Please enter your database 'root' password: ";
45     system("stty -echo");
46     chomp($p=<STDIN>);
47     print "\n";
48     system("stty echo");
49     return $p;
50 }
51
52 my $re_ip_num = qr{([01]?\d\d?|2[0-4]\d|25[0-5]|%)};
53 my $re_ip = qr{^$re_ip_num\.$re_ip_num\.$re_ip_num\.$re_ip_num$};
54
55 sub get_mgr_addr {
56     print "Will the database and the manager run on the same system? [yes] ";
57     while(1) {
58         my $in;
59         chomp($in = <STDIN>);
60         if ($in eq "" or $in eq "yes") {
61             return "";
62         }
63         elsif ($in eq "no") {
64             print "Enter the address the manager will connect from.\n".
65                 "(wildcard is %): ";
66             while (1) {
67                 my $addr;
68                 chomp($addr = <STDIN>);
69                 print "\n";
70                 # TODO: Need to check for a valid IP address/netmask.
71                 #if ($addr =~ /$re_ip/) {
72                     return $addr;
73                 #}
74                 print "Invalid Entry. Enter the manager address: ";
75             }
76         }
77         else {
78             print "Invalid Entry. (type yes or no): ";
79         }
80     }
81 }
Note: See TracBrowser for help on using the browser.