fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / bin / freeside-setup
1 #!/usr/bin/perl -w
2
3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
5
6 #to allow initial insert
7 use FS::part_pkg;
8 $FS::part_pkg::setup_hack = 1;
9 $FS::part_pkg::setup_hack = 1;
10
11 use strict;
12 use vars qw($opt_u $opt_d $opt_v $opt_q);
13 use Getopt::Std;
14 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
15 use FS::CurrentUser;
16 use FS::Schema qw( dbdef_dist reload_dbdef );
17 use FS::Record qw( qsearch );
18 #use FS::raddb;
19 use FS::Setup qw(create_initial_data);
20 use FS::Conf;
21
22 die "Not running uid freeside!" unless checkeuid();
23
24 #my %attrib2db =
25 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
26
27 getopts("u:vqd:");
28 $opt_v = 1 unless $opt_q; #verbose by default now
29
30 my $config_dir = shift || '%%%DIST_CONF%%%' ;
31 $config_dir =~ /^([\w.:=\/]+)$/
32   or die "unacceptable configuration directory name";
33 $config_dir = $1;
34
35 getsecrets($opt_u);
36
37 #needs to match FS::Record
38 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
39
40 ###
41
42 my $username_len = 32;
43
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).
49 #
50 #First enter any additional RADIUS check attributes you need to track for each 
51 #user, separated by whitespace.
52 #END
53 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
54 #                         split(" ",&getvalue);
55 #
56 #print "\n\n", <<END, ":";
57 #Now enter any additional reply attributes you need to track for each user,
58 #separated by whitespace.
59 #END
60 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
61 #                   split(" ",&getvalue);
62 #
63 #print "\n\n", <<END, ":";
64 #Do you wish to enable the tracking of a second, separate shipping/service
65 #address?
66 #END
67 #my $ship = &_yesno;
68 #
69 #sub getvalue {
70 #  my($x)=scalar(<STDIN>);
71 #  chop $x;
72 #  $x;
73 #}
74 #
75 #sub _yesno {
76 #  print " [y/N]:";
77 #  my $x = scalar(<STDIN>);
78 #  $x =~ /^y/i;
79 #}
80
81 #my @check_attributes = (); #add later
82 #my @attributes = (); #add later
83 #my $ship = $opt_s;
84
85 ###
86 # create a dbdef object from the old data structure
87 ###
88
89 warn "Loading schema objects\n" if $opt_v;
90
91 my $dbdef = dbdef_dist(datasrc);
92
93 #important
94 $dbdef->save($dbdef_file);
95 &FS::Schema::reload_dbdef($dbdef_file);
96
97 ###
98 # create 'em
99 ###
100
101 warn "Connecting to database\n" if $opt_v;
102
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;
107
108 #create tables
109 $|=1;
110
111 warn "Creating tables and indices\n" if $opt_v;
112
113 foreach my $statement ( $dbdef->sql($dbh) ) {
114   $dbh->do( $statement )
115     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
116 }
117
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);
123
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;
128
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);
135   if ($error) {
136     $dbh->rollback or die $dbh->errstr;
137     die $error;
138   }
139 }
140
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;
145
146 $dbh = adminsuidsetup $opt_u;
147 create_initial_data('domain' => $opt_d);
148
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;
153
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;
157   $dbdef->save($file);
158 }
159
160 sub usage {
161   die "Usage:\n  freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
162   # [ -u user ] for devel/multi-db installs
163 }
164
165 1;
166
167