Skip to content

Commit b73745a

Browse files
committed
installers write error message in deployment result if fail or need_reboot
1 parent 68b8c78 commit b73745a

File tree

7 files changed

+58
-26
lines changed

7 files changed

+58
-26
lines changed

src/main/java/org/scm4j/deployer/installers/Copy.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ else if (filesForDeploy.size() == 1 && fileName != null)
3737
}
3838
return OK;
3939
} catch (IOException e) {
40-
log.warn(e.getMessage());
41-
return REBOOT_CONTINUE;
40+
log.warn(e.toString());
41+
DeploymentResult dr = REBOOT_CONTINUE;
42+
dr.setErrorMsg(e.toString());
43+
return dr;
4244
}
4345
}
4446

@@ -52,7 +54,9 @@ public DeploymentResult undeploy() {
5254
return OK;
5355
} catch (IOException e) {
5456
log.warn(e.toString());
55-
return REBOOT_CONTINUE;
57+
DeploymentResult dr = REBOOT_CONTINUE;
58+
dr.setErrorMsg(e.toString());
59+
return dr;
5660
}
5761
}
5862

src/main/java/org/scm4j/deployer/installers/Delete.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ private DeploymentResult deleteFile(String fileToDelete) {
3030
File file = new File(fileToDelete);
3131
if (!file.exists())
3232
return DeploymentResult.OK;
33-
if (file.delete())
33+
if (file.delete()) {
3434
return DeploymentResult.OK;
35-
else
36-
return DeploymentResult.FAILED;
35+
} else {
36+
DeploymentResult dr = DeploymentResult.FAILED;
37+
dr.setErrorMsg("File doesn't deleted!");
38+
return dr;
39+
}
3740
}
3841

3942
@Override

src/main/java/org/scm4j/deployer/installers/Exec.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import org.scm4j.deployer.api.IComponentDeployer;
1111
import org.scm4j.deployer.api.IDeploymentContext;
1212

