Add camdbinit tool, convert from Perl to Go.

This commit is contained in:
Brad Fitzpatrick 2011-05-01 18:21:22 -07:00
parent 9f486934f5
commit cc12a8788b
4 changed files with 136 additions and 59 deletions

View File

@ -391,6 +391,7 @@ sub read_targets {
__DATA__ __DATA__
TARGET: clients/go/camdbinit
TARGET: clients/go/camget TARGET: clients/go/camget
TARGET: clients/go/camput TARGET: clients/go/camput
TARGET: clients/go/cammount TARGET: clients/go/cammount

1
clients/go/camdbinit/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
camdbinit

View File

@ -0,0 +1,121 @@
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"flag"
"os"
"strings"
mysql "camli/third_party/github.com/Philio/GoMySQL"
)
var flagUser = flag.String("user", "root", "MySQL admin user")
var flagPassword = flag.String("password", "(prompt)", "MySQL admin password")
var flagHost = flag.String("host", "localhost", "MySQ host[:port]")
var flagDatabase = flag.String("database", "", "MySQL camlistore to wipe/create database")
var flagWipe = flag.Bool("wipe", false, "Wipe the database and re-create it?")
var flagIgnore = flag.Bool("ignoreexists", false, "Treat existence of the database as okay and exit.")
func main() {
flag.Parse()
if *flagDatabase == "" {
exitf("--database flag required")
}
db, err := mysql.DialTCP(*flagHost, *flagUser, *flagPassword, "")
if err != nil {
exitf("Error connecting to database: %v", err)
}
dbname := *flagDatabase
exists := dbExists(db, dbname)
if exists {
if *flagIgnore {
return
}
if !*flagWipe {
exitf("Databases %q already exists, but --wipe not given. Stopping.", dbname)
}
do(db, "DROP DATABASE "+dbname)
}
do(db, "CREATE DATABASE "+dbname)
do(db, "USE "+dbname)
do(db, `CREATE TABLE blobs (
blobref VARCHAR(128) NOT NULL PRIMARY KEY,
size INTEGER NOT NULL,
type VARCHAR(100))`)
do(db, `CREATE TABLE claims (
blobref VARCHAR(128) NOT NULL PRIMARY KEY,
signer VARCHAR(128) NOT NULL,
date VARCHAR(40) NOT NULL,
INDEX (signer, date),
unverified CHAR(1) NULL,
claim VARCHAR(50) NOT NULL,
permanode VARCHAR(128) NOT NULL,
INDEX (permanode, signer, date),
attr VARCHAR(128) NULL,
value VARCHAR(128) NULL)`)
do(db, `CREATE TABLE permanodes (
blobref VARCHAR(128) NOT NULL PRIMARY KEY,
unverified CHAR(1) NULL,
signer VARCHAR(128) NOT NULL DEFAULT '',
lastmod VARCHAR(40) NOT NULL DEFAULT '',
INDEX (signer, lastmod))`)
}
func do(db *mysql.Client, sql string) {
err := db.Query(sql)
if err == nil {
return
}
exitf("Error %v running SQL: %s", err, sql)
}
func dbExists(db *mysql.Client, dbname string) bool {
check(db.Query("SHOW DATABASES"))
result, err := db.UseResult()
check(err)
defer result.Free()
for {
row := result.FetchRow()
if row == nil {
break
}
if row[0].(string) == dbname {
return true
}
}
return false
}
func check(err os.Error) {
if err == nil {
return
}
panic(err)
}
func exitf(format string, args ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(1)
}

View File

@ -3,7 +3,6 @@
use strict; use strict;
use FindBin qw($Bin); use FindBin qw($Bin);
use Getopt::Long; use Getopt::Long;
use DBI;
sub usage { sub usage {
die "Usage: dev-indexer [--wipe] <portnumber> -- [other_blobserver_opts]"; die "Usage: dev-indexer [--wipe] <portnumber> -- [other_blobserver_opts]";
@ -16,69 +15,24 @@ GetOptions("wipe" => \$opt_wipe)
my $port = shift || "3200"; my $port = shift || "3200";
usage() unless $port =~ /^\d+$/; usage() unless $port =~ /^\d+$/;
system("./build.pl", "server/go/camlistored") and die "Failed to build.\n"; system("./build.pl", "server/go/camlistored") and die "Failed to build camlistored";
system("./build.pl", "clients/go/camdbinit") and die "Failed to build camdbinit";
# NOTE: Root required but not used by indexer blobserver
my $root = "/tmp/camliroot/port$port/";
if ($opt_wipe && -d $root) {
print "Wiping $root\n";
system("rm", "-rf", $root) and die "Failed to wipe $root.\n";
}
unless (-d $root) {
system("mkdir", "-p", $root) and die "Failed to create $root.\n";
}
my $DBNAME = "devcamlistore"; my $DBNAME = "devcamlistore";
my @opts;
my $dbh = DBI->connect("DBI:mysql:database=mysql", "root", "root") or
die "Failed to connect to MySQL on localhost with user/pass of root/root.\n";
my $camdb = DBI->connect("DBI:mysql:database=$DBNAME", "root", "root", {
PrintWarn => 0,
PrintError => 0,
});
unless ($camdb) {
print "Database '$DBNAME' doesn't exist; assuming --wipe and initializing.\n";
$opt_wipe = 1;
}
if ($opt_wipe) { if ($opt_wipe) {
system("mysqladmin", "-uroot", "-proot", "--force", "drop", "$DBNAME"); push @opts, "-wipe";
system("mysqladmin", "-uroot", "-proot", "create", "$DBNAME") and die "Could not create MySQL database on localhost.\n"; } else {
my $dbh = DBI->connect("DBI:mysql:database=$DBNAME", "root", "root", { push @opts, "-ignoreexists";
"RaiseError" => 1,
}) or die "Failed to connect to MySQL on localhost.\n";
# Begin schema creation.
$dbh->do("CREATE TABLE blobs (".
"blobref VARCHAR(128) NOT NULL PRIMARY KEY, ".
"size INTEGER NOT NULL, ".
"type VARCHAR(100)".
")");
$dbh->do("CREATE TABLE claims (".
"blobref VARCHAR(128) NOT NULL PRIMARY KEY, ".
"signer VARCHAR(128) NOT NULL, ".
"date VARCHAR(40) NOT NULL, ".
"INDEX (signer, date), ".
"unverified CHAR(1) NULL, ".
"claim VARCHAR(50) NOT NULL, ".
"permanode VARCHAR(128) NOT NULL, ".
"INDEX (permanode, signer, date), ".
"attr VARCHAR(128) NULL, ".
"value VARCHAR(128) NULL ".
")");
$dbh->do("CREATE TABLE permanodes (".
"blobref VARCHAR(128) NOT NULL PRIMARY KEY, ".
"unverified CHAR(1) NULL, ".
"signer VARCHAR(128) NOT NULL DEFAULT '', ".
"lastmod VARCHAR(40) NOT NULL DEFAULT '', ".
"INDEX (signer, lastmod) ".
")");
} }
system("./clients/go/camdbinit/camdbinit",
"-user=root",
"-password=root",
"-host=localhost",
"-database=$DBNAME",
@opts) and die "Failed to run camdbinit.\n";
print "Starting indexer with indexer on http://localhost:$port/indexer/\n"; print "Starting indexer with indexer on http://localhost:$port/indexer/\n";
$ENV{CAMLI_PASSWORD} = "pass$port"; $ENV{CAMLI_PASSWORD} = "pass$port";