diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/DeployCommands.cs b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/DeployCommands.cs index c6456564..39491a2c 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/DeployCommands.cs +++ b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/DeployCommands.cs @@ -89,7 +89,14 @@ public static class DeployCommands /// internal static readonly TimeSpan BulkDeployTimeout = Timeout.InfiniteTimeSpan; - internal static int HandleSiteDeploymentResponse(string json, string format) + /// + /// Renders a deployment summary and turns its failureCount into the exit code: + /// 0 when every site/instance succeeded, 1 when any failed. Shared by deploy site + /// and both artifact-deploy commands (deploy artifacts, site deploy-artifacts), + /// whose summaries all carry failureCount — a per-site failure is a successful HTTP + /// call, so without this the process exited 0 and scripts could not detect it. + /// + internal static int HandleSiteDeploymentResponse(string json, string format, string summaryName = "site deployment") { CommandHelpers.HandleResponse(new ManagementResponse(200, json, null, null), format); try @@ -100,7 +107,7 @@ public static class DeployCommands return count == 0 ? 0 : 1; } catch (JsonException) { } - OutputFormatter.WriteError("The server returned an invalid site deployment summary.", "INVALID_RESPONSE"); + OutputFormatter.WriteError($"The server returned an invalid {summaryName} summary.", "INVALID_RESPONSE"); return 1; } @@ -113,7 +120,9 @@ public static class DeployCommands { var siteId = result.GetValue(siteIdOption); return await CommandHelpers.ExecuteCommandAsync( - result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDeployArtifactsCommand(siteId)); + result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDeployArtifactsCommand(siteId), + onSuccess: json => HandleSiteDeploymentResponse( + json, CommandHelpers.ResolveFormat(result, formatOption, CliConfig.Load()), "artifact deployment")); }); return cmd; } diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/SiteCommands.cs b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/SiteCommands.cs index e026fe20..6c0a900f 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/SiteCommands.cs +++ b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/SiteCommands.cs @@ -222,8 +222,11 @@ public static class SiteCommands cmd.SetAction(async (ParseResult result) => { var siteId = result.GetValue(siteIdOption); + // Exit 1 when any site failed (see DeployCommands.HandleSiteDeploymentResponse). return await CommandHelpers.ExecuteCommandAsync( - result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDeployArtifactsCommand(siteId)); + result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDeployArtifactsCommand(siteId), + onSuccess: json => DeployCommands.HandleSiteDeploymentResponse( + json, CommandHelpers.ResolveFormat(result, formatOption, CliConfig.Load()), "artifact deployment")); }); return cmd; } diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/README.md b/src/ZB.MOM.WW.ScadaBridge.CLI/README.md index c3f144f4..8a76c6ac 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/README.md +++ b/src/ZB.MOM.WW.ScadaBridge.CLI/README.md @@ -965,6 +965,11 @@ scadabridge --url site deploy-artifacts [--site-id ] |--------|----------|-------------| | `--site-id` | no | Target site ID; deploys system-wide artifacts to exactly that site. Omit to deploy fleet-wide to all sites. A site-scoped (non-Administrator) Deployer must supply an in-scope `--site-id`; fleet-wide deployment requires a system-wide Deployer or Administrator (arch-review C2). | +Output is the per-site result matrix (`siteId`, `siteName`, `success`, `errorMessage`, +`secretsDeployed`, `secretsRemoved`) plus `successCount` / `failureCount`. The command exits `1` +when `failureCount` is non-zero — a site that failed (unreachable, a missing or unreadable +secret, a partial secret apply) is a failed command even though the HTTP call succeeded. + #### `site area list` List all areas for a site. @@ -1107,6 +1112,8 @@ scadabridge --url deploy artifacts [--site-id ] |--------|----------|-------------| | `--site-id` | no | Target site ID; deploys to exactly that site. Omit to deploy fleet-wide. A site-scoped (non-Administrator) Deployer must supply an in-scope `--site-id`; fleet-wide requires a system-wide Deployer or Administrator (arch-review C2). | +Same output and exit-code rule as `site deploy-artifacts`: exit `1` when any site failed. + #### `deploy status` Query deployment records, with optional filters and pagination. diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/DeployArtifactsExitCodeTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/DeployArtifactsExitCodeTests.cs new file mode 100644 index 00000000..d283c977 --- /dev/null +++ b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/DeployArtifactsExitCodeTests.cs @@ -0,0 +1,66 @@ +using System.CommandLine; +using System.Net; +using System.Net.Sockets; +using System.Text; +using ZB.MOM.WW.ScadaBridge.CLI.Commands; + +namespace ZB.MOM.WW.ScadaBridge.CLI.Tests; + +/// +/// Both artifact-deploy commands (site deploy-artifacts and deploy artifacts) exit +/// 1 when any site in the summary failed. A per-site failure is a successful HTTP call, so they +/// used to exit 0 and a script could not tell a failed site from a clean deploy. +/// +public class DeployArtifactsExitCodeTests +{ + private const string AllSucceeded = + "{\"deploymentId\":\"d1\",\"siteResults\":[{\"siteId\":\"site-a\",\"siteName\":\"A\",\"success\":true}]," + + "\"successCount\":1,\"failureCount\":0}"; + + private const string OneFailed = + "{\"deploymentId\":\"d1\",\"siteResults\":[{\"siteId\":\"site-a\",\"siteName\":\"A\",\"success\":true}," + + "{\"siteId\":\"site-b\",\"siteName\":\"B\",\"success\":false,\"errorMessage\":\"Secret 'k' ... missing\"}]," + + "\"successCount\":1,\"failureCount\":1}"; + + [Theory] + [InlineData("site", AllSucceeded, 0)] + [InlineData("site", OneFailed, 1)] + [InlineData("site", "{\"unexpected\":true}", 1)] + [InlineData("deploy", AllSucceeded, 0)] + [InlineData("deploy", OneFailed, 1)] + [InlineData("deploy", "{\"unexpected\":true}", 1)] + public async Task ArtifactDeployExitCodeFollowsFailureCount(string group, string response, int expected) + { + var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + try + { + var port = ((IPEndPoint)listener.LocalEndpoint).Port; + var server = Task.Run(async () => + { + using var client = await listener.AcceptTcpClientAsync(); + await using var stream = client.GetStream(); + using var reader = new StreamReader(stream, Encoding.ASCII, leaveOpen: true); + while (await reader.ReadLineAsync() is { Length: > 0 }) { } + var body = Encoding.UTF8.GetBytes(response); + var headers = Encoding.ASCII.GetBytes($"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {body.Length}\r\nConnection: close\r\n\r\n"); + await stream.WriteAsync(headers); + await stream.WriteAsync(body); + }); + var root = new RootCommand(); + var url = new Option("--url") { Recursive = true }; + var format = new Option("--format") { Recursive = true }; + var user = new Option("--username") { Recursive = true }; + var password = new Option("--password") { Recursive = true }; + root.Add(url); root.Add(format); root.Add(user); root.Add(password); + root.Add(SiteCommands.Build(url, format, user, password)); + root.Add(DeployCommands.Build(url, format, user, password)); + string[] verb = group == "site" ? ["site", "deploy-artifacts"] : ["deploy", "artifacts"]; + var exit = await root.Parse([.. verb, "--url", $"http://127.0.0.1:{port}", + "--username", "test", "--password", "test", "--format", "json"]).InvokeAsync(); + await server.WaitAsync(TimeSpan.FromSeconds(5)); + Assert.Equal(expected, exit); + } + finally { listener.Stop(); } + } +}