-
Notifications
You must be signed in to change notification settings - Fork 2
/
PerforceAdmin.pm
155 lines (120 loc) · 3.21 KB
/
PerforceAdmin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package PerforceAdmin;
use strict;
# core libs
use File::Spec;
use File::Path;
use File::Basename;
# custom libs
use PerforceMail;
# deduce the name of this script that is running
my $g_ScriptName = $0;
$g_ScriptName =~ s/\\/\//g;
$g_ScriptName =~ s/\/\//\//g;
$g_ScriptName = basename $g_ScriptName, ".pl";
# deduce the dir the script is running from
my $g_ScriptDir = dirname $0;
# make name for our log file
my $g_LogFile ="$g_ScriptName.log";
sub ScriptDir
{
return $g_ScriptDir;
}
sub NetworkStorage
{
my $computer = shift;
if ( !defined $computer )
{
$computer = $ENV{ COMPUTERNAME };
}
return File::Spec->catfile( $ENV{ PERFORCE_NETWORK_STORAGE }, $computer );
}
sub Startup
{
# check the declaration of the network storage location
die "PERFORCE_NETWORK_STORAGE is not defined in the environment" if not defined $ENV{ PERFORCE_NETWORK_STORAGE };
# check the definition of the computer name
die "COMPUTERNAME is not defined in the environment" if not defined $ENV{ COMPUTERNAME };
# rotate the log file for this script
system( File::Spec->catfile( $g_ScriptDir, "rotate_log.pl" ) . " $g_LogFile 10" );
# redirect all the output of this to the log file
open(STDOUT, '>', $g_LogFile) || die "Can't redirect stdout";
open(STDERR, ">&STDOUT") || die "Can't redirect stderr";
# make unbuffered
select STDOUT;
$| = 1;
select STDERR;
$| = 1;
# dump server info
system( "p4 info" );
}
sub Shutdown
{
# stop redirecting output
close(STDERR);
close(STDOUT);
# gather the log text
open( LOG, $g_LogFile );
my @lines = <LOG>;
close( LOG );
# send notification that we are starting the script
PerforceMail::SendMessage( "$g_ScriptName completed on $ENV{COMPUTERNAME}", join( '', @lines ), $g_LogFile );
}
sub Exit
{
my $code = shift;
Shutdown();
exit( $code );
}
sub Execute
{
my $command = shift;
my $startTime = time;
print("\n>>>> Executing: $command\n");
system($command);
my $elapsed = time - $startTime;
my $elapsedTime = sprintf( "%dm, %ds", int $elapsed / 60, $elapsed % 60 );
print("<<<< Took $elapsedTime\n");
return $? >> 8;
}
sub Trace
{
my $command = shift;
my $startTime = time;
print("\n>>>> Executing: $command\n");
my @result = `$command`;
print("@result");
my $elapsed = time - $startTime;
my $elapsedTime = sprintf( "%dm, %ds", int $elapsed / 60, $elapsed % 60 );
print("<<<< Took $elapsedTime\n");
return @result;
}
sub Publish
{
my $file = shift;
# publish the journal backup to network storage
if ( defined $file )
{
# create the target directory
my $mkpathErrors = [ ];
mkpath( PerforceAdmin::NetworkStorage(), { error => \$mkpathErrors } );
if ( @$mkpathErrors )
{
print("Unable to mkpath '" . PerforceAdmin::NetworkStorage() . "'\n");
Exit( 1 );
}
# copy journal file to backup location
PerforceAdmin::Execute("copy /v $file " . PerforceAdmin::NetworkStorage());
# check the copy result
if ( $? eq 0 )
{
# erase local copy
PerforceAdmin::Execute("del /f $file");
}
else
{
# do NOT delete the local file
print "Failed to copy '$file' to '" . PerforceAdmin::NetworkStorage() . "'\n";
}
}
}
1;