default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / bin / freeside-setup
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 freeside-setup
6
7 =cut
8
9 #to delay loading dbdef until we're ready
10 BEGIN { $FS::Schema::setup_hack = 1; }
11
12 #to allow initial insert
13 use FS::part_pkg;
14 $FS::part_pkg::setup_hack = 1;
15 $FS::part_pkg::setup_hack = 1;
16
17 use strict;
18 use vars qw($opt_u $opt_d $opt_v $opt_q);
19 use Getopt::Std;
20 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
21 use FS::CurrentUser;
22 use FS::Schema qw( dbdef_dist reload_dbdef );
23 use FS::Record qw( qsearch );
24 #use FS::raddb;
25 use FS::Setup qw(create_initial_data);
26 use FS::Conf;
27
28 die "Not running uid freeside!" unless checkeuid();
29
30 #my %attrib2db =
31 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
32
33 getopts("u:vqd:");
34 $opt_v = 1 unless $opt_q; #verbose by default now
35
36 my $config_dir = shift || '%%%DIST_CONF%%%' ;
37 $config_dir =~ /^([\w.:=\/]+)$/
38   or die "unacceptable configuration directory name";
39 $config_dir = $1;
40
41 getsecrets();
42
43 #needs to match FS::Record
44 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
45
46 my $username_len = 32;
47
48 ###
49 # create a dbdef object from the old data structure
50 ###
51
52 warn "Loading schema objects\n" if $opt_v;
53
54 my $dbdef = dbdef_dist(datasrc);
55
56 #important
57 $dbdef->save($dbdef_file);
58 &FS::Schema::reload_dbdef($dbdef_file);
59
60 ###
61 # create 'em
62 ###
63
64 warn "Connecting to database\n" if $opt_v;
65
66 $FS::CurrentUser::upgrade_hack = 1;
67 $FS::UID::callback_hack = 1;
68 my $dbh = adminsuidsetup $opt_u; #$user;
69 $FS::UID::callback_hack = 0;
70
71 #create tables
72 $|=1;
73
74 warn "Creating tables and indices\n" if $opt_v;
75
76 foreach my $statement ( $dbdef->sql($dbh) ) {
77   $dbh->do( $statement )
78     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
79 }
80
81 #now go back and reverse engineer the db
82 #so we pick up the correct column DEFAULTs for #oidless inserts
83 dbdef_create($dbh, $dbdef_file);
84 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
85 reload_dbdef($dbdef_file);
86
87 warn "Tables and indices created - commiting transaction\n" if $opt_v;
88 $dbh->commit or die $dbh->errstr;
89 $dbh->disconnect or die $dbh->errstr;
90 warn "Database schema committed successfully\n" if $opt_v;
91
92 warn "Initializing configuration\n" if $opt_v;
93 $FS::UID::callback_hack = 1;
94 $dbh = adminsuidsetup $opt_u;
95 $FS::UID::callback_hack = 0;
96 if (!scalar(qsearch('conf', {}))) {
97   my $error = FS::Conf::init_config($config_dir);
98   if ($error) {
99     $dbh->rollback or die $dbh->errstr;
100     die $error;
101   }
102 }
103
104 warn "Configuration initialized - commiting transaction\n" if $opt_v;
105 $dbh->commit or die $dbh->errstr;
106 $dbh->disconnect or die $dbh->errstr;
107 warn "Configuration committed successfully\n" if $opt_v;
108
109 $dbh = adminsuidsetup $opt_u;
110 create_initial_data('domain' => $opt_d);
111
112 warn "Database initialized - commiting transaction\n" if $opt_v;
113 $dbh->commit or die $dbh->errstr;
114 $dbh->disconnect or die $dbh->errstr;
115 warn "Database initialization committed successfully\n" if $opt_v;
116
117 sub dbdef_create { # reverse engineer the schema from the DB and save to file
118   my( $dbh, $file ) = @_;
119   my $dbdef = new_native DBIx::DBSchema $dbh;
120   $dbdef->save($file);
121 }
122
123 sub usage {
124   die "Usage:\n  freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
125   # [ -u user ] for devel/multi-db installs
126 }
127
128 1;
129
130