-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimployCore.java
115 lines (86 loc) · 2.69 KB
/
SimployCore.java
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
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
public class SimployCore {
static final PrintStream SYSOUT = System.out;
private static StringBuffer _outputsBeingCaptured;
private static Date _lastBuildDate;
private static String _lastBuildStatus;
private static StringBuffer _lastBuildOutputs;
private static Date _lastSuccessDate;
synchronized
static void runCommandIfNewVersionPresent() {
runCommand(false);
}
synchronized
static void runCommand() {
runCommand(true);
}
static private void runCommand(boolean runEvenIfNoChanges) {
startCapturingOutputs();
try {
tryToRunCommand(runEvenIfNoChanges);
} finally {
stopCapturingOutputs();
}
}
private static void tryToRunCommand(boolean runEvenIfNoChanges) {
boolean hasChanges = pullChanges();
if (!hasChanges && !runEvenIfNoChanges)
return;
_lastBuildDate = new Date();
_lastBuildStatus = "IN PROGRESS...";
_lastBuildOutputs = _outputsBeingCaptured;
try {
execCommand();
_lastSuccessDate = _lastBuildDate;
_lastBuildStatus = "SUCCESS";
} catch (Exception e) {
e.printStackTrace();
_lastBuildStatus = "FAILED";
}
}
private static boolean pullChanges() {
try {
String stdOut = SimployCommandRunner.exec("git pull");
return !stdOut.contains("Already up-to-date.");
} catch (Exception e) {
e.printStackTrace();
_lastBuildDate = new Date();
_lastBuildStatus = "GIT PULL FAILED";
_lastBuildOutputs = _outputsBeingCaptured;
return false;
}
}
private static void execCommand() throws Exception {
SimployCommandRunner.exec("/bin/bash simploy.sh");
}
private static void startCapturingOutputs() {
_outputsBeingCaptured = new StringBuffer();
PrintStream filter = new PrintStream(new FilterOutputStream(SYSOUT) { @Override public void write(int b) throws IOException {
super.write(b);
_outputsBeingCaptured.append((char)b);
}});
System.setOut(filter);
System.setErr(filter);
}
private static void stopCapturingOutputs() {
System.setOut(SYSOUT);
System.setErr(SYSOUT);
}
static String report() {
return
"Last build status: " + _lastBuildStatus +
"\nLast build date : " + _lastBuildDate +
"\nLast success date: " + _lastSuccessDate +
"\n" +
"\nLast build outputs (stdOut and stdErr): " +
"\n" + _lastBuildOutputs +
"\n\n" +
"Last build status: " + _lastBuildStatus +
"\n\n\n-----------------------------------------------------" +
"\nRAM used: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "MB (Max " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "MB)" +
"\nReport produced by Simploy.";
}
}