/
c3mc-ci-tag-save
executable file
·116 lines (87 loc) · 3.47 KB
/
c3mc-ci-tag-save
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
#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib
use strict;
use warnings;
use uuid;
use POSIX;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
$|++;
use MYDB;
use FindBin qw( $RealBin );
use MYDan::Util::OptConf;
use YAML::XS;
use Encode;
=head1 SYNOPSIS
$0 projectid version
=cut
my $db = MYDB->new( "/data/Software/mydan/CI/conf/conn", delayedconnection => 1 );
my %vcount;
sub vcount
{
my $projectid = shift @_;
return $vcount{$projectid} if defined $vcount{$projectid};
my $x = eval{ $db->query( "select count(id) from openc3_ci_version where projectid='$projectid'" ); };
die "get verions list from database fail: $@" if $@;
return $vcount{$projectid} = $x->[0][0];
}
my %cislaveeventcount;
sub cislaveeventcount
{
my $projectid = shift @_;
# 可以去掉,为了更高效。没有配置cislave的c3就直接跳过了
return 0 unless -f '/data/Software/mydan/CI/cislave/conf/master.yml';
return $cislaveeventcount{$projectid} if defined $cislaveeventcount{$projectid};
my $cislaveeventcont = `/data/Software/mydan/CI/cislave/mark/event '$projectid'`;
chomp $cislaveeventcont;
die "get cislaveeventcont fail" unless defined $cislaveeventcont && $cislaveeventcont =~ /^\d+$/;
return $cislaveeventcount{$projectid} = $cislaveeventcont;
}
sub save
{
my ( $id, $version ) = split /;/, shift , 2;
unless( $version =~ /^[a-zA-Z0-9][a-zA-Z0-9\.\-_]+$/ )
{
print "error $version\n";
return;
}
my $v = eval{ $db->query( "select `name` from openc3_ci_version where projectid='$id' and name='$version'" ); };
die "get verions list from database fail: $@" if $@;
if( @$v > 0 )
{
print "archived $version\n";
return;
}
print "new $version\n";
my $autobuild = eval{ $db->query( "select autobuild from openc3_ci_project where id='$id'" ); };
die "get autobuild from database fail: $@" if $@;
my $auto = ( $autobuild && $autobuild->[0][0] eq '1' ) ? 1 : 0;
my ( $status, $reason ) = $auto ? vcount( $id ) ? ( 'init', '' ) : ( 'done', 'first load, build skip' ) : ( 'done', 'auto build off' );
( $status, $reason ) = ( 'done', 'sys@app up ci' ) if $ENV{OPENC3_C3MC_CI_STATUS_UP};
$autobuild = `c3mc-sys-ctl ci.autobuild`;
chomp $autobuild;
( $status, $reason ) = ( 'done', 'sys@app sysctl.ci.autobuild off' ) unless $autobuild && $autobuild eq '1';
( $status, $reason ) = ( 'done', 'sys@app c3.maintain' ) if -f '/data/Software/mydan/Connector/c3.maintain';
my $cislaveeventcont = cislaveeventcount( $id );
( $status, $reason ) = ( 'done', 'sys@app cislave init' ) if $status ne 'done' && $cislaveeventcont > 0;
my $uuid = uuid->new()->create_str;
eval{ $db->execute( "insert into openc3_ci_version (`projectid`,`name`,`status`,`calltype`,`user`,`uuid`,`slave`,`reason`)
values('$id','$version','$status','findtags','crontab','$uuid','','$reason')" ); };
my $finishtime = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime );
eval{ $db->execute( "update `openc3_ci_project` set last_findtags_success='$finishtime' where id='$id'" ); };
die "update project info fail:$@" if $@;
}
if( @ARGV )
{
save( "$ARGV[0];$ARGV[1]" );
}
else
{
while(<STDIN>){ chomp; save($_); }
}
for my $projectid ( keys %cislaveeventcount )
{
my $eventcont = $cislaveeventcount{$projectid};
next unless $eventcont;
system "/data/Software/mydan/CI/cislave/mark/reset '$projectid'";
}