3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
7 use vars qw($opt_u $opt_d $opt_v);
9 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
11 use FS::Schema qw( dbdef_dist reload_dbdef );
12 use FS::Record qw( qsearch );
14 use FS::Setup qw(create_initial_data);
17 die "Not running uid freeside!" unless checkeuid();
20 # map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
23 my $config_dir = shift || 'conf' ;
24 $config_dir =~ /^([\w.:=]+)$/
25 or die "unacceptable configuration directory name";
30 #needs to match FS::Record
31 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
35 #print "\nEnter the maximum username length: ";
36 #my($username_len)=&getvalue;
37 my $username_len = 32; #usernamemax config file
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).
45 #First enter any additional RADIUS check attributes you need to track for each
46 #user, separated by whitespace.
48 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
49 # split(" ",&getvalue);
51 #print "\n\n", <<END, ":";
52 #Now enter any additional reply attributes you need to track for each user,
53 #separated by whitespace.
55 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
56 # split(" ",&getvalue);
58 #print "\n\n", <<END, ":";
59 #Do you wish to enable the tracking of a second, separate shipping/service
65 # my($x)=scalar(<STDIN>);
72 # my $x = scalar(<STDIN>);
76 #my @check_attributes = (); #add later
77 #my @attributes = (); #add later
81 # create a dbdef object from the old data structure
84 my $dbdef = dbdef_dist;
87 $dbdef->save($dbdef_file);
88 &FS::Schema::reload_dbdef($dbdef_file);
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;
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";
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);
114 warn "Freeside schema initialized - commiting transaction\n" if $opt_v;
116 $dbh->commit or die $dbh->errstr;
117 $dbh->disconnect or die $dbh->errstr;
119 warn "Database schema committed successfully\n" if $opt_v;
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);
128 $dbh->rollback or die $dbh->errstr;
133 warn "Freeside configuration initialized - commiting transaction\n" if $opt_v;
135 $dbh->commit or die $dbh->errstr;
136 $dbh->disconnect or die $dbh->errstr;
138 warn "Freeside configuration committed successfully\n" if $opt_v;
140 $dbh = adminsuidsetup $opt_u;
141 create_initial_data('domain' => $opt_d);
143 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
145 $dbh->commit or die $dbh->errstr;
146 $dbh->disconnect or die $dbh->errstr;
148 warn "Database initialization committed successfully\n" if $opt_v;
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;
157 die "Usage:\n freeside-setup -d domain.name [ -v ] [ config/dir ]\n"
158 # [ -u user ] for devel/multi-db installs