fix error handling
[freeside.git] / FS / bin / freeside-setup
1 #!/usr/bin/perl -Tw
2
3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
5
6 use strict;
7 use vars qw($opt_u $opt_d $opt_v);
8 use Getopt::Std;
9 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
10 use FS::CurrentUser;
11 use FS::Schema qw( dbdef_dist reload_dbdef );
12 use FS::Record qw( qsearch );
13 #use FS::raddb;
14 use FS::Setup qw(create_initial_data);
15 use FS::Conf;
16
17 die "Not running uid freeside!" unless checkeuid();
18
19 #my %attrib2db =
20 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
21
22 getopts("u:vd:");
23 my $config_dir = shift || 'conf' ;
24 $config_dir =~ /^([\w.:=]+)$/
25   or die "unacceptable configuration directory name";
26 $config_dir = $1;
27
28 getsecrets($opt_u);
29
30 #needs to match FS::Record
31 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
32
33 ###
34
35 #print "\nEnter the maximum username length: ";
36 #my($username_len)=&getvalue;
37 my $username_len = 32; #usernamemax config file
38
39 #print "\n\n", <<END, ":";
40 #Freeside tracks the RADIUS User-Name, check attribute Password and
41 #reply attribute Framed-IP-Address for each user.  You can specify additional
42 #check and reply attributes (or you can add them later with the
43 #fs-radius-add-check and fs-radius-add-reply programs).
44 #
45 #First enter any additional RADIUS check attributes you need to track for each 
46 #user, separated by whitespace.
47 #END
48 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
49 #                         split(" ",&getvalue);
50 #
51 #print "\n\n", <<END, ":";
52 #Now enter any additional reply attributes you need to track for each user,
53 #separated by whitespace.
54 #END
55 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
56 #                   split(" ",&getvalue);
57 #
58 #print "\n\n", <<END, ":";
59 #Do you wish to enable the tracking of a second, separate shipping/service
60 #address?
61 #END
62 #my $ship = &_yesno;
63 #
64 #sub getvalue {
65 #  my($x)=scalar(<STDIN>);
66 #  chop $x;
67 #  $x;
68 #}
69 #
70 #sub _yesno {
71 #  print " [y/N]:";
72 #  my $x = scalar(<STDIN>);
73 #  $x =~ /^y/i;
74 #}
75
76 #my @check_attributes = (); #add later
77 #my @attributes = (); #add later
78 #my $ship = $opt_s;
79
80 ###
81 # create a dbdef object from the old data structure
82 ###
83
84 my $dbdef = dbdef_dist;
85
86 #important
87 $dbdef->save($dbdef_file);
88 &FS::Schema::reload_dbdef($dbdef_file);
89
90 ###
91 # create 'em
92 ###
93
94 $FS::CurrentUser::upgrade_hack = 1;
95 $FS::UID::callback_hack = 1;
96 my $dbh = adminsuidsetup $opt_u; #$user;
97 $FS::UID::callback_hack = 0;
98
99 #create tables
100 $|=1;
101
102 foreach my $statement ( $dbdef->sql($dbh) ) {
103   warn $statement if $statement =~ /TABLE cdr/;
104   $dbh->do( $statement )
105     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
106 }
107
108 #now go back and reverse engineer the db
109 #so we pick up the correct column DEFAULTs for #oidless inserts
110 dbdef_create($dbh, $dbdef_file);
111 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
112 reload_dbdef($dbdef_file);
113
114 warn "Freeside schema initialized - commiting transaction\n" if $opt_v;
115
116 $dbh->commit or die $dbh->errstr;
117 $dbh->disconnect or die $dbh->errstr;
118
119 warn "Database schema committed successfully\n" if $opt_v;
120
121 warn "Initializing freeside configuration\n" if $opt_v;
122 $FS::UID::callback_hack = 1;
123 $dbh = adminsuidsetup $opt_u;
124 $FS::UID::callback_hack = 0;
125 if (!scalar(qsearch('conf', {}))) {
126   my $error = FS::Conf::init_config($config_dir);
127   if ($error) {
128     $dbh->rollback or die $dbh->errstr;
129     die $error;
130   }
131 }
132
133 warn "Freeside configuration initialized - commiting transaction\n" if $opt_v;
134
135 $dbh->commit or die $dbh->errstr;
136 $dbh->disconnect or die $dbh->errstr;
137
138 warn "Freeside configuration committed successfully\n" if $opt_v;
139
140 $dbh = adminsuidsetup $opt_u;
141 create_initial_data('domain' => $opt_d);
142
143 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
144
145 $dbh->commit or die $dbh->errstr;
146 $dbh->disconnect or die $dbh->errstr;
147
148 warn "Database initialization committed successfully\n" if $opt_v;
149
150 sub dbdef_create { # reverse engineer the schema from the DB and save to file
151   my( $dbh, $file ) = @_;
152   my $dbdef = new_native DBIx::DBSchema $dbh;
153   $dbdef->save($file);
154 }
155
156 sub usage {
157   die "Usage:\n  freeside-setup -d domain.name [ -v ] [ config/dir ]\n"
158   # [ -u user ] for devel/multi-db installs
159 }
160
161 1;
162
163