3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
6 #to allow initial insert
8 $FS::part_pkg::setup_hack = 1;
9 $FS::part_pkg::setup_hack = 1;
12 use vars qw($opt_u $opt_d $opt_v $opt_q);
14 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
16 use FS::Schema qw( dbdef_dist reload_dbdef );
17 use FS::Record qw( qsearch );
19 use FS::Setup qw(create_initial_data);
22 die "Not running uid freeside!" unless checkeuid();
25 # map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
28 $opt_v = 1 unless $opt_q; #verbose by default now
30 my $config_dir = shift || '%%%DIST_CONF%%%' ;
31 $config_dir =~ /^([\w.:=\/]+)$/
32 or die "unacceptable configuration directory name";
37 #needs to match FS::Record
38 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
42 my $username_len = 32;
44 #print "\n\n", <<END, ":";
45 #Freeside tracks the RADIUS User-Name, check attribute Password and
46 #reply attribute Framed-IP-Address for each user. You can specify additional
47 #check and reply attributes (or you can add them later with the
48 #fs-radius-add-check and fs-radius-add-reply programs).
50 #First enter any additional RADIUS check attributes you need to track for each
51 #user, separated by whitespace.
53 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
54 # split(" ",&getvalue);
56 #print "\n\n", <<END, ":";
57 #Now enter any additional reply attributes you need to track for each user,
58 #separated by whitespace.
60 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
61 # split(" ",&getvalue);
63 #print "\n\n", <<END, ":";
64 #Do you wish to enable the tracking of a second, separate shipping/service
70 # my($x)=scalar(<STDIN>);
77 # my $x = scalar(<STDIN>);
81 #my @check_attributes = (); #add later
82 #my @attributes = (); #add later
86 # create a dbdef object from the old data structure
89 warn "Loading schema objects\n" if $opt_v;
91 my $dbdef = dbdef_dist(datasrc);
94 $dbdef->save($dbdef_file);
95 &FS::Schema::reload_dbdef($dbdef_file);
101 warn "Connecting to database\n" if $opt_v;
103 $FS::CurrentUser::upgrade_hack = 1;
104 $FS::UID::callback_hack = 1;
105 my $dbh = adminsuidsetup $opt_u; #$user;
106 $FS::UID::callback_hack = 0;
111 warn "Creating tables and indices\n" if $opt_v;
113 foreach my $statement ( $dbdef->sql($dbh) ) {
114 $dbh->do( $statement )
115 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
118 #now go back and reverse engineer the db
119 #so we pick up the correct column DEFAULTs for #oidless inserts
120 dbdef_create($dbh, $dbdef_file);
121 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
122 reload_dbdef($dbdef_file);
124 warn "Tables and indices created - commiting transaction\n" if $opt_v;
125 $dbh->commit or die $dbh->errstr;
126 $dbh->disconnect or die $dbh->errstr;
127 warn "Database schema committed successfully\n" if $opt_v;
129 warn "Initializing configuration\n" if $opt_v;
130 $FS::UID::callback_hack = 1;
131 $dbh = adminsuidsetup $opt_u;
132 $FS::UID::callback_hack = 0;
133 if (!scalar(qsearch('conf', {}))) {
134 my $error = FS::Conf::init_config($config_dir);
136 $dbh->rollback or die $dbh->errstr;
141 warn "Configuration initialized - commiting transaction\n" if $opt_v;
142 $dbh->commit or die $dbh->errstr;
143 $dbh->disconnect or die $dbh->errstr;
144 warn "Configuration committed successfully\n" if $opt_v;
146 $dbh = adminsuidsetup $opt_u;
147 create_initial_data('domain' => $opt_d);
149 warn "Database initialized - commiting transaction\n" if $opt_v;
150 $dbh->commit or die $dbh->errstr;
151 $dbh->disconnect or die $dbh->errstr;
152 warn "Database initialization committed successfully\n" if $opt_v;
154 sub dbdef_create { # reverse engineer the schema from the DB and save to file
155 my( $dbh, $file ) = @_;
156 my $dbdef = new_native DBIx::DBSchema $dbh;
161 die "Usage:\n freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
162 # [ -u user ] for devel/multi-db installs