head	1.10;
access;
symbols;
locks; strict;
comment	@# @;


1.10
date	2001.08.23.02.31.55;	author stack;	state Exp;
branches;
next	1.9;

1.9
date	2001.08.17.21.22.53;	author edk;	state Exp;
branches;
next	1.8;

1.8
date	2001.08.10.22.18.15;	author edk;	state Exp;
branches;
next	1.7;

1.7
date	2001.08.10.21.46.42;	author edk;	state Exp;
branches;
next	1.6;

1.6
date	2001.08.08.23.49.25;	author edk;	state Exp;
branches;
next	1.5;

1.5
date	2001.07.22.20.26.01;	author evan;	state Exp;
branches;
next	1.4;

1.4
date	2001.07.21.17.14.14;	author evan;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.06.17.42.55;	author edk;	state Exp;
branches;
next	1.2;

1.2
date	2001.05.02.23.08.13;	author stack;	state Exp;
branches;
next	1.1;

1.1
date	2001.04.26.04.53.00;	author edk;	state Exp;
branches;
next	;


desc
@@


1.10
log
@This commit guards against the case where the domain is already
set correctly. If the old domain is 'idendev.com' and the new is
'staging.idendev.com' AND the location we're to replace already has
'staging.idendev.com' -- i.e. the correct domain -- do not interpolate.
(edk work.  small change by stack).
@
text
@#!/usr/bin/perl -w
# see showHelp (or --help) for usage information.

use strict;
use File::Find;
use Getopt::Long;

my $harness = "$ENV{SANDBOX}/site/scripts/anzu/harness";

# split this into two parts, one for anzu related changes, and another
# to update fileserv/ references to hostname.  we may want to update this later.
# - evan
# 
main();
sub main
{
    $main::options = getOptions();

    handleAnzuLists();
    transformFiles();
    renameDomainDirs($main::options->{old}, $main::options->{new}); 
        
    if (!$main::options->{no_htaccess})
    {
        updateHtaccessFiles($main::options->{old}, $main::options->{new});
    }
}

# change all the files except for the subscribers lists, which we
# can't edit this way w/o corrupting them
sub transformFiles {
    # the sandbox should be down
    if (-e "$ENV{SANDBOX}/httpd.pid") {
        die "can't run while the sandbox is running\n";
    }
    system('/usr/bin/svok', "$ENV{SANDBOX}/site/scripts/anzu/anzu-server");
    my $exit_value  = $? >> 8;
    if (!$exit_value) {
        die "can't run while the anzu server is running\n";
    }

    # Traverse desired filesystems
    File::Find::find({wanted => \&wanted}, "$ENV{SANDBOX}/data");

}

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    /^subscribers$/s &&
    ($File::Find::prune = 1)
    ||
    ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
    -f _ && &peruseFile($File::Find::name);
}

sub peruseFile {
    my ($file) = @@_;
    my ($mark);

    chomp($file);

    open (FH, "< $file");
#    print "peruseFile: Opened $file\n";
    while (<FH>) { 
       if ($_ =~ /$main::options->{old}/) {
           $mark++;
       }
    }
    close (FH);

    if ($mark) {
        editFile($main::options->{old}, $main::options->{new}, $file);
        if ($main::options->{debug}) {
            print "peruseFile: File contains $main::options->{old} : $File::Find::name\n";
        }
    }
    $mark = 0;
}

sub handleAnzuLists
{
    print "Starting handleAnzuLists\n";
    system("$ENV{SANDBOX}/site/scripts/anzu/start_anzu");
    sleep(15);
    my $lists = getMailingLists();
    print "got lists @@$lists\n" if $main::options->{debug};
    if (not scalar(@@$lists))
    {
        warn "ERROR : no mailing lists???";
    }
    foreach my $list (@@$lists) {
        transformListSubscribers($list);
    }
    system("$ENV{SANDBOX}/site/scripts/anzu/stop_anzu");
}

