Apache-SiteConfig

 view release on metacpan or  search on metacpan

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

    }
}

sub execute_command {
    my ($self,$cmd, $abort_on_failure) = @_;

    if( $self->{args}->{su} ) {
        $cmd = sprintf( 'sudo -u %s %s', $self->{args}->{su} , $cmd );
    }

    say $cmd;
    if( $abort_on_failure ) {
        system( $cmd ) == 0 or die $!;
    } else {
        system( $cmd );
    }
}

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

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


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};

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

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
    my $context = $template->build( 
        ServerName => $args->{domain},
        ServerAlias => $args->{domain_alias},
        DocumentRoot => $args->{document_root},
        CustomLog => $args->{access_log} , 
        ErrorLog => $args->{error_log} 
    );
    my $config_content = $context->to_string;

    # get site config directory
    my $apache_dir_debian = '/etc/apache2/sites-available';
    if( -e $apache_dir_debian ) {
        say "Apache Site Config Dir Found.";

        my $config_file = File::Spec->join( $apache_dir_debian , $args->{name} );

        if ( -e $config_file ) {
            say "$config_file exists, skipped.";
        } else {
            say "Writing site config to $config_file.";
            open my $fh , ">", $config_file;
            print $fh $config_content;
            close $fh;

            say "Enabling $args->{name}";
            system("a2ensite $args->{name}");

            say "Reloading apache";
            system("/etc/init.d/apache2 reload");
        }
    } 
    else {

        # try to find where apachectl is located.
        my $apachectl_bin = qx(which apachectl);
        chomp( $apachectl_bin );

        my $apache_dir = dirname(dirname( $apachectl_bin ));
        my $apache_conf_dir = File::Spec->join( $apache_dir , 'conf' );

        if( -e $apache_conf_dir ) {
            say "Found apache configuration directory: $apache_conf_dir";
            # prepare site config dir
            mkpath [ File::Spec->join( $apache_conf_dir , 'sites' ) ];

            my $httpd_conf = File::Spec->join( $apache_conf_dir , 'httpd.conf' );

            # write site config to apache conf dir
            my $config_file =  File::Spec->join( $apache_conf_dir , 'sites' , $args->{name} . '.conf' );
            say "Writing config file: $config_file";
            open my $fh , ">" , $config_file or die $!;
            print $fh $config_content;
            close $fh;

            say "Appending Include statement to $httpd_conf";
            open my $fh2 , ">>" , $httpd_conf or die $!;
            print $fh2 "###### Apache Site Configurations \n";
            print $fh2 "Include conf/sites/$args->{name}.conf\n";
            close $fh2;

        } else {
            # apachectl not found
            mkpath [ 'apache2' ];

            # TODO: run apache configtest here
            # /opt/local/apache2/bin/apachectl -t -f /path/to/config file
            my $config_file = File::Spec->join(  'apache2' , 'sites' , $args->{name} );  # apache config
            mkpath [ File::Spec->join('apache2','sites') ];

            say "Writing site config file: $config_file";
            open my $fh , ">", $config_file or die "Can not write config file $config_file: $!";
            print $fh $config_content;
            close $fh;
        }

    }


}

t/02-template.t  view on Meta::CPAN

use feature ':5.10';
use Test::More;
use lib 'lib';
use Apache::SiteConfig;
use Apache::SiteConfig::Template;

# default template
my $template = Apache::SiteConfig::Template->new;
ok( $template );
# my $context = $template->build( );
# say $context->to_string;




done_testing;



( run in 0.814 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )