-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathec_setup.pl
160 lines (136 loc) · 4.94 KB
/
ec_setup.pl
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
156
157
158
159
160
use Cwd;
use File::Spec;
use POSIX;
use MIME::Base64;
use File::Temp qw(tempfile tempdir);
use Archive::Zip;
use Digest::MD5 qw(md5_hex);
my $dir = getcwd;
my $logfile ="";
my $pluginDir;
if ( defined $ENV{QUERY_STRING} ) { # Promotion through UI
$pluginDir = $ENV{COMMANDER_PLUGINS} . "/$pluginName";
}
else {
my $commanderPluginDir = $commander->getProperty('/server/settings/pluginsDirectory')->findvalue('//value');
# We are not checking for the directory, because we can run this script on a different machine
$pluginDir = "$commanderPluginDir/$pluginName";
}
$logfile .= "Plugin directory is $pluginDir";
$commander->setProperty("/plugins/$pluginName/project/pluginDir", {value=>$pluginDir});
$logfile .= "Plugin Name: $pluginName\n";
$logfile .= "Current directory: $dir\n";
# Evaluate promote.groovy or demote.groovy based on whether plugin is being promoted or demoted ($promoteAction)
local $/ = undef;
my $demoteDsl = q{
# demote.groovy placeholder
};
my $promoteDsl = q{
# promote.groovy placeholder
};
my $dsl;
if ($promoteAction eq 'promote') {
$dsl = $promoteDsl;
}
else {
$dsl = $demoteDsl;
}
my $dslReponse = $commander->evalDsl(
$dsl, {
parameters => qq(
{
"pluginName":"$pluginName",
"upgradeAction":"$upgradeAction",
"otherPluginName":"$otherPluginName"
}
),
debug => 'false',
serverLibraryPath => "$pluginDir/dsl"
},
);
$logfile .= $dslReponse->findnodes_as_string("/");
my $errorMessage = $commander->getError();
if ( !$errorMessage ) {
# This is here because we cannot do publishArtifactVersion in dsl today
# delete artifact if it exists first
my $dependenciesProperty = '/projects/@PLUGIN_NAME@/ec_groovyDependencies';
my $base64 = '';
my $xpath;
eval {
$xpath = $commander->getProperties({path => $dependenciesProperty});
1;
};
unless($@) {
my $blocks = {};
my $checksum = '';
for my $prop ($xpath->findnodes('//property')) {
my $name = $prop->findvalue('propertyName')->string_value;
my $value = $prop->findvalue('value')->string_value;
if ($name eq 'checksum') {
$checksum = $value;
}
else {
my ($number) = $name =~ /ec_dependencyChunk_(\d+)$/;
$blocks->{$number} = $value;
}
}
for my $key (sort {$a <=> $b} keys %$blocks) {
$base64 .= $blocks->{$key};
}
my $resultChecksum = md5_hex($base64);
unless($checksum) {
die "No checksum found in dependendencies property, please reinstall the plugin";
}
if ($resultChecksum ne $checksum) {
die "Wrong dependency checksum: original checksum is $checksum";
}
}
if ($base64) {
my $grapesVersion = '1.0.0';
my $cleanup = 1;
my $groupId = 'com.electriccloud';
$commander->deleteArtifactVersion($groupId . ':@PLUGIN_KEY@-Grapes:' . $grapesVersion);
my $binary = decode_base64($base64);
my ($tempFh, $tempFilename) = tempfile(CLEANUP => $cleanup);
binmode($tempFh);
print $tempFh $binary;
close $tempFh;
my ($tempDir) = tempdir(CLEANUP => $cleanup);
my $zip = Archive::Zip->new();
unless($zip->read($tempFilename) == Archive::Zip::AZ_OK()) {
die "Cannot read .zip dependencies: $!";
}
$zip->extractTree("", $tempDir . "/");
if ( $promoteAction eq "promote" ) {
#publish jars to the repo server if the plugin project was created successfully
my $am = new ElectricCommander::ArtifactManagement($commander);
my $artifactVersion = $am->publish(
{ groupId => $groupId,
artifactKey => '@PLUGIN_KEY@-Grapes',
version => $grapesVersion,
includePatterns => "**",
fromDirectory => File::Spec->catfile($tempDir, 'lib/grapes'),
description => 'JARs that @PLUGIN_KEY@ plugin procedures depend on'
}
);
# Print out the xml of the published artifactVersion.
$logfile .= $artifactVersion->xml() . "\n";
if ( $artifactVersion->diagnostics() ) {
$logfile .= "\nDetails:\n" . $artifactVersion->diagnostics();
}
}
}
}
if ( $promoteAction eq "promote" ) {
# Use createProperty (and ignore errors) so that we do not overwrite existing properties
$commander->abortOnError(0);
$commander->createProperty("/server/@PLUGIN_KEY@/timeout",
{description=>'Timeout for evalDsl that can be increased if files are too long',
value=>'600'});
# Reset error handling at this point
$commander->abortOnError(1);
}
# Create output property for plugin setup debug logs
my $nowString = localtime;
$commander->setProperty( "/plugins/$pluginName/project/logs/$nowString", { value => $logfile } );
die $errorMessage unless !$errorMessage;