From: Frank Brehm Date: Wed, 8 Aug 2007 14:30:00 +0000 (+0000) Subject: Bis zum Anlegen der ersten Zieltabelle gekommen X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=df5050b42b8f4f4dedb8f024d6f86da9b7eb104e;p=cookbook.git Bis zum Anlegen der ersten Zieltabelle gekommen git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@8 191103c4-1d37-0410-b3e5-d8c2315c0aac --- diff --git a/cookbook.yml b/cookbook.yml index 8409f10..c4f0b7d 100644 --- a/cookbook.yml +++ b/cookbook.yml @@ -6,6 +6,27 @@ database: schema: cookbook user: cookbook # passwd: +authentication: + dbic: + # Note this first definition would be the same as setting + # __PACKAGE__->config->{authentication}->{dbic}->{user_class} = ’MyAppDB::User’ + # in lib/MyApp.pm (IOW, each hash key becomes a "name:" in the YAML file). + # + # This is the model object created by Catalyst::Model::DBIC from your + # schema (you created ’MyAppDB::User’ but as the Catalyst startup + # debug messages show, it was loaded as ’MyApp::Model::MyAppDB::User’). + # NOTE: Omit ’MyApp::Model’ to avoid a component lookup issue in Catalyst 5.66 + user_class: CookBookModel::User + # + # This is the name of the field in your ’users’ table that contains the user’s name + user_field: login + # + # This is the name of the field in your ’users’ table that contains the password + password_field: password + # + # Other options can go here for hashed passwords + password_type: salted_hash + password_salt_len: 4 kreceipes-database: host: localhost port: 3306 diff --git a/sbin/initial_import.pl b/sbin/initial_import.pl index fc693ae..2b7aaba 100755 --- a/sbin/initial_import.pl +++ b/sbin/initial_import.pl @@ -10,6 +10,9 @@ use File::Spec; use Cwd 'abs_path'; use YAML 'LoadFile'; use Data::Dumper; +use DBI; +use Term::ANSIColor; +use Crypt::SaltedHash; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1; @@ -22,10 +25,24 @@ use CookBook::Common; ################## +$| = 1; + +my @target_tables = qw( + users +); + +my $admin_data = { + 'login' => 'frank', + 'vorname' => 'Frank', + 'nachname' => 'Brehm', + 'email' => 'frank@brehm-online.com', + 'passwd' => 'uhu', +}; + my $debug = $ENV{'COOKBOOK_DEBUG'} ? 1 : 0; my $home_dir = abs_path( File::Spec->canonpath( $FindBin::Bin . "/.." ) ); -print "Home-Dir: '$home_dir'\n"; +print "Home-Dir: '$home_dir'\n" if $debug; my $global_conf_file = $home_dir . "/cookbook.yml"; my $local_conf_file = $home_dir . "/cookbook_local.yml"; @@ -39,31 +56,32 @@ unless ( -f $global_conf_file ) { } # Konfiguration einlesen ... -print "Lese globale Konfiguration '$global_conf_file' ein ... "; +print green_star() . " Lese globale Konfiguration '$global_conf_file' ein ... "; eval { ($global_conf) = LoadFile($global_conf_file); }; if ($@) { - print "NOT OK\n"; + print not_ok() . "\n"; warn $@ . "\n"; exit 5; } -print "OK\n"; +print ok() . "\n"; print "Globale Konfiguration: " . Dumper($global_conf) if $debug; if ( -f $local_conf_file ) { - print "Lese lokale Konfiguration '$local_conf_file' ein ... "; + print green_star() . " Lese lokale Konfiguration '$local_conf_file' ein ... "; eval { ($local_conf) = LoadFile($local_conf_file); }; if ($@) { - print "NOT OK\n"; + print not_ok() . "\n"; warn $@ . "\n"; exit 5; } - print "OK\n"; + print ok() . "\n"; } ## end if ( -f $local_conf_file ) print "Lokale Konfiguration: " . Dumper($local_conf) if $debug; # DSN's und Nutzerangaben ermitteln: my ( $target_dsn, $target_user, $target_passwd, $source_dsn, $source_user, $source_passwd ); +my ( $target_dbh, $target_sth, $source_dbh, $source_sth ); my $port; @@ -105,6 +123,293 @@ if ($debug) { printf "Ziel-Datenbank: DSN='%s', User='%s', Pwd='%s'\n", $target_dsn, $target_user, $target_passwd; } +END { + close_dbs(); +} + +print "\n"; +open_dbs(); print "\n"; +exit 7 unless drop_target_tables(); +exit 8 unless create_target_tables(); + exit 0; + +############################################################### +# + +=head1 Funktionen + +Alle hier verwendeten Funktionen ... + +=cut + +#------------------------------------------------------------- + +=head2 close_dbs( ) + +Schließt alle Datenbank-Verbindungen. + +=cut + +sub close_dbs { + + if ( $target_dbh and $target_dbh->{'Active'} ) { + print green_star() . " Schließe Ziel-Datenbank ... "; + $target_dbh->disconnect(); + $target_dbh = undef; + print ok() . "\n"; + } + + if ( $source_dbh and $source_dbh->{'Active'} ) { + print green_star() . " Schließe Quell-Datenbank ... "; + $source_dbh->disconnect(); + $source_dbh = undef; + print ok() . "\n"; + } + + print "\n"; + +} ## end sub close_dbs + +#------------------------------------------------------------- + +=head2 open_dbs( ) + +Oeffnen der beiden Datenbanken und Speicher in $source_dbh und $target_dbh + +Gibt zurueck, ob erfolgreich oder nicht ... + +=cut + +sub open_dbs { + + my $attribs = {}; + $attribs->{'RaiseError'} = 0; + $attribs->{'AutoCommit'} = 1; + + if ( $source_dbh and $source_dbh->{'Active'} ) { + warn warning() . " Quell-Datenbank ist bereits geöffnet.\n"; + } + else { + print green_star() . " Öffne Quell-Datenbank ... "; + eval { + unless ( $source_dbh = DBI->connect( $source_dsn, $source_user, $source_passwd, $attribs ) ) + { + die $DBI::errstr; + } + }; + if ($@) { + print not_ok() . "\n"; + warn warning() . " Konnte Quell-Datenbank nicht öffnen: " . $@ . "\n"; + exit 5; + } + print ok() . "\n"; + } ## end else [ if ( $source_dbh and $source_dbh->{'Active'... + + if ( $target_dbh and $target_dbh->{'Active'} ) { + warn warning() . " Ziel-Datenbank ist bereits geöffnet.\n"; + } + else { + print green_star() . " Öffne Ziel-Datenbank ... "; + eval { + unless ( $target_dbh = DBI->connect( $target_dsn, $target_user, $target_passwd, $attribs ) ) + { + die $DBI::errstr; + } + }; + if ($@) { + print not_ok() . "\n"; + warn warning() . " Konnte Ziel-Datenbank nicht öffnen: " . $@ . "\n"; + exit 5; + } + print ok() . "\n"; + } ## end else [ if ( $target_dbh and $target_dbh->{'Active'... + + return 1; + +} ## end sub open_dbs + +#------------------------------------------------------------- + +=head2 red_star( ), yellow_star( ) und green_star( ) + +Geben einen Stern in der gegebenen Farbe zurueck. + +=cut + +sub red_star { + return colored( "*", 'bold red' ); +} + +sub yellow_star { + return colored( "*", 'bold yellow' ); +} + +sub green_star { + return colored( "*", 'bold green' ); +} + +#------------------------------------------------------------- + +=head2 ok( ) + +Gibt ein gruenes 'OK' zurueck. + +=cut + +sub ok { + return "[" . colored( "OK", 'bold green' ) . "]"; +} + +#------------------------------------------------------------- + +=head2 warning( ) + +Gibt ein gelbes 'WARNING' zurueck. + +=cut + +sub warning { + return "[" . colored( "WARNING", 'bold yellow' ) . "]"; +} + +#------------------------------------------------------------- + +=head2 not_ok( ) + +Gibt ein rotes 'NOT OK' zurueck. + +=cut + +sub not_ok { + return "[" . colored( "NOT OK", 'bold red' ) . "]"; +} + +#------------------------------------------------------------- + +=head2 drop_target_tables( ) + +Loescht in der Ziel-Datenbank alle Tabellen ... + +=cut + +sub drop_target_tables { + + print green_star() . " Lösche alle Zieltabellen ...\n"; + for my $table ( reverse(@target_tables) ) { + my $sql = "DROP TABLE IF EXISTS `" . $table . "`"; + print " - " . $table . " "; + eval { die "Nicht gelöscht.\n" unless $target_dbh->do($sql); }; + if ($@) { + print not_ok() . "\n"; + warn "$@\n"; + return undef; + } + print ok() . "\n"; + } ## end for my $table ( reverse(@target_tables) ) + print "\n"; + + return 1; + +} ## end sub drop_target_tables + +#------------------------------------------------------------- + +=head2 create_target_tables( ) + +Erstellt alle notwendigen Ziel-Tabellen ... + +=cut + +sub create_target_tables { + + my %create_method = ( 'users' => \&create_user_table, ); + + print green_star() . " Erstelle Ziel-Tabellen ...\n"; + + for my $table (@target_tables) { + my $method = $create_method{$table}; + unless ($method) { + die "Keine Create-Methode für Tabelle '$method' gefunden.\n"; + } + print " - " . $table . " "; + eval { die "Tabelle '" . $table . "' wurde nicht erstellt.\n" unless $method->(); }; + if ($@) { + print not_ok() . "\n"; + warn $@ . "\n"; + return undef; + } + print ok() . "\n"; + } ## end for my $table (@target_tables) + print "\n"; + + return 1; +} ## end sub create_target_tables + +#------------------------------------------------------------- + +=head2 create_user_table( ) + +Erstellt die Tabelle 'users' ... + +=cut + +sub create_user_table { + + my $sql = <do($sql); + + my $saltlen = $local_conf->{'authentication'}{'dbic'}{'password_salt_len'} + || $global_conf->{'authentication'}{'dbic'}{'password_salt_len'} + || 4; + my $csh = Crypt::SaltedHash->new( 'algorithm' => 'SHA-1', 'salt_len' => $saltlen ); + $csh->add( $admin_data->{'passwd'} ); + my $passwd = $csh->generate(); + + $sql = <quote( $admin_data->{'login'} ), + $target_dbh->quote( $admin_data->{'vorname'} ), + $target_dbh->quote( $admin_data->{'nachname'} ), + $target_dbh->quote($passwd), + $target_dbh->quote( $admin_data->{'email'} ) ); + + return undef unless $target_dbh->do($sql); + + return 1; + +} ## end sub create_user_table + +#------------------------------------------------------------- + +__END__ +