This commit was generated by cvs2svn to compensate for changes in r6252,
[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 my $username_len = 32;
36
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).
42 #
43 #First enter any additional RADIUS check attributes you need to track for each 
44 #user, separated by whitespace.
45 #END
46 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
47 #                         split(" ",&getvalue);
48 #
49 #print "\n\n", <<END, ":";
50 #Now enter any additional reply attributes you need to track for each user,
51 #separated by whitespace.
52 #END
53 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
54 #                   split(" ",&getvalue);
55 #
56 #print "\n\n", <<END, ":";
57 #Do you wish to enable the tracking of a second, separate shipping/service
58 #address?
59 #END
60 #my $ship = &_yesno;
61 #
62 #sub getvalue {
63 #  my($x)=scalar(<STDIN>);
64 #  chop $x;
65 #  $x;
66 #}
67 #
68 #sub _yesno {
69 #  print " [y/N]:";
70 #  my $x = scalar(<STDIN>);
71 #  $x =~ /^y/i;
72 #}
73
74 #my @check_attributes = (); #add later
75 #my @attributes = (); #add later
76 #my $ship = $opt_s;
77
78 ###
79 # create a dbdef object from the old data structure
80 ###
81
82 my $dbdef = dbdef_dist(datasrc);
83
84 #important
85 $dbdef->save($dbdef_file);
86 &FS::Schema::reload_dbdef($dbdef_file);
87
88 ###
89 # create 'em
90 ###
91
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;
96
97 #create tables
98 $|=1;
99
100 foreach my $statement ( $dbdef->sql($dbh) ) {
101   $dbh->do( $statement )
102     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
103 }
104
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);
110
111 warn "Freeside schema initialized - commiting transaction\n" if $opt_v;
112
113 $dbh->commit or die $dbh->errstr;
114 $dbh->disconnect or die $dbh->errstr;
115
116 warn "Database schema committed successfully\n" if $opt_v;
117
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);
124   if ($error) {
125     $dbh->rollback or die $dbh->errstr;
126     die $error;
127   }
128 }
129
130 warn "Freeside configuration initialized - commiting transaction\n" if $opt_v;
131
132 $dbh->commit or die $dbh->errstr;
133 $dbh->disconnect or die $dbh->errstr;
134
135 warn "Freeside configuration committed successfully\n" if $opt_v;
136
137 $dbh = adminsuidsetup $opt_u;
138 create_initial_data('domain' => $opt_d);
139
140 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
141
142 $dbh->commit or die $dbh->errstr;
143 $dbh->disconnect or die $dbh->errstr;
144
145 warn "Database initialization committed successfully\n" if $opt_v;
146
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;
150   $dbdef->save($file);
151 }
152
153 sub usage {
154   die "Usage:\n  freeside-setup -d domain.name [ -v ] [ config/dir ]\n"
155   # [ -u user ] for devel/multi-db installs
156 }
157
158 1;
159
160