sub transformListSubscribers
{
    my ($list_item) = @@_;
    my ($list, $project, $domain) = @@$list_item;
    my $list_dir_base = "$ENV{SANDBOX}/data/anzu/domains/$domain/" .
            "projects/$project/addresses/$list";
    foreach my $subdir ('', '/allow', '/mod', '/digest')
    {
        transformDirSubscribers($list_dir_base . $subdir);
    }
}

sub transformDirSubscribers
{
    my ($directory) = @@_;
    # first, the list of subscribers
    open (IN, "ezmlm-list $directory |");
    my @@subs_needing_transform = ();
    my $match_strict = '@@' . $main::options->{old} . '$';
    my $match_lenient = $main::options->{old};
    my $substitute =    $main::options->{new};
    while (<IN>) {
        chomp;
        next if not m/$match_strict/o;
        # we have a subscriber which should be transformed
        push @@subs_needing_transform, $_;
    }
    close IN;
    foreach my $subscription (@@subs_needing_transform) {
        my $new_subscription = $subscription;
        $new_subscription =~ s/$match_lenient/$substitute/g;
        if ($main::options->{debug}) {
            print "in $directory, $subscription => $new_subscription\n";
        }
        system("ezmlm-unsub", $directory, $subscription);
        system("ezmlm-sub", $directory, $new_subscription);
    }
}

# find the list of lists
sub getMailingLists
{
    my $command = "$harness dump 2>&1";
    open (LISTS, "$command |");
    my @@lists;
    while (<LISTS>) {
        print "harness: $_" if $main::options->{debug};
        next unless m/^list (\S+) (\S+) (\S+)$/;
        my ($domain, $project, $list) = ($1, $2, $3);
        print "got list $list \@@ $project . $domain\n" if
            $main::options->{debug};
        push @@lists, [$list, $project, $domain];
    }
    close LISTS;
    return \@@lists;
}

# Rename directories named for domains under eyebrowse and anzu.
#
sub renameDomainDirs
{
    my ( $old_domain, $new_domain ) = @@_;
    my $data_dir="$ENV{SANDBOX}/data";
    my $anzu_domain_dir="$data_dir/anzu/domains";
    my $eyebrowse_domain_dir="$data_dir/eyebrowse";

    renameDir( "$anzu_domain_dir/$old_domain", 
        "$anzu_domain_dir/$new_domain" );
    renameDir( "$eyebrowse_domain_dir/$old_domain", 
        "$eyebrowse_domain_dir/$new_domain" );

    return;
}

# Find .htaccess files and call the editing function
# note: make this use File::Find and pre-check file for old hostname
# string
#
sub updateHtaccessFiles
{
    my (@@list_of_files, $file_name);
      
    @@list_of_files = 
    `find $ENV{SANDBOX}/data/helm/fileserv/ -type f -name .htaccess`;

    if ($main::options->{debug}) {
        print "updateHtaccessFiles: Updating fileserv .htaccess files:\n";
    }

    foreach $file_name (@@list_of_files) {
        editFile($main::options->{old}, $main::options->{new}, $file_name);
    }
}

# Edit file. Change domain references from old to new.
#
sub editFile 
{
    my ($old_name, $new_name, $file_name) = @@_;
    my (@@edited_file);

    # The following guards against the case where the domain is already
    # set correctly. If the old domain is 'idendev.com' and the new is 
    # 'staging.idendev.com' AND the location we're to replace already has
    # 'staging.idendev.com' -- i.e. the correct domain -- do not interpolate.  
    #
    my $assert_needs_change = '';
    if ($new_name =~ m/(.+)$old_name/)
    {
       $assert_needs_change = $1;
    }

    open (CURRENT_FILE, "< $file_name") or die "unable to read $file_name: $!";
    while (<CURRENT_FILE>) {
        unless ($assert_needs_change)
        {
            $_ =~ s/$old_name/$new_name/g;
        }
        else
        {
            $_ =~ s/(?<!$assert_needs_change)$old_name/$new_name/g;
        }
        push (@@edited_file, $_);
    }
    close CURRENT_FILE;

    my $mode = (stat("$file_name"))[2] or do {
        warn "unable to stat $file_name: $!";
        return;
    };
    $mode &= 07555;
    # make sure it's writeable right now.
    # FIXME : rather than reading the file into memory, rename it to a temp
    # location, open the output file for writing, and write lines as we go.
    # then do the chmod at the end to ensure the perms remain the same.
    chmod 07755, $file_name;

    open (CURRENT_FILE, "> $file_name") or die "unable to write $file_name: $!";
    print CURRENT_FILE @@edited_file;
    close CURRENT_FILE;
      
    chmod $mode, $file_name;

    if ($main::options->{debug}) {
        print "editFile: Edited $file_name\n";
    }
}

