"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/utils/AbsenceInstall.pm" (20 Oct 2013, 8177 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbsenceInstall.pm" see the Fossies "Dox" file reference documentation.

    1 #======================================================================
    2 #    This file is part of Absence.
    3 #
    4 #    Absence is free software: you can redistribute it and/or modify
    5 #    it under the terms of the GNU General Public License as published by
    6 #    the Free Software Foundation, either version 3 of the License, or
    7 #    (at your option) any later version.
    8 #
    9 #    Absence is distributed in the hope that it will be useful,
   10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 #    GNU General Public License for more details.
   13 #
   14 #    You should have received a copy of the GNU General Public License
   15 #    along with Absence.  If not, see <http://www.gnu.org/licenses/>.
   16 #======================================================================
   17 
   18 # copyright Robert Urban
   19 
   20 package AbsenceInstall;
   21 
   22 use FileHandle;
   23 use IPC::Open3;
   24 use IO::Select;
   25 use POSIX ":sys_wait_h";
   26 use English;
   27 use DBI;
   28 use Exporter;
   29 
   30 use AbsenceUtils;
   31 
   32 @ISA = qw(Exporter);
   33 @EXPORT = qw();
   34 @EXPORT_OK = qw();
   35 
   36 #use AbsenceImport;
   37 
   38 my $VERBOSE = 1;
   39 
   40 #-------------------------------------------------------------------------
   41 # popuateDatabase()
   42 #   reads an sql-file and runs commands
   43 # params:
   44 #   user    => <admin-user>
   45 #   pass    => <admin-pass>
   46 #   name    => <database-name>
   47 #   file    => <sql-input-file>
   48 #-------------------------------------------------------------------------
   49 sub populateDatabase
   50 {
   51     my %args = @_;
   52 
   53     my $dbh = connectDb($args{user}, $args{pass}, $args{name}, $args{host});
   54 
   55     my @statements = readSqlFile($args{file});
   56 
   57     foreach my $sql (@statements) {
   58         if (!executeSql($dbh, $sql)) {
   59             die "sql statment failed:\n$sql\n--end--.\n";
   60         }
   61     }
   62     $dbh->disconnect();
   63     return 1;
   64 }
   65 
   66 sub testConnection
   67 {
   68     my %args = @_;
   69 
   70     my $dbh = connectDb($args{user}, $args{pass}, 'template1', $args{host});
   71 
   72     if (defined($dbh)) {
   73         $dbh->disconnect;
   74         return 1;
   75     }
   76 
   77     return 0;
   78 }
   79 
   80 #-------------------------------------------------------------------------
   81 # adminCreateAbsenceUser
   82 # creates the db-user and the db
   83 # params:
   84 #   user    => <admin-user>
   85 #   pass    => <admin-pass>
   86 #   host    => <db-host>
   87 #   db_user => <owner of absence-db>
   88 #   db_pass => <owner password>
   89 #   create_db   => [0|1]    # give the user CREATEDB privilege
   90 #-------------------------------------------------------------------------
   91 sub adminCreateAbsenceUser
   92 {
   93     my %args = @_;
   94 
   95     my $dbh = connectDb($args{a_user}, $args{a_pass}, 'template1', $args{host});
   96     if (!defined($dbh)) {
   97         die "unable to connect to host [$args{host}], user='$args{user}, pass='$args{pass}\n";
   98     }
   99 
  100     my $create_db = $args{create_db} ? 'CREATEDB' : 'NOCREATEDB';
  101 
  102     #-------------------------------------
  103     # create db owner user
  104     #-------------------------------------
  105     my $sql = qq{
  106         CREATE ROLE $args{db_user}
  107             NOSUPERUSER $create_db NOCREATEROLE INHERIT LOGIN;
  108     };
  109 
  110     $VERBOSE && print "creating db-user '$args{db_user}'\n";
  111     if (!executeSql($dbh, $sql)) {
  112         die "attempt to create absence user failed.\n";
  113     }
  114 
  115     #-------------------------------------
  116     # set password
  117     #-------------------------------------
  118     $sql = qq{
  119         ALTER USER $args{db_user} ENCRYPTED PASSWORD '$args{db_pass}';
  120     };
  121     $VERBOSE && print "setting password for db-user to '$args{db_pass}'\n";
  122     #print "SQL=[$sql]\n";
  123     if (!executeSql($dbh, $sql)) {
  124         die "attempt to set password for absence user failed.\n";
  125     }
  126 
  127     #-------------------------------------
  128     # disconnect as admin
  129     #-------------------------------------
  130     $dbh->disconnect();
  131 }
  132 
  133 sub adminCheckUser
  134 {
  135     my %args = @_;
  136 
  137     my $dbh = connectDb($args{a_user}, $args{a_pass}, 'template1', $args{host});
  138 
  139     if (!defined($dbh)) {
  140         die "unable to connect to host [$args{host}], user='$args{a_user}, pass='$args{a_pass}\n";
  141     }
  142 
  143     my $sql = qq{
  144         SELECT * FROM pg_user WHERE usename = '$args{user}';
  145     };
  146 
  147     my $sth = $dbh->prepare($sql);
  148     defined($sth) || die "adminCheckUser: prepare for SQL [$sql] failed";
  149 
  150     $sth->execute || die "execute of SQL [$sql] failed";
  151     my $res = $sth->fetchrow_hashref;
  152     $sth->finish;
  153     $dbh->disconnect();
  154 
  155     return defined($res) ? 1 : 0;
  156 }
  157 
  158 sub checkUser
  159 {
  160     my %args = @_;
  161     my $dbh = connectDb($args{db_user}, $args{db_pass}, 'template1', $args{host});
  162     if (!defined($dbh)) {
  163         return 'not_exist';
  164     }
  165 
  166     my $sql = "SELECT * FROM pg_roles WHERE rolname = '$args{db_user}';";
  167 
  168     my $sth = $dbh->prepare($sql);
  169     defined($sth) || die "checkUser: prepare for SQL [$sql] failed";
  170 
  171     $sth->execute || die "checkUser: execute of SQL [$sql] failed";
  172     my $res = $sth->fetchrow_hashref;
  173     $sth->finish;
  174     $dbh->disconnect();
  175 
  176     if (!$res->{rolcreatedb}) { return 'no_createdb'; }
  177 
  178     return 'ok';
  179 }
  180 
  181 sub checkDatabase
  182 {
  183     my %args = @_;
  184 
  185     #my $dbh = connectDb($args{a_user}, $args{a_pass}, 'template1', $args{host});
  186     my $dbh = connectDb($args{db_user}, $args{db_pass}, 'template1', $args{host});
  187 
  188     if (!defined($dbh)) {
  189         die "unable to connect to host [$args{host}], user='$args{a_user}, pass='$args{a_pass}\n";
  190     }
  191 
  192     my $sql = qq{
  193         SELECT * FROM pg_database WHERE datname = '$args{db}';
  194     };
  195 
  196     my $sth = $dbh->prepare($sql);
  197     defined($sth) || die "checkDatabase: prepare for SQL [$sql] failed";
  198 
  199     $sth->execute || die "execute of SQL [$sql] failed";
  200     my $res = $sth->fetchrow_hashref;
  201     $sth->finish;
  202     $dbh->disconnect();
  203 
  204     return defined($res) ? 1 : 0;
  205 }
  206 
  207 sub dropDatabase
  208 {
  209     my %args = @_;
  210 
  211     my $dbh = connectDb($args{a_user}, $args{a_pass}, 'template1', $args{host});
  212 
  213     if (!defined($dbh)) {
  214         die "unable to connect to host [$args{host}], user='$args{a_user}, pass='$args{a_pass}\n";
  215     }
  216 
  217     my $sql = qq{
  218         DROP DATABASE $args{db};
  219     };
  220 
  221     my $rv = $dbh->do($sql);
  222     $dbh->disconnect();
  223     defined($rv) || die "drop database failed";
  224 }
  225 
  226 sub dropUser
  227 {
  228     my %args = @_;
  229 
  230     my $dbh = connectDb($args{a_user}, $args{a_pass}, 'template1', $args{host});
  231 
  232     if (!defined($dbh)) {
  233         die "unable to connect to host [$args{host}], user='$args{a_user}, pass='$args{a_pass}\n";
  234     }
  235 
  236     my $sql = qq{
  237         DROP ROLE $args{user};
  238     };
  239 
  240     my $rv = $dbh->do($sql);
  241     $dbh->disconnect();
  242     defined($rv) || die "drop role failed";
  243 }
  244 
  245 #-------------------------------------------------------------------------
  246 # createAbsenceDb()
  247 # params:
  248 #   user        => <user>   # connect with this user
  249 #   pass        => <pass>   # password for above user
  250 #   owner       => <owner>  # db-owner
  251 #   name        => <name>   # db-name
  252 #   tablespace  => <ts>     # default-tablespace
  253 #-------------------------------------------------------------------------
  254 sub createAbsenceDb
  255 {
  256     my %args = @_;
  257 
  258     my $dbh = connectDb($args{user}, $args{pass}, 'template1', $args{host});
  259     if (!defined($dbh)) {
  260         die "unable to connect to host [$args{host}], user='$args{user}, pass='$args{pass}\n";
  261     }
  262 
  263     my $ts = exists($args{tablespace})
  264         ? "TABLESPACE '$args{tablespace}'"
  265         : '';
  266     
  267     my $owner = '';
  268     if (exists($args{owner})) {
  269         $owner = "OWNER $args{owner}";
  270     }
  271 
  272     my $sql = qq{
  273         CREATE DATABASE $args{name} WITH ENCODING 'UTF8' ${ts} $owner
  274     };
  275     $VERBOSE && print "creating DB '$args{name}'\n";
  276     if (!executeSql($dbh, $sql)) {
  277         die "create DB '$args{name} failed.\n";
  278     }
  279 
  280     #-------------------------------------
  281     # disconnect 
  282     #-------------------------------------
  283     $dbh->disconnect();
  284 }
  285 
  286 sub readSqlFile
  287 {
  288     my $file = shift;
  289 
  290     my $comment = 0;
  291     my $debug = 0;
  292 
  293     my $fh = FileHandle->new($file);
  294     defined($fh) || die "unable to open [$file] for reading: $!";
  295 
  296     my @sql;
  297     my $sql = '';
  298     my $quoted = 0;
  299 
  300     while(<$fh>) {
  301         $debug && print "LINE: $_";
  302         if ($comment) {
  303             if (m!\*/(.*)$!) {
  304                 $debug && print "  comment = 0\n";
  305                 $comment = 0;
  306                 if (length($1)) {
  307                     $_ = $1;
  308                     redo;
  309                 }
  310             }
  311         } else {
  312             if (/^\s*$/) { next; }
  313             if (m!^\s*/\*(.*)$!) {
  314                 $debug && print "  comment = 1\n";
  315                 $comment = 1;
  316                 my $rest = $1;
  317                 if ($rest =~ m!\*/(.*)$!) {
  318                     $comment = 0;
  319                     if (length($1)) {
  320                         $_ = $1;
  321                         redo;
  322                     }
  323                 }
  324                 next;
  325             }
  326             $sql .= $_;
  327             if ($quoted) {
  328                 if (m!^'!) {
  329                     $quoted = 0;
  330                     $debug && print "  quoted = 0\n";
  331                 }
  332             } else {
  333                 if (m!'$!) {
  334                     $quoted = 1;
  335                     $debug && print "  quoted = 1\n";
  336                     next;
  337                 }
  338             }
  339             if (!$quoted) {
  340                 if ($sql =~ /;\s*$/) {
  341                     push(@sql, $sql);
  342                     $sql = '';
  343                     $debug && print "  -end of sql-\n";
  344                 }
  345             }
  346         }
  347     }
  348     $fh->close;
  349 
  350     return @sql;
  351 }
  352 
  353 1;