/**
 * Launches a clustered and load-balanced set of web servers. Demonstrates syntax, so many of the
 * options used here are the defaults. (So the class could be much simpler, as in
 * WebClusterExampleAlt.)
 *
 * <p>Requires: -Xmx512m -Xms128m -XX:MaxPermSize=256m and brooklyn-all jar, and this jar or classes
 * dir, on classpath.
 */
public class WebClusterExample extends ApplicationBuilder {
  public static final Logger LOG = LoggerFactory.getLogger(WebClusterExample.class);

  static BrooklynProperties config = BrooklynProperties.Factory.newDefault();

  public static final String DEFAULT_LOCATION = "localhost";

  public static final String WAR_PATH = "classpath://hello-world-webapp.war";

  private NginxController nginxController;
  private ControlledDynamicWebAppCluster web;

  protected void doBuild() {
    nginxController =
        createChild(
            BasicEntitySpec.newInstance(NginxController.class)
                // .configure("domain", "webclusterexample.brooklyn.local")
                .configure("port", "8000+"));

    web =
        createChild(
            ControlledDynamicWebAppCluster.Spec.newInstance()
                .displayName("WebApp cluster")
                .controller(nginxController)
                .initialSize(1)
                .memberSpec(
                    BasicEntitySpec.newInstance(JBoss7Server.class)
                        .configure("httpPort", "8080+")
                        .configure("war", WAR_PATH)));

    web.getCluster()
        .addPolicy(
            AutoScalerPolicy.builder()
                .metric(DynamicWebAppCluster.AVERAGE_REQUESTS_PER_SECOND)
                .sizeRange(1, 5)
                .metricRange(10, 100)
                .build());
  }

  public static void main(String[] argv) {
    List<String> args = Lists.newArrayList(argv);
    String port = CommandLineUtil.getCommandLineOption(args, "--port", "8081+");
    String location = CommandLineUtil.getCommandLineOption(args, "--location", DEFAULT_LOCATION);

    // TODO Want to parse, to handle multiple locations
    BrooklynLauncherCli launcher =
        BrooklynLauncherCli.newInstance()
            .application(new WebClusterExample().appDisplayName("Brooklyn WebApp Cluster example"))
            .webconsolePort(port)
            .location(location)
            .start();

    Entities.dumpInfo(launcher.getApplications());
  }
}
예제 #2
0
  /**
   * Usual constructor, takes a set of properties; also (experimental) permits defining a
   * brooklynProperties source
   */
  public AbstractApplication(Map properties) {
    super(properties);

    if (properties.containsKey("mgmt")) {
      mgmt = (ManagementContext) properties.remove("mgmt");
    }

    // TODO decide whether this is the best way to inject properties like this
    Object propsSource = null;
    if (properties.containsKey("brooklynProperties")) {
      propsSource = properties.remove("brooklynProperties");
    } else if (properties.containsKey("brooklyn.properties")) {
      propsSource = properties.remove("brooklyn.properties");
    }
    if (propsSource instanceof String) {
      Properties p = new Properties();
      try {
        p.load(new ResourceUtils(this).getResourceFromUrl((String) propsSource));
      } catch (IOException e) {
        throw new IllegalArgumentException(
            "Invalid brooklyn properties source " + propsSource + ": " + e, e);
      }
      propsSource = p;
    }
    if (propsSource instanceof BrooklynProperties) {
      brooklynProperties = (BrooklynProperties) propsSource;
    } else if (propsSource instanceof Map) {
      brooklynProperties = BrooklynProperties.Factory.newEmpty().addFromMap((Map) propsSource);
    } else {
      if (propsSource != null)
        throw new IllegalArgumentException("Invalid brooklyn properties source " + propsSource);
      brooklynProperties = BrooklynProperties.Factory.newDefault();
    }

    setAttribute(SERVICE_UP, false);
    setAttribute(Attributes.SERVICE_STATE, Lifecycle.CREATED);
  }
 private static BrooklynProperties emptyIfNull(BrooklynProperties bp) {
   if (bp != null) return bp;
   return BrooklynProperties.Factory.newEmpty();
 }
 public Builder useDefaultProperties() {
   properties = BrooklynProperties.Factory.newDefault();
   return this;
 }
  /**
   * [sam] Other tests rely on brooklyn.properties not containing security properties so .. I think
   * the best way to test this is to set a security provider, then reload properties and check no
   * authentication is required.
   *
   * <p>[aled] Changing this test so doesn't rely on brooklyn.properties having no security provider
   * (that can lead to failures locally when running just this test). Asserts
   */
  @Test(groups = "Integration")
  public void testSecurityProviderUpdatesWhenPropertiesReloaded() {
    BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
    brooklynProperties.put("brooklyn.webconsole.security.users", "admin");
    brooklynProperties.put("brooklyn.webconsole.security.user.admin.password", "mypassword");
    UsernamePasswordCredentials defaultCredential =
        new UsernamePasswordCredentials("admin", "mypassword");

    ManagementContext mgmt = new LocalManagementContext(brooklynProperties);

    try {
      Server server =
          useServerForTest(
              BrooklynRestApiLauncher.launcher()
                  .managementContext(mgmt)
                  .withoutJsgui()
                  .securityProvider(TestSecurityProvider.class)
                  .start());
      String baseUri = getBaseUri(server);

      HttpToolResponse response;
      final URI uri = URI.create(getBaseUri() + "/v1/server/properties/reload");
      final Map<String, String> args = Collections.emptyMap();

      // Unauthorised when no credentials, and when default credentials.
      response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).build(), uri, args, args);
      assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);

      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(defaultCredential).build(),
              uri,
              args,
              args);
      assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);

      // Accepts TestSecurityProvider credentials, and we reload.
      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(TestSecurityProvider.CREDENTIAL).build(),
              uri,
              args,
              args);
      HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());

      // Has no gone back to credentials from brooklynProperties; TestSecurityProvider credentials
      // no longer work
      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(defaultCredential).build(),
              uri,
              args,
              args);
      HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());

      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(TestSecurityProvider.CREDENTIAL).build(),
              uri,
              args,
              args);
      assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);

    } finally {
      ((ManagementContextInternal) mgmt).terminate();
    }
  }
 private static JcloudsLocation resolve(String spec) {
   BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newDefault();
   return (JcloudsLocation)
       new LocationRegistry(brooklynProperties).resolve(JcloudsResolver.JCLOUDS + ":" + spec);
 }