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