Checking in changes prior to tagging of version 2.73.
[MogileFS-Server.git] / mogdbsetup
blob00b58a62854e4e6a4c5860324f941dcc084a08f5
1 #!/usr/bin/perl
2 use strict;
3 use Getopt::Long;
4 use lib 'lib';
5 use MogileFS::Store;
6 use MogileFS::Config;
8 # Rename binary in process list to make init scripts saner
9 $0 = $_ = $0;
11 my %args = (
12 dbhost => "localhost",
13 dbport => undef,
14 dbname => "mogilefs",
15 dbrootuser => undef,
16 dbrootpass => "",
17 dbuser => "mogile",
18 dbpass => "",
21 my $opt_help;
22 my $opt_verbose = 0;
23 my $opt_yes = 0;
24 my $opt_noschemabump;
25 my $dbtype = "MySQL";
26 my $plugins;
28 usage()
29 unless GetOptions(
30 "dbhost=s" => \$args{dbhost},
31 "dbport=s" => \$args{dbport},
32 "dbname=s" => \$args{dbname},
33 "dbrootuser=s" => \$args{dbrootuser},
34 "dbrootpassword:s" => \$args{dbrootpass},
35 "dbuser=s" => \$args{dbuser},
36 "dbpassword:s" => \$args{dbpass},
37 "help" => \$opt_help,
38 "verbose" => \$opt_verbose,
39 "yes" => \$opt_yes,
40 "noschemabump" => \$opt_noschemabump,
41 "type=s" => \$dbtype,
42 "plugins=s" => \$plugins,
45 usage() if $opt_help;
47 # Be nice about what the default admin user is called.
48 if(!defined($args{dbrootuser})) {
49 $args{dbrootuser} = 'root' if $dbtype =~ /MySQL/i;
50 $args{dbrootuser} = 'postgres' if $dbtype =~ /Postgres/i;
52 # Saner port management.
53 # This should default to the UNIX sockets on localhost
54 if(!defined($args{dbport}) and $args{dbhost} != "localhost" and $args{dbhost} != "127.0.0.1") {
55 $args{dbport} = '3306' if $dbtype =~ /MySQL/i;
56 $args{dbport} = '5432' if $dbtype =~ /Postgres/i;
59 sub usage {
60 die <<USAGE;
61 Usage: mogdbsetup [opts]
63 Options:
65 Default Description
66 ============ ===========================================
67 --verbose <off> Be verbose about what\'s happening.
69 --dbhost= localhost hostname or IP to database server.
71 --dbport= dbd default port number to database server.
73 --dbname= mogilefs database name to create/upgrade.
75 --dbrootuser= root Database administrator username. Only needed
76 for initial setup, not subsequent upgrades.
78 --dbrootpass= <blank> Database administrator password. Only needed
79 for initial setup, not subsequent upgrades.
81 --dbuser= mogile Regular database user to create and/or use
82 for MogileFS database. This is what the
83 mogilefsd trackers connect as.
85 --dbpass= <blank> You should change this, especially if your
86 database servers are accessible to other users
87 on the network. But they shouldn't be
88 if you're running MogileFS, because MogileFS
89 assumes your network is closed.
91 --type= MySQL Which MogileFS::Store implementation to use.
92 Available: MySQL, Postgres
94 --yes Run without questions.
96 USAGE
99 my $sclass = "MogileFS::Store::$dbtype";
100 eval "use $sclass; 1;" or die "Failed to load $sclass: $@";
102 foreach my $plugin (split /\s*,\s*/, $plugins) {
103 eval "use MogileFS::Plugin::$plugin; 1;" or die "Failed to load plugin $plugin: $@";
106 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);
108 $sclass->on_status(\&status);
109 $sclass->on_confirm(\&confirm);
111 MogileFS::Config->load_config;
113 my $sto = $sclass->new_from_mogdbsetup(
114 map { $_ => $args{$_} }
115 qw(dbhost dbport dbname
116 dbrootuser dbrootpass
117 dbuser dbpass)
119 my $dbh = $sto->dbh;
121 $sto->setup_database
122 or die "Database upgrade failed.\n";
124 my $latestver = MogileFS::Store->latest_schema_version;
125 if ($opt_noschemabump) {
126 warn "\n*\n* Per your request, NOT UPGRADING to $latestver. I assume you understand why.\n*\n";
127 } else {
128 $sto->set_schema_vesion($latestver);
132 warn "Done.\n" if $opt_verbose;
133 exit 0;
135 ############################################################################
137 sub confirm {
138 my $q = shift;
139 my $def = shift;
140 $def = 1 unless defined $def;
142 return 1 if $opt_yes;
143 my $deftext = $def ? "[Y/n]" : "[N/y]";
145 print "\n$q $deftext: ";
146 my $ans = <STDIN>;
147 if ($ans =~ /^\s*$/) {
148 die "Stopped.\n" unless $def;
149 return 0;
151 return 1 if $ans =~ /^y/i;
152 die "Stopped.\n";
155 sub status {
156 warn "$_[0]\n" if $opt_verbose;