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 my $username_len = 32;
37 #print "\n\n", <<END, ":";
38 #Freeside tracks the RADIUS User-Name, check attribute Password and
39 #reply attribute Framed-IP-Address for each user. You can specify additional
40 #check and reply attributes (or you can add them later with the
41 #fs-radius-add-check and fs-radius-add-reply programs).
43 #First enter any additional RADIUS check attributes you need to track for each
44 #user, separated by whitespace.
46 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
47 # split(" ",&getvalue);
49 #print "\n\n", <<END, ":";
50 #Now enter any additional reply attributes you need to track for each user,
51 #separated by whitespace.
53 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
54 # split(" ",&getvalue);
56 #print "\n\n", <<END, ":";
57 #Do you wish to enable the tracking of a second, separate shipping/service
63 # my($x)=scalar(<STDIN>);
70 # my $x = scalar(<STDIN>);
74 #my @check_attributes = (); #add later
75 #my @attributes = (); #add later
79 # create a dbdef object from the old data structure
82 my $dbdef = dbdef_dist(datasrc);
85 $dbdef->save($dbdef_file);
86 &FS::Schema::reload_dbdef($dbdef_file);
92 $FS::CurrentUser::upgrade_hack = 1;
93 $FS::UID::callback_hack = 1;
94 my $dbh = adminsuidsetup $opt_u; #$user;
95 $FS::UID::callback_hack = 0;
100 foreach my $statement ( $dbdef->sql($dbh) ) {
101 $dbh->do( $statement )
102 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
105 #now go back and reverse engineer the db
106 #so we pick up the correct column DEFAULTs for #oidless inserts
107 dbdef_create($dbh, $dbdef_file);
108 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
109 reload_dbdef($dbdef_file);
111 warn "Freeside schema initialized - commiting transaction\n" if $opt_v;
113 $dbh->commit or die $dbh->errstr;
114 $dbh->disconnect or die $dbh->errstr;
116 warn "Database schema committed successfully\n" if $opt_v;
118 warn "Initializing freeside configuration\n" if $opt_v;
119 $FS::UID::callback_hack = 1;
120 $dbh = adminsuidsetup $opt_u;
121 $FS::UID::callback_hack = 0;
122 if (!scalar(qsearch('conf', {}))) {
123 my $error = FS::Conf::init_config($config_dir);
125 $dbh->rollback or die $dbh->errstr;
130 warn "Freeside configuration initialized - commiting transaction\n" if $opt_v;
132 $dbh->commit or die $dbh->errstr;
133 $dbh->disconnect or die $dbh->errstr;
135 warn "Freeside configuration committed successfully\n" if $opt_v;
137 $dbh = adminsuidsetup $opt_u;
138 create_initial_data('domain' => $opt_d);
140 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
142 $dbh->commit or die $dbh->errstr;
143 $dbh->disconnect or die $dbh->errstr;
145 warn "Database initialization committed successfully\n" if $opt_v;
147 sub dbdef_create { # reverse engineer the schema from the DB and save to file
148 my( $dbh, $file ) = @_;
149 my $dbdef = new_native DBIx::DBSchema $dbh;
154 die "Usage:\n freeside-setup -d domain.name [ -v ] [ config/dir ]\n"
155 # [ -u user ] for devel/multi-db installs