CVS: ispman/scripts make_midgard.pl,NONE,1.1.2.1 make.pl,1.16,1.16.2.1

Tarjei Huse <[email protected]>
Newsgroups gmane.comp.isp.ispman.cvs
Message-ID <[email protected]>
Update of /cvsroot/ispman/ispman/scripts
In directory sc8-pr-cvs1:/tmp/cvs-serv30245/scripts

Modified Files:
      Tag: ALTVHOSTS
	make.pl 
Added Files:
      Tag: ALTVHOSTS
	make_midgard.pl 
Log Message:
This should be everything for the ALTVHOSTbranch to be functional.

The todolist is long, but at least it's all commited now.




--- NEW FILE ---
#!/usr/bin/perl
#Author: Atif Ghaffar


#---------------------------------------------------------------------
package Text::Wrapper;
#
# Copyright 1998 Christopher J. Madsen
#
# Author: Christopher J. Madsen <[email protected]>
# Created: 06 Mar 1998
# Version: 1.000 (27-Oct-1998)
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the
# GNU General Public License or the Artistic License for more details.
#
# Word wrap text by breaking long lines
#---------------------------------------------------------------------

require 5.004;
use Carp;
use strict;
use vars qw($AUTOLOAD $VERSION);

#=====================================================================
# Package Global Variables:

BEGIN
{
    # Convert RCS revision number to d.ddd format:
    $VERSION = sprintf('%d.%03d', '1.000 ' =~ /(\d+)\.(\d+)/);
} # end BEGIN

#=====================================================================
# Methods:
#---------------------------------------------------------------------
# Provide methods for getting/setting fields:

sub AUTOLOAD
{
    my $self = $_[0];
    my $type = ref($self) or croak("$self is not an object");
    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion
    my $field = $name;
    $field =~ s/_([a-z])/\u$1/g; # squash underlines into mixed case
    unless (exists $self->{$field}) {
        # Ignore special methods like DESTROY:
        return undef if $name =~ /^[A-Z]+$/;
        croak("Can't locate object method \"$name\" via package \"$type\"");
    }
    return $self->{$field} = $_[1] if $#_;
    $self->{$field};
} # end AUTOLOAD

#---------------------------------------------------------------------
sub new
{
    my $self = bless {
        'bodyStart' => '',
        'columns'   => 70,
        'parStart'  => '',
    }, shift;

    croak "Missing parameter" unless (scalar @_ % 2) == 0;
    while (@_) {
        $AUTOLOAD = shift;
        defined eval { &AUTOLOAD($self, shift) }
        or croak("Unknown parameter `$AUTOLOAD'");
    }

    $self;
} # end new

#---------------------------------------------------------------------
sub wrap
{
    my $self = shift;
    my $width = $self->{'columns'};
    my $text = $self->{'parStart'};
    my $length = length $text;
    my $lineStart = $length;
    my $parStart = $text;
    my $parStartLen = $length;
    my $continue = "\n" . $self->{'bodyStart'};
    my $contLen  = length $self->{'bodyStart'};
    pos($_[0]) = 0;             # Make sure we start at the beginning
    for (;;) {
        if ($_[0] =~ m/\G[ \t]*(\n+)/gc) {
            $text .= $1 . $parStart;
            $lineStart = $length = $parStartLen;
        } else {
            $_[0] =~ m/\G(\s*(?:[^-\s]+-*|\S+))/g or last;
            my $word = $1;
          again:
            if (($length + length $word <= $width) or ($length == $lineStart)) {
                $length += length $word;
                $text .= $word;
            } else {
                $text .= $continue;
                $lineStart = $length = $contLen;
                $word =~ s/^\s+//;
                goto again;
            }
        }
    } # end forever
    if ($length != $lineStart) { $text .= "\n" }
    else { $text =~ s/(?:\Q$continue\E|\n\Q$parStart\E)\Z/\n/ }

    $text;
} # end wrap

#=====================================================================
# Package Return Value:

1;

#__END__#

package main;
no strict;

$wrapper = Text::Wrapper->new(columns => 60);



$OLD_CONF_FILE="/etc/ispman/conf/ispman.conf";
$CONF_FILE="tmp/conf/ispman.conf";
$LDAP_CONF="tmp/conf/ldap.conf";
$SLAPD_CONF="tmp/conf/slapd.conf";
$LDIF_DIR="tmp/ldif";
$INSTALL_TIMESTAMP=0;
$ISUPDATE=0;
$HOSTNAME=$ENV{'HOSTNAME'}||`hostname`;
chomp $HOSTNAME;
$PWD=$ENV{'PWD'};
system("clear");

#this hashref defines sections that we need to configure and their descriptions.
#each key is a section eg ldap, mail, dns etc
#description is what the user sees when configuring these sections
#vars is the list of vars that we should set. See $vars hashref
#optional  . err I forgot what that for.


