File: setup.pl

package info (click to toggle)
blootbot 1.2.0-6.3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,368 kB
  • ctags: 603
  • sloc: perl: 16,607; sh: 147; makefile: 54; sql: 47
file content (103 lines) | stat: -rwxr-xr-x 3,390 bytes parent folder | download | duplicates (2)
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
#!/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 "") {
	&ERROR("error: adminuser is blank.");
	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("Database '$param{DBName}' already exists. Continuing...");
    } else {
	&status("Creating db ...");
	&dbRaw("create(database)", "CREATE DATABASE $param{DBName}");
    }

    &status("--- Adding user $param{'SQLUser'} privileges on database $param{DBName}...");

    $query = "GRANT ALTER, CREATE, DELETE, DROP, INSERT, SELECT, UPDATE " .
      "ON $param{DBName}.* TO $param{SQLUser} IDENTIFIED BY '$param{SQLPass}'";
    &dbRaw("create(user)", $query);

    &status("--- Adding user $param{'SQLUser'}\@localhost privileges on database $param{DBName}...");
    $query = "GRANT ALTER, CREATE, DELETE, DROP, INSERT, SELECT, UPDATE " .
      "ON $param{DBName}.* TO $param{SQLUser}\@localhost IDENTIFIED BY '$param{SQLPass}'";

    &dbRaw("create(user)", $query);

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

&status("Done.");

&closeDB();