@Override
  public void undeploy(String targetName) {
    checkNotNull(targetName, "targetName");
    targetName = FILENAME_TO_WEB_CONTEXT_MAPPER.convertDeploymentTargetNameToContext(targetName);

    // set it up so future nodes get the right wars
    if (!removeFromWarsByContext(this, targetName)) {
      DynamicTasks.submit(
          Tasks.warning(
              "Context "
                  + targetName
                  + " not known at "
                  + this
                  + "; attempting to undeploy regardless",
              null),
          this);
    }

    log.debug(
        "Undeploying "
            + targetName
            + " across cluster "
            + this
            + "; WARs now "
            + getConfig(WARS_BY_CONTEXT));

    Iterable<CanDeployAndUndeploy> targets =
        Iterables.filter(getChildren(), CanDeployAndUndeploy.class);
    TaskBuilder<Void> tb =
        Tasks.<Void>builder()
            .parallel(true)
            .name(
                "Undeploy "
                    + targetName
                    + " across cluster (size "
                    + Iterables.size(targets)
                    + ")");
    for (Entity target : targets) {
      tb.add(
          whenServiceUp(
              target,
              Effectors.invocation(target, UNDEPLOY, MutableMap.of("targetName", targetName)),
              "Undeploy " + targetName + " at " + target + " when ready"));
    }
    DynamicTasks.queueIfPossible(tb.build()).orSubmitAsync(this).asTask().getUnchecked();

    // Update attribute
    Set<String> deployedWars = MutableSet.copyOf(getAttribute(DEPLOYED_WARS));
    deployedWars.remove(
        FILENAME_TO_WEB_CONTEXT_MAPPER.convertDeploymentTargetNameToContext(targetName));
    setAttribute(DEPLOYED_WARS, deployedWars);
  }
  @Override
  public void redeployAll() {
    Map<String, String> wars = MutableMap.copyOf(getConfig(WARS_BY_CONTEXT));
    String redeployPrefix = "Redeploy all WARs (count " + wars.size() + ")";

    log.debug("Redeplying all WARs across cluster " + this + ": " + getConfig(WARS_BY_CONTEXT));

    Iterable<CanDeployAndUndeploy> targetEntities =
        Iterables.filter(getChildren(), CanDeployAndUndeploy.class);
    TaskBuilder<Void> tb =
        Tasks.<Void>builder()
            .parallel(true)
            .name(redeployPrefix + " across cluster (size " + Iterables.size(targetEntities) + ")");
    for (Entity targetEntity : targetEntities) {
      TaskBuilder<Void> redeployAllToTarget =
          Tasks.<Void>builder()
              .name(redeployPrefix + " at " + targetEntity + " (after ready check)");
      for (String warContextPath : wars.keySet()) {
        redeployAllToTarget.add(
            Effectors.invocation(
                targetEntity,
                DEPLOY,
                MutableMap.of("url", wars.get(warContextPath), "targetName", warContextPath)));
      }
      tb.add(
          whenServiceUp(
              targetEntity,
              redeployAllToTarget.build(),
              redeployPrefix + " at " + targetEntity + " when ready"));
    }
    DynamicTasks.queueIfPossible(tb.build()).orSubmitAsync(this).asTask().getUnchecked();
  }
    protected void startWithKnifeAsync() {
      Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_RUN_LIST);
      Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_ATTRIBUTES);

      DynamicTasks.queue(
          ChefServerTasks.knifeConvergeRunList("postgresql::server")
              .knifeAddAttributes(
                  Jsonya.at("postgresql", "config")
                      .add("port", entity().getPostgreSqlPort(), "listen_addresses", "*")
                      .getRootMap())
              .knifeAddAttributes(
                  Jsonya.at("postgresql", "pg_hba")
                      .list()
                      .map()
                      .add(
                          "type",
                          "host",
                          "db",
                          "all",
                          "user",
                          "all",
                          "addr",
                          "0.0.0.0/0",
                          "method",
                          "md5")
                      .getRootMap())
          // no other arguments currenty supported; chef will pick a password for us
          );
    }
 /**
  * attempts to resolve hostnameTarget from origin
  *
  * @return null if it definitively can't be resolved, best-effort IP address if possible, or blank
  *     if we could not run ssh or make sense of the output
  */
 public static String getResolvedAddress(
     Entity entity, SshMachineLocation origin, String hostnameTarget) {
   ProcessTaskWrapper<Integer> task =
       SshTasks.newSshExecTaskFactory(origin, "ping -c 1 -t 1 " + hostnameTarget)
           .summary("checking resolution of " + hostnameTarget)
           .allowingNonZeroExitCode()
           .newTask();
   DynamicTasks.queueIfPossible(task).orSubmitAndBlock(entity).asTask().blockUntilEnded();
   if (task.asTask().isError()) {
     log.warn(
         "ping could not be run, at "
             + entity
             + " / "
             + origin
             + ": "
             + Tasks.getError(task.asTask()));
     return "";
   }
   if (task.getExitCode() == null || task.getExitCode() != 0) {
     if (task.getExitCode() != null && task.getExitCode() < 10) {
       // small number means ping failed to resolve or ping the hostname
       log.debug(
           "not able to resolve "
               + hostnameTarget
               + " from "
               + origin
               + " for "
               + entity
               + " because exit code was "
               + task.getExitCode());
       return null;
     }
     // large number means ping probably did not run
     log.warn(
         "ping not run as expected, at "
             + entity
             + " / "
             + origin
             + " (code "
             + task.getExitCode()
             + "):\n"
             + task.getStdout().trim()
             + " --- "
             + task.getStderr().trim());
     return "";
   }
   String out = task.getStdout();
   try {
     String line1 = Strings.getFirstLine(out);
     String ip = Strings.getFragmentBetween(line1, "(", ")");
     if (Strings.isNonBlank(ip)) return ip;
   } catch (Exception e) {
     Exceptions.propagateIfFatal(e);
     /* ignore non-parseable output */
   }
   if (out.contains("127.0.0.1")) return "127.0.0.1";
   return "";
 }
 public String call(ConfigBag parameters) {
   return DynamicTasks.queue(
           SshEffectorTasks.ssh(
                   BashCommands.pipeTextTo(
                       parameters.get(SCRIPT),
                       BashCommands.sudoAsUser("postgres", "psql --file -")))
               .requiringExitCodeZero())
       .getStdout();
 }
  @Override
  public void deploy(String url, String targetName) {
    checkNotNull(url, "url");
    checkNotNull(targetName, "targetName");
    targetName = FILENAME_TO_WEB_CONTEXT_MAPPER.convertDeploymentTargetNameToContext(targetName);

    // set it up so future nodes get the right wars
    addToWarsByContext(this, url, targetName);

    log.debug(
        "Deploying "
            + targetName
            + "->"
            + url
            + " across cluster "
            + this
            + "; WARs now "
            + getConfig(WARS_BY_CONTEXT));

    Iterable<CanDeployAndUndeploy> targets =
        Iterables.filter(getChildren(), CanDeployAndUndeploy.class);
    TaskBuilder<Void> tb =
        Tasks.<Void>builder()
            .parallel(true)
            .name("Deploy " + targetName + " to cluster (size " + Iterables.size(targets) + ")");
    for (Entity target : targets) {
      tb.add(
          whenServiceUp(
              target,
              Effectors.invocation(
                  target, DEPLOY, MutableMap.of("url", url, "targetName", targetName)),
              "Deploy " + targetName + " to " + target + " when ready"));
    }
    DynamicTasks.queueIfPossible(tb.build()).orSubmitAsync(this).asTask().getUnchecked();

    // Update attribute
    // TODO support for atomic sensor update (should be part of standard tooling; NB there is some
    // work towards this, according to @aledsage)
    Set<String> deployedWars = MutableSet.copyOf(getAttribute(DEPLOYED_WARS));
    deployedWars.add(targetName);
    setAttribute(DEPLOYED_WARS, deployedWars);
  }