my $vars_info={
   conf_vars =>{
      'description' => qq|
         These are minimum questions that we will keep in the configuration file.
         The rest of the information will be kept in the LDAP directory and can
			be edited using the web interface.
      |,
      'vars' => [qw(
         domain
         rootEmail
         ldapBaseDN
         ldapRootDN
         ldapRootPass
         ldapHost
         ldapInstallPath
         imapAdminUsername
         imapAdminPass
         imapPort
         installDir
         debug
         logmethod
         logfacility
         logsock
         minuid
         maxuid
         mingid
         maxgid
         pid_file
         log_file
         msgSpoolDir
         MidgardStagingDB_name
         MidgardStagingDB_user
         MidgardStagingDB_pass
         MidgardStagingDB_blobdir
         MidgardLiveDB_name
         MidgardLiveDB_user
         MidgardLiveDB_pass
         MidgardLiveDB_blobdir
         
	)],
      'optional' => 1,  # this should not be 1 , otherwise default substitution will take place
   },
   web_vars =>{
      'description' => qq|
         These are minimum questions that relate to web installation
      |,
      'vars' => [qw(ispmanUrlPrefix)],
      'optional' => 1,  # this should not be 1 , otherwise default substitution will take place
   },
   midgard_vars => {
      'description' => qq|
         Variables related to the Midgard extentions
      |,
      vars => [qw(
        MidgardStagingDB_name
        MidgardStagingDB_user
        MidgardStagingDB_pass
        MidgardStagingDB_blobdir
        MidgardLiveDB_name
        MidgardLiveDB_user
        MidgardLiveDB_pass
        MidgardLiveDB_blobdir)],
      'optional'=> 1,                                                    
   },
   
};

                                                                                                                           

#this hahref holds all variables that we need to set
#local defines that its only for install.pl use and no question is asked to the user.
# anything between %% and %% is evaluated as a key  of the  hashref $vars
#example %%hostname%% gets the value from $vars->{'hostname'}

my $vars={
   hostname => {
      'default' => $HOSTNAME,
      'question' =>"",
      'local' => 1,
   },

   domain => {
      'default' => "",
      'question' => "What is your domain? ",
   },

   minuid	=> {
           default => '1000',
           question => "When creating users or domains, what is the minimum uid that I should use",
   },
   maxuid	=> {
           default => '9999',
           question => "When creating users or domains, what is the maximum uid that I should use",
   },
   mingid	=> {
           default => '1000',
           question => "When creating users or domains, what is the minimum gid that I should use",
   },
   maxgid	=> {
           default => '9999',
           question => "When creating users or domains, what is the maximum gid that I should use",
   },
   pid_file	=> {
           default => '',
           question => "Where would you like the ispman-agent pid file created, blank means default location",
   },
   log_file	=> {
           default => '',
           question => "Where would you like the ispman-agent log file created, blank means default location",
   },
   debug => {
           default => '1',
           question => "Whats the debug level that you would like for this box? 
           0: none, 
           1: light, 
           2: heavy, 
           3: very heavy for debugging",
           
   },
   
   logmethod => {
           default => 'syslog',
           question => "Whats the log method that you would like? old | syslog
           Method 'syslog' is preferred method, because you can log centrally on a syslog server. 
           Method 'old'  prints the logs to STDERR if run from shell or to apache's error log if run via HTTP",
   },
   
   logfacility => {
           default => 'local5',
           question => "
           Log facility that you would like to use with syslog? 
           Ignore, if you are using 'old' as log method",
   },
   
   logsock => {
           default => 'unix',
           question => "Log socket for syslog unix | inet
           Use 'unix' if you want to use a unix socket to log to local syslog server.
           Use 'inet' if you are loging to a remote server, or if 'unix' does not work for you.
           Ignore, if you are using file based logging",
   },
   
      ldapInstallPath => {
              default => '/etc/openldap',
              question => "path to ldap configuration directory.",
      },

      ldapRootPass => {
              default => 'secret',
              question => "Password for the LDAP administrator",
              
      },
      ldapHost => {
              default => 'localhost',
              question => "Host name of master LDAP server.\n",
      },
      ldapBaseDN => {
                default => $ENV{'ldapBaseDN'} || 'o=ispman',
                question => "LDAP base DN.",
                
      },
      ldapRootDN => {
                default => 'cn=Directory Administrator, %%ldapBaseDN%%',
                question => "RootDN (Admin's DN) for the LDAP server",
                
      },
	imapAdminPass => {
		default => 'cyrus',
		question => "Password for the Cyrus admin",
		
	},
	imapPort => {
		default => '143',
		question => "IMAP port",
		
	},
	imapAdminUsername => {
		default => 'cyrus',
		question => "Cyrus Admin's username",
		
	},
        rootEmail => {
		default => 'root@%%domain%%',
		question => "Email of the administrator.\nThis email adress is used to send emails if something fails",
		
	},
	
	installDir => {
		default => $ENV{'ISPMAN_INSTALL_PATH'},
		question => "Directory where you want to install ispman components.\nVery Important.",
		
	},
  
        ispmanUrlPrefix => {
               default=>"",
               question => "prefix for ispman URI.\nIf you want to run on a dedicated server or a Virtualhost, \nyou can leave this value blank\n\nPlease leave it blank at the moment",
               },
   	msgSpoolDir => {
		default => '/tmp',
		question => "Directory where user notification messages are temporarily stored.",
		
	},
    usemidgard => {
        default => false,
        question => "Do you want to use Ispmans support for the Midgard CMS system?\n
                    Please note: if you do not want it, just go with the defaults to this \n
                    question and nothing will be installed.
                    ",
    },
    MidgardStagingDB_name => {
        default => 'midgardstaging',
        question => "What is the name of the Midgard Staging database.",
    },
    MidgardStagingDB_user => {
        default => 'midgardstaging',
        question => "What is the username to the midgard staging database?",
    },
    MidgardStagingDB_pass => {
        default => "",
        question => "What is the password to the staging database?",
    },
    MidgardStagingDB_blobdir => {
        default => "/ispman/blobs_staging",
        question => "Where is the blobdir for the staging database?",
    },
    MidgardLiveDB_name => {
        default => 'midgardlive',
        question => "What is the name of the Midgard live database.",
    },
    MidgardLiveDB_user => {
        default => 'midgardlive',
        question => "What is the username to the midgard live database?",
    },
    MidgardLiveDB_pass => {
        default => "",
        question => "What is the password to the live database?",
    },
    MidgardLiveDB_blobdir => {
        default => "/ispman/blobs_live",
        question => "Where is the blobdir for the live database?",
    },


  };



