Subversion Repositories havirt

Rev

Rev 10 | Rev 15 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10 Rev 12
Line 43... Line 43...
43
 
43
 
44
our @ISA = qw( Exporter );
44
our @ISA = qw( Exporter );
45
our @EXPORT = qw( 
45
our @EXPORT = qw( 
46
                  &readDB &writeDB
46
                  &readDB &writeDB
47
                  &report
47
                  &report
48
                  &loadNodePopulations
-
 
49
                  &loadNodeDB
-
 
50
                  &loadVirtDB
-
 
51
                );
48
                );
52
 
49
 
53
 
-
 
-
 
50
# read a DB file (just a YAML)
-
 
51
# if $lock is set, will create a "lock" file so other processes will
-
 
52
# not try to write to it. Using custom code as flock is automagically
-
 
53
# release when the file is read
54
 
54
 
55
sub readDB {
55
sub readDB {
56
   my ($filename) = @_;
56
   my $lock = shift;
-
 
57
   my $lockFileName = "$main::statusDBName.lock";
-
 
58
   my $lockTime = 5; # maximum time to wait for lock to clear
-
 
59
   # wait for lock to clear if it exists, if we are wanting a lock
-
 
60
   # and we have tried it for $locktime iterations
-
 
61
   while ( $lock && -f $lockFileName && $lockTime-- ) {
-
 
62
      sleep 1; # wait one second, then try again
-
 
63
   }
-
 
64
   if ( $lock ) {
-
 
65
      die "Something has $main::statusDBName locked, aborting\n" if -f $lockFileName;
-
 
66
      `touch $lockFileName`;
-
 
67
   }
57
   my $yaml = YAML::Tiny->new( {} );
68
   my $yaml = YAML::Tiny->new( {} );
58
   if ( -f $filename ) {
69
   if ( -f $main::statusDBName ) {
59
      $yaml = YAML::Tiny->read( $filename );
70
      $yaml = YAML::Tiny->read( $main::statusDBName );
60
   }
71
   }
61
   return $yaml->[0];
72
   $main::statusDB = $yaml->[0];
62
}
73
}
63
 
74
 
64
sub writeDB {
75
sub writeDB {
65
   my ($filename,$data) = @_;
-
 
66
   my $yaml = YAML::Tiny->new( $data );
76
   my $yaml = YAML::Tiny->new( $main::statusDB );
67
   $yaml->write( $filename );
77
   $yaml->write( $main::statusDBName );
68
}
-
 
69
 
-
 
70
sub loadNodePopulations {
-
 
71
   return if $main::nodePopulations;
-
 
72
   $main::nodePopulations = &readDB( $main::nodePopulationDBName );
78
   unlink "$main::statusDBName.lock" if -f "$main::statusDBName.lock"; # release any lock we might have on it
73
}
79
}
74
 
80
 
75
sub report {
81
sub report {
76
   if ( $main::reportFormat eq 'tsv' ) {
82
   if ( $main::reportFormat eq 'tsv' ) {
77
      return &report_tsv( @_ );
83
      return &report_tsv( @_ );
Line 118... Line 124...
118
      $output .= sprintf( $format, @{$data->[$row]} );
124
      $output .= sprintf( $format, @{$data->[$row]} );
119
   } # for row
125
   } # for row
120
   return $output;
126
   return $output;
121
}
127
}
122
 
128
 
123
# read the node database into memory, if it was not already loaded
-
 
124
sub loadNodeDB {
-
 
125
   print "In loadNodeDB, reading $main::nodeDBName\n" if $main::DEBUG > 1;
-
 
126
   return if $main::nodeDB;
-
 
127
   $main::nodeDB = &main::readDB( $main::nodeDBName );
-
 
128
   print "Success\n" if $main::DEBUG > 1;
-
 
129
}
-
 
130
 
-
 
131
sub loadVirtDB {
-
 
132
   return if $main::virtDB;
-
 
133
   $main::virtDB = &main::readDB( $main::domainDBName );
-
 
134
}
-
 
135
 
-