public static ResourceModelSourceService getInstanceForFramework(final Framework framework) {
   if (null == framework.getService(SERVICE_NAME)) {
     final ResourceModelSourceService service = new ResourceModelSourceService(framework);
     framework.setService(SERVICE_NAME, service);
     return service;
   }
   return (ResourceModelSourceService) framework.getService(SERVICE_NAME);
 }
  public void setUp() {
    super.setUp();
    final Framework frameworkInstance = getFrameworkInstance();

    final IRundeckProject frameworkProject =
        frameworkInstance.getFrameworkProjectMgr().createFrameworkProject(PROJ_NAME);
    generateProjectResourcesFile(
        new File("src/test/resources/com/dtolabs/rundeck/core/common/test-nodes1.xml"),
        frameworkProject);
  }
 /**
  * Create a NodeFileParser given the project and the source file, using the predetermined format
  *
  * @param file the nodes resource file
  * @param format the file format
  * @return a new parser based on the determined format
  * @throws ResourceModelSourceException if the format is not supported
  */
 protected ResourceFormatParser createParser(final File file, final String format)
     throws ResourceModelSourceException {
   try {
     if (null != format) {
       return framework.getResourceFormatParserService().getParserForFormat(format);
     } else {
       return framework.getResourceFormatParserService().getParserForFileExtension(file);
     }
   } catch (UnsupportedFormatException e) {
     throw new ResourceModelSourceException(e);
   }
 }
  public void setUp() {
    super.setUp();

    final Framework frameworkInstance = getFrameworkInstance();
    final FrameworkProject frameworkProject =
        frameworkInstance.getFrameworkProjectMgr().createFrameworkProject(PROJ_NAME);
    File resourcesfile = new File(frameworkProject.getNodesResourceFilePath());
    // copy test nodes to resources file
    try {
      FileUtils.copyFileStreams(
          new File("src/test/resources/com/dtolabs/rundeck/core/common/test-nodes1.xml"),
          resourcesfile);
    } catch (IOException e) {
      throw new RuntimeException("Caught Setup exception: " + e.getMessage(), e);
    }
  }
