Setup.pm.in 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. # This perl module provides a way to set up a new installation after
  41. # the binaries have already been extracted. This is typically after
  42. # using native packaging support to install the package e.g. RPM,
  43. # pkgadd, depot, etc. This script will show the license, readme,
  44. # dsktune, then run the usual setup pre and post installers.
  45. #
  46. ##########################
  47. package Setup;
  48. use Exporter ();
  49. @ISA = qw(Exporter);
  50. @EXPORT = qw($SILENT $EXPRESS $TYPICAL $CUSTOM);
  51. @EXPORT_OK = qw($SILENT $EXPRESS $TYPICAL $CUSTOM);
  52. # hostname
  53. use Net::Domain qw(hostfqdn);
  54. # load perldap
  55. use Mozilla::LDAP::Conn;
  56. use Mozilla::LDAP::Utils qw(normalizeDN);
  57. use Mozilla::LDAP::API qw(ldap_explode_dn);
  58. use Mozilla::LDAP::LDIF;
  59. use Getopt::Long;
  60. use SetupLog;
  61. use DSUtil;
  62. use Inf;
  63. use strict;
  64. use vars qw($EXPRESS $TYPICAL $CUSTOM $SILENT);
  65. # the setup types
  66. $EXPRESS = 1;
  67. $TYPICAL = 2;
  68. $CUSTOM = 3;
  69. $SILENT = 4;
  70. # process command line options
  71. Getopt::Long::Configure(qw(bundling)); # bundling allows -ddddd
  72. sub VersionMessage {
  73. print "@capbrand@ Directory Server Setup Program Version @PACKAGE_VERSION@\n";
  74. }
  75. sub HelpMessage {
  76. print <<EOF;
  77. Usage: $0 [--options] -- [args]
  78. options:
  79. --help This message
  80. --version Print the version and exit
  81. --debug Turn on debugging
  82. --silent Use silent setup - no user input
  83. --file=name Use the file 'name' in .inf format to supply the default answers
  84. --keepcache Do not delete the temporary .inf file generated by this program
  85. --logfile Log setup messages to this file - otherwise, a temp file will be used
  86. --update Update an existing installation (e.g. after upgrading packages)
  87. --continue (update only) keep going despite errors (also --force)
  88. For all options, you can also use the short name e.g. -h, -d, etc. For the -d argument,
  89. specifying it more than once will increase the debug level e.g. -ddddd
  90. args:
  91. You can supply default .inf data in this format:
  92. section.param=value
  93. e.g.
  94. General.FullMachineName=foo.example.com
  95. or
  96. "slapd.Suffix=dc=example,dc=com"
  97. Values passed in this manner will override values in an .inf file given with the -f argument.
  98. EOF
  99. }
  100. sub new {
  101. my $type = shift;
  102. my $self = {};
  103. $self = bless $self, $type;
  104. $self->init(@_);
  105. return $self;
  106. }
  107. sub init {
  108. my $self = shift;
  109. $self->{res} = shift;
  110. my ($silent, $inffile, $keep, $preonly, $logfile, $update, $force);
  111. GetOptions('help|h|?' => sub { VersionMessage(); HelpMessage(); exit 0 },
  112. 'version|v' => sub { VersionMessage(); exit 0 },
  113. 'debug|d+' => \$DSUtil::debuglevel,
  114. 'silent|s' => \$silent,
  115. 'file|f=s' => \$inffile,
  116. 'keepcache|k' => \$keep,
  117. 'preonly|p' => \$preonly,
  118. 'logfile|l=s' => \$logfile,
  119. 'update|u' => \$update,
  120. 'continue|force|c' => \$force
  121. );
  122. $self->{silent} = $silent;
  123. $self->{keep} = $keep;
  124. $self->{preonly} = $preonly;
  125. $self->{update} = $update;
  126. $self->{force} = $force;
  127. $self->{logfile} = $logfile;
  128. $self->{log} = new SetupLog($self->{logfile});
  129. DSUtil::setDebugLog($self->{log});
  130. # if user supplied inf file, use that to initialize
  131. if (defined($inffile)) {
  132. $self->{inf} = new Inf($inffile);
  133. } else {
  134. $self->{inf} = new Inf;
  135. }
  136. # see if user passed in default inf values - also, command line
  137. # arguments override those passed in via an inf file - this
  138. # allows the reuse of .inf files with some parameters overridden
  139. if (!$self->{inf}->updateFromArgs(@ARGV)) {
  140. HelpMessage();
  141. exit 1;
  142. }
  143. # this is the base config directory - the directory containing
  144. # the slapd-instance instance specific config directories
  145. $self->{configdir} = $ENV{DS_CONFIG_DIR} || "@instconfigdir@";
  146. }
  147. # log only goes the the logfile
  148. sub log {
  149. my $self = shift;
  150. my $level = shift;
  151. $self->{log}->logMessage($level, "Setup", @_);
  152. }
  153. # msg does to the screen and optionally to the log file
  154. # if you use msg like this:
  155. # msg(0, "some message")
  156. # it will go only to the screen
  157. # if you use msg like this:
  158. # msg($WARN, "some message")
  159. # it will go to the screen and to the log at the $WARN level
  160. # all messages are localizable - you must define a resource key
  161. # the first string passed to this method is a resource key
  162. # additional strings are used as "arguments" to that resource key
  163. # if you want to print un-localizable messages, use debug or write
  164. # directly to the log or screen
  165. sub msg {
  166. my $self = shift;
  167. my $level = shift;
  168. my @ary = @_;
  169. if (!$level && @ary) {
  170. # e.g. msg(0, "string") - no logging
  171. } elsif ($level and @ary and grep {/^$level$/} $self->{log}->levels()) {
  172. # e.g. msg($WARN, "string") - print and log
  173. } else {
  174. # log at default INFO level
  175. unshift @ary, $level;
  176. $level = $INFO;
  177. }
  178. # @text is an array of strings for one message or
  179. # an array of array refs, each one is a message
  180. while (@ary) {
  181. my @text = shift @ary;
  182. last if (!@text or !$text[0]);
  183. # element is an array ref - just pass to getText
  184. # else is a list of strings
  185. # NOTE: this will NOT work if ary contains
  186. # consecutive simple string errors not separated
  187. # by an array ref e.g. this will work
  188. # ARRAY, 'errkey', arg, arg, ARRAY
  189. # this will not work
  190. # ARRAY, 'errkey', arg, 'errkey2', arg2, ARRAY
  191. while (@ary and !ref($ary[0])) {
  192. push @text, shift @ary;
  193. }
  194. my $string = $self->{res}->getText(@text);
  195. if ($level) {
  196. $self->log($level, $string);
  197. }
  198. print $string;
  199. }
  200. }
  201. sub doExit {
  202. my $self = shift;
  203. my $code = shift;
  204. if (!defined($code)) {
  205. $code = 1;
  206. }
  207. if ($code) {
  208. $self->msg($FATAL, 'setup_exiting', $self->{log}->{filename});
  209. } else {
  210. $self->msg($SUCCESS, 'setup_exiting', $self->{log}->{filename});
  211. }
  212. exit $code;
  213. }
  214. # get a list of the directory servers in configdir
  215. sub getDirServers {
  216. my $self = shift;
  217. if (!$self->{dirservers}) {
  218. $self->{dirservers} = [];
  219. for my $dir (glob("$self->{configdir}/slapd-*")) {
  220. next if ($dir =~ /\.removed$/); # skip removed instances
  221. if (-d $dir) {
  222. $dir =~ s,$self->{configdir}/,,; # strip off dir part
  223. push @{$self->{dirservers}}, $dir;
  224. }
  225. }
  226. }
  227. return @{$self->{dirservers}};
  228. }
  229. #############################################################################
  230. # Mandatory TRUE return value.
  231. #
  232. 1;
  233. # emacs settings
  234. # Local Variables:
  235. # mode:perl
  236. # indent-tabs-mode: nil
  237. # tab-width: 4
  238. # End: