From a75a744ae06091e9565cbbcef7f20b2e45ee375a Mon Sep 17 00:00:00 2001 From: Dilshat Aliev Date: Fri, 15 Mar 2019 07:47:32 +0600 Subject: [PATCH] #2408 --- .../ConfigureContainerStateHandler.java | 16 +++-- .../creation/steps/RegisterSshStep.java | 9 ++- .../core/localpeer/impl/LocalPeerImpl.java | 59 ++++++++++++++----- .../rest/EnvironmentRestService.java | 1 + .../rest/EnvironmentRestServiceImpl.java | 4 +- .../core/peer/impl/EnvironmentWebClient.java | 6 +- .../core/peer/impl/RemotePeerImpl.java | 4 +- .../java/io/subutai/common/peer/Peer.java | 11 +++- 8 files changed, 79 insertions(+), 31 deletions(-) diff --git a/management/server/core/bazaar-manager/bazaar-manager-impl/src/main/java/io/subutai/core/bazaarmanager/impl/environment/state/create/ConfigureContainerStateHandler.java b/management/server/core/bazaar-manager/bazaar-manager-impl/src/main/java/io/subutai/core/bazaarmanager/impl/environment/state/create/ConfigureContainerStateHandler.java index da24b12932b..200fe876cc1 100644 --- a/management/server/core/bazaar-manager/bazaar-manager-impl/src/main/java/io/subutai/core/bazaarmanager/impl/environment/state/create/ConfigureContainerStateHandler.java +++ b/management/server/core/bazaar-manager/bazaar-manager-impl/src/main/java/io/subutai/core/bazaarmanager/impl/environment/state/create/ConfigureContainerStateHandler.java @@ -9,6 +9,12 @@ import com.google.common.collect.Maps; +import io.subutai.bazaar.share.dto.environment.EnvironmentDto; +import io.subutai.bazaar.share.dto.environment.EnvironmentNodeDto; +import io.subutai.bazaar.share.dto.environment.EnvironmentNodesDto; +import io.subutai.bazaar.share.dto.environment.EnvironmentPeerDto; +import io.subutai.bazaar.share.dto.environment.SSHKeyDto; +import io.subutai.common.environment.Containers; import io.subutai.common.environment.Environment; import io.subutai.common.environment.EnvironmentNotFoundException; import io.subutai.common.environment.HostAddresses; @@ -24,11 +30,6 @@ import io.subutai.core.bazaarmanager.api.exception.BazaarManagerException; import io.subutai.core.bazaarmanager.impl.environment.state.Context; import io.subutai.core.bazaarmanager.impl.environment.state.StateHandler; -import io.subutai.bazaar.share.dto.environment.EnvironmentDto; -import io.subutai.bazaar.share.dto.environment.EnvironmentNodeDto; -import io.subutai.bazaar.share.dto.environment.EnvironmentNodesDto; -import io.subutai.bazaar.share.dto.environment.EnvironmentPeerDto; -import io.subutai.bazaar.share.dto.environment.SSHKeyDto; public class ConfigureContainerStateHandler extends StateHandler @@ -151,7 +152,10 @@ private EnvironmentPeerDto configureSsh( EnvironmentPeerDto peerDto, Environment { if ( peer.isOnline() ) { - peer.configureSshInEnvironment( environment.getEnvironmentId(), sshKeys ); + Containers failedHosts = + peer.configureSshInEnvironment( environment.getEnvironmentId(), sshKeys ); + + //TODO check failed hosts //add peer to dto for ( SSHKeyDto sshKeyDto : peerDto.getEnvironmentInfo().getSshKeys() ) diff --git a/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/workflow/creation/steps/RegisterSshStep.java b/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/workflow/creation/steps/RegisterSshStep.java index a9b16085d84..0a1dca491f2 100644 --- a/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/workflow/creation/steps/RegisterSshStep.java +++ b/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/workflow/creation/steps/RegisterSshStep.java @@ -6,7 +6,9 @@ import com.google.common.collect.Sets; +import io.subutai.common.environment.Containers; import io.subutai.common.environment.Topology; +import io.subutai.common.exception.ActionFailedException; import io.subutai.common.peer.Host; import io.subutai.common.peer.Peer; import io.subutai.common.peer.PeerException; @@ -86,7 +88,12 @@ protected void appendSshKeys( final SshKeys sshKeys ) throws EnvironmentManagerE @Override public Object call() throws Exception { - peer.configureSshInEnvironment( environment.getEnvironmentId(), sshKeys ); + Containers failedHosts = peer.configureSshInEnvironment( environment.getEnvironmentId(), sshKeys ); + + if ( !failedHosts.getContainers().isEmpty() ) + { + throw new ActionFailedException( "Failed to add ssh keys on each host" ); + } return null; } diff --git a/management/server/core/local-peer/local-peer-impl/src/main/java/io/subutai/core/localpeer/impl/LocalPeerImpl.java b/management/server/core/local-peer/local-peer-impl/src/main/java/io/subutai/core/localpeer/impl/LocalPeerImpl.java index 092e4882d20..1b50592e370 100644 --- a/management/server/core/local-peer/local-peer-impl/src/main/java/io/subutai/core/localpeer/impl/LocalPeerImpl.java +++ b/management/server/core/local-peer/local-peer-impl/src/main/java/io/subutai/core/localpeer/impl/LocalPeerImpl.java @@ -38,7 +38,6 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; @@ -543,20 +542,23 @@ public SshKeys readOrCreateSshKeysForEnvironment( final EnvironmentId environmen @Override - public void configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys ) + public Containers configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys ) throws PeerException { + Preconditions.checkNotNull( environmentId, "Environment id is null" ); Preconditions.checkNotNull( sshKeys, "SshPublicKey is null" ); Preconditions.checkArgument( !sshKeys.isEmpty(), "No ssh keys" ); Set hosts = Sets.newHashSet(); + Set failedHosts = Sets.newHashSet(); + Containers failedContainers = new Containers(); hosts.addAll( findContainersByEnvironmentId( environmentId.getId() ) ); if ( hosts.isEmpty() ) { - return; + return failedContainers; } //add keys in portions, since all can not fit into one command, it fails @@ -586,33 +588,57 @@ public void configureSshInEnvironment( final EnvironmentId environmentId, final { LOG.error( "Failed to add ssh keys on host {}: {}", result.getHost().getHostname(), result.getFailureReason() ); + + //collect failed hosts + failedHosts.add( ( ContainerHost ) result.getHost() ); } } + } + } + + if ( failedHosts.isEmpty() ) + { + //config ssh + CommandUtil.HostCommandResults configResults = commandUtil + .executeFailFast( localPeerCommands.getConfigSSHCommand(), hosts, environmentId.getId() ); - if ( appendResults.hasFailures() ) + for ( CommandUtil.HostCommandResult result : configResults.getCommandResults() ) + { + if ( !result.hasSucceeded() ) { - throw new PeerException( "Failed to add ssh keys on each host" ); + LOG.error( "Failed to configure ssh on host {}: {}", result.getHost().getHostname(), + result.getFailureReason() ); + + //collect failed hosts + failedHosts.add( ( ContainerHost ) result.getHost() ); } } } - //config ssh - CommandUtil.HostCommandResults configResults = - commandUtil.executeFailFast( localPeerCommands.getConfigSSHCommand(), hosts, environmentId.getId() ); - - for ( CommandUtil.HostCommandResult result : configResults.getCommandResults() ) + try { - if ( !result.hasSucceeded() ) + + for ( ContainerHost c : failedHosts ) { - LOG.error( "Failed to configure ssh on host {}: {}", result.getHost().getHostname(), - result.getFailureReason() ); + ContainerHostInfo info; + try + { + info = hostRegistry.getContainerHostInfoById( c.getId() ); + } + catch ( HostDisconnectedException e ) + { + info = new ContainerHostInfoModel( c ); + } + failedContainers.addContainer( info ); } } - - if ( configResults.hasFailures() ) + catch ( Exception e ) { - throw new PeerException( "Failed to configure ssh on each host" ); + LOG.error( e.getMessage() ); + throw new PeerException( String.format( "Error getting environment containers: %s", e.getMessage() ), e ); } + + return failedContainers; } @@ -726,6 +752,7 @@ public SshKey createSshKey( final EnvironmentId environmentId, final ContainerId } + //todo we can return ids of hosts where ssh key addition/configuration failed @Override public void addToAuthorizedKeys( final EnvironmentId environmentId, final String sshPublicKey ) throws PeerException { diff --git a/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestService.java b/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestService.java index 438283e7d66..5ac85a6b2d8 100644 --- a/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestService.java +++ b/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestService.java @@ -114,6 +114,7 @@ void addContainerSnapshot( @PathParam( "containerId" ) ContainerId containerId, @POST @Path( "{environmentId}/containers/sshkeys" ) @Consumes( MediaType.APPLICATION_JSON ) + @Produces( MediaType.APPLICATION_JSON ) Response configureSshInEnvironment( @PathParam( "environmentId" ) EnvironmentId environmentId, SshKeys sshKeys ); @GET diff --git a/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestServiceImpl.java b/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestServiceImpl.java index 2b6cb46cd1a..0f78f4d4030 100644 --- a/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestServiceImpl.java +++ b/management/server/core/local-peer/local-peer-rest/src/main/java/io/subutai/core/localpeer/rest/EnvironmentRestServiceImpl.java @@ -312,9 +312,7 @@ public Response configureSshInEnvironment( final EnvironmentId environmentId, fi Preconditions.checkNotNull( sshKeys ); Preconditions.checkArgument( !sshKeys.isEmpty() ); - localPeer.configureSshInEnvironment( environmentId, sshKeys ); - - return Response.ok().build(); + return Response.ok(localPeer.configureSshInEnvironment( environmentId, sshKeys )).build(); } catch ( Exception e ) { diff --git a/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/EnvironmentWebClient.java b/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/EnvironmentWebClient.java index 2f212a16634..7c42aee9f4c 100644 --- a/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/EnvironmentWebClient.java +++ b/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/EnvironmentWebClient.java @@ -12,6 +12,7 @@ import com.google.common.base.Preconditions; import io.subutai.bazaar.share.quota.ContainerQuota; +import io.subutai.common.environment.Containers; import io.subutai.common.environment.HostAddresses; import io.subutai.common.environment.PeerTemplatesDownloadProgress; import io.subutai.common.host.ContainerHostState; @@ -458,7 +459,7 @@ public SshKeys generateSshKeysForEnvironment( final EnvironmentId environmentId, } - public void configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys ) + public Containers configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys ) throws PeerException { WebClient client = null; @@ -470,6 +471,7 @@ public void configureSshInEnvironment( final EnvironmentId environmentId, final client = WebClientBuilder.buildEnvironmentWebClient( peerInfo, path, provider ); client.type( MediaType.APPLICATION_JSON ); + client.accept( MediaType.APPLICATION_JSON ); response = client.post( sshKeys ); } @@ -483,7 +485,7 @@ public void configureSshInEnvironment( final EnvironmentId environmentId, final WebClientBuilder.close( client ); } - WebClientBuilder.checkResponse( response ); + return WebClientBuilder.checkResponse( response, Containers.class ); } diff --git a/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/RemotePeerImpl.java b/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/RemotePeerImpl.java index 6dd3fd18f3c..7a6d61de3f9 100644 --- a/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/RemotePeerImpl.java +++ b/management/server/core/peer-manager/peer-manager-impl/src/main/java/io/subutai/core/peer/impl/RemotePeerImpl.java @@ -415,14 +415,14 @@ public SshKeys readOrCreateSshKeysForEnvironment( final EnvironmentId environmen @RolesAllowed( "Environment-Management|Update" ) @Override - public void configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys ) + public Containers configureSshInEnvironment( final EnvironmentId environmentId, final SshKeys sshKeys ) throws PeerException { Preconditions.checkNotNull( environmentId, "Environment id is null" ); Preconditions.checkNotNull( sshKeys, "SshPublicKey is null" ); Preconditions.checkArgument( !sshKeys.isEmpty(), "No ssh keys" ); - environmentWebClient.configureSshInEnvironment( environmentId, sshKeys ); + return environmentWebClient.configureSshInEnvironment( environmentId, sshKeys ); } diff --git a/management/server/subutai-common/src/main/java/io/subutai/common/peer/Peer.java b/management/server/subutai-common/src/main/java/io/subutai/common/peer/Peer.java index 6716568c3c5..f8ea8abe44a 100644 --- a/management/server/subutai-common/src/main/java/io/subutai/common/peer/Peer.java +++ b/management/server/subutai-common/src/main/java/io/subutai/common/peer/Peer.java @@ -3,6 +3,7 @@ import java.util.Date; import java.util.Map; +import java.util.Set; import org.bouncycastle.openpgp.PGPPublicKeyRing; @@ -278,7 +279,15 @@ void updatePeerEnvironmentPubKey( EnvironmentId environmentId, PGPPublicKeyRing SshKeys readOrCreateSshKeysForEnvironment( EnvironmentId environmentId, SshEncryptionType sshKeyType ) throws PeerException; - void configureSshInEnvironment( EnvironmentId environmentId, SshKeys sshKeys ) throws PeerException; + /** + * Adds {@param sshKeys} to each host that belongs to the specified environment + * + * @param environmentId id of environment + * @param sshKeys ssh keys to add + * + * @return set of hosts where ssh key addition failed + */ + Containers configureSshInEnvironment( EnvironmentId environmentId, SshKeys sshKeys ) throws PeerException; void removeFromAuthorizedKeys( EnvironmentId environmentId, String sshPublicKey ) throws PeerException;