if (-e $OLD_CONF_FILE){
	$ISUPDATE=1;
        $existing_config=$OLD_CONF_FILE;
}

if (-e $CONF_FILE){
	$ISUPDATE=1;
        $existing_config=$CONF_FILE;
}


if (-e "/opt/ispman/conf/ispman.conf"){
	#this is the most recent
        $ISUPDATE=1;
        $existing_config="/opt/ispman/conf/ispman.conf";
}





if ($ISUPDATE) {
	print "Existing ISPMan installation found\nReading defaults from $existing_config\n";
	do $existing_config;
	*Config=*ISPMan::Config;
	$Config->{'imapHosts'}=join " ", @{$Config->{'imapHosts'}};
	# now read the new conf file so new InstallPath can be found if defined via --prefix
	do $CONF_FILE;

	for (keys %$Config){
		$vars->{$_}{'default'}=$Config->{$_};
	}
}


# this function subtitute the %%someting%% struct recursively and returns the value
# I am not sure about the recursive part, can someone please check and verify
sub getVar {
   my ($key, $branch)=@_;
   my $val=$vars->{$key}{$branch};
   ""=~/()/;
   $val=~s/%%(\w*)?%%/getVar($1, 'default')/ge;
   
   my @lines=();

   if ($branch eq "question") {
      @lines=split (/\n/, $val);
      for (@lines) {
         s/^ *//;
      }
      $val=join "\n", "", $wrapper->wrap(join "\n", @lines);
   }
   return $val;
}



# sets default values after substition if
# optional was set to 1 for $vars_info{$_}
sub setDefaults {
	print "Setting Defaults\n";
	for (@_) {
		$vars->{$_}{'default'}=getVar($_, 'default');
		print "\t$_=$vars->{$_}{'default'}\n";
	}
}



# this routine asks questions and saves defaults in the vars hash
sub configure{
	for (@_) {
           $question=getVar($_, 'question');
		if ($question=~/\S/) {
			print $question;
			if ($vars->{$_}{'default'}=~/\S/) {
				print " [";
				print getVar($_, 'default');
				print "]: ";
			}
			chomp(my $answer=<>);
			if ($answer) {
				$vars->{$_}{'default'}=$answer;
			} else {
				$vars->{$_}{'default'}=getVar($_, 'default');
			}
		}
	
	}
}



sub write_config {
	system("clear");
	print "Writting ispman.conf";
        open ("F", ">$CONF_FILE") or die "Cant open $CONF_FILE $!";
	print F "package ISPMan;\n";

        my $key;
	my $text="";
	for $key(keys %$vars_info) {
		my $description=$vars_info->{$key}{'description'};
		$description=~s/\n/\n# /g;
		print F "\n# $description\n";
		print F "\n";
		for my $var(@{$vars_info->{$key}{'vars'}}){
                        next if exists $vars->{$var}{'local'}; 
			
                        my $val=$vars->{$var}{'default'};
			##$val=~s/\@/\\@/g; #for the email address
			$text="\$Config->\{'$var'}";
			$text.=" " x (50-length($text)); # just beautifying
			$text.="= \'$val\';";
			print F "$text\n";
		
		}
	}





        #print F "\n1;\n";
	close(F);
}