# Rename directory if it exists.
#
sub renameDir
{
    my ( $old_name, $new_name ) = @@_;
    # print "RenameDir: $old_name, $new_name\n";
    
    if( -d "$old_name" )
    {
        rename( "$old_name", "$new_name" );
    }
} 

sub getOptions
{
    my %options;
    if (!GetOptions(\%options,
                "debug",
                "help",
                "no_htaccess",
                "new=s",
                "old=s")
            or $options{help}) {
        showUsage();
    }
    showUsage("old domain name required") unless $options{old};
    showUsage("new domain name required") unless $options{new};

    return \%options;
}

sub showUsage
{
    my ($message) = @@_;
    print <<EOM;
Usage: domain_change.pl [--debug] [--no_htaccess] [--help] [--anzu]
                --old={domain_name} --new={domain_name}
        help            show this message
        debug           include debugging output
        old             old domain
        new             new domain
        no_htaccess     do not modify .htaccess files

This script is designed to handle all the steps required to change the
domain name under the filesystem data directory at SANDBOX/data.
It does not change database contents.  Changing the domain through any 
other mechanism may cause problems.  This script does a variety of 
things to rename domains, subscribers, as well as using find and 
perl -p -ie to modify files.  You must have sourced env.sh before you 
can use it.

EOM

    print "\n\tERROR MESSAGE\n\t$message\n" if ($message);

    exit;
}


@


1.9
log
@provide an option to skip dealing with .htaccess files.
@
text
@d192 1
a192 1
# Edit a given .htaccess file 
d199 11
d212 8
a219 1
        $_ =~ s/$old_name/$new_name/g;
@


1.8
log
@for some reason, File::Find is leaving in newlines.
@
text
@d23 4
a26 1
    updateHtaccessFiles($main::options->{old}, $main::options->{new});
d247 1
d263 2
a264 2
Usage: domain_change.pl [--debug] [--help] [--anzu] [--fileserv] --old={domain_name}
                --new={domain_name}
d267 3
a269 2
        old_domain      old domain
        new_domain      new domain
@


1.7
log
@minor syntax changes, sequence reorg
@
text
@d58 2
@


1.6
log
@* cleanup the use of File::Find slightly
* standardize formatting
* make email stuff work (start anzu server, fix order of operation)
@
text
@d19 1
d21 1
d23 1
a23 3
    renameDomainDirs( $main::options->{old}, $main::options->{new} ); 
    handleAnzuLists();
    updateHtaccessFiles( $main::options->{old}, $main::options->{new});
d78 1
d83 4
d99 2
a100 1
    foreach my $subdir ('', '/allow', '/mod', '/digest') {
d201 4
a204 1
    my $mode = (stat("$file_name"))[2];
@


1.5
log
@
Issue number:
Obtained from:
Submitted by:
Reviewed by:
@
text
@d7 1
d21 3
a23 7
    my $lists = getMailingLists();
        foreach my $list (@@$lists) {
        transformListSubscribers($list);
        }
    renameDomainDirs( "$main::options->{old}", "$main::options->{new}" ); 
    updateHtaccessFiles( "$main::options->{old}", "$main::options->{new}");

d39 2
a40 5
# Note: replaced system find w/ File::Find and code generated by find2perl
# Possibly look at making the wanted function more portable for other calls
# to File::Find::find
#
# - evan
d42 1
a42 12
###
### below is the result of find2perl output  
###

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, "$ENV{SANDBOX}/data");
d47 1
a47 1
    /^subscribers\z/s &&
