Apache-SiteConfig

 view release on metacpan or  search on metacpan

lib/Apache/SiteConfig/Deploy.pm  view on Meta::CPAN


sub domain_alias  { 
    my $self = shift;
    $self->{args}->{domain_alias} = $_[0]; 
}

sub source  { 
    my ($self,$type,$uri) = @_;
    $self->{args}->{source} ||= {};
    $self->{args}->{source}->{ $type } = $uri;
}

sub webroot {
    my ($self,$path) = @_;
    $self->{args}->{webroot} = $path;
}

sub task ($&) {
    my ($self,$name,$closure) = @_;
    $self->{tasks}->{ $name } = $closure;
}

sub preprocess_meta {
    my $self = shift;
    my $args = { %{ $self->{args} } };  # copy args
    $args->{sites_dir} ||= File::Spec->join( '/var/sites' );
    $args->{site_dir} ||= File::Spec->join( $args->{sites_dir} , $args->{name} );
    $args->{document_root} = File::Spec->join( 
            $args->{site_dir} , $args->{webroot} );

    $args->{log_dir} ||= 
            File::Spec->join( $args->{sites_dir} , $args->{name} , 'apache2' , 'logs' );
                # File::Spec->join( '/var/log/sites/' , $args->{name} , 'apache2' , 'logs' )

    $args->{access_log} ||= File::Spec->join( $args->{log_dir} , 'access.log' );
    $args->{error_log}  ||= File::Spec->join( $args->{log_dir} , 'error.log' );
    return $args;
}

sub prepare_paths {
    my ($self,$args) = @_;
    for my $path ( qw(sites_dir site_dir document_root) ) {
        next unless $args->{ $path };
        mkpath [ $args->{ $path } ] unless -e $args->{ $path };
    }
}

sub prepare_log_path {
    my ($self,$args) = @_;
    mkpath [ $args->{log_dir} ] unless -e $args->{log_dir};

}

sub clean {
    my $self = shift;
    my $args = $self->preprocess_meta;
    say "Cleanning up $args->{site_dir}";
    rmtree( $args->{site_dir} );
}

sub update {
    my $self = shift;
    my $args = $self->preprocess_meta;

    if( $args->{source} ) {
        chdir $args->{site_dir};

        if( $args->{source}->{git} ) {
            my $branch = $args->{source}->{branch} || 'master';
            $self->execute_command("git pull origin $branch") if $branch eq 'master';
        } 
        elsif ( $args->{source}->{hg} ) {
            $self->execute_command("hg pull -u");
        }
    }

}

sub deploy {
    my ($self) = @_;
    my $args = $self->preprocess_meta;
    $self->prepare_paths( $args );

    SKIP_SOURCE_CLONE:
    if( $args->{source} ) {

        if( $args->{source}->{git} ) {
            last SKIP_SOURCE_CLONE if -e File::Spec->join( $args->{site_dir} , '.git' );
            say "Cloning git repository from $args->{source}->{git} to $args->{site_dir}";

            $self->execute_command("git clone $args->{source}->{git} $args->{site_dir}",1);

            # if branch is specified, then check the branch out.
            my $branch = $args->{source}->{branch};
            $self->execute_command("git checkout -t origin/$branch",1) if $branch;

            # if tag is specified, then check the tag out.
            my $tag = $args->{source}->{tag};
            $self->execute_command("git checkout $tag -b $tag",1) if $tag;

        }
        elsif( $args->{source}->{hg} ) {
            last SKIP_SOURCE_CLONE if -e File::Spec->join( $args->{site_dir} , '.git' );

            say "Cloning hg repository from $args->{source}->{hg} to $args->{site_dir}";
            $self->execute_command("hg clone $args->{source}->{hg} $args->{site_dir}") == 0 or die($?);
        }

    }

    $self->prepare_log_path( $args );

    if( $args->{chown} ) {
        say "Changing owner to $args->{site_dir}";
        $self->execute_command( sprintf( 'chown -R %s: ' , $args->{site_dir} ) ,1);
    }
    


    # Default template
    my $template = Apache::SiteConfig::Template->new;  # apache site config template



( run in 1.593 second using v1.01-cache-2.11-cpan-2398b32b56e )