Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2408 #2826

Merged
merged 1 commit into from
Mar 15, 2019
Merged

#2408 #2826

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Host> hosts = Sets.newHashSet();
Set<ContainerHost> 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
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
}
Expand All @@ -483,7 +485,7 @@ public void configureSshInEnvironment( final EnvironmentId environmentId, final
WebClientBuilder.close( client );
}

WebClientBuilder.checkResponse( response );
return WebClientBuilder.checkResponse( response, Containers.class );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import java.util.Date;
import java.util.Map;
import java.util.Set;

import org.bouncycastle.openpgp.PGPPublicKeyRing;

Expand Down Expand Up @@ -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;

Expand Down