13+
import java.io.BufferedReader;
1314
import java.io.File;
1415
import java.io.IOException;
1516
import java.io.InputStream;
17+
import java.io.InputStreamReader;
1618
import java.io.PrintStream;
1719
import java.nio.file.Files;
1820
import java.nio.file.Path;
@@ -69,7 +71,7 @@ public Exec onStop() {
6971
private File defaultDeployExecutable;
7072

7173
@SneakyThrows
72-
public static int exec(List<String> command, Map<String, String> env, File directory) {
74+
public static Process exec(List<String> command, Map<String, String> env, File directory) {
7375
ProcessBuilder builder = new ProcessBuilder(command)
7476
.directory(directory);
7577
if (env != null && !env.isEmpty())
@@ -79,18 +81,22 @@ public static int exec(List<String> command, Map<String, String> env, File direc
7981
Process p = builder.start();
8082
realInheritIO(p.getInputStream(), System.out);
8183
realInheritIO(p.getErrorStream(), System.err);
82-
return p.waitFor();
84+
return p;
8385
}
8486

8587
private static void realInheritIO(final InputStream src, final PrintStream dest) {
8688
new Thread(() -> {
8789
try (Scanner sc = new Scanner(src)) {
88-
while (sc.hasNextLine())
89-
dest.println(sc.nextLine());
90+
while (sc.hasNextLine()) {
91+
String line = sc.nextLine();
92+
dest.println(line);
93+
log.info(line);
94+
}
9095
}
9196
}).start();
9297
}
9398

99+
@SneakyThrows
94100
private DeploymentResult executeCommand(String executable, String[] args, Map<String, String> env) {
95101
List<String> command = new ArrayList<>();
96102
command.add("cmd");
@@ -110,17 +116,32 @@ private DeploymentResult executeCommand(String executable, String[] args, Map<St
110116

111117
String workingDirectoryName = workingDirectory != null ? workingDirectory : deploymentPath;
112118

113-
int exitValue = exec(command, env, new File(workingDirectoryName));
119+
Process p = exec(command, env, new File(workingDirectoryName));
120+
int exitValue = p.waitFor();
114121

115122
log.info("Exit value is " + exitValue);
116123

117124
if (exitValue == needRebootExitValue)
118125
return DeploymentResult.NEED_REBOOT;
119-
if (!ignoreExitValue && exitValue != 0)
126+
if (!ignoreExitValue && exitValue != 0) {
127+
DeploymentResult dr = DeploymentResult.FAILED;
128+
dr.setErrorMsg(errorStreamToString(p));
120129
return DeploymentResult.FAILED;
130+
}
121131
return DeploymentResult.OK;
122132
}
123133

134+
@SneakyThrows
135+
private String errorStreamToString(Process p) {
136+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
137+
StringBuilder sb = new StringBuilder();
138+
String line;
139+
while ((line = br.readLine()) != null) {
140+
sb.append(line);
141+
}
142+
return sb.toString();
143+
}
144+
124145
public Exec setArgs(String... args) {
125146
this.args = args;
126147
return this;
@@ -160,7 +181,7 @@ private String findLatestUnins(String deploymentPath, String executablePrefix) t
160181
.map(Path::toString)
161182
.filter(p -> p.startsWith(executablePrefix))
162183
.filter(p -> p.endsWith(".exe"))
163-
.min(Comparator.reverseOrder())
184+
.max(Comparator.naturalOrder())
164185
.orElseThrow(() -> new RuntimeException("Can't find executable file to undeploy product"));
165186
}
166187
}

src/main/java/org/scm4j/deployer/installers/ProcrunDeployer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public DeploymentResult stop() {
126126

127127
@SneakyThrows
128128
private void execute(List<String> command) {
129-
int res = Exec.exec(command, null, new File(deploymentPath));
129+
int res = Exec.exec(command, null, new File(deploymentPath)).waitFor();
130130
if (res != 0)
131131
throw new Exception(String.format("%s: %d", command.toString(), res));
132132
}

src/main/java/org/scm4j/deployer/installers/Shortcut.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ public DeploymentResult deploy() {
8282
String destFile = dest.getPath().replace('\\', '/') + '/' + name + ".lnk";
8383
sl.saveTo(destFile);
8484
} catch (IOException e) {
85-
log.warn(e.getMessage());
86-
return FAILED;
85+
DeploymentResult dr = FAILED;
86+
dr.setErrorMsg(e.toString());
87+
return dr;
8788
}
8889
return OK;
8990
}
@@ -96,8 +97,11 @@ public DeploymentResult undeploy() {
9697
File shortcutFile = new File(dest, name + ".lnk");
9798
FileUtils.deleteQuietly(shortcutFile);
9899
if (shortcutFile.exists()) {
99-
log.warn("Can't delete shortcut " + shortcutFile.getPath());
100-
return FAILED;
100+
String errorMsg = "Can't delete shortcut " + shortcutFile.getPath();
101+
log.warn(errorMsg);
102+
DeploymentResult dr = FAILED;
103+
dr.setErrorMsg(errorMsg);
104+
return dr;
101105
} else {
102106
return OK;
103107
}

src/main/java/org/scm4j/deployer/installers/UntillExec.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
@Slf4j
77
public class UntillExec extends Exec {
88

9-
@Override
10-
public DeploymentResult stop() {
11-
super.stop();
12-
return DeploymentResult.OK;
13-
}
14-
159
@Override
1610
public DeploymentResult deploy() {
1711
DeploymentResult res = super.deploy();

src/main/java/org/scm4j/deployer/installers/Unzip.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.zip.ZipEntry;
1919
import java.util.zip.ZipFile;
2020

21+
import static org.scm4j.deployer.api.DeploymentResult.REBOOT_CONTINUE;
22+
2123
@Slf4j
2224
public class Unzip implements IComponentDeployer {
2325

@@ -50,7 +52,9 @@ public DeploymentResult deploy() {
5052
return DeploymentResult.OK;
5153
} catch (IOException e) {
5254
log.warn(e.getMessage());
53-
return DeploymentResult.REBOOT_CONTINUE;
55+
DeploymentResult dr = REBOOT_CONTINUE;
56+
dr.setErrorMsg(e.toString());
57+
return dr;
5458
}
5559
}
5660

@@ -69,7 +73,9 @@ public DeploymentResult undeploy() {
6973
return DeploymentResult.OK;
7074
} catch (IOException e) {
7175
log.warn(e.getMessage());
72-
return DeploymentResult.REBOOT_CONTINUE;
76+
DeploymentResult dr = REBOOT_CONTINUE;
77+
dr.setErrorMsg(e.toString());
78+
return dr;
7379
}
7480
}
7581

0 commit comments

Comments
 (0)