EnvironmentManagerImpl extends HostListener implements EnvironmentManager, PeerActionListener, AlertListener, BazaarEventListener, LocalPeerEventListener { @Override public SshTunnel setupSshTunnelForContainer( final String containerHostId, final String environmentId ) throws EnvironmentModificationException, EnvironmentNotFoundException, ContainerHostNotFoundException { Preconditions.checkArgument( !StringUtils.isBlank( containerHostId ), "Invalid container id" ); Preconditions.checkArgument( !StringUtils.isBlank( environmentId ), "Invalid environment id" ); final LocalEnvironment environment = ( LocalEnvironment ) loadEnvironment( environmentId ); EnvironmentContainerHost environmentContainer = environment.getContainerHostById( containerHostId ); TrackerOperation operationTracker = tracker.createTrackerOperation( MODULE_NAME, String.format( "Setting up ssh tunnel for container %s ", environmentContainer.getHostname() ) ); try { SshTunnel sshTunnel = peerManager.getLocalPeer().setupSshTunnelForContainer( environmentContainer.getIp(), Common.CONTAINER_SSH_TIMEOUT_SEC ); operationTracker.addLogDone( String.format( "Ssh for container %s is ready on tunnel %s", environmentContainer.getHostname(), sshTunnel ) ); return sshTunnel; } catch ( Exception e ) { operationTracker.addLogFailed( String.format( "Error setting up ssh for container %s: %s", environmentContainer.getHostname(), e.getMessage() ) ); throw new EnvironmentModificationException( e ); } } EnvironmentManagerImpl( final TemplateManager templateManager, final PeerManager peerManager,
                                   SecurityManager securityManager, final IdentityManager identityManager,
                                   final Tracker tracker, final RelationManager relationManager,
                                   BazaaarAdapter bazaaarAdapter, final EnvironmentService environmentService,
                                   final SystemManager systemManager ); void init(); void dispose(); IdentityManager getIdentityManager(); RelationManager getRelationManager(); @Override String getName(); @Override PeerActionResponse onPeerAction( final PeerAction peerAction ); @Override Set<Environment> getEnvironments(); void setTransientFields( Set<Environment> envs ); @Override Set<Environment> getEnvironmentsByOwnerId( long userId ); @Override EnvironmentCreationRef createEnvironment( final Topology topology, final boolean async ); @Override Set<EnvironmentContainerHost> growEnvironment( final String environmentId, final Topology topology,
                                                          final boolean async ); @Override EnvironmentCreationRef modifyEnvironment( final String environmentId, final Topology topology,
                                                     final Set<String> removedContainers,
                                                     final Map<String, ContainerQuota> changedContainers,
                                                     final boolean async ); @Override void addSshKey( final String environmentId, final String sshKey, final boolean async ); @Override void removeSshKey( final String environmentId, final String sshKey, final boolean async ); @Override SshKeys getSshKeys( final String environmentId, final SshEncryptionType encType ); @Override SshKeys createSshKey( final String environmentId, final String hostname, final SshEncryptionType encType ); @Override void resetP2PSecretKey( final String environmentId, final String newP2pSecretKey,
                                   final long p2pSecretKeyTtlSec, final boolean async ); @Override void destroyEnvironment( final String environmentId, final boolean async ); @Override void destroyContainer( final String environmentId, final String containerId, final boolean async ); @Override void changeContainerHostname( final ContainerId containerId, final String newHostname, final boolean async ); @Override void createTemplate( final String environmentId, final String containerId, final String templateName,
                                final String version, final boolean privateTemplate ); @Override void cancelEnvironmentWorkflow( final String environmentId ); @Override Map<String, CancellableWorkflow> getActiveWorkflows(); @Override Environment loadEnvironment( final String environmentId ); @Override SshTunnel setupSshTunnelForContainer( final String containerHostId, final String environmentId ); void registerListener( final EnvironmentEventListener listener ); void unregisterListener( final EnvironmentEventListener listener ); void notifyOnEnvironmentCreated( final Environment environment ); void notifyOnEnvironmentGrown( final Environment environment,
                                          final Set<EnvironmentContainerHost> containers ); @Override void notifyOnContainerDestroyed( final Environment environment, final String containerId ); @Override void notifyOnEnvironmentDestroyed( final String environmentId ); void notifyOnContainerStarted( final Environment environment, final String containerId ); void notifyOnContainerStopped( final Environment environment, final String containerId ); @Override void onContainerDestroyed( final ContainerHostInfo containerInfo ); Peer resolvePeer( final String peerId ); void save( final LocalEnvironment environment ); synchronized LocalEnvironment update( LocalEnvironment environment ); void remove( final LocalEnvironment environment ); synchronized EnvironmentContainerImpl update( final EnvironmentContainerImpl container ); @Override void addAlertHandler( final AlertHandler alertHandler ); @Override void removeAlertHandler( final AlertHandler alertHandler ); @Override Collection<AlertHandler> getRegisteredAlertHandlers(); @Override String getId(); @Override void onAlert( final AlertEvent alertEvent ); @Override EnvironmentAlertHandlers getEnvironmentAlertHandlers( final EnvironmentId environmentId ); @Override void startMonitoring( final String handlerId, final AlertHandlerPriority handlerPriority,
                                 final String environmentId ); @Override void stopMonitoring( final String handlerId, final AlertHandlerPriority handlerPriority,
                                final String environmentId ); @Override void addSshKeyToEnvironmentEntity( final String environmentId, final String sshKey ); @Override void excludePeerFromEnvironment( final String environmentId, final String peerId ); @Override void excludeContainerFromEnvironment( final String environmentId, final String containerId ); @Override void updateContainerHostname( final String environmentId, final String containerId, final String hostname ); @Override Set<EnvironmentDto> getTenantEnvironments(); @Override String getEnvironmentOwnerName( Environment environment ); @Override void onRegistrationSucceeded(); @Override void onUnregister(); Set<RemoteEnvironment> getLocallyRegisteredBazaarEnvironments(); boolean rhHasEnvironments( String rhId ); @Override void onContainerHostnameChanged( final ContainerHostInfo containerInfo, final String previousHostname,
                                            final String currentHostname ); @Override void onContainerDestroyed( final ContainerHost containerHost ); @Override Set<String> getDeletedEnvironmentsFromBazaar(); @Override void placeEnvironmentInfoByContainerIp( final String containerIp ); @Override Environment getEnvironment( String environmentId ); @Override void placeEnvironmentInfoByContainerId( final String environmentId, final String containerId ); void cleanupEnvironment( EnvironmentId environmentId );  }
@Test( expected = EnvironmentModificationException.class ) public void testSetupSshTunnelForContainer() throws Exception { environmentManager.setupSshTunnelForContainer( TestHelper.CONTAINER_ID, TestHelper.ENV_ID ); verify( localPeer ).setupSshTunnelForContainer( Common.LOCAL_HOST_IP, Common.CONTAINER_SSH_TIMEOUT_SEC ); doThrow( new PeerException() ).when( localPeer ).setupSshTunnelForContainer( Common.LOCAL_HOST_IP, Common.CONTAINER_SSH_TIMEOUT_SEC ); environmentManager.setupSshTunnelForContainer( TestHelper.CONTAINER_ID, TestHelper.ENV_ID ); }