~ubuntu-branches/ubuntu/precise/389-ds-base/precise

« back to all changes in this revision

Viewing changes to ldap/admin/src/scripts/Setup.pm.in

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-01 23:54:24 UTC
  • Revision ID: package-import@ubuntu.com-20120301235424-kaim42d08pic3xi3
Tags: upstream-1.2.10.2
ImportĀ upstreamĀ versionĀ 1.2.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# BEGIN COPYRIGHT BLOCK
 
2
# This Program is free software; you can redistribute it and/or modify it under
 
3
# the terms of the GNU General Public License as published by the Free Software
 
4
# Foundation; version 2 of the License.
 
5
 
6
# This Program is distributed in the hope that it will be useful, but WITHOUT
 
7
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
8
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
9
 
10
# You should have received a copy of the GNU General Public License along with
 
11
# this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
12
# Place, Suite 330, Boston, MA 02111-1307 USA.
 
13
 
14
# In addition, as a special exception, Red Hat, Inc. gives You the additional
 
15
# right to link the code of this Program with code not covered under the GNU
 
16
# General Public License ("Non-GPL Code") and to distribute linked combinations
 
17
# including the two, subject to the limitations in this paragraph. Non-GPL Code
 
18
# permitted under this exception must only link to the code of this Program
 
19
# through those well defined interfaces identified in the file named EXCEPTION
 
20
# found in the source code files (the "Approved Interfaces"). The files of
 
21
# Non-GPL Code may instantiate templates or use macros or inline functions from
 
22
# the Approved Interfaces without causing the resulting work to be covered by
 
23
# the GNU General Public License. Only Red Hat, Inc. may make changes or
 
24
# additions to the list of Approved Interfaces. You must obey the GNU General
 
25
# Public License in all respects for all of the Program code and other code used
 
26
# in conjunction with the Program except the Non-GPL Code covered by this
 
27
# exception. If you modify this file, you may extend this exception to your
 
28
# version of the file, but you are not obligated to do so. If you do not wish to
 
29
# provide this exception without modification, you must delete this exception
 
30
# statement from your version and license this file solely under the GPL without
 
31
# exception. 
 
32
 
33
 
34
# Copyright (C) 2007 Red Hat, Inc.
 
35
# All rights reserved.
 
36
# END COPYRIGHT BLOCK
 
37
#
 
38
 
 
39
###########################
 
40
#
 
41
# This perl module provides a way to set up a new installation after
 
42
# the binaries have already been extracted.  This is typically after
 
43
# using native packaging support to install the package e.g. RPM,
 
44
# pkgadd, depot, etc.  This script will show the license, readme,
 
45
# dsktune, then run the usual setup pre and post installers.
 
46
#
 
47
##########################
 
48
 
 
49
package Setup;
 
50
use Exporter ();
 
51
@ISA       = qw(Exporter);
 
52
@EXPORT    = qw($SILENT $EXPRESS $TYPICAL $CUSTOM);
 
53
@EXPORT_OK = qw($SILENT $EXPRESS $TYPICAL $CUSTOM);
 
54
 
 
55
# hostname
 
56
use Net::Domain qw(hostfqdn);
 
57
 
 
58
# load perldap
 
59
use Mozilla::LDAP::Conn;
 
60
use Mozilla::LDAP::Utils qw(normalizeDN);
 
61
use Mozilla::LDAP::API qw(ldap_explode_dn);
 
62
use Mozilla::LDAP::LDIF;
 
63
 
 
64
use Getopt::Long;
 
65
 
 
66
use SetupLog;
 
67
use DSUtil;
 
68
use Inf;
 
69
 
 
70
use strict;
 
71
use vars qw($EXPRESS $TYPICAL $CUSTOM $SILENT);
 
72
 
 
73
# the setup types
 
74
$EXPRESS = 1;
 
75
$TYPICAL = 2;
 
76
$CUSTOM = 3;
 