sub main {
	for (qw(midgard_vars)){
		print "\n";
		print "*"x72, "\n";
		
                # just a convinience.
                # to neatly define description in the hash, without worrying about the white space displaying.
                $vars_info->{$_}{'description'}=~s/\n/<br> /g;
		$vars_info->{$_}{'description'}=~s/\s\s+//g;
		$vars_info->{$_}{'description'}=~s/\<br>/\n/g;

		print $vars_info->{$_}{'description'};
		print "\n";
		print "*"x72, "\n";
		if ($vars_info->{$_}{'optional'}) {
			print "You may skip this section to get the default values\nDo you want to set them up? y/n [y] ";
			chomp(my $ans=<>);
			if ($ans=~/^\s*n/i){
				setDefaults(@{$vars_info->{$_}{'vars'}});
				next;
			}
			$vars_info->{$_}{'set'}=1;
		}
		configure(@{$vars_info->{$_}{'vars'}});
		system("clear");
	}
	write_config();
}





sub get_backup_timestamp {
   unless ($INSTALL_TIMESTAMP) {
      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time); 
      ++$mon;
      $year+=1900;
      $mon=($mon<10)?"0$mon":$mon;
      $mday=($mday<10)?"0$mday":$mday;
      $INSTALL_TIMESTAMP="$year.$mon.$mday\_$hour.$min.$sec";
   }
   return $INSTALL_TIMESTAMP;
}

sub backup {
   my $file=shift;
   if (-e $file) {
      print "File $file already exists\nMaking backup to ";
      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time); 
      ++$mon;
      $year+=1900;
      $mon=($mon<10)?"0$mon":$mon;
      $mday=($mday<10)?"0$mday":$mday;
      
      my $backup_timestamp=get_backup_timestamp();
      my $backupfile= "$file-save-$backup_timestamp";
      print "$backupfile\n";
      system("cp $file $backupfile");
      print "chmoding 0600 $backupfile\n";
      system("chmod 0600 $backupfile");
   }
}





&main();
use Data::Dumper;
open "F", ">tmp/vars.pl";
print F Dumper($vars);
print F "\n1;\n";


print "\n\nConfiguration files have been created and are in the ./tmp directory.\n";
print "You may wish to examine them before installing further\n";


END{
   print "Enjoy!\n";
}

__END__


Index: make.pl
===================================================================
RCS file: /cvsroot/ispman/ispman/scripts/make.pl,v
retrieving revision 1.16
retrieving revision 1.16.2.1
diff -C2 -r1.16 -r1.16.2.1
*** make.pl	31 Jul 2003 15:12:41 -0000	1.16
--- make.pl	8 Sep 2003 16:19:20 -0000	1.16.2.1
***************
*** 179,182 ****
--- 179,183 ----
           log_file
           msgSpoolDir
+          
  	)],
        'optional' => 1,  # this should not be 1 , otherwise default substitution will take place
***************
*** 327,330 ****
--- 328,372 ----
  		
  	},
+     usemidgard => {
+         default => false,
+         question => "Do you want to use Ispmans support for the Midgard CMS system?\n
+                     Please note: if you do not want it, just go with the defaults to this \n
+                     question and nothing will be installed.
+                     ",
+     },
+     MidgardStagingDB_name => {
+         default => 'midgardstaging',
+         question => "What is the name of the Midgard Staging database.",
+     },
+     MidgardStagingDB_user => {
+         default => 'midgardstaging',
+         question => "What is the username to the midgard staging database?",
+     },
+     MidgardStagingDB_pass => {
+         default => "",
+         question => "What is the password to the staging database?",
+     },
+     MidgardStagingDB_blobdir => {
+         default => "/ispman/blobs_staging",
+         question => "Where is the blobdir for the staging database?",
+     },
+     MidgardLiveDB_name => {
+         default => 'midgardlive',
+         question => "What is the name of the Midgard live database.",
+     },
+     MidgardLiveDB_user => {
+         default => 'midgardstaging',
+         question => "What is the username to the midgard live database?",
+     },
+     MidgardLiveDB_pass => {
+         default => "",
+         question => "What is the password to the live database?",
+     },
+     MidgardLiveDB_blobdir => {
+         default => "/ispman/blobs_live",
+         question => "Where is the blobdir for the live database?",
+     },
+ 
+ 
    };
  



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.