Skip to content

Commit

Permalink
Add OSDF_HEALTHCHECK_TIMEOUT and OSDF_DEPLOY_FAILFAST flags (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
SniXosha authored Aug 18, 2020
1 parent 9a437aa commit aae5f9a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'io.microconfig.osdf'
version '1.5.13'
version '1.5.14'

sourceCompatibility = 11

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -46,26 +40,9 @@ public boolean deploy(List<String> requiredServiceNames, boolean smart) {
List<Application> appsToDeploy = filterApps(smart, allApps);
if (appsToDeploy.isEmpty()) return true;

Integer maxParallel = osdfConfig(paths).maxParallel();
GroupDeployer groupDeployer = groupDeployer(cli, osdfConfig(paths).maxParallel());
List<List<Application>> groups = startGroupSplitter().split(appsToDeploy);
return groups.stream().allMatch(apps -> deployGroup(apps, maxParallel));
}

private boolean deployGroup(List<Application> 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<Application> filterApps(boolean smart, List<Application> allApps) {
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/osdf/actions/management/deploy/GroupDeployer.java
Original file line number Diff line number Diff line change
@@ -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<Application> 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<Deployable> appsParallelStream(List<Application> apps) {
return apps.parallelStream()
.map(app -> of(app, cli));
}

private int parallelism(List<Application> apps) {
return maxParallel == null ? apps.size() : maxParallel;
}
}

0 comments on commit aae5f9a

Please sign in to comment.