%PDF- %PDF-
| Direktori : /etc/ansible/roles/web/library/ |
| Current File : //etc/ansible/roles/web/library/bx_test_site_in_directories |
#!/usr/bin/perl
# return exists or not dircetory for:
# site
# upload
# php sessions files
use strict;
use warnings;
use File::Spec;
use File::Basename qw( dirname basename );
use JSON;
use Data::Dumper;
# search="server_name www.bitrix.ru;" basedir=/etc/nginx/bx/site_avaliable regexp=\.conf$
my $options_file = $ARGV[0];
# parse ansible argv file
my $gtn_parse_ansible_argv = parse_ansible_argv ( $options_file );
if ( $gtn_parse_ansible_argv->[0] > 0 ) {
print_message(
{ 'msg' => $gtn_parse_ansible_argv->[1], 'failed' => "true" },
$gtn_parse_ansible_argv->[0],
);
}
my $site_wwwdir = File::Spec->catfile($gtn_parse_ansible_argv->[1]->{'base'}, $gtn_parse_ansible_argv->[1]->{'sitedir'} );
my $site_phpses = File::Spec->catfile($gtn_parse_ansible_argv->[1]->{'phpsess'}, $gtn_parse_ansible_argv->[1]->{'sitedir'} );
my $site_upload = File::Spec->catfile($gtn_parse_ansible_argv->[1]->{'upload'}, $gtn_parse_ansible_argv->[1]->{'sitedir'} );
foreach my $dir ( $site_wwwdir, $site_phpses, $site_upload ){
if ( -d $dir ){
# test if directory empty or not
my $file_count = 0;
opendir(my $td, $dir)
or print_message(
{ 'msg' => "Cannot read $dir: $!", 'failed' => 'true' },
1,
);
while(my $file = readdir($td)){
next if ($file =~ /^\.\.?$/);
$file_count++;
}
close $td;
if($file_count > 0){
print_message(
{ found => 1, directory => $dir },
0,
);
}
}
}
print_message(
{ found => 0 },
0,
);
exit 0;
# print json output for ansible
# input: return_hash, exit_code
sub print_message {
my $rh = shift;
my $c = shift;
my $json = to_json( $rh, pretty => 1 );
print $json;
exit $c;
}
# parse opt file
sub parse_ansible_argv {
my $f = shift;
my $r = {};
open ( my $fh, $f ) or return [ 1, "Cannot open options $f: $!" ];
while ( <$fh> ){
next if ( /^$/ );
if ( /base=(\S+)/ ) { $r->{'base'} = $1 };
if ( /phpsess=(\S+)/ ) { $r->{'phpsess'} = $1 };
if ( /upload=(\S+)/ ) { $r->{'upload'} = $1 };
if ( /sitedir=(\S+)/ ) { $r->{'sitedir'} = $1 };
}
close $fh;
if ( ! $r->{'base'} ) { return [ 1, "You must send site base directory via base option." ] };
if ( ! $r->{'phpsess'} ) { return [ 1, "You must send php session base directory via phpsess option." ] };
if ( ! $r->{'upload'} ) { return [ 1, "You must send upload base directory via upload option." ] };
if ( ! $r->{'sitedir'} ) { return [ 1, "You must send site directory via sitedir option." ] };
return [ 0, $r ];
}