@Override public void connectController(String host, int port) { if (host == null) { host = defaultControllerHost; } if (port < 0) { port = defaultControllerPort; } try { ModelControllerClient newClient = ModelControllerClient.Factory.create(host, port); if (this.client != null) { disconnectController(); } List<String> nodeTypes = Util.getNodeTypes(newClient, new DefaultOperationRequestAddress()); if (!nodeTypes.isEmpty()) { domainMode = nodeTypes.contains("server-group"); printLine( "Connected to " + (domainMode ? "domain controller at " : "standalone controller at ") + host + ":" + port); client = newClient; this.controllerHost = host; this.controllerPort = port; } else { printLine("The controller is not available at " + host + ":" + port); } } catch (UnknownHostException e) { printLine("Failed to resolve host '" + host + "': " + e.getLocalizedMessage()); } }
@Override public void addSecurityDomain(String name, Map<String, String> authenticationOptions) throws Exception { final ModelControllerClient client = ModelControllerClient.Factory.create(address, PORT); final List<ModelNode> updates = new ArrayList<ModelNode>(); ModelNode op = new ModelNode(); op.get(OP).set(ADD); op.get(OP_ADDR).add(SUBSYSTEM, "security"); op.get(OP_ADDR).add(SECURITY_DOMAIN, name); updates.add(op); op = new ModelNode(); op.get(OP).set(ADD); op.get(OP_ADDR).add(SUBSYSTEM, "security"); op.get(OP_ADDR).add(SECURITY_DOMAIN, name); op.get(OP_ADDR).add(AUTHENTICATION, CLASSIC); final ModelNode loginModule = op.get(LOGIN_MODULES).add(); loginModule.get(CODE).set("UsersRoles"); loginModule.get(FLAG).set("required"); op.get(OPERATION_HEADERS).get(ALLOW_RESOURCE_SERVICE_RESTART).set(true); updates.add(op); final ModelNode moduleOptions = loginModule.get(MODULE_OPTIONS); if (authenticationOptions != null) { for (final String k : authenticationOptions.keySet()) { moduleOptions.add(k, authenticationOptions.get(k)); } } applyUpdates(updates, client); }
@Override public void start() { if (STARTED.compareAndSet(false, true)) { try { final DomainCommandBuilder commandBuilder = DomainCommandBuilder.of(Environment.WILDFLY_HOME) .setBindAddressHint("management", Environment.HOSTNAME) .addHostControllerJavaOption("-Djboss.management.http.port=" + Environment.PORT); final Process process = Launcher.of(commandBuilder).setRedirectErrorStream(true).launch(); startConsoleConsumer(process); shutdownThread = ProcessHelper.addShutdownHook(process); client = DomainClient.Factory.create( ModelControllerClient.Factory.create(Environment.HOSTNAME, Environment.PORT)); currentProcess = process; final Map<ServerIdentity, ServerStatus> servers = new HashMap<>(); ServerHelper.waitForDomain(process, client, servers, Environment.TIMEOUT); this.servers.putAll(servers); } catch (Throwable t) { try { throw new RuntimeException("Failed to start server", t); } finally { STARTED.set(false); cleanUp(); } } } }
private ModelControllerClient createClient( String userName, boolean allowLocalAuth, JBossAsManagedConfiguration clientConfig) throws UnknownHostException { return ModelControllerClient.Factory.create( clientConfig.getHostControllerManagementProtocol(), clientConfig.getHostControllerManagementAddress(), clientConfig.getHostControllerManagementPort(), new RbacAdminCallbackHandler(userName), allowLocalAuth ? Collections.<String, String>emptyMap() : SASL_OPTIONS); }
public DeploymentManagerTarget(URI deployURI, String username, String password) { ROOT_LOGGER.debugf("new DeploymentManagerTarget: %s", deployURI); try { URIParser parser = new URIParser(deployURI); String serverHost = parser.getParameter("serverHost"); String serverPort = parser.getParameter("serverPort"); String host = serverHost != null ? serverHost : "127.0.0.1"; Integer port = serverPort != null ? Integer.parseInt(serverPort) : 9999; if (username != null && password != null) { this.modelControllerClient = ModelControllerClient.Factory.create( host, port, getCallbackHandler(username, password)); } else { this.modelControllerClient = ModelControllerClient.Factory.create(host, port); } this.deploymentManager = ServerDeploymentManager.Factory.create(modelControllerClient); this.deployURI = deployURI; } catch (UnknownHostException ex) { throw new IllegalArgumentException(MESSAGES.cannotConnectToManagementTarget(deployURI), ex); } }
@Override public void removeSecurityDomain(String name) throws Exception { final ModelControllerClient client = ModelControllerClient.Factory.create(address, PORT); final ModelNode op = new ModelNode(); op.get(OP).set(REMOVE); op.get(OP_ADDR).add(SUBSYSTEM, "security"); op.get(OP_ADDR).add(SECURITY_DOMAIN, name); // Don't rollback when the AS detects the war needs the module op.get(OPERATION_HEADERS, ROLLBACK_ON_RUNTIME_FAILURE).set(false); applyUpdate(op, client); }
@Test @RunAsClient public void testClient() throws Exception { ModelControllerClient client = ModelControllerClient.Factory.create( "localhost", 9990, new AuthCallbackHandler("bob", "tacos!")); ModelNode response = client.execute(Operations.createOperation("whoami")); assertThat(response.get("outcome").asString()).isEqualTo("success"); ModelNode result = response.get("result"); assertThat(result).isNotNull(); assertThat(result.isDefined()).isTrue(); ModelNode identity = result.get("identity"); assertThat(identity).isNotNull(); assertThat(identity.isDefined()).isTrue(); assertThat(identity.get("username").asString()).isEqualTo("bob"); // === response = client.execute( Operations.createOperation( "read-resource", PathAddress.pathAddress(PathElement.pathElement("deployment", "*")).toModelNode())); assertThat(response.get("outcome").asString()).isEqualTo("success"); result = response.get("result"); assertThat(result).isNotNull(); assertThat(result.isDefined()).isTrue(); assertThat(result.getType()).isEqualTo(ModelType.LIST); assertThat(result.asList()).hasSize(1); ModelNode myapp = result.get(0); assertThat(myapp).isNotNull(); assertThat(myapp.isDefined()).isTrue(); ModelNode myappResult = myapp.get("result"); assertThat(myappResult).isNotNull(); assertThat(myappResult.isDefined()).isTrue(); assertThat(myappResult.get("name").asString()).isEqualTo("myapp.jar"); }
private ModelControllerClient getNativeModelControllerClient() { ModelControllerClient client = null; try { client = ModelControllerClient.Factory.create( "remote", InetAddress.getByName(TestSuiteEnvironment.getServerAddress()), MANAGEMENT_NATIVE_PORT, new CallbackHandler()); } catch (UnknownHostException e) { throw new RuntimeException(e); } return client; }
@Test public void testXmlConfigDemo() throws Exception { ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999); try { ModelNode request = new ModelNode(); request.get("operation").set("read-config-as-xml"); request.get("address").setEmptyList(); ModelNode r = client.execute(OperationBuilder.Factory.create(request).build()); Assert.assertEquals(SUCCESS, r.require(OUTCOME).asString()); } finally { StreamUtils.safeClose(client); } }
private void executeReloadOperation(Boolean restartServers, Boolean adminOnly) throws Exception { ModelNode operation = new ModelNode(); operation.get(OP).set("reload"); operation .get(OP_ADDR) .set(PathAddress.pathAddress(PathElement.pathElement(HOST, "master")).toModelNode()); if (restartServers != null) { operation.get(ModelDescriptionConstants.RESTART_SERVERS).set(restartServers); } if (adminOnly != null) { operation.get(ModelDescriptionConstants.ADMIN_ONLY).set(adminOnly); } final ModelControllerClient client = ModelControllerClient.Factory.create( DomainTestSupport.masterAddress, HC_PORT, getCallbackHandler()); try { Assert.assertEquals(SUCCESS, client.execute(operation).get(OUTCOME).asString()); } catch (IOException canHappenWhenShuttingDownController) { } }
private boolean lookupServerInModel(String host, String server) throws Exception { final ModelNode operation = new ModelNode(); operation.get(OP).set(READ_RESOURCE_OPERATION); operation.get(OP_ADDR).set(getHostControllerServerAddress(host, server)); final ModelControllerClient client = ModelControllerClient.Factory.create("localhost", HC_PORT); try { final ModelNode result = client.execute(operation); if (result.get(OUTCOME).asString().equals(SUCCESS)) { final ModelNode model = result.require(RESULT); if (model.hasDefined(NAME) && model.get(NAME).asString().equals(server)) { return true; } } } catch (IOException e) { } finally { StreamUtils.safeClose(client); } return false; }
private static void withManagementClient(String host, int port, ManagementRunnable runnable) throws ManagementClientException { InetAddress addr; try { addr = InetAddress.getByName(host); } catch (UnknownHostException ex) { throw new ManagementClientException(String.format("Failed to resolve host '%s'.", host), ex); } ModelControllerClient client = ModelControllerClient.Factory.create("http-remoting", addr, port); try { runnable.run(client); } catch (Exception ex) { throw new ManagementClientException("", ex); } finally { try { client.close(); } catch (IOException e) { // Ignore } } }
public DomainClientImpl(InetAddress address, int port, CallbackHandler handler) { this.delegate = ModelControllerClient.Factory.create(address, port, handler); }
public DomainClientImpl(InetAddress address, int port) { this.delegate = ModelControllerClient.Factory.create(address, port); }
protected static ModelControllerClient createClient2() throws UnknownHostException { return ModelControllerClient.Factory.create( InetAddress.getByName(TestSuiteEnvironment.getServerAddress()), TestSuiteEnvironment.getServerPort() + 100, Authentication.getCallbackHandler()); }
@Test public void testFilesystemDeployment() throws Exception { final JavaArchive archive = ShrinkWrapUtils.createJavaArchive( "servermodule/test-deployment.sar", Simple.class.getPackage()); final File dir = new File("target/archives"); dir.mkdirs(); final File file = new File(dir, "test-deployment.sar"); archive.as(ZipExporter.class).exportZip(file, true); final File deployDir = new File("target", "deployments"); cleanFile(deployDir); deployDir.mkdirs(); Assert.assertTrue(deployDir.exists()); ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999); ModelNode add = new ModelNode(); add.get(OP).set(ADD); ModelNode addr = new ModelNode(); addr.add("subsystem", "deployment-scanner"); addr.add("scanner", "test"); add.get(OP_ADDR).set(addr); add.get("path").set(deployDir.getAbsolutePath()); add.get("scan-enabled").set(true); add.get("scan-interval").set(1000); ModelNode result = client.execute(add); Assert.assertEquals( ModelDescriptionConstants.SUCCESS, result.require(ModelDescriptionConstants.OUTCOME).asString()); try { final File target = new File(deployDir, "test-deployment.sar"); final File deployed = new File(deployDir, "test-deployment.sar.isdeployed"); Assert.assertFalse(target.exists()); testDeployments( new DeploymentExecutor() { @Override public void initialDeploy() throws IOException { // Copy file to deploy directory final InputStream in = new BufferedInputStream(new FileInputStream(file)); try { final OutputStream out = new BufferedOutputStream(new FileOutputStream(target)); try { int i = in.read(); while (i != -1) { out.write(i); i = in.read(); } } finally { StreamUtils.safeClose(out); } } finally { StreamUtils.safeClose(in); } // Create the .dodeploy file final File dodeploy = new File(deployDir, "test-deployment.sar.dodeploy"); final OutputStream out = new BufferedOutputStream(new FileOutputStream(dodeploy)); try { out.write("test-deployment.sar".getBytes()); } finally { StreamUtils.safeClose(out); } Assert.assertTrue(dodeploy.exists()); } @Override public void fullReplace() throws IOException { // The test is going to call this as soon as the deployment sends a notification // but often before the scanner has completed the process and deleted the // .dodpeloy put down by initialDeploy(). So pause a bit to let that complete // so we don't end up having our own file deleted final File dodeploy = new File(deployDir, "test-deployment.sar.dodeploy"); for (int i = 0; i < 100; i++) { if (!dodeploy.exists()) { break; } // Wait for the last action to complete :( try { Thread.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } // Copy file to deploy directory again initialDeploy(); } @Override public void undeploy() { final File dodeploy = new File(deployDir, "test-deployment.sar.dodeploy"); for (int i = 0; i < 100; i++) { if (!dodeploy.exists() && deployed.exists()) { break; } // Wait for the last action to complete :( try { Thread.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } // Delete file from deploy directory deployed.delete(); } }); } finally { try { client.execute(result.get(ModelDescriptionConstants.COMPENSATING_OPERATION)); } catch (Exception e) { client.close(); } } }