示例#5
0
 public ProjectTool() {
   final File basedir = new File(Constants.getSystemBaseDir());
   /** Initialize the log4j logger */
   PropertyConfigurator.configure(
       new File(new File(basedir, "etc"), "log4j.properties").getAbsolutePath());
   framework = Framework.getInstance(Constants.getSystemBaseDir());
 }
  /**
   * Execute a workflow item, returns true if the item succeeds. This method will throw an exception
   * if the workflow item fails and the Workflow is has keepgoing==false.
   *
   * @param failedMap List to add any messages if the item fails
   * @param c index of the WF item
   * @param cmd WF item descriptor
   * @return true if the execution succeeds, false otherwise
   */
  protected StepExecutionResult executeWFItem(
      final StepExecutionContext executionContext,
      final Map<Integer, StepExecutionResult> failedMap,
      final int c,
      final StepExecutionItem cmd) {

    if (null != executionContext.getExecutionListener()) {
      executionContext
          .getExecutionListener()
          .log(Constants.DEBUG_LEVEL, c + ": Workflow step executing: " + cmd);
    }
    StepExecutionResult result;
    try {
      result =
          framework
              .getExecutionService()
              .executeStep(
                  ExecutionContextImpl.builder(executionContext).stepNumber(c).build(), cmd);
      if (!result.isSuccess()) {
        failedMap.put(c, result);
      }
    } catch (StepException e) {
      result = StepExecutionResultImpl.wrapStepException(e);
      failedMap.put(c, result);
    }
    if (null != executionContext.getExecutionListener()) {
      executionContext
          .getExecutionListener()
          .log(Constants.DEBUG_LEVEL, c + ": Workflow step finished, result: " + result);
    }
    return result;
  }
  private void generateResourcesFile(final File resfile, final String format)
      throws ResourceModelSourceException {
    final NodeEntryImpl node = framework.createFrameworkNode();
    node.setFrameworkProject(configuration.project);
    final ResourceFormatGenerator generator;
    if (null != format) {
      try {
        generator = framework.getResourceFormatGeneratorService().getGeneratorForFormat(format);
      } catch (UnsupportedFormatException e) {
        throw new ResourceModelSourceException(e);
      }
    } else {
      try {
        generator =
            framework.getResourceFormatGeneratorService().getGeneratorForFileExtension(resfile);
      } catch (UnsupportedFormatException e) {
        throw new ResourceModelSourceException(e);
      }
    }
    if (configuration.includeServerNode) {
      NodeSetImpl nodes = new NodeSetImpl();
      nodes.putNode(node);

      if (!resfile.getParentFile().exists()) {
        if (!resfile.getParentFile().mkdirs()) {
          throw new ResourceModelSourceException(
              "Parent dir for resource file does not exists, and could not be created: " + resfile);
        }
      }

      try {
        final FileOutputStream stream = new FileOutputStream(resfile);
        try {
          generator.generateDocument(nodes, stream);
        } finally {
          stream.close();
        }
      } catch (IOException e) {
        throw new ResourceModelSourceException(e);
      } catch (ResourceFormatGeneratorException e) {
        throw new ResourceModelSourceException(e);
      }
    }
  }
 private void loadNodes(final File nodesFile, final String format)
     throws ResourceModelSourceException {
   if (!nodesFile.isFile() && configuration.generateFileAutomatically) {
     generateResourcesFile(nodesFile, format);
   } else if (configuration.includeServerNode) {
     final NodeEntryImpl node = framework.createFrameworkNode();
     nodeSet.putNode(node);
   }
   if (nodesFile.isFile()) {
     final ResourceFormatParser parser = createParser(nodesFile, format);
     try {
       final INodeSet set = parser.parseDocument(nodesFile);
       if (null != set) {
         nodeSet.putNodes(set);
       }
     } catch (ResourceFormatParserException e) {
       throw new ResourceModelSourceException(e);
     }
   } else if (configuration.requireFileExists) {
     throw new ResourceModelSourceException("File does not exist: " + nodesFile);
   }
 }
  public void testGenericItem() {

    {
      // test jobref item
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      final testWorkflowCmdItem item = new testWorkflowCmdItem();
      item.type = "my-type";
      commands.add(item);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      interpreterService.registerInstance("my-type", interpreterMock);
      interpreterService.registerInstance("exec", failMock);
      interpreterService.registerInstance("script", failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);

      // set resturn result
      interpreterMock.resultList.add(
          new InterpreterResult() {
            public boolean isSuccess() {
              return true;
            }
          });

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, interpreterMock.executionItemList.size());
      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof testWorkflowCmdItem);
      testWorkflowCmdItem execItem = (testWorkflowCmdItem) executionItem1;
      assertNotNull(execItem.getType());
      assertEquals("my-type", execItem.getType());
      assertEquals(1, interpreterMock.executionContextList.size());
      final ExecutionContext executionContext = interpreterMock.executionContextList.get(0);
      assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
      assertNull(executionContext.getArgs());
      assertNull(executionContext.getDataContext());
      assertEquals(0, executionContext.getLoglevel());
      assertEquals("user1", executionContext.getUser());
      assertEquals(nodeset, executionContext.getNodeSelector());
    }
  }
  public void testExecuteWorkflow() throws Exception {
    {
      final FrameworkProject frameworkProject =
          testFramework.getFrameworkProjectMgr().getFrameworkProject(TEST_PROJECT);
      final INodeSet nodes = frameworkProject.getNodeSet();
      assertNotNull(nodes);
      assertEquals(2, nodes.getNodes().size());
    }

    {
      // test empty workflow
      final NodeSet nodeset = new NodeSet();
      final WorkflowImpl workflow =
          new WorkflowImpl(new ArrayList<ExecutionItem>(), 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, interpreterMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(0, interpreterMock.executionItemList.size());
    }
    {
      // test undefined workflow item
      final NodeSet nodeset = new NodeSet();
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      commands.add(new testWorkflowCmdItem());

      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset.nodeSelectorWithDefaultAll())
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, interpreterMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertFalse(result.isSuccess());
      assertEquals(0, interpreterMock.executionItemList.size());
      assertNotNull("threw exception: " + result.getException(), result.getException());
      assertTrue(
          "threw exception: " + result.getException(),
          result.getException() instanceof WorkflowStepFailureException);
      assertEquals(
          "threw exception: " + result.getException(),
          "Step 1 of the workflow threw an exception: Failed dispatching to node test1: provider name was null for Service: CommandInterpreter",
          result.getException().getMessage());
    }

    {
      // test script exec item
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      final ExecutionItem testWorkflowCmdItem =
          new ScriptFileCommandBase() {
            @Override
            public String getScript() {
              return "a command";
            }
          };
      commands.add(testWorkflowCmdItem);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      interpreterService.registerInstance("exec", failMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, failMock);

      // set resturn result
      interpreterMock.resultList.add(
          new InterpreterResult() {
            public boolean isSuccess() {
              return true;
            }
          });

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, interpreterMock.executionItemList.size());
      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) executionItem1;
      assertEquals("a command", scriptItem.getScript());
      assertNull(scriptItem.getScriptAsStream());
      assertNull(scriptItem.getServerScriptFilePath());
      assertEquals(1, interpreterMock.executionContextList.size());
      final ExecutionContext executionContext = interpreterMock.executionContextList.get(0);
      assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
      assertNull(executionContext.getArgs());
      assertNull(executionContext.getDataContext());
      assertEquals(0, executionContext.getLoglevel());
      assertEquals("user1", executionContext.getUser());
      assertEquals(
          "expected " + nodeset + ", but was " + executionContext.getNodeSelector(),
          nodeset,
          executionContext.getNodeSelector());
    }
    {
      // test command exec item
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      final ExecutionItem testWorkflowCmdItem =
          new ExecCommandBase() {
            @Override
            public String[] getCommand() {
              return new String[] {"a", "command"};
            }
          };

      commands.add(testWorkflowCmdItem);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, failMock);

      // set resturn result
      interpreterMock.resultList.add(
          new InterpreterResult() {
            public boolean isSuccess() {
              return true;
            }
          });

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, interpreterMock.executionItemList.size());
      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ExecCommandExecutionItem);
      ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
      assertNotNull(execItem.getCommand());
      assertEquals(2, execItem.getCommand().length);
      assertEquals("a", execItem.getCommand()[0]);
      assertEquals("command", execItem.getCommand()[1]);
      assertEquals(1, interpreterMock.executionContextList.size());
      final ExecutionContext executionContext = interpreterMock.executionContextList.get(0);
      assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
      assertNull(executionContext.getArgs());
      assertNull(executionContext.getDataContext());
      assertEquals(0, executionContext.getLoglevel());
      assertEquals("user1", executionContext.getUser());
      assertEquals(nodeset, executionContext.getNodeSelector());
    }
    {
      // test workflow of three successful items
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();

      final ExecutionItem testWorkflowCmdItem =
          new ExecCommandBase() {
            @Override
            public String[] getCommand() {
              return new String[] {"a", "2", "command"};
            }
          };

      commands.add(testWorkflowCmdItem);

      final ExecutionItem testWorkflowCmdItemScript =
          new ScriptFileCommandBase() {
            @Override
            public String getScript() {
              return "a command";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "1"};
            }
          };
      commands.add(testWorkflowCmdItemScript);

      final ExecutionItem testWorkflowCmdItemScript2 =
          new ScriptFileCommandBase() {
            @Override
            public String getServerScriptFilePath() {
              return "/some/file/path";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "2"};
            }
          };
      commands.add(testWorkflowCmdItemScript2);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .args(new String[] {"test", "args"})
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);

      // set resturn results
      interpreterMock.resultList.add(new testResult(true, 0));
      interpreterMock.resultList.add(new testResult(true, 1));
      interpreterMock.resultList.add(new testResult(true, 2));

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, result.getResultSet().size());
      assertNotNull(result.getResultSet());
      assertNotNull(
          "missing key " + testnode + ": " + result.getResultSet().keySet(),
          result.getResultSet().get(testnode));
      final List<StatusResult> test1 = result.getResultSet().get(testnode);
      assertEquals(3, test1.size());
      for (final int i : new int[] {0, 1, 2}) {
        final StatusResult interpreterResult = test1.get(i);
        assertTrue(interpreterResult instanceof testResult);
        testResult val = (testResult) interpreterResult;
        assertTrue(val.isSuccess());
        assertEquals(i, val.flag);
      }

      assertEquals(3, interpreterMock.executionItemList.size());

      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ExecCommandExecutionItem);
      ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
      assertNotNull(execItem.getCommand());
      assertEquals(3, execItem.getCommand().length);
      assertEquals("a", execItem.getCommand()[0]);
      assertEquals("2", execItem.getCommand()[1]);
      assertEquals("command", execItem.getCommand()[2]);

      final ExecutionItem item2 = interpreterMock.executionItemList.get(1);
      assertTrue(
          "wrong class: " + item2.getClass().getName(),
          item2 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) item2;
      assertEquals("a command", scriptItem.getScript());
      assertNull(scriptItem.getScriptAsStream());
      assertNull(scriptItem.getServerScriptFilePath());

      final ExecutionItem item3 = interpreterMock.executionItemList.get(2);
      assertTrue(
          "wrong class: " + item3.getClass().getName(),
          item2 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem2 = (ScriptFileCommandExecutionItem) item3;
      assertNull(scriptItem2.getScript());
      assertNull(scriptItem2.getScriptAsStream());
      assertEquals("/some/file/path", scriptItem2.getServerScriptFilePath());
      assertNotNull(scriptItem2.getArgs());
      assertEquals(2, scriptItem2.getArgs().length);
      assertEquals("-testargs", scriptItem2.getArgs()[0]);
      assertEquals("2", scriptItem2.getArgs()[1]);

      assertEquals(3, interpreterMock.executionContextList.size());

      for (final int i : new int[] {0, 1, 2}) {
        final ExecutionContext executionContext = interpreterMock.executionContextList.get(i);
        assertEquals("item " + i, TEST_PROJECT, executionContext.getFrameworkProject());
        assertNull("item " + i, executionContext.getDataContext());
        assertEquals("item " + i, 0, executionContext.getLoglevel());
        assertEquals("item " + i, "user1", executionContext.getUser());
        assertEquals("item " + i, nodeset, executionContext.getNodeSelector());
        assertNotNull("item " + i, executionContext.getArgs());
        assertEquals("item " + i, 2, executionContext.getArgs().length);
        assertEquals("item " + i, "test", executionContext.getArgs()[0]);
        assertEquals("item " + i, "args", executionContext.getArgs()[1]);
      }
    }
    {
      // test a workflow with a failing item (1), with keepgoing=false
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();

      final ExecutionItem testWorkflowCmdItem =
          new ExecCommandBase() {
            @Override
            public String[] getCommand() {
              return new String[] {"a", "2", "command"};
            }
          };

      commands.add(testWorkflowCmdItem);

      final ExecutionItem testWorkflowCmdItemScript =
          new ScriptFileCommandBase() {
            @Override
            public String getScript() {
              return "a command";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "1"};
            }
          };
      commands.add(testWorkflowCmdItemScript);

      final ExecutionItem testWorkflowCmdItemScript2 =
          new ScriptFileCommandBase() {
            @Override
            public String getServerScriptFilePath() {
              return "/some/file/path";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "2"};
            }
          };
      commands.add(testWorkflowCmdItemScript2);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      workflow.setKeepgoing(false);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);

      // set resturn results, fail on second item
      interpreterMock.resultList.add(new testResult(true, 0));
      interpreterMock.resultList.add(new testResult(false, 1));
      interpreterMock.resultList.add(new testResult(true, 2));

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertFalse(result.isSuccess());
      assertNotNull("threw exception: " + result.getException(), result.getException());
      assertTrue(
          "threw exception: " + result.getException(),
          result.getException() instanceof WorkflowStepFailureException);
      WorkflowStepFailureException wfsfe = (WorkflowStepFailureException) result.getException();
      assertEquals(2, wfsfe.getWorkflowStep());
      assertNotNull(wfsfe.getExecutionResult());
      final ExecutionResult executionResult = wfsfe.getExecutionResult();
      assertNotNull(executionResult.getResultObject());
      assertNotNull(executionResult.getResultObject().getResults());
      assertEquals(1, executionResult.getResultObject().getResults().size());
      assertNotNull(executionResult.getResultObject().getResults().get(testnode));
      final StatusResult testnode1 = executionResult.getResultObject().getResults().get(testnode);
      assertNotNull(testnode1);
      assertTrue(testnode1 instanceof testResult);
      testResult failResult = (testResult) testnode1;
      assertEquals(1, failResult.flag);

      assertEquals(1, result.getResultSet().size());
      assertNotNull(result.getResultSet());
      assertNotNull(
          "missing key" + testnode + ": " + result.getResultSet().keySet(),
          result.getResultSet().get(testnode));
      final List<StatusResult> test1 = result.getResultSet().get(testnode);
      assertEquals(2, test1.size());
      for (final int i : new int[] {0, 1}) {
        final StatusResult interpreterResult = test1.get(i);
        assertTrue(interpreterResult instanceof testResult);
        testResult val = (testResult) interpreterResult;
        assertEquals(i, val.flag);
        if (0 == i) {
          assertTrue(val.isSuccess());
        } else {
          assertFalse(val.isSuccess());
        }
      }

      assertEquals(2, interpreterMock.executionItemList.size());

      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ExecCommandExecutionItem);
      ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
      assertNotNull(execItem.getCommand());
      assertEquals(3, execItem.getCommand().length);
      assertEquals("a", execItem.getCommand()[0]);
      assertEquals("2", execItem.getCommand()[1]);
      assertEquals("command", execItem.getCommand()[2]);

      final ExecutionItem item2 = interpreterMock.executionItemList.get(1);
      assertTrue(
          "wrong class: " + item2.getClass().getName(),
          item2 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) item2;
      assertEquals("a command", scriptItem.getScript());
      assertNull(scriptItem.getScriptAsStream());
      assertNull(scriptItem.getServerScriptFilePath());
      assertNotNull(scriptItem.getArgs());
      assertEquals(2, scriptItem.getArgs().length);
      assertEquals("-testargs", scriptItem.getArgs()[0]);
      assertEquals("1", scriptItem.getArgs()[1]);

      assertEquals(2, interpreterMock.executionContextList.size());

      for (final int i : new int[] {0, 1}) {
        final ExecutionContext executionContext = interpreterMock.executionContextList.get(i);
        assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
        assertNull(executionContext.getDataContext());
        assertEquals(0, executionContext.getLoglevel());
        assertEquals("user1", executionContext.getUser());
        assertEquals(nodeset, executionContext.getNodeSelector());
        assertNull(executionContext.getArgs());
      }
    }
  }
  public DispatcherResult dispatch(
      final ExecutionContext context, final ExecutionItem item, final Dispatchable toDispatch)
      throws DispatcherException {
    final NodesSelector nodesSelector = context.getNodeSelector();
    INodeSet nodes = null;
    try {
      nodes =
          framework.filterAuthorizedNodes(
              context.getFrameworkProject(),
              new HashSet<String>(Arrays.asList("read", "run")),
              framework.filterNodeSet(
                  nodesSelector, context.getFrameworkProject(), context.getNodesFile()));
    } catch (NodeFileParserException e) {
      throw new DispatcherException(e);
    }
    if (nodes.getNodes().size() < 1) {
      throw new DispatcherException("No nodes matched");
    }
    boolean keepgoing = context.isKeepgoing();

    context
        .getExecutionListener()
        .log(4, "preparing for sequential execution on " + nodes.getNodes().size() + " nodes");
    final HashSet<String> nodeNames = new HashSet<String>(nodes.getNodeNames());
    final HashMap<String, Object> failures = new HashMap<String, Object>();
    FailedNodesListener failedListener = context.getExecutionListener().getFailedNodesListener();
    if (null != failedListener) {
      failedListener.matchedNodes(nodeNames);
    }
    boolean interrupted = false;
    final Thread thread = Thread.currentThread();
    boolean success = true;
    final HashMap<String, StatusResult> resultMap = new HashMap<String, StatusResult>();
    final Collection<INodeEntry> nodes1 = nodes.getNodes();
    // reorder based on configured rank property and order
    final String rankProperty =
        null != context.getNodeRankAttribute() ? context.getNodeRankAttribute() : "nodename";
    final boolean rankAscending = context.isNodeRankOrderAscending();
    final INodeEntryComparator comparator = new INodeEntryComparator(rankProperty);
    final TreeSet<INodeEntry> orderedNodes =
        new TreeSet<INodeEntry>(rankAscending ? comparator : Collections.reverseOrder(comparator));
    orderedNodes.addAll(nodes1);
    for (final Object node1 : orderedNodes) {
      if (thread.isInterrupted()
          || thread instanceof ExecutionServiceThread
              && ((ExecutionServiceThread) thread).isAborted()) {
        interrupted = true;
        break;
      }
      final INodeEntry node = (INodeEntry) node1;
      context
          .getExecutionListener()
          .log(
              Constants.DEBUG_LEVEL,
              "Executing command on node: " + node.getNodename() + ", " + node.toString());
      try {

        if (thread.isInterrupted()
            || thread instanceof ExecutionServiceThread
                && ((ExecutionServiceThread) thread).isAborted()) {
          interrupted = true;
          break;
        }
        final StatusResult result;
        final ExecutionContext interimcontext =
            new ExecutionContextImpl.Builder(context)
                .nodeSelector(SelectorUtils.singleNode(node.getNodename()))
                .build();
        if (null != item) {
          result = framework.getExecutionService().interpretCommand(interimcontext, item, node);
        } else {
          result = toDispatch.dispatch(interimcontext, node);
        }
        if (null != result) {
          resultMap.put(node.getNodename(), result);
        }
        if (null == result || !result.isSuccess()) {
          success = false;
          //                    context.getExecutionListener().log(Constants.ERR_LEVEL,
          //                        "Failed execution for node " + node.getNodename() + ": " +
          // result);
          if (null != result) {
            failures.put(node.getNodename(), result);
          } else {
            failures.put(node.getNodename(), "Failed execution, result was null");
          }
          if (!keepgoing) {
            break;
          }
        } else {
          nodeNames.remove(node.getNodename());
        }
      } catch (Throwable e) {
        success = false;
        failures.put(
            node.getNodename(), "Error dispatching command to the node: " + e.getMessage());
        context
            .getExecutionListener()
            .log(
                Constants.ERR_LEVEL,
                "Failed dispatching to node " + node.getNodename() + ": " + e.getMessage());

        final StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        context
            .getExecutionListener()
            .log(
                Constants.DEBUG_LEVEL,
                "Failed dispatching to node "
                    + node.getNodename()
                    + ": "
                    + stringWriter.toString());

        if (!keepgoing) {
          if (failures.size() > 0 && null != failedListener) {
            // tell listener of failed node list
            failedListener.nodesFailed(failures);
          }
          throw new DispatcherException(
              "Failed dispatching to node " + node.getNodename() + ": " + e.getMessage(), e, node);
        }
      }
    }
    if (keepgoing && nodeNames.size() > 0) {
      if (null != failedListener) {
        // tell listener of failed node list
        failedListener.nodesFailed(failures);
      }
      // now fail
      // XXX: needs to change from exception
      throw new NodesetFailureException(failures);
    } else if (null != failedListener && failures.isEmpty() && !interrupted) {
      failedListener.nodesSucceeded();
    }
    if (interrupted) {
      throw new DispatcherException("Node dispatch interrupted");
    }

    final boolean status = success;
    return new DispatcherResult() {
      public Map<String, ? extends StatusResult> getResults() {
        return resultMap;
      }

      public boolean isSuccess() {
        return status;
      }

      @Override
      public String toString() {
        return "DispatcherResult{"
            + "status="
            + isSuccess()
            + ", "
            + "results="
            + getResults()
            + "}";
      }
    };
  }
