From cc12a8788b72c0731b2f79a0ebfaf813c64821e6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 1 May 2011 18:21:22 -0700 Subject: [PATCH] Add camdbinit tool, convert from Perl to Go. --- build.pl | 1 + clients/go/camdbinit/.gitignore | 1 + clients/go/camdbinit/camdbinit.go | 121 ++++++++++++++++++++++++++++++ dev-indexer | 72 ++++-------------- 4 files changed, 136 insertions(+), 59 deletions(-) create mode 100644 clients/go/camdbinit/.gitignore create mode 100644 clients/go/camdbinit/camdbinit.go diff --git a/build.pl b/build.pl index 01cc15a97..09c86b857 100755 --- a/build.pl +++ b/build.pl @@ -391,6 +391,7 @@ sub read_targets { __DATA__ +TARGET: clients/go/camdbinit TARGET: clients/go/camget TARGET: clients/go/camput TARGET: clients/go/cammount diff --git a/clients/go/camdbinit/.gitignore b/clients/go/camdbinit/.gitignore new file mode 100644 index 000000000..d1d06a264 --- /dev/null +++ b/clients/go/camdbinit/.gitignore @@ -0,0 +1 @@ +camdbinit diff --git a/clients/go/camdbinit/camdbinit.go b/clients/go/camdbinit/camdbinit.go new file mode 100644 index 000000000..62973d9d6 --- /dev/null +++ b/clients/go/camdbinit/camdbinit.go @@ -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) +} diff --git a/dev-indexer b/dev-indexer index a31e6bbf3..b1b8ef425 100755 --- a/dev-indexer +++ b/dev-indexer @@ -3,7 +3,6 @@ use strict; use FindBin qw($Bin); use Getopt::Long; -use DBI; sub usage { die "Usage: dev-indexer [--wipe] -- [other_blobserver_opts]"; @@ -16,69 +15,24 @@ GetOptions("wipe" => \$opt_wipe) my $port = shift || "3200"; usage() unless $port =~ /^\d+$/; -system("./build.pl", "server/go/camlistored") and die "Failed to build.\n"; - -# 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"; -} - +system("./build.pl", "server/go/camlistored") and die "Failed to build camlistored"; +system("./build.pl", "clients/go/camdbinit") and die "Failed to build camdbinit"; my $DBNAME = "devcamlistore"; - -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; -} - +my @opts; if ($opt_wipe) { - system("mysqladmin", "-uroot", "-proot", "--force", "drop", "$DBNAME"); - system("mysqladmin", "-uroot", "-proot", "create", "$DBNAME") and die "Could not create MySQL database on localhost.\n"; - my $dbh = DBI->connect("DBI:mysql:database=$DBNAME", "root", "root", { - "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) ". - ")"); + push @opts, "-wipe"; +} else { + push @opts, "-ignoreexists"; } +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"; $ENV{CAMLI_PASSWORD} = "pass$port";