77
$SILENT = 4;
 
78
 
 
79
# process command line options
 
80
Getopt::Long::Configure(qw(bundling)); # bundling allows -ddddd
 
81
 
 
82
sub VersionMessage {
 
83
    print "@capbrand@ Directory Server Setup Program Version @PACKAGE_VERSION@\n";
 
84
}
 
85
 
 
86
sub HelpMessage {
 
87
    print <<EOF;
 
88
Usage: $0 [--options] -- [args]
 
89
options:
 
90
    --help       This message
 
91
    --version    Print the version and exit
 
92
    --debug      Turn on debugging
 
93
    --silent     Use silent setup - no user input
 
94
    --file=name  Use the file 'name' in .inf format to supply the default answers
 
95
    --keepcache  Do not delete the temporary .inf file generated by this program
 
96
    --logfile    Log setup messages to this file - otherwise, a temp file will be used
 
97
    --update     Update an existing installation (e.g. after upgrading packages)
 
98
    --continue   (update only) keep going despite errors (also --force)
 
99
For all options, you can also use the short name e.g. -h, -d, etc.  For the -d argument,
 
100
specifying it more than once will increase the debug level e.g. -ddddd
 
101
 
 
102
args:
 
103
You can supply default .inf data in this format:
 
104
    section.param=value
 
105
e.g.
 
106
    General.FullMachineName=foo.example.com
 
107
or
 
108
    "slapd.Suffix=dc=example,dc=com"
 
109
Values passed in this manner will override values in an .inf file given with the -f argument.
 
110
EOF
 
111
}
 
112
 
 
113
sub new {
 
114
    my $type = shift;
 
115
    my $self = {};
 
116
    $self = bless $self, $type;
 
117
    $self->init(@_);
 
118
    return $self;
 
119
}
 
120
 
 
121
sub init {
 
122
    my $self = shift;
 
123
    $self->{res} = shift;
 
124
    my ($silent, $inffile, $keep, $preonly, $logfile, $update, $force);
 
125
 
 
126
    GetOptions('help|h|?' => sub { VersionMessage(); HelpMessage(); exit 0 },
 
127
               'version|v' => sub { VersionMessage(); exit 0 },
 
128
               'debug|d+' => \$DSUtil::debuglevel,
 
129
               'silent|s' => \$silent,
 
130
               'file|f=s' => \$inffile,
 
131
               'keepcache|k' => \$keep,
 
132
               'preonly|p' => \$preonly,
 
133
               'logfile|l=s' => \$logfile,
 
134
               'update|u' => \$update,
 
135
               'continue|force|c' => \$force
 
136
               );
 
137
 
 
138
    $self->{silent} = $silent;
 
139
    $self->{keep} = $keep;
 
140
    $self->{preonly} = $preonly;
 
141
    $self->{update} = $update;
 
142
    $self->{force} = $force;
 
143
    $self->{logfile} = $logfile;
 
144
    $self->{log} = new SetupLog($self->{logfile});
 
145
    DSUtil::setDebugLog($self->{log});
 
146
    # if user supplied inf file, use that to initialize
 
147
    if (defined($inffile)) {
 
148
        $self->{inf} = new Inf($inffile);
 
149
    } else {
 
150
        $self->{inf} = new Inf;
 
151
    }
 
152
 
 
153
    # see if user passed in default inf values - also, command line
 
154
    # arguments override those passed in via an inf file - this
 
155
    # allows the reuse of .inf files with some parameters overridden
 
156
    if (!$self->{inf}->updateFromArgs(@ARGV)) {
 
157
        HelpMessage();
 
158
        exit 1;
 
159
    }
 
160
 
 
161
    # this is the base config directory - the directory containing
 
162
    # the slapd-instance instance specific config directories
 
163
    $self->{configdir} = $ENV{DS_CONFIG_DIR} || "@instconfigdir@";
 
164
}
 
