Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / lib / RT / Installer.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::Installer;
50 use strict;
51 use warnings;
52
53 use DateTime;
54
55 require UNIVERSAL::require;
56 my %Meta = (
57     DatabaseType => {
58         Widget          => '/Widgets/Form/Select',
59         WidgetArguments => {
60             Description => 'Database type',    # loc
61             Values      => [
62                 grep {
63                     my $m = 'DBD::' . $_;
64                     $m->require ? 1 : 0
65                   } qw/mysql Pg SQLite Oracle/
66             ],
67             ValuesLabel => {
68                 mysql  => 'MySQL',             #loc
69                 Pg     => 'PostgreSQL',        #loc
70                 SQLite => 'SQLite',            #loc
71                 Oracle => 'Oracle',            #loc
72             },
73         },
74     },
75     DatabaseHost => {
76         Widget          => '/Widgets/Form/String',
77         WidgetArguments => {
78             Description => 'Database host', #loc
79             Default => 1,
80             DefaultLabel => "Keep 'localhost' if you're not sure. Leave blank to connect locally over a socket", #loc
81             Hints => "The domain name of your database server (like 'db.example.com').",       #loc
82         },
83     },
84     DatabasePort => {
85         Widget          => '/Widgets/Form/Integer',
86         WidgetArguments => {
87             Description => 'Database port',         #loc
88             Default     => 1,
89             DefaultLabel =>
90               'Leave empty to use the default value for your database',              #loc
91         },
92     },
93     DatabaseName => {
94         Widget          => '/Widgets/Form/String',
95         WidgetArguments => {
96             Description => 'Database name',                       #loc
97         },
98     },
99     DatabaseAdmin => {
100         SkipWrite       => 1,
101         Widget          => '/Widgets/Form/String',
102         WidgetArguments => {
103             Default => 1,
104             Hints => "Leave this alone to use the default dba username for your database type", #loc
105             Description => 'DBA username', # loc
106             DefaultLabel => '',
107         },
108     },
109     DatabaseAdminPassword => {
110         SkipWrite       => 1,
111         Widget          => '/Widgets/Form/String',
112         WidgetArguments => {
113             Description => 'DBA password',  #loc
114             DefaultLabel => "The DBA's database password",#loc
115             Type => 'password',
116             Hints => "You must provide the dba's password so we can create the RT database and user.",
117         },
118     },
119     DatabaseUser => {
120         Widget          => '/Widgets/Form/String',
121         WidgetArguments => {
122             Description => 'Database username for RT',                      #loc
123             Hints => 'RT will connect to the database using this user.  It will be created for you.', #loc
124         },
125     },
126     DatabasePassword => {
127         Widget          => '/Widgets/Form/String',
128         WidgetArguments => {
129             Description => 'Database password for RT',                      #loc
130             Type        => 'password',
131             Hints       => 'The password RT should use to connect to the database.',
132         },
133     },
134     rtname => {
135         Widget          => '/Widgets/Form/String',
136         WidgetArguments => {
137             Description => 'Site name',                        #loc
138             Hints => 'RT will use this string to uniquely identify your installation and looks for it in the subject of emails to decide what ticket a message applies to.  We recommend that you set this to your internet domain. (ex: example.com)' #loc
139         },
140     },
141     MinimumPasswordLength => {
142         Widget          => '/Widgets/Form/Integer',
143         WidgetArguments => {
144             Description => 'Minimum password length',    #loc
145         },
146     },
147     Password => {
148         SkipWrite       => 1,
149         Widget          => '/Widgets/Form/String',
150         WidgetArguments => {
151             Description => 'Administrative password', #loc
152             Hints => 'RT will create a user called "root" and set this as their password', #loc
153             Type => 'password',
154         },
155     },
156     OwnerEmail => {
157         Widget          => '/Widgets/Form/String',
158         WidgetArguments => {
159             Description => 'RT Administrator Email',                   #loc
160             Hints => "When RT can't handle an email message, where should it be forwarded?", #loc
161         },
162     },
163     CommentAddress => {
164         Widget          => '/Widgets/Form/String',
165         WidgetArguments => {
166             Description => 'Comment address',                           #loc
167             Hints =>
168 'the default addresses that will be listed in From: and Reply-To: headers of comment mail.' #loc
169         },
170     },
171     CorrespondAddress => {
172         Widget          => '/Widgets/Form/String',
173         WidgetArguments => {
174             Description => 'Correspond address',    #loc
175             Hints =>
176 'the default addresses that will be listed in From: and Reply-To: headers of correspondence mail.' #loc
177         },
178     },
179     SendmailPath => {
180         Widget          => '/Widgets/Form/String',
181         WidgetArguments => {
182             Hints => 'Where to find your sendmail binary.',    #loc
183             Description => 'Path to sendmail', #loc
184         },
185     },
186     Timezone => {
187         Widget          => '/Widgets/Form/Select',
188         WidgetArguments => {
189             Description => 'Timezone',                              #loc
190             Callback    => sub {
191                 my $ret;
192                 $ret->{Values} = ['', DateTime::TimeZone->all_names];
193
194                 my $dt = DateTime->now;
195                 for my $tz ( DateTime::TimeZone->all_names ) {
196                     $dt->set_time_zone( $tz );
197                     $ret->{ValuesLabel}{$tz} =
198                         $tz . ' ' . $dt->strftime('%z');
199                 }
200                 $ret->{ValuesLabel}{''} = 'System Default'; #loc
201
202                 return $ret;
203             },
204         },
205     },
206     WebDomain => {
207         Widget          => '/Widgets/Form/String',
208         WidgetArguments => {
209             Description => 'Domain name',                  #loc
210             Hints => "Don't include http://, just something like 'localhost', 'rt.example.com'", #loc
211         },
212     },
213     WebPort => {
214         Widget          => '/Widgets/Form/Integer',
215         WidgetArguments => {
216             Description => 'Web port',                     #loc
217             Hints => 'which port your web server will listen to, e.g. 8080', #loc
218         },
219     },
220
221 );
222
223 sub Meta {
224     my $class = shift;
225     my $type  = shift;
226     return $Meta{$type} if $type;
227     return \%Meta;
228 }
229
230 sub CurrentValue {
231     my $class = shift;
232     my $type  = shift;
233     $type = $class if !ref $class && $class && $class ne 'RT::Installer';
234
235     return undef unless $type;
236     return $RT::Installer
237       && exists $RT::Installer->{InstallConfig}{$type}
238       ? $RT::Installer->{InstallConfig}{$type}
239       : scalar RT->Config->Get($type);
240 }
241
242 sub CurrentValues {
243     my $class = shift;
244     my @types = @_;
245     push @types, $class if !ref $class && $class && $class ne 'RT::Installer';
246
247     return { map { $_ => CurrentValue($_) } @types };
248 }
249
250 sub ConfigFile {
251     require File::Spec;
252     return $ENV{RT_SITE_CONFIG} || File::Spec->catfile( $RT::EtcPath, 'RT_SiteConfig.pm' );
253 }
254
255 sub SaveConfig {
256     my $class = shift;
257
258     my $file = $class->ConfigFile;
259
260     my $content;
261
262     {
263         local $/;
264         open( my $fh, '<', $file ) or die $!;
265         $content = <$fh>;
266         $content =~ s/^\s*1;\s*$//m;
267     }
268
269     # make organization the same as rtname
270     $RT::Installer->{InstallConfig}{Organization} =
271       $RT::Installer->{InstallConfig}{rtname};
272
273     if ( open my $fh, '>', $file ) {
274         for ( sort keys %{ $RT::Installer->{InstallConfig} } ) {
275
276             # we don't want to store root's password in config.
277             next if $class->Meta($_) and $class->Meta($_)->{SkipWrite};
278
279             $RT::Installer->{InstallConfig}{$_} = ''
280               unless defined $RT::Installer->{InstallConfig}{$_};
281
282             # remove obsolete settings we'll add later
283             $content =~ s/^\s* Set \s* \( \s* \$$_ .*$//xm;
284
285             my $value = $RT::Installer->{InstallConfig}{$_};
286             $value =~ s/(['\\])/\\$1/g;
287             $content .= "Set( \$$_, '$value' );\n";
288         }
289         $content .= "1;\n";
290         print $fh $content;
291         close $fh;
292
293         return ( 1, "Successfully saved configuration to $file." );
294     }
295
296     return ( 0, "Cannot save configuration to $file: $!" );
297 }
298
299 =head1 NAME
300
301     RT::Installer - RT's Installer
302
303 =head1 SYNOPSYS
304
305     use RT::Installer;
306     my $meta = RT::Installer->Meta;
307
308 =head1 DESCRIPTION
309
310 C<RT::Installer> class provides access to RT Installer Meta
311
312 =cut
313
314 RT::Base->_ImportOverlays();
315
316 1;
317