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