165
 
 
166
# log only goes the the logfile
 
167
sub log {
 
168
    my $self = shift;
 
169
    my $level = shift;
 
170
    $self->{log}->logMessage($level, "Setup", @_);
 
171
}
 
172
 
 
173
# msg does to the screen and optionally to the log file
 
174
# if you use msg like this:
 
175
# msg(0, "some message")
 
176
# it will go only to the screen
 
177
# if you use msg like this:
 
178
# msg($WARN, "some message")
 
179
# it will go to the screen and to the log at the $WARN level
 
180
# all messages are localizable - you must define a resource key
 
181
# the first string passed to this method is a resource key
 
182
# additional strings are used as "arguments" to that resource key
 
183
# if you want to print un-localizable messages, use debug or write
 
184
# directly to the log or screen
 
185
sub msg {
 
186
    my $self = shift;
 
187
    my $level = shift;
 
188
    my @ary = @_;
 
189
    if (!$level && @ary) {
 
190
        # e.g. msg(0, "string") - no logging
 
191
    } elsif ($level and @ary and grep {/^$level$/} $self->{log}->levels()) {
 
192
        # e.g. msg($WARN, "string") - print and log
 
193
    } else {
 
194
        # log at default INFO level
 
195
        unshift @ary, $level;
 
196
        $level = $INFO;
 
197
    }
 
198
    # @text is an array of strings for one message or 
 
199
    # an array of array refs, each one is a message
 
200
    while (@ary) {
 
201
        my @text = shift @ary;
 
202
 
 
203
        last if (!@text or !$text[0]);
 
204
 
 
205
        # element is an array ref - just pass to getText
 
206
        # else is a list of strings
 
207
        # NOTE: this will NOT work if ary contains
 
208
        # consecutive simple string errors not separated
 
209
        # by an array ref e.g. this will work
 
210
        # ARRAY, 'errkey', arg, arg, ARRAY
 
211
        # this will not work
 
212
        # ARRAY, 'errkey', arg, 'errkey2', arg2, ARRAY
 
213
        while (@ary and !ref($ary[0])) {
 
214
            push @text, shift @ary;
 
215
        }
 
216
        my $string = $self->{res}->getText(@text);
 
217
        if ($level) {
 
218
            $self->log($level, $string);
 
219
        }
 
220
        print $string;
 
221
    }
 
222
}
 
223
 
 
224
sub doExit {
 
225
    my $self = shift;
 
226
    my $code = shift;
 
227
    if (!defined($code)) {
 
228
        $code = 1;
 
229
    }
 
230
 
 
231
    if ($code) {
 
232
        $self->msg($FATAL, 'setup_exiting', $self->{log}->{filename});
 
233
    } else {
 
234
        $self->msg($SUCCESS, 'setup_exiting', $self->{log}->{filename});
 
235
    }
 
236
        exit $code;
 
237
}
 
238
 
 
239
# get a list of the directory servers in configdir
 
240
sub getDirServers {
 
241
    my $self = shift;
 
242
    if (!$self->{dirservers}) {
 
243
        $self->{dirservers} = [];
 
244
        for my $dir (glob("$self->{configdir}/slapd-*")) {
 
245
            next if ($dir =~ /\.removed$/); # skip removed instances
 
246
            if (-d $dir) {
 
247
                $dir =~ s,$self->{configdir}/,,; # strip off dir part
 
248
                push @{$self->{dirservers}}, $dir;
 
249
            }
 
250
        }
 
251
    }
 
252
    return @{$self->{dirservers}};
 
253
}
 
254
 
 
255
 
 
256
#############################################################################
 
257
# Mandatory TRUE return value.
 
258
#
 
259
1;
 
260
 
 
261
# emacs settings
 
262
# Local Variables:
 
263
# mode:perl
 
264
# indent-tabs-mode: nil
 
265
# tab-width: 4
 
266
# End: