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