From aae5f9abbd4649e79151f5ce14c2ef67f7b44901 Mon Sep 17 00:00:00 2001 From: SniXosha Date: Tue, 18 Aug 2020 16:43:26 +0300 Subject: [PATCH] Add OSDF_HEALTHCHECK_TIMEOUT and OSDF_DEPLOY_FAILFAST flags (#67) --- build.gradle | 2 +- .../healthcheck/app/ServiceHealthChecker.java | 11 ++++ .../management/deploy/AppsDeployCommand.java | 29 +-------- .../management/deploy/GroupDeployer.java | 61 +++++++++++++++++++ 4 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 src/main/java/io/osdf/actions/management/deploy/GroupDeployer.java diff --git a/build.gradle b/build.gradle index 5d853f84..cf38550b 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group 'io.microconfig.osdf' -version '1.5.13' +version '1.5.14' sourceCompatibility = 11 diff --git a/src/main/java/io/osdf/actions/info/healthcheck/app/ServiceHealthChecker.java b/src/main/java/io/osdf/actions/info/healthcheck/app/ServiceHealthChecker.java index 2c8d8746..6b854720 100644 --- a/src/main/java/io/osdf/actions/info/healthcheck/app/ServiceHealthChecker.java +++ b/src/main/java/io/osdf/actions/info/healthcheck/app/ServiceHealthChecker.java @@ -12,7 +12,9 @@ import java.util.Optional; import static io.microconfig.utils.Logger.error; +import static io.osdf.common.utils.StringUtils.castToInteger; import static io.osdf.core.application.service.ServiceApplication.serviceApplication; +import static java.lang.System.getenv; import static java.util.Objects.requireNonNullElse; @RequiredArgsConstructor @@ -41,8 +43,17 @@ public boolean check(Application app) { } private int timeout(ServiceApplication service) { + Integer timeoutFromEnv = timeoutFromEnv(); + if (timeoutFromEnv != null) return timeoutFromEnv; + Integer timeoutInSec = service.files().deployProperties() .get("osdf.healthcheck.timeoutInSec"); return requireNonNullElse(timeoutInSec, 60); } + + private Integer timeoutFromEnv() { + String timeoutFromEnv = getenv("OSDF_HEALTHCHECK_TIMEOUT"); + if (timeoutFromEnv != null) return castToInteger(timeoutFromEnv.trim()); + return null; + } } diff --git a/src/main/java/io/osdf/actions/management/deploy/AppsDeployCommand.java b/src/main/java/io/osdf/actions/management/deploy/AppsDeployCommand.java index da77199a..d9cef1f8 100644 --- a/src/main/java/io/osdf/actions/management/deploy/AppsDeployCommand.java +++ b/src/main/java/io/osdf/actions/management/deploy/AppsDeployCommand.java @@ -1,6 +1,5 @@ package io.osdf.actions.management.deploy; -import io.osdf.actions.management.deploy.deployer.Deployable; import io.osdf.actions.management.deploy.smart.hash.ResourcesHashComputer; import io.osdf.actions.management.deploy.smart.image.ImageVersionReplacer; import io.osdf.core.application.core.Application; @@ -10,23 +9,18 @@ import java.util.List; -import static io.microconfig.utils.ConsoleColor.green; -import static io.microconfig.utils.ConsoleColor.red; import static io.microconfig.utils.Logger.announce; -import static io.microconfig.utils.Logger.info; -import static io.osdf.actions.management.deploy.deployer.Deployable.of; +import static io.osdf.actions.management.deploy.GroupDeployer.groupDeployer; import static io.osdf.actions.management.deploy.groups.StartGroupSplitter.startGroupSplitter; import static io.osdf.actions.management.deploy.smart.UpToDateAppFilter.upToDateAppFilter; import static io.osdf.actions.management.deploy.smart.hash.ResourcesHashComputer.resourcesHashComputer; import static io.osdf.actions.management.deploy.smart.image.ImageVersionReplacer.imageVersionReplacer; import static io.osdf.actions.management.deploy.smart.image.digest.DigestGetterImpl.digestGetter; -import static io.osdf.common.utils.ThreadUtils.runInParallel; import static io.osdf.core.application.core.AllApplications.all; import static io.osdf.core.application.core.files.loaders.ApplicationFilesLoaderImpl.activeRequiredAppsLoader; import static io.osdf.settings.OsdfConfig.osdfConfig; import static java.util.Collections.emptyList; import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.toUnmodifiableList; @RequiredArgsConstructor public class AppsDeployCommand { @@ -46,26 +40,9 @@ public boolean deploy(List requiredServiceNames, boolean smart) { List appsToDeploy = filterApps(smart, allApps); if (appsToDeploy.isEmpty()) return true; - Integer maxParallel = osdfConfig(paths).maxParallel(); + GroupDeployer groupDeployer = groupDeployer(cli, osdfConfig(paths).maxParallel()); List> groups = startGroupSplitter().split(appsToDeploy); - return groups.stream().allMatch(apps -> deployGroup(apps, maxParallel)); - } - - private boolean deployGroup(List apps, Integer maxParallel) { - announce("\nDeploying group - " + apps.stream().map(Application::name).collect(toUnmodifiableList())); - return runInParallel(maxParallel == null ? apps.size() : maxParallel, () -> - apps.parallelStream() - .map(app -> of(app, cli)) - .allMatch(app -> { - if (!app.deploy()) return statusWithLogging(false, app); - - return statusWithLogging(app.check(), app); - })); - } - - private boolean statusWithLogging(boolean status, Deployable app) { - info(app.name() + " " + (status ? green("OK") : red("FAILED"))); - return status; + return groups.stream().allMatch(groupDeployer::deployGroup); } private List filterApps(boolean smart, List allApps) { diff --git a/src/main/java/io/osdf/actions/management/deploy/GroupDeployer.java b/src/main/java/io/osdf/actions/management/deploy/GroupDeployer.java new file mode 100644 index 00000000..3083759e --- /dev/null +++ b/src/main/java/io/osdf/actions/management/deploy/GroupDeployer.java @@ -0,0 +1,61 @@ +package io.osdf.actions.management.deploy; + +import io.osdf.actions.management.deploy.deployer.Deployable; +import io.osdf.core.application.core.Application; +import io.osdf.core.connection.cli.ClusterCli; +import lombok.RequiredArgsConstructor; + +import java.util.List; +import java.util.stream.Stream; + +import static io.microconfig.utils.ConsoleColor.green; +import static io.microconfig.utils.ConsoleColor.red; +import static io.microconfig.utils.Logger.announce; +import static io.microconfig.utils.Logger.info; +import static io.osdf.actions.management.deploy.deployer.Deployable.of; +import static io.osdf.common.utils.ThreadUtils.runInParallel; +import static java.lang.System.getenv; +import static java.util.stream.Collectors.toUnmodifiableList; + +@RequiredArgsConstructor +public class GroupDeployer { + private final ClusterCli cli; + private final Integer maxParallel; + private final boolean failFast; + + public static GroupDeployer groupDeployer(ClusterCli cli, Integer maxParallel) { + return new GroupDeployer(cli, maxParallel, !"false".equals(getenv("OSDF_DEPLOY_FAILFAST"))); + } + + public boolean deployGroup(List apps) { + announce("\nDeploying group - " + apps.stream().map(Application::name).collect(toUnmodifiableList())); + return runInParallel(parallelism(apps), () -> + failFast ? + appsParallelStream(apps) + .allMatch(this::deployAndReturnStatus) : + appsParallelStream(apps) + .map(this::deployAndReturnStatus) + .collect(toUnmodifiableList()).stream() + .allMatch(t -> t) + ); + } + + private Boolean deployAndReturnStatus(Deployable app) { + if (!app.deploy()) return statusWithLogging(false, app); + return statusWithLogging(app.check(), app); + } + + private boolean statusWithLogging(boolean status, Deployable app) { + info(app.name() + " " + (status ? green("OK") : red("FAILED"))); + return status; + } + + private Stream appsParallelStream(List apps) { + return apps.parallelStream() + .map(app -> of(app, cli)); + } + + private int parallelism(List apps) { + return maxParallel == null ? apps.size() : maxParallel; + } +}