/** * Saves the instances into a file. * @param app the application (not null) * @param configurationDirectory the configuration directory */ public static void saveInstances( Application app ) { File targetFile = new File( app.getDirectory(), Constants.PROJECT_DIR_INSTANCES + "/" + INSTANCES_FILE ); try { Utils.createDirectory( targetFile.getParentFile()); RuntimeModelIo.writeInstances( targetFile, app.getRootInstances()); } catch( IOException e ) { Logger logger = Logger.getLogger( ConfigurationUtils.class.getName()); logger.severe( "Failed to save instances. " + e.getMessage()); Utils.logException( logger, e ); } }
/** * Constructor. * @param template */ public Application( ApplicationTemplate template ) { this.template = template; // We must duplicate all the instances if( template != null ) { template.associateApplication( this ); for( Instance rootInstance : template.getRootInstances()) getRootInstances().add( InstanceHelpers.replicateInstance( rootInstance )); } }
@Override public void undeployAll( ManagedApplication ma, Instance instance ) throws IOException { this.messagingMngr.checkMessagingConfiguration(); Collection<Instance> initialInstances; if( instance != null ) initialInstances = Collections.singletonList(instance); else initialInstances = ma.getApplication().getRootInstances(); // We do not need to undeploy all the instances, just the first instance List<Exception> exceptions = new ArrayList<> (); for( Instance initialInstance : initialInstances ) { try { changeInstanceState( ma, initialInstance, InstanceStatus.NOT_DEPLOYED ); } catch( Exception e ) { exceptions.add( e ); } } processExceptions( this.logger, exceptions, "One or several errors occurred while undeploying instances." ); }
/** * Finds an instance by name. * @param rootInstance a root instance * @param instancePath the instance path * @return an instance, or null if it was not found */ public static Instance findInstanceByPath( Instance rootInstance, String instancePath ) { Application tempApplication = new Application( new ApplicationTemplate()); if( rootInstance != null ) tempApplication.getRootInstances().add( rootInstance ); return findInstanceByPath( tempApplication, instancePath ); }
@Override public void deployAndStartAll( ManagedApplication ma, Instance instance ) throws IOException { this.messagingMngr.checkMessagingConfiguration(); Collection<Instance> initialInstances; if( instance != null ) initialInstances = Collections.singletonList(instance); else initialInstances = ma.getApplication().getRootInstances(); List<Exception> exceptions = new ArrayList<> (); for( Instance initialInstance : initialInstances ) { for( Instance i : InstanceHelpers.buildHierarchicalList( initialInstance )) { try { changeInstanceState( ma, i, InstanceStatus.DEPLOYED_STARTED ); } catch( Exception e ) { exceptions.add( e ); } } } processExceptions( this.logger, exceptions, "One or several errors occurred while deploying and starting instances." ); }
@Override public void resynchronizeAgents( ManagedApplication ma ) throws IOException { this.messagingMngr.checkMessagingConfiguration(); this.logger.fine( "Resynchronizing agents in " + ma.getName() + "..." ); for( Instance rootInstance : ma.getApplication().getRootInstances()) { if( rootInstance.getStatus() == InstanceStatus.DEPLOYED_STARTED ) this.messagingMngr.sendMessageDirectly( ma, rootInstance, new MsgCmdResynchronize()); } this.logger.fine( "Requests were sent to resynchronize agents in " + ma.getName() + "." ); }
@Override public void stopAll( ManagedApplication ma, Instance instance ) throws IOException { this.messagingMngr.checkMessagingConfiguration(); Collection<Instance> initialInstances; if( instance != null ) initialInstances = Collections.singletonList(instance); else initialInstances = ma.getApplication().getRootInstances(); // We do not need to stop all the instances, just the first children. // Stop does not mean anything for targetsMngr. List<Exception> exceptions = new ArrayList<> (); for( Instance initialInstance : initialInstances ) { try { if( ! InstanceHelpers.isTarget( initialInstance )) changeInstanceState( ma, initialInstance, InstanceStatus.DEPLOYED_STOPPED ); else for( Instance i : initialInstance.getChildren()) changeInstanceState( ma, i, InstanceStatus.DEPLOYED_STOPPED ); } catch( Exception e ) { exceptions.add( e ); } } processExceptions( this.logger, exceptions, "One or several errors occurred while stopping instances." ); }
for( Instance rootInstance : app.getRootInstances()) rootInstance.data.put( Instance.APPLICATION_NAME, app.getName());
for( Instance rootInstance : app.getRootInstances()) { if( rootInstance.getStatus() == InstanceStatus.PROBLEM ) { info = "warn";
checkErrors( ilr.getLoadErrors(), this.logger ); ma.getApplication().getRootInstances().clear(); ma.getApplication().getRootInstances().addAll( ilr.getRootInstances());
@Test public void testGetAllInstances() { Application app = new Application( new ApplicationTemplate()); Instance[] rootInstances = new Instance[ 8 ]; for( int i=0; i<rootInstances.length; i++ ) { rootInstances[ i ] = new Instance( "i-" + i ); InstanceHelpers.insertChild( rootInstances[ i ], new Instance( "child-" + i )); } app.getRootInstances().addAll( Arrays.asList( rootInstances )); List<Instance> allInstances = InstanceHelpers.getAllInstances( app ); Assert.assertEquals( rootInstances.length * 2, allInstances.size()); for( Instance rootInstance : rootInstances ) Assert.assertTrue( rootInstance.getName(), allInstances.contains( rootInstance )); }
ma.getApplication().getRootInstances().remove( instance ); this.autonomicMngr.notifyVmWasDeletedByHand( instance );
for( Instance rootInstance : ma.getApplication().getRootInstances()) { if( rootInstance.getStatus() != InstanceStatus.NOT_DEPLOYED ) throw new UnauthorizedActionException( applicationName + " contains instances that are still deployed." );
@Test public void test_N_InstantiationsWithConflict() throws Exception { // The graph Graphs graphs = new Graphs(); Component vmComponent = new Component( "VM" ).installerName( "target" ); graphs.getRootComponents().add( vmComponent ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Component warComponent = new Component( "WAR" ).installerName( "script" ); tomcatComponent.addChild( warComponent ); // The file to read File f = TestUtils.findTestFile( "/configurations/invalid/instanceof-name-conflict-with-count.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 1, fromDef.getErrors().size()); Assert.assertEquals( ErrorCode.CO_CONFLICTING_INFERRED_INSTANCE, fromDef.getErrors().iterator().next().getErrorCode()); // The assertions Application app = new Application( new ApplicationTemplate()); app.getRootInstances().addAll( rootInstances ); Assert.assertEquals( 5, rootInstances.size()); // Should be 6, but there two have the same path. // So, one is overriding the other. }
tempApp.getRootInstances().addAll( newRootInstances );
@Test public void testFindAllScopedInstances() { Instance root = new Instance( "root" ).component( new Component( "Root" ).installerName( Constants.TARGET_INSTALLER )); Instance server = new Instance( "server" ).component( new Component( "Server" ).installerName( Constants.TARGET_INSTALLER )); Instance app1 = new Instance( "app1" ).component( new Component( "Application" ).installerName( "whatever" )); Instance app2 = new Instance( "app2" ).component( new Component( "Application" ).installerName( Constants.TARGET_INSTALLER )); Instance server2 = new Instance( "server2" ).component( new Component( "Server" ).installerName( "whatever" )); InstanceHelpers.insertChild( root, server ); InstanceHelpers.insertChild( root, server2 ); InstanceHelpers.insertChild( server, app1 ); InstanceHelpers.insertChild( server2, app2 ); Application app = new Application( "test", new ApplicationTemplate()); app.getRootInstances().add( root ); List<Instance> instances = InstanceHelpers.findAllScopedInstances( app ); Assert.assertEquals( 3, instances.size()); Assert.assertTrue( instances.contains( root )); Assert.assertTrue( instances.contains( server )); Assert.assertTrue( instances.contains( app2 )); }
app.getRootInstances().addAll( rootInstances );
app.getRootInstances().addAll( rootInstances );
app.getRootInstances().addAll( rootInstances );