File: setup.pl

package info (click to toggle)
blootbot 1.1.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,420 kB
  • ctags: 586
  • sloc: perl: 15,941; sh: 154; makefile: 56; sql: 45
file content (121 lines) | stat: -rwxr-xr-x 3,664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl
# setup_tables: setup MYSQL/PGSQL side of things for blootbot.
# written by the xk.
###

$bot_config_dir	= '/etc/blootbot';

require "$bot_config_dir/directories";
my $bot_base_dir = $bot_state_dir;

require "$bot_src_dir/core.pl";
require "$bot_src_dir/logger.pl";
require "$bot_src_dir/modules.pl";
require "$bot_src_dir/Misc.pl";
require "$bot_src_dir/interface.pl";

# read param stuff from blootbot.config.
&loadConfig("$bot_config_dir/blootbot.config");
&loadDBModules();
my $dbname = $param{'DBName'};
my $query;

if ($dbname eq "") {
  print "error: appears that the config file was not loaded properly.\n";
  exit 1;
}

if ($param{'DBType'} =~ /mysql/i) {
    use DBI;

    # OK, can I find a mysql server running?
#    {
#      my $rc = system("mysqlshow --host=$param{SQLHost} >/dev/null 2>&1") >> 8;
#      die "Couldn't connect to SQL server" if $rc;
#    }
    # Well, we have a mysql server. Let's take a crack at connecting normally,
    #  maybe the work is already done?
    &status("Trying to see if we have already done this stuff...");
    if (&openDB($param{DBName}, $param{SQLUser}, $param{SQLPass}, 1)) {
      &closeDB();
      &status("I was able to connect to the database, stopping processing now");
      print "It appears that you already have a database set up.\n";
      print "(This script is not capable of fixing a broken database)\n";
      print "If you need to recreate the database, either delete the existing one,\n";
      print " or dpkg-reconfigure blootbot and give a new database name\n";
      exit 0;
    }
    &status("Could not connect to database $param{DBName}, continuing");

    print "Please enter user information for root access to the mysql database.\n";
    print "The username is probably 'root', the password you should have set\n";
    print "yourself. If you haven't set one yet, leave it blank, and read\n";
    print "/usr/share/doc/mysql-server/README.Debian\n";
    # username.
    print "Username: ";
    chop (my $adminuser = <STDIN>);

    # passwd.
    system "stty -echo";
    print "Password: ";
    chop(my $adminpass = <STDIN>);
    print "\n";
    system "stty echo";

    if ($adminuser eq "" or $adminpass eq "") {
	&ERROR("error: adminuser || adminpass is NULL.");
	exit 1;
    }

    &openDB("mysql", $adminuser, $adminpass);

    my $database_exists = 0;
    foreach $database (&dbRawReturn("SHOW DATABASES")) {
	$database_exists++ if $database eq $param{DBName};
    }
    if ($database_exists) {
	&status("Hmm, database '$param{DBName}' already exists. Continuing...");
    } else {
	&status("Creating db ...");
	&dbRaw("create(database)", "CREATE DATABASE $param{DBName}");
    }

    &status("--- Adding user information.");

    if (!&dbGet("user","user", "user=".&dbQuote($param{'SQLUser'}) ) ) {
	&status("--- Adding user $param{'SQLUser'} $dbname/user table...");

	$query = "INSERT INTO user VALUES ".
		"('localhost', '$param{'SQLUser'}', ".
		"password('$param{'SQLPass'}'), ";

	$query .= "'Y','Y','Y','Y','Y','Y','N','N','N','N','N','N','N','N')";

	&dbRaw("create(user)", $query);
    } else {
	&status("... User information already present.");
    }

    if (!&dbGet("db","db","db=".&dbQuote($param{'SQLUser'}) ) ) {
	&status("--- Adding 'db' stuff.");

	$query = "INSERT INTO db VALUES ".
		"('localhost', '$dbname', ".
		"'$param{'SQLUser'}', ";

	$query .= "'Y','Y','Y','Y','Y','Y','Y','N','N','N')";

	&dbRaw("create(db)", $query);
    } else {
	&status("... db info already present.");
    }

    # flush.
    &status("Flushing privileges...");
    $query = "FLUSH PRIVILEGES";
    &dbRaw("mysql(flush)", $query);
}

&status("Done.");

&closeDB();