d51 1
a51 1
    -f _ && &peruseFile($name);
d61 3
a63 3
           if ($_ =~ /$main::options->{old}/) {
               $mark++;
           }
d68 4
a71 4
    editFile("$main::options->{old}", "$main::options->{new}", $file);
             if ($main::options->{debug}) {
                 print "peruseFile: File contains $main::options->{old} : $name\n";
             }
d76 10
a110 1
        print "got : '$_'\n";
d133 1
d167 1
a167 1
      my (@@list_of_files, $file_name);
d169 6
a174 2
      @@list_of_files = 
      `find $ENV{SANDBOX}/data/helm/fileserv/ -type f -name .htaccess`;
d176 3
a178 7
      if ($main::options->{debug}) {
      print "updateHtaccessFiles: Updating fileserv .htaccess files:\n";
      }

      foreach $file_name (@@list_of_files) {
      editFile("$main::options->{old}", "$main::options->{new}", $file_name);
      }
d185 2
d188 20
a207 2
      my ($old_name, $new_name, $file_name) = @@_;
      my (@@edited_file);
d209 3
a211 15
      open (CURRENT_FILE, "< $file_name");
      while (<CURRENT_FILE>) {
              $_ =~ s/$old_name/$new_name/g;
              push (@@edited_file, $_);
      }
      close CURRENT_FILE;

      open (CURRENT_FILE, "> $file_name");
      print CURRENT_FILE @@edited_file;
      close CURRENT_FILE;
      
      if ($main::options->{debug}) {
      print "editFile: Edited $file_name\n";
      }
      
@


1.4
log
@Reviewed by: Stack

Added File::Find functionality into domain_change.pl, and support
for updating hostname string for fileserv/ files.

- Evan
@
text
@a0 1

a1 1

d5 1
a5 1
use File::Find ();
@


1.3
log
@handle files w/ single quotes or other random characters which need to
be specially escaped
@
text
@d1 1
d7 1
d11 4
d19 1
d21 1
d23 1
a23 1
    foreach my $list (@@$lists) {
d25 3
a27 1
    }
a28 1
    renameDomainDirs( "$main::options->{old}", "$main::options->{new}" );
d44 51
a94 5
    system("/usr/bin/find $ENV{SANDBOX}/data -name subscribers -prune " .
        " -o -type f " .
        "-exec grep -s -q " . $main::options->{old} . " {} \\; -print0 | " .
        "xargs --null perl -pi -e ".
        "'s/\Q" . $main::options->{old} . "\E/" . $main::options->{new} . "/g'");
d170 45
d249 1
a249 1
Usage: domain_change.pl [--debug] [--help] --old={domain_name}
@


1.2
log
@Qualified varible names and added an escape to make the big find concat
palatable.  Updated the usage text.  Added renaming of domain dirs under
eyebrowse and anzu.
@
text
@d37 2
a38 2
        "-exec grep -l " . $main::options->{old} . " {} \\; | " .
        "xargs perl -pi -e ".
@


1.1
log
@untested (in this form) script to handle changing the domain name.  parts
of this have been tested, but it should be considered experimental until
well tested.
@
text
@d1 1
a1 1
#!/usr/bin/perl
d18 2
d35 5
a39 4
    system("/usr/bin/find $SANDBOX/data -name subscribers -prune -o -type f " .
           "-exec grep -l " . $options->{old} . " {} \; | " .
           "xargs perl -pi -e ".
           "'s/\Q" . $options->{old} . "\E/" . $options->{new} . "/g'");
d44 2
a45 2
    my ($list) = @@_;
    my ($list, $project, $domain) = @@$list;
d98 30
d157 6
a162 4
domain name for a given install.  Changing the domain through any other
mechanism may cause problems.  This script does a variety of things to
rename domains, subscribers, as well as using find and perl -p -ie to
modify files.  You must have sourced env.sh before you can use it.
@

