-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript-deploy
More file actions
executable file
·164 lines (135 loc) · 3.9 KB
/
script-deploy
File metadata and controls
executable file
·164 lines (135 loc) · 3.9 KB
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
161
162
163
164
#!/usr/bin/php -q
<?php
define('PROJECT_DIR', realpath(dirname($_SERVER['argv'][0])));
define('PROJECT_COMMAND', "mvn clean deploy");
define('PROJECT_CLEAN_REPOSITORY', true);
$PROJECT_DIR = PROJECT_DIR;
$PROJECT_COMMAND = PROJECT_COMMAND;
echo <<<EOF
===============================================================================
| Run '$PROJECT_COMMAND'
| in $PROJECT_DIR
|
| @author Moises P. Sena <moisespsena@gmail.com>
===============================================================================
EOF;
class Main {
public function __construct() {
$pomFile = PROJECT_DIR . '/pom.xml';
$xml = simplexml_load_file($pomFile);
if(!empty($xml->parent)) {
$xml->groupId = $xml->parent->groupId;
$xml->version = $xml->parent->version;
}
$artifactId = trim((string) $xml->artifactId);
$groupId = trim((string) $xml->groupId);
$version = trim((string) $xml->version);
if(PROJECT_CLEAN_REPOSITORY) {
$path = escapeshellarg("~/.m2/repository/"
. str_replace(".", "/", $groupId)
. "/{$artifactId}/{$version}");
Logger::info("Clean MVN cache repository ({$path})");
Shell::exec("rm -vrf $path", false);
}
$projectDir = escapeshellarg(PROJECT_DIR);
Shell::exec("cd $projectDir && " . PROJECT_COMMAND);
}
}
// script-file
class Shell {
const BUF_SIZ = 1024; // max buffer size
const FD_WRITE = 0; // stdin
const FD_READ = 1; // stdout
const FD_ERR = 2; // stderr
public static function printOut($msg) {
echo $msg;
}
private static function procExec($cmd) {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$first_exitcode = 0;
$ptr = proc_open($cmd, $descriptorspec, $pipes, NULL, $_ENV);
if (!is_resource($ptr)) {
return false;
}
$errbuf = "";
while (($buffer = fgets($pipes[self::FD_READ], self::BUF_SIZ)) != NULL
|| ($errbuf = fgets($pipes[self::FD_ERR], self::BUF_SIZ)) != NULL) {
if (!isset($flag)) {
$pstatus = proc_get_status($ptr);
$first_exitcode = $pstatus["exitcode"];
$flag = true;
}
if (strlen($buffer)) {
self::printOut($buffer);
}
if (strlen($errbuf)) {
Logger::error($errbuf);
}
}
foreach ($pipes as $pipe) {
fclose($pipe);
}
/* Get the expected *exit* code to return the value */
$pstatus = proc_get_status($ptr);
if (!strlen($pstatus["exitcode"]) || $pstatus["running"]) {
/* we can trust the retval of proc_close() */
if ($pstatus["running"]) {
proc_terminate($ptr);
}
$ret = proc_close($ptr);
} else {
if ((($first_exitcode + 256) % 256) == 255
&& (($pstatus["exitcode"] + 256) % 256) != 255) {
$ret = $pstatus["exitcode"];
}
elseif (!strlen($first_exitcode)) {
$ret = $pstatus["exitcode"];
}
elseif ((($first_exitcode + 256) % 256) != 255) {
$ret = $first_exitcode;
}
else {
$ret = 0; /* we "deduce" an EXIT_SUCCESS ;) */
}
proc_close($ptr);
}
return ($ret + 256) % 256;
}
private static function getEnv() {
$env = `env`;
$env = preg_replace("/^([^=]+)=([^\n]+)\n/msi", "$1='$2'\n", $env);
$export = preg_replace("/^(\w)/msi", "export $1", $env);
$env = $env . "\n\n" . $export;
return $env;
}
public static function exec($cmd, $exit = true) {
$cmd = self::getEnv() . "\n" . $cmd;
if (($ret = self::procExec($cmd)) === false) {
Logger::error("not enough FD or out of memory.", $exit);
} elseif ($ret == 127) {
Logger::error("Command not found (returned by sh).", $exit, $ret);
} else if($exit) {
exit($ret);
}
}
}
class Logger {
public static function help() {
$cmd = $_SERVER ['argv'] [0];
}
public static function error($msg, $exit = true, $code = 1) {
Shell::printOut("[ERROR]: $msg\n");
self::help ();
if($exit) {
exit ( $code );
}
}
public static function info($msg) {
Shell::printOut("[INFO]: $msg\n");
}
}
new Main();