Crop/Install.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 99 CC 4 SCAR 20.8
CC
4
Cov
0%
sql
20.0
new
2.0
complete
2.0
CRAP 8.0
1 package Crop::Install;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Crop::Install - Installation utilities for the Crop framework
8  
9 =head1 SYNOPSIS
10  
11 use Crop::Install;
12 # ...usage...
13  
14 =head1 DESCRIPTION
15  
16 Crop::Install provides installation-related utilities for the Crop framework.
17  
18 =head1 AUTHORS
19  
20 Euvgenio (Core Developer)
21  
22 Alex (Contributor)
23  
24 =head1 COPYRIGHT AND LICENSE
25  
26 Apache 2.0
27  
28 =cut
29  
30 use base qw/ Crop /;
31  
32 =begin nd
33 Class: Crop::Install
34 Generate the Apache config and run sql scripts.
35  
36 Example:
37 (start code)
38 #! /usr/bin/perl
39  
40 use v5.14;
41 use warnings;
42  
43 use Crop::Install;
44  
45 my $install = Crop::Install->new(__FILE__);
46  
47 $install->generate_fcgi_config('init.conf');
48 $install->sql('http.sql');
49 $install->complete;
50 (end)
51 =cut
52  
53 use v5.14;
54 use warnings;
55  
56 use IO::File;
57 use Pg::CLI::psql;
58  
59 use constant {
60 # FCGI_TPL_PATH => '/fcgi',
61 # FCGI_CONF_PATH => '/conf/fcgid.conf.example',
62 SQL_PATH => '/sql',
63 DONE_PATH => '/steps.done',
64 DEFAULT_DB => 'main',
65 };
66  
67 =begin nd
68 Constructor new ($stepname, $db)
69 Remember $stepname and $db.
70
71 Parameters:
72 $stepname - step filename withou dir
73 $db - optinal database name
74
75 Returns:
76 $self
77 =cut
78 sub new {
79 my ($class, $stepname, $db) = @_;
80  
81 $stepname =~ s!^.*/!!;
82 $db //= DEFAULT_DB;
83  
84 bless {
85 stepname => $stepname,
86 db => $db,
87 }, $class;
88 }
89  
90 =begin nd
91 Method: complete ($message)
92 Finalize step.
93
94 Parameters:
95 $message - to caller
96 =cut
97 sub complete {
98 my ($self, $message) = @_;
99  
100 my $path = $self->C->{install}{path} . DONE_PATH;
101
102 my $completed = IO::File->new(">> $path") or die "Can't open special 'steps.completed' file '$path': $!";
103 say $completed "$self->{stepname}";
104 close $completed or die "Can't close special 'steps.completed' file $path: $!";
105  
106 binmode STDOUT, ":utf8";
107 say $message || "Done";
108 }
109  
110 =begin nd
111 Method: sql ($filename)
112 =cut
113 sub sql {
114 my ($self, $filename) = @_;
115  
116 my $undo = $ARGV[0];
117 if ($undo && $undo eq '--undo') {
118 $filename =~ s/(\.sql)\Z/\.undo$1/g;
119 print "> SQL undo step: ".$self->{stepname}." called\n";
120 print "> undo filename=$filename;\n";
121 } else {
122 print "> SQL commit step: ".$self->{stepname}." called\n";
123 print "> filename=$filename;\n";
124 }
125
126 die 'No sql filename specified' unless defined $filename and $filename =~ /\.sql$/;
127  
128 my $C = $self->C;
129  
130 # die "It's not allowed to alter tables with that script. Check the <sql_master> tag in global.xml." unless $C->{sql_master};
131  
132 my $dbconf = $C->{warehouse}{db}{$self->{db}};
133 my $statement_file = $C->{install}{path} . SQL_PATH . "/$filename";
134
135 my $fh = IO::File->new("< $statement_file") or die "Can't open SQL file $statement_file: $!";
136 my $statement;
137 while (<$fh>) {
138 s/\@{2}DBUSER\@{2}/$dbconf->{role}{user}{login}/g;
139 $statement .= $_;
140 }
141  
142 $fh->close or die "Can not close SQL file: $!";
143  
144 my $psql = Pg::CLI::psql->new(
145 username => $dbconf->{role}{admin}{login},
146 password => $dbconf->{role}{admin}{pass},
147 host => $dbconf->{server}{host},
148 port => $dbconf->{server}{port},
149 );
150  
151 my $errors;
152 my $s = $psql->run(
153 database => $dbconf->{name},
154 stdin => \$statement,
155 stderr => \$errors
156 );
157  
158 die "Can not execute:\n$statement \n $errors" if $errors =~ /ERROR:/;
159 }
160  
161 1;