Пример #7
0
  @Override
  public void install() {
    // will fail later if can't sudo (if sudo is required)
    DynamicTasks.queueIfPossible(SshTasks.dontRequireTtyForSudo(getMachine(), false))
        .orSubmitAndBlock();

    DownloadResolver nginxResolver = mgmt().getEntityDownloadsManager().newDownloader(this);
    List<String> nginxUrls = nginxResolver.getTargets();
    String nginxSaveAs = nginxResolver.getFilename();
    setExpandedInstallDir(
        getInstallDir()
            + "/"
            + nginxResolver.getUnpackedDirectoryName(format("nginx-%s", getVersion())));

    boolean sticky = ((NginxController) entity).isSticky();
    boolean isMac = getMachine().getOsDetails().isMac();

    MutableMap<String, String> installGccPackageFlags =
        MutableMap.of(
            "onlyifmissing", "gcc",
            "yum", "gcc",
            "apt", "gcc",
            "zypper", "gcc",
            "port", null);
    MutableMap<String, String> installMakePackageFlags =
        MutableMap.of(
            "onlyifmissing", "make",
            "yum", "make",
            "apt", "make",
            "zypper", "make",
            "port", null);
    MutableMap<String, String> installPackageFlags =
        MutableMap.of(
            "yum", "openssl-devel pcre-devel",
            "apt", "libssl-dev zlib1g-dev libpcre3-dev",
            "zypper", "libopenssl-devel pcre-devel",
            "port", null);

    String stickyModuleVersion = entity.getConfig(NginxController.STICKY_VERSION);
    DownloadResolver stickyModuleResolver =
        mgmt()
            .getEntityDownloadsManager()
            .newDownloader(
                this, "stickymodule", ImmutableMap.of("addonversion", stickyModuleVersion));
    List<String> stickyModuleUrls = stickyModuleResolver.getTargets();
    String stickyModuleSaveAs = stickyModuleResolver.getFilename();
    String stickyModuleExpandedInstallDir =
        String.format(
            "%s/src/%s",
            getExpandedInstallDir(),
            stickyModuleResolver.getUnpackedDirectoryName(
                "nginx-sticky-module-" + stickyModuleVersion));

    List<String> cmds = Lists.newArrayList();

    cmds.add(BashCommands.INSTALL_TAR);
    cmds.add(
        BashCommands.alternatives(
            BashCommands.ifExecutableElse0(
                "apt-get", BashCommands.installPackage("build-essential")),
            BashCommands.ifExecutableElse0(
                "yum",
                BashCommands.sudo("yum -y --nogpgcheck groupinstall \"Development Tools\""))));
    cmds.add(BashCommands.installPackage(installGccPackageFlags, "nginx-prerequisites-gcc"));
    cmds.add(BashCommands.installPackage(installMakePackageFlags, "nginx-prerequisites-make"));
    cmds.add(BashCommands.installPackage(installPackageFlags, "nginx-prerequisites"));
    cmds.addAll(BashCommands.commandsToDownloadUrlsAs(nginxUrls, nginxSaveAs));

    String pcreExpandedInstallDirname = "";
    if (isMac) {
      String pcreVersion = entity.getConfig(NginxController.PCRE_VERSION);
      DownloadResolver pcreResolver =
          mgmt()
              .getEntityDownloadsManager()
              .newDownloader(this, "pcre", ImmutableMap.of("addonversion", pcreVersion));
      List<String> pcreUrls = pcreResolver.getTargets();
      String pcreSaveAs = pcreResolver.getFilename();
      pcreExpandedInstallDirname = pcreResolver.getUnpackedDirectoryName("pcre-" + pcreVersion);

      // Install PCRE
      cmds.addAll(BashCommands.commandsToDownloadUrlsAs(pcreUrls, pcreSaveAs));
      cmds.add(format("mkdir -p %s/pcre-dist", getInstallDir()));
      cmds.add(format("tar xvzf %s", pcreSaveAs));
      cmds.add(format("cd %s", pcreExpandedInstallDirname));
      cmds.add(format("./configure --prefix=%s/pcre-dist", getInstallDir()));
      cmds.add("make");
      cmds.add("make install");
      cmds.add("cd ..");
    }

    cmds.add(format("tar xvzf %s", nginxSaveAs));
    cmds.add(format("cd %s", getExpandedInstallDir()));

    if (sticky) {
      cmds.add("cd src");
      cmds.addAll(BashCommands.commandsToDownloadUrlsAs(stickyModuleUrls, stickyModuleSaveAs));
      cmds.add(format("tar xvzf %s", stickyModuleSaveAs));
      cmds.add("cd ..");
    }

    // Note that for OS X, not including space after "-L" because broken in 10.6.8 (but fixed in
    // 10.7.x)
    //      see http://trac.nginx.org/nginx/ticket/227
    String withLdOpt = entity.getConfig(NginxController.WITH_LD_OPT);
    if (isMac)
      withLdOpt =
          format("-L%s/pcre-dist/lib", getInstallDir())
              + (Strings.isBlank(withLdOpt) ? "" : " " + withLdOpt);
    String withCcOpt = entity.getConfig(NginxController.WITH_CC_OPT);

    StringBuilder configureCommand =
        new StringBuilder("./configure")
            .append(format(" --prefix=%s/dist", getExpandedInstallDir()))
            .append(" --with-http_ssl_module")
            .append(sticky ? format(" --add-module=%s ", stickyModuleExpandedInstallDir) : "")
            .append(!Strings.isBlank(withLdOpt) ? format(" --with-ld-opt=\"%s\"", withLdOpt) : "")
            .append(!Strings.isBlank(withCcOpt) ? format(" --with-cc-opt=\"%s\"", withCcOpt) : "");
    if (isMac) {
      configureCommand
          .append(" --with-pcre=")
          .append(getInstallDir())
          .append("/")
          .append(pcreExpandedInstallDirname);
    }

    cmds.addAll(ImmutableList.of("mkdir -p dist", configureCommand.toString(), "make install"));

    ScriptHelper script =
        newScript(INSTALLING)
            .body
            .append(cmds)
            .header
            .prepend("set -x")
            .gatherOutput()
            .failOnNonZeroResultCode(false);

    int result = script.execute();

    if (result != 0) {
      String notes =
          "likely an error building nginx. consult the brooklyn log ssh output for further details.\n"
              + "note that this Brooklyn nginx driver compiles nginx from source. "
              + "it attempts to install common prerequisites but this does not always succeed.\n";
      OsDetails os = getMachine().getOsDetails();
      if (os.isMac()) {
        notes +=
            "deploying to Mac OS X, you will require Xcode and Xcode command-line tools, and on "
                + "some versions the pcre library (e.g. using macports, sudo port install pcre).\n";
      }
      if (os.isWindows()) {
        notes +=
            "this nginx driver is not designed for windows, unless cygwin is installed, and you are patient.\n";
      }
      if (getEntity().getApplication().getClass().getCanonicalName().startsWith("brooklyn.demo.")) {
        // this is maybe naughty ... but since we use nginx in the first demo example,
        // and since it's actually pretty complicated, let's give a little extra hand-holding
        notes +=
            "if debugging this is all a bit much and you just want to run a demo, "
                + "you have two fairly friendly options.\n"
                + "1. you can use a well known cloud, like AWS or Rackspace, where this should run "
                + "in a tried-and-tested Ubuntu or CentOS environment, without any problems "
                + "(and if it does let us know and we'll fix it!).\n"
                + "2. or you can just use the demo without nginx, instead access the appserver instances directly.\n";
      }

      if (!script.getResultStderr().isEmpty()) {
        notes += "\n" + "STDERR\n" + script.getResultStderr() + "\n";
        Streams.logStreamTail(
            log,
            "STDERR of problem in " + Tasks.current(),
            Streams.byteArrayOfString(script.getResultStderr()),
            1024);
      }
      if (!script.getResultStdout().isEmpty()) {
        notes += "\n" + "STDOUT\n" + script.getResultStdout() + "\n";
        Streams.logStreamTail(
            log,
            "STDOUT of problem in " + Tasks.current(),
            Streams.byteArrayOfString(script.getResultStdout()),
            1024);
      }

      Tasks.setExtraStatusDetails(notes.trim());

      throw new IllegalStateException(
          "Installation of nginx failed (shell returned non-zero result " + result + ")");
    }
  }