示例#12
0
  public void testRun() throws Exception {
    final Framework framework = getFrameworkInstance();
    {
      // test list action
      // test null  result

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      try {
        tool.run(new String[] {"list"});
        fail("run should fail");
      } catch (JobsToolException e) {
        assertTrue(e.getMessage().startsWith("List request returned null"));
      }
    }
    {
      // test list action
      // test 0 items result

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertNull(centralDispatcher1.listStoredJobsOutput);
    }
    {
      // test list action with output file
      // test 0 items result

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);
      File t = File.createTempFile("TestJobsTool", "xml");
      t.deleteOnExit();

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "-f", t.getAbsolutePath()});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertNotNull(centralDispatcher1.listStoredJobsOutput);
    }
    {
      // test list action with query params, -n

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "-" + JobsTool.NAME_OPTION, "name1"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNull(centralDispatcher1.listStoredJobsOutput);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertEquals("name1", centralDispatcher1.listStoredJobsQuery.getNameMatch());
    }
    {
      // test list action with query params, --name

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "--" + JobsTool.NAME_OPTION_LONG, "name1"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNull(centralDispatcher1.listStoredJobsOutput);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertEquals("name1", centralDispatcher1.listStoredJobsQuery.getNameMatch());
    }
    {
      // test list action with query params, -g

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "-" + JobsTool.GROUP_OPTION, "group1"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNull(centralDispatcher1.listStoredJobsOutput);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertEquals("group1", centralDispatcher1.listStoredJobsQuery.getGroupMatch());
    }
    {
      // test list action with query params, --group

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "--" + JobsTool.GROUP_OPTION_LONG, "group2"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNull(centralDispatcher1.listStoredJobsOutput);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertEquals("group2", centralDispatcher1.listStoredJobsQuery.getGroupMatch());
    }
    {
      // test list action with query params, -i

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "-" + JobsTool.IDLIST_OPTION, "1,2"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNull(centralDispatcher1.listStoredJobsOutput);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertNull(centralDispatcher1.listStoredJobsQuery.getCommand());
      assertNull(centralDispatcher1.listStoredJobsQuery.getType());
      assertNull(centralDispatcher1.listStoredJobsQuery.getResource());
      assertEquals("1,2", centralDispatcher1.listStoredJobsQuery.getIdlist());
    }
    {
      // test list action with query params, --idlist

      final JobsTool tool = new JobsTool(framework);
      final testCentralDispatcher1 centralDispatcher1 = new testCentralDispatcher1();
      framework.setCentralDispatcherMgr(centralDispatcher1);

      final ArrayList<IStoredJob> jobs = new ArrayList<IStoredJob>();
      centralDispatcher1.listJobsResult = jobs;

      tool.run(new String[] {"list", "--" + JobsTool.IDLIST_OPTION_LONG, "3,4"});
      assertTrue("list action was not called", centralDispatcher1.listStoredJobsCalled);
      assertFalse("load action should not be called", centralDispatcher1.loadJobsCalled);
      assertNull(centralDispatcher1.listStoredJobsOutput);
      assertNotNull(centralDispatcher1.listStoredJobsQuery);
      assertNull(centralDispatcher1.listStoredJobsQuery.getCommand());
      assertNull(centralDispatcher1.listStoredJobsQuery.getType());
      assertNull(centralDispatcher1.listStoredJobsQuery.getResource());
      assertEquals("3,4", centralDispatcher1.listStoredJobsQuery.getIdlist());
    }
  }