add new worker: job_master
[MogileFS-Server.git] / mogdbsetup
blobde9b73c6fdf5ff104152474ca38d4555077a6fc5
1 #!/usr/bin/perl
2 use strict;
3 use Getopt::Long;
4 use lib 'lib';
5 use MogileFS::Store;
7 # Rename binary in process list to make init scripts saner
8 $0 = $_ = $0;
10 my %args = (
11 dbhost => "localhost",
12 dbport => undef,
13 dbname => "mogilefs",
14 dbrootuser => undef,
15 dbrootpass => "",
16 dbuser => "mogile",
17 dbpass => "",
20 my $opt_help;
21 my $opt_verbose = 0;
22 my $opt_yes = 0;
23 my $opt_noschemabump;
24 my $dbtype = "MySQL";
25 my $plugins;
27 usage()
28 unless GetOptions(
29 "dbhost=s" => \$args{dbhost},
30 "dbport=s" => \$args{dbport},
31 "dbname=s" => \$args{dbname},
32 "dbrootuser=s" => \$args{dbrootuser},
33 "dbrootpassword=s" => \$args{dbrootpass},
34 "dbuser=s" => \$args{dbuser},
35 "dbpassword=s" => \$args{dbpass},
36 "help" => \$opt_help,
37 "verbose" => \$opt_verbose,
38 "yes" => \$opt_yes,
39 "noschemabump" => \$opt_noschemabump,
40 "type=s" => \$dbtype,
41 "plugins=s" => \$plugins,
44 usage() if $opt_help;
46 # Be nice about what the default admin user is called.
47 if(!defined($args{dbrootuser})) {
48 $args{dbrootuser} = 'root' if $dbtype =~ /MySQL/i;
49 $args{dbrootuser} = 'postgres' if $dbtype =~ /Postgres/i;
51 # Saner port management.
52 # This should default to the UNIX sockets on localhost
53 if(!defined($args{dbport}) and $args{dbhost} != "localhost" and $args{dbhost} != "127.0.0.1") {
54 $args{dbport} = '3306' if $dbtype =~ /MySQL/i;
55 $args{dbport} = '5432' if $dbtype =~ /Postgres/i;
58 sub usage {
59 die <<USAGE;
60 Usage: mogdbsetup [opts]
62 Options:
64 Default Description
65 ============ ===========================================
66 --verbose <off> Be verbose about what\'s happening.
68 --dbhost= localhost hostname or IP to database server.
70 --dbport= dbd default port number to database server.
72 --dbname= mogilefs database name to create/upgrade.
74 --dbrootuser= root Database administrator username. Only needed
75 for initial setup, not subsequent upgrades.
77 --dbrootpass= <blank> Database administrator password. Only needed
78 for initial setup, not subsequent upgrades.
80 --dbuser= mogile Regular database user to create and/or use
81 for MogileFS database. This is what the
82 mogilefsd trackers connect as.
84 --dbpass= <blank> You should change this, especially if your
85 database servers are accessible to other users
86 on the network. But they shouldn't be
87 if you're running MogileFS, because MogileFS
88 assumes your network is closed.
90 --type= MySQL Which MogileFS::Store implementation to use.
91 Available: MySQL, Postgres
93 --yes Run without questions.
95 USAGE
98 my $sclass = "MogileFS::Store::$dbtype";
99 eval "use $sclass; 1;" or die "Failed to load $sclass: $@";
101 foreach my $plugin (split /\s*,\s*/, $plugins) {
102 eval "use MogileFS::Plugin::$plugin; 1;" or die "Failed to load plugin $plugin: $@";
105 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);
107 $sclass->on_status(\&status);
108 $sclass->on_confirm(\&confirm);
110 my $sto = $sclass->new_from_mogdbsetup(
111 map { $_ => $args{$_} }
112 qw(dbhost dbport dbname
113 dbrootuser dbrootpass
114 dbuser dbpass)
116 my $dbh = $sto->dbh;
118 $sto->setup_database
119 or die "Database upgrade failed.\n";
121 my $latestver = MogileFS::Store->latest_schema_version;
122 if ($opt_noschemabump) {
123 warn "\n*\n* Per your request, NOT UPGRADING to $latestver. I assume you understand why.\n*\n";
124 } else {
125 $sto->set_schema_vesion($latestver);
129 warn "Done.\n" if $opt_verbose;
130 exit 0;
132 ############################################################################
134 sub confirm {
135 my $q = shift;
136 my $def = shift;
137 $def = 1 unless defined $def;
139 return 1 if $opt_yes;
140 my $deftext = $def ? "[Y/n]" : "[N/y]";
142 print "\n$q $deftext: ";
143 my $ans = <STDIN>;
144 if ($ans =~ /^\s*$/) {
145 die "Stopped.\n" unless $def;
146 return 0;
148 return 1 if $ans =~ /^y/i;
149 die "Stopped.\n";
152 sub status {
153 warn "$_[0]\n" if $opt_verbose;