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