-
Notifications
You must be signed in to change notification settings - Fork 109
/
mogdbsetup
executable file
·159 lines (123 loc) · 5 KB
/
mogdbsetup
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl
use strict;
use Getopt::Long;
use lib 'lib';
use MogileFS::Store;
use MogileFS::Config;
# Rename binary in process list to make init scripts saner
$0 = $_ = $0;
my %args = (
dbhost => "localhost",
dbport => undef,
dbname => "mogilefs",
dbrootuser => undef,
dbrootpass => "",
dbuser => "mogile",
dbpass => "",
);
my $opt_help;
my $opt_verbose = 0;
my $opt_yes = 0;
my $opt_noschemabump;
my $dbtype = "MySQL";
my $plugins;
usage()
unless GetOptions(
"dbhost=s" => \$args{dbhost},
"dbport=s" => \$args{dbport},
"dbname=s" => \$args{dbname},
"dbrootuser=s" => \$args{dbrootuser},
"dbrootpassword:s" => \$args{dbrootpass},
"dbuser=s" => \$args{dbuser},
"dbpassword:s" => \$args{dbpass},
"help" => \$opt_help,
"verbose" => \$opt_verbose,
"yes" => \$opt_yes,
"noschemabump" => \$opt_noschemabump,
"type=s" => \$dbtype,
"plugins=s" => \$plugins,
);
usage() if $opt_help;
# Be nice about what the default admin user is called.
if(!defined($args{dbrootuser})) {
$args{dbrootuser} = 'root' if $dbtype =~ /MySQL/i;
$args{dbrootuser} = 'postgres' if $dbtype =~ /Postgres/i;
}
# Saner port management.
# This should default to the UNIX sockets on localhost
if(!defined($args{dbport}) and $args{dbhost} != "localhost" and $args{dbhost} != "127.0.0.1") {
$args{dbport} = '3306' if $dbtype =~ /MySQL/i;
$args{dbport} = '5432' if $dbtype =~ /Postgres/i;
}
sub usage {
die <<USAGE;
Usage: mogdbsetup [opts]
Options:
Default Description
============ ===========================================
--verbose <off> Be verbose about what\'s happening.
--dbhost= localhost hostname or IP to database server.
--dbport= dbd default port number to database server.
--dbname= mogilefs database name to create/upgrade.
--dbrootuser= root Database administrator username. Only needed
for initial setup, not subsequent upgrades.
--dbrootpass= <blank> Database administrator password. Only needed
for initial setup, not subsequent upgrades.
--dbuser= mogile Regular database user to create and/or use
for MogileFS database. This is what the
mogilefsd trackers connect as.
--dbpass= <blank> You should change this, especially if your
database servers are accessible to other users
on the network. But they shouldn't be
if you're running MogileFS, because MogileFS
assumes your network is closed.
--type= MySQL Which MogileFS::Store implementation to use.
Available: MySQL, Postgres
--yes Run without questions.
USAGE
}
my $sclass = "MogileFS::Store::$dbtype";
eval "use $sclass; 1;" or die "Failed to load $sclass: $@";
foreach my $plugin (split /\s*,\s*/, $plugins) {
eval "use MogileFS::Plugin::$plugin; 1;" or die "Failed to load plugin $plugin: $@";
}
confirm("This will attempt to setup or upgrade your MogileFS database.\nIt won't destroy existing data.\nRun with --help for more information. Run with --yes to shut up these prompts.\n\nContinue?", 0);
$sclass->on_status(\&status);
$sclass->on_confirm(\&confirm);
MogileFS::Config->load_config;
my $sto = $sclass->new_from_mogdbsetup(
map { $_ => $args{$_} }
qw(dbhost dbport dbname
dbrootuser dbrootpass
dbuser dbpass)
);
my $dbh = $sto->dbh;
$sto->setup_database
or die "Database upgrade failed.\n";
my $latestver = MogileFS::Store->latest_schema_version;
if ($opt_noschemabump) {
warn "\n*\n* Per your request, NOT UPGRADING to $latestver. I assume you understand why.\n*\n";
} else {
$sto->set_schema_vesion($latestver);
}
warn "Done.\n" if $opt_verbose;
exit 0;
############################################################################
sub confirm {
my $q = shift;
my $def = shift;
$def = 1 unless defined $def;
return 1 if $opt_yes;
my $deftext = $def ? "[Y/n]" : "[N/y]";
print "\n$q $deftext: ";
my $ans = <STDIN>;
if ($ans =~ /^\s*$/) {
die "Stopped.\n" unless $def;
return 0;
}
return 1 if $ans =~ /^y/i;
die "Stopped.\n";
}
sub status {
warn "$_[0]\n" if $opt_verbose;
}