@BeforeClass
 public void initDirs() throws Exception {
   this.tmpDir = new File("target/antbundletest/tmp");
   FileUtil.purge(this.tmpDir, true);
   this.bundleFilesDir = new File("target/antbundletest/bundlefiles");
   FileUtil.purge(this.bundleFilesDir, true);
   this.destDir = new File("target/antbundletest/destination");
   FileUtil.purge(this.destDir, true);
 }
  @Override
  public void addFile(String source, String destination) {
    super.addFile(source, destination);

    File sourceFile = new File(this.baseWorkingDirectory, source);
    File destinationFile = new File(destination);

    try {
      destinationFile.getParentFile().mkdirs();
      FileUtil.copyFile(sourceFile, destinationFile);
      audit(
          "file",
          BundleResourceDeploymentHistory.Status.SUCCESS,
          "copied file [" + sourceFile + "] to [" + destinationFile + "]");
    } catch (Exception e) {
      String msg = "Failed to copy file [" + sourceFile + "] to [" + destinationFile + "]";
      audit("file", BundleResourceDeploymentHistory.Status.FAILURE, msg);
      throw new RuntimeException(msg, e);
    }

    return;
  }
Exemple #3
0
 /**
  * Given a directory where the current deployment's bundle files will reside, this method will
  * clear out any other files located in other peer directories. In other words, the given
  * directory and all its subdirectories and child files are left untouched, but all files and
  * directories found in peer directories are wiped. This helps clean out our temp directory so we
  * don't fill up the file system with old downloaded files that we don't need anymore. See BZ
  * 752550.
  *
  * @param currentBundleVersionFilesDir
  */
 private void removeOldDownloadedBundleFiles(final File currentBundleVersionFilesDir) {
   File parent = null;
   try {
     parent = currentBundleVersionFilesDir.getParentFile();
     File[] doomedFiles =
         parent.listFiles(
             new FileFilter() {
               public boolean accept(File child) {
                 return !currentBundleVersionFilesDir.equals(child);
               }
             });
     for (File doomedFile : doomedFiles) {
       FileUtil.purge(doomedFile, true);
     }
   } catch (Exception e) {
     log.warn(
         "Failed to clean up old downloaded bundle files in ["
             + parent
             + "]. You can ignore this but if the agent is asked to deploy a lot of bundles, the file system may fill up."
             + " Cause: "
             + e);
   }
 }
  private void upgrade(boolean clean) throws Exception {
    testAntBundleInitialInstall(); // install a bundle first
    cleanPluginDirs(); // clean everything but the dest dir - we want to upgrade the destination
    prepareBeforeTestMethod(); // prepare for our new test

    // deploy upgrade and test it
    ResourceType resourceType =
        new ResourceType("testSimpleBundle2Type", "plugin", ResourceCategory.SERVER, null);
    BundleType bundleType = new BundleType("testSimpleBundle2BType", resourceType);
    Repo repo = new Repo("test-bundle-two");
    PackageType packageType = new PackageType("test-bundle-two", resourceType);
    Bundle bundle = new Bundle("test-bundle-two", bundleType, repo, packageType);
    BundleVersion bundleVersion =
        new BundleVersion(
            "test-bundle-two", "3.0", bundle, getRecipeFromFile("test-bundle-three.xml"));
    BundleDestination destination =
        new BundleDestination(
            bundle,
            "testSimpleBundle2Dest",
            new ResourceGroup("testSimpleBundle2Group"),
            DEST_BASE_DIR_NAME,
            this.destDir.getAbsolutePath());

    Configuration config = new Configuration();
    String customPropName = "custom.prop";
    String customPropValue = "DEF";
    String onePropName = "one.prop";
    String onePropValue = "one-one-one";
    String threePropName = "three.prop";
    String threePropValue = "333";
    config.put(new PropertySimple(customPropName, customPropValue));
    config.put(new PropertySimple(onePropName, onePropValue));
    config.put(new PropertySimple(threePropName, threePropValue));

    BundleDeployment deployment = new BundleDeployment();
    deployment.setId(456);
    deployment.setName("test bundle 3 deployment name - upgrades test bundle 2");
    deployment.setBundleVersion(bundleVersion);
    deployment.setConfiguration(config);
    deployment.setDestination(destination);

    // copy the test archive file to the bundle files dir
    FileUtil.copyFile(
        new File("src/test/resources/test-bundle-three-archive.zip"),
        new File(this.bundleFilesDir, "test-bundle-three-archive.zip"));

    // create test.properties file in the bundle files dir
    File file1 = new File(this.bundleFilesDir, "test.properties");
    Properties props = new Properties();
    props.setProperty(customPropName, "@@" + customPropName + "@@");
    FileOutputStream outputStream = new FileOutputStream(file1);
    props.store(outputStream, "test.properties comment");
    outputStream.close();

    // create some additional files - note: recipe says to ignore "ignore/**"
    File ignoreDir = new File(this.destDir, "ignore");
    File extraDir = new File(this.destDir, "extra");
    ignoreDir.mkdirs();
    extraDir.mkdirs();
    File ignoredFile = new File(ignoreDir, "ignore-file.txt");
    File extraFile = new File(extraDir, "extra-file.txt");
    FileUtil.writeFile(new ByteArrayInputStream("ignore".getBytes()), ignoredFile);
    FileUtil.writeFile(new ByteArrayInputStream("extra".getBytes()), extraFile);

    BundleDeployRequest request = new BundleDeployRequest();
    request.setBundleFilesLocation(this.bundleFilesDir);
    request.setResourceDeployment(new BundleResourceDeployment(deployment, null));
    request.setBundleManagerProvider(new MockBundleManagerProvider());
    request.setAbsoluteDestinationDirectory(this.destDir);
    request.setCleanDeployment(clean);

    BundleDeployResult results = plugin.deployBundle(request);

    assertResultsSuccess(results);

    // test that the prop was replaced in raw file test.properties
    Properties realizedProps = new Properties();
    loadProperties(
        realizedProps, new FileInputStream(new File(this.destDir, "config/test.properties")));
    assert customPropValue.equals(realizedProps.getProperty(customPropName))
        : "didn't replace prop";

    // test that the archive was extracted properly. These are the files in the archive or removed
    // from original:
    // REMOVED: zero-file.txt
    // one/one-file.txt (content: "@@one.prop@@") <-- recipe says this is to be replaced
    // two/two-file.txt (content: "@@two.prop@@") <-- recipe does not say to replace this
    // three/three-file.txt (content: "@@three.prop@@") <-- recipe says this is to be replaced
    File zeroFile = new File(this.destDir, "zero-file.txt");
    File oneFile = new File(this.destDir, "one/one-file.txt");
    File twoFile = new File(this.destDir, "two/two-file.txt");
    File threeFile = new File(this.destDir, "three/three-file.txt");
    assert !zeroFile.exists() : "zero file should have been removed during upgrade";
    assert oneFile.exists() : "one file missing";
    assert twoFile.exists() : "two file missing";
    assert threeFile.exists() : "three file missing";
    if (clean) {
      assert !ignoredFile.exists()
          : "ignored file should have been deleted due to clean deployment request";
      assert !extraFile.exists()
          : "extra file should have been deleted due to clean deployment request";
    } else {
      assert ignoredFile.exists() : "ignored file wasn't ignored, it was deleted";
      assert !extraFile.exists() : "extra file ignored, but it should have been deleted/backed up";
    }
    assert readFile(oneFile).startsWith(onePropValue);
    assert readFile(twoFile).startsWith("@@two.prop@@");
    assert readFile(threeFile).startsWith(threePropValue);

    DeploymentsMetadata metadata = new DeploymentsMetadata(this.destDir);
    DeploymentProperties deploymentProps = metadata.getDeploymentProperties(deployment.getId());
    assert deploymentProps.getDeploymentId() == deployment.getId();
    assert deploymentProps.getBundleName().equals(bundle.getName());
    assert deploymentProps.getBundleVersion().equals(bundleVersion.getVersion());
    assert deploymentProps.getManageRootDir() == true;

    DeploymentProperties currentProps = metadata.getCurrentDeploymentProperties();
    assert deploymentProps.equals(currentProps);

    // check the backup directory - note, clean flag is irrelevent when determining what should be
    // backed up
    File backupDir = metadata.getDeploymentBackupDirectory(deployment.getId());
    File extraBackupFile =
        new File(backupDir, extraDir.getName() + File.separatorChar + extraFile.getName());
    File ignoredBackupFile =
        new File(backupDir, ignoreDir.getName() + File.separatorChar + ignoredFile.getName());
    assert !ignoredBackupFile.exists() : "ignored file was backed up but it should not have been";
    assert extraBackupFile.exists() : "extra file was not backed up";
    assert "extra".equals(new String(StreamUtil.slurp(new FileInputStream(extraBackupFile))))
        : "bad backup of extra";

    DeploymentProperties previousProps = metadata.getPreviousDeploymentProperties(456);
    assert previousProps != null : "There should be previous deployment metadata";
    assert previousProps.getDeploymentId() == 123
        : "bad previous deployment metadata"; // testAntBundleInitialInstall used 123
    assert previousProps.getBundleName().equals(deploymentProps.getBundleName());
    assert previousProps
        .getBundleVersion()
        .equals("2.5"); // testAntBundleInitialInstall deployed version 2.5
    assert previousProps.getManageRootDir() == true;
  }
  /** Test deployment of an RHQ bundle recipe with archive file and raw file */
  @Test(enabled = true)
  public void testAntBundleInitialInstall() throws Exception {
    ResourceType resourceType =
        new ResourceType("testSimpleBundle2Type", "plugin", ResourceCategory.SERVER, null);
    BundleType bundleType = new BundleType("testSimpleBundle2BType", resourceType);
    Repo repo = new Repo("test-bundle-two");
    PackageType packageType = new PackageType("test-bundle-two", resourceType);
    Bundle bundle = new Bundle("test-bundle-two", bundleType, repo, packageType);
    BundleVersion bundleVersion =
        new BundleVersion(
            "test-bundle-two", "2.5", bundle, getRecipeFromFile("test-bundle-two.xml"));
    BundleDestination destination =
        new BundleDestination(
            bundle,
            "testSimpleBundle2Dest",
            new ResourceGroup("testSimpleBundle2Group"),
            DEST_BASE_DIR_NAME,
            this.destDir.getAbsolutePath());

    Configuration config = new Configuration();
    String customPropName = "custom.prop";
    String customPropValue = "ABC";
    String onePropName = "one.prop";
    String onePropValue = "111";
    config.put(new PropertySimple(customPropName, customPropValue));
    config.put(new PropertySimple(onePropName, onePropValue));

    BundleDeployment deployment = new BundleDeployment();
    deployment.setId(123);
    deployment.setName("test bundle 2 deployment name");
    deployment.setBundleVersion(bundleVersion);
    deployment.setConfiguration(config);
    deployment.setDestination(destination);

    // copy the test archive file to the bundle files dir
    FileUtil.copyFile(
        new File("src/test/resources/test-bundle-two-archive.zip"),
        new File(this.bundleFilesDir, "test-bundle-two-archive.zip"));

    // create test.properties file in the bundle files dir
    File file1 = new File(this.bundleFilesDir, "test.properties");
    Properties props = new Properties();
    props.setProperty(customPropName, "@@" + customPropName + "@@");
    FileOutputStream outputStream = new FileOutputStream(file1);
    props.store(outputStream, "test.properties comment");
    outputStream.close();

    BundleDeployRequest request = new BundleDeployRequest();
    request.setBundleFilesLocation(this.bundleFilesDir);
    request.setResourceDeployment(new BundleResourceDeployment(deployment, null));
    request.setBundleManagerProvider(new MockBundleManagerProvider());
    request.setAbsoluteDestinationDirectory(this.destDir);

    BundleDeployResult results = plugin.deployBundle(request);

    assertResultsSuccess(results);

    // test that the prop was replaced in raw file test.properties
    Properties realizedProps = new Properties();
    loadProperties(
        realizedProps, new FileInputStream(new File(this.destDir, "config/test.properties")));
    assert customPropValue.equals(realizedProps.getProperty(customPropName))
        : "didn't replace prop";

    // test that the archive was extracted properly. These are the files in the archive:
    // zero-file.txt (content: "zero")
    // one/one-file.txt (content: "@@one.prop@@") <-- recipe says this is to be replaced
    // two/two-file.txt (content: "@@two.prop@@") <-- recipe does not say to replace this
    File zeroFile = new File(this.destDir, "zero-file.txt");
    File oneFile = new File(this.destDir, "one/one-file.txt");
    File twoFile = new File(this.destDir, "two/two-file.txt");
    assert zeroFile.exists() : "zero file missing";
    assert oneFile.exists() : "one file missing";
    assert twoFile.exists() : "two file missing";
    assert readFile(zeroFile).startsWith("zero");
    assert readFile(oneFile).startsWith(onePropValue);
    assert readFile(twoFile).startsWith("@@two.prop@@");

    DeploymentsMetadata metadata = new DeploymentsMetadata(this.destDir);
    DeploymentProperties deploymentProps = metadata.getDeploymentProperties(deployment.getId());
    assert deploymentProps.getDeploymentId() == deployment.getId();
    assert deploymentProps.getBundleName().equals(bundle.getName());
    assert deploymentProps.getBundleVersion().equals(bundleVersion.getVersion());
    assert deploymentProps.getManageRootDir() == true;
    DeploymentProperties currentProps = metadata.getCurrentDeploymentProperties();
    assert deploymentProps.equals(currentProps);
    DeploymentProperties previousProps =
        metadata.getPreviousDeploymentProperties(deployment.getId());
    assert previousProps == null : "There should not be any previous deployment metadata";
  }
  @Test(enabled = true)
  public void testAntBundleRevert() throws Exception {
    // install then upgrade a bundle first
    testAntBundleUpgrade();
    cleanPluginDirs(); // clean everything but the dest dir - we want to keep the metadata
    prepareBeforeTestMethod(); // prepare for our new test

    // we installed version 2.5 then upgraded to 3.0
    // now we want to revert back to 2.5
    ResourceType resourceType =
        new ResourceType("testSimpleBundle2Type", "plugin", ResourceCategory.SERVER, null);
    BundleType bundleType = new BundleType("testSimpleBundle2BType", resourceType);
    Repo repo = new Repo("test-bundle-two");
    PackageType packageType = new PackageType("test-bundle-two", resourceType);
    Bundle bundle = new Bundle("test-bundle-two", bundleType, repo, packageType);
    BundleVersion bundleVersion =
        new BundleVersion(
            "test-bundle-two", "2.5", bundle, getRecipeFromFile("test-bundle-two.xml"));
    BundleDestination destination =
        new BundleDestination(
            bundle,
            "testSimpleBundle2Dest",
            new ResourceGroup("testSimpleBundle2Group"),
            DEST_BASE_DIR_NAME,
            this.destDir.getAbsolutePath());

    Configuration config = new Configuration();
    String customPropName = "custom.prop";
    String customPropValue = "ABC-revert";
    String onePropName = "one.prop";
    String onePropValue = "111-revert";
    config.put(new PropertySimple(customPropName, customPropValue));
    config.put(new PropertySimple(onePropName, onePropValue));

    BundleDeployment deployment = new BundleDeployment();
    deployment.setId(789);
    deployment.setName("test bundle 2 deployment name - REVERT");
    deployment.setBundleVersion(bundleVersion);
    deployment.setConfiguration(config);
    deployment.setDestination(destination);

    // copy the test archive file to the bundle files dir
    FileUtil.copyFile(
        new File("src/test/resources/test-bundle-two-archive.zip"),
        new File(this.bundleFilesDir, "test-bundle-two-archive.zip"));

    // create test.properties file in the bundle files dir
    File file1 = new File(this.bundleFilesDir, "test.properties");
    Properties props = new Properties();
    props.setProperty(customPropName, "@@" + customPropName + "@@");
    FileOutputStream outputStream = new FileOutputStream(file1);
    props.store(outputStream, "test.properties comment");
    outputStream.close();

    BundleDeployRequest request = new BundleDeployRequest();
    request.setBundleFilesLocation(this.bundleFilesDir);
    request.setResourceDeployment(new BundleResourceDeployment(deployment, null));
    request.setBundleManagerProvider(new MockBundleManagerProvider());
    request.setAbsoluteDestinationDirectory(this.destDir);
    request.setRevert(true);

    BundleDeployResult results = plugin.deployBundle(request);

    assertResultsSuccess(results);

    // test that the prop was replaced in raw file test.properties
    Properties realizedProps = new Properties();
    loadProperties(
        realizedProps, new FileInputStream(new File(this.destDir, "config/test.properties")));
    assert customPropValue.equals(realizedProps.getProperty(customPropName))
        : "didn't replace prop";

    // test that the archive was extracted properly. These are the files in the archive:
    // zero-file.txt (content: "zero")
    // one/one-file.txt (content: "@@one.prop@@") <-- recipe says this is to be replaced
    // two/two-file.txt (content: "@@two.prop@@") <-- recipe does not say to replace this
    // REMOVED: three/three-file.txt <-- this existed in the upgrade, but not the original
    // ----- the following was backed up and should be reverted
    // extra/extra-file.txt

    File zeroFile = new File(this.destDir, "zero-file.txt");
    File oneFile = new File(this.destDir, "one/one-file.txt");
    File twoFile = new File(this.destDir, "two/two-file.txt");
    File threeFile = new File(this.destDir, "three/three-file.txt");
    assert zeroFile.exists() : "zero file should have been restored during revert";
    assert oneFile.exists() : "one file missing";
    assert twoFile.exists() : "two file missing";
    assert !threeFile.exists() : "three file should have been deleted during revert";

    assert readFile(zeroFile).startsWith("zero") : "bad restore of zero file";
    assert readFile(oneFile).startsWith(onePropValue);
    assert readFile(twoFile).startsWith("@@two.prop@@");

    // make sure the revert restored the backed up files
    File extraFile = new File(this.destDir, "extra/extra-file.txt");
    assert extraFile.exists()
        : "extra file should have been restored due to revert deployment request";
    assert readFile(extraFile).startsWith("extra") : "bad restore of extra file";

    DeploymentsMetadata metadata = new DeploymentsMetadata(this.destDir);
    DeploymentProperties deploymentProps = metadata.getDeploymentProperties(deployment.getId());
    assert deploymentProps.getDeploymentId() == deployment.getId();
    assert deploymentProps.getBundleName().equals(bundle.getName());
    assert deploymentProps.getBundleVersion().equals(bundleVersion.getVersion());
    assert deploymentProps.getManageRootDir() == true;

    DeploymentProperties currentProps = metadata.getCurrentDeploymentProperties();
    assert deploymentProps.equals(currentProps);

    // check the backup directory - note, clean flag is irrelevent when determining what should be
    // backed up
    File backupDir = metadata.getDeploymentBackupDirectory(deployment.getId());
    File ignoredBackupFile = new File(backupDir, "ignore/ignore-file.txt");
    assert ignoredBackupFile.isFile() : "old recipe didn't ignore these, should be backed up";

    DeploymentProperties previousProps = metadata.getPreviousDeploymentProperties(789);
    assert previousProps != null : "There should be previous deployment metadata";
    assert previousProps.getDeploymentId() == 456
        : "bad previous deployment metadata"; // testAntBundleUpgrade used 456
    assert previousProps.getBundleName().equals(deploymentProps.getBundleName());
    assert previousProps
        .getBundleVersion()
        .equals("3.0"); // testAntBundleUpgrade deployed version 3.0
  }
 @AfterMethod(alwaysRun = true)
 public void cleanDestDir() {
   FileUtil.purge(this.destDir, true);
 }
 @AfterMethod(alwaysRun = true)
 public void cleanPluginDirs() {
   FileUtil.purge(this.tmpDir, true);
   FileUtil.purge(this.bundleFilesDir, true);
 }
  public void createCluster() {
    if (log.isDebugEnabled()) {
      log.debug(
          "Installing embedded "
              + deploymentOptions.getNumNodes()
              + " node cluster to "
              + deploymentOptions.getClusterDir());
    } else {
      log.info("Installing embedded cluster");
    }

    File clusterDir = new File(deploymentOptions.getClusterDir());
    File installedMarker = new File(clusterDir, ".installed");

    if (installedMarker.exists()) {
      log.info("It appears that the cluster already exists in " + clusterDir);
      log.info("Skipping cluster creation.");
      getStorageClusterConfiguration();
    }
    FileUtil.purge(clusterDir, false);

    String seeds = collectionToString(calculateLocalIPAddresses(deploymentOptions.getNumNodes()));

    this.nodes = new String[deploymentOptions.getNumNodes()];
    this.jmxPorts = new int[deploymentOptions.getNumNodes()];
    this.cqlPort = deploymentOptions.getCqlPort();

    boolean useRemoteJMX = this.nodes.length > 1;

    for (int i = 0; i < deploymentOptions.getNumNodes(); ++i) {
      File basedir = new File(deploymentOptions.getClusterDir(), "node" + i);
      String address = getLocalIPAddress(i + 1);

      DeploymentOptionsFactory factory = new DeploymentOptionsFactory();
      DeploymentOptions nodeOptions = factory.newDeploymentOptions();
      nodeOptions.setSeeds(seeds);
      nodeOptions.setJmxPort(deploymentOptions.getJmxPort() + i);
      nodeOptions.setBasedir(basedir.getAbsolutePath());
      nodeOptions.setListenAddress(address);
      nodeOptions.setRpcAddress(address);
      nodeOptions.setCommitLogDir(new File(basedir, "commit_log").getAbsolutePath());
      nodeOptions.setDataDir(new File(basedir, "data").getAbsolutePath());
      nodeOptions.setSavedCachesDir(new File(basedir, "saved_caches").getAbsolutePath());

      nodeOptions.merge(deploymentOptions);
      try {
        nodeOptions.load();
        Deployer deployer = new Deployer();
        deployer.setDeploymentOptions(nodeOptions);

        deployer.unzipDistro();
        deployer.applyConfigChanges();
        deployer.updateFilePerms();
        deployer.updateStorageAuthConf(calculateLocalIPAddresses(deploymentOptions.getNumNodes()));

        if (useRemoteJMX) {
          File confDir = new File(nodeOptions.getBasedir(), "conf");
          File cassandraJvmPropsFile = new File(confDir, "cassandra-jvm.properties");
          PropertiesFileUpdate propertiesUpdater =
              new PropertiesFileUpdate(cassandraJvmPropsFile.getAbsolutePath());
          Properties properties = propertiesUpdater.loadExistingProperties();
          String jmxOpts =
              "\"-Djava.rmi.server.hostname="
                  + nodeOptions.getRpcAddress()
                  + " -Dcom.sun.management.jmxremote.port="
                  + nodeOptions.getJmxPort()
                  + " -Dcom.sun.management.jmxremote.rmi.port="
                  + nodeOptions.getJmxPort()
                  + " -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false \"";

          properties.setProperty("JMX_OPTS", jmxOpts);

          propertiesUpdater.update(properties);
        }

        this.nodes[i] = address;
        this.jmxPorts[i] = deploymentOptions.getJmxPort() + i;

        installedNodeDirs.add(basedir);
      } catch (Exception e) {
        log.error("Failed to install node at " + basedir);
        throw new RuntimeException("Failed to install node at " + basedir, e);
      }
    }
    try {
      FileUtil.writeFile(new ByteArrayInputStream(new byte[] {0}), installedMarker);
    } catch (IOException e) {
      log.warn("Failed to write installed file marker to " + installedMarker, e);
    }
  }