Skip to content

Commit

Permalink
Add UTF_8 encoding while formulating json
Browse files Browse the repository at this point in the history
Summary: Adds UTF_8 encoding while formulating json

Test Plan: Manually tested the universe creation post creating the YBA installer with the generated build

Reviewers: vbansal, vkumar

Reviewed By: vkumar

Differential Revision: https://phorge.dev.yugabyte.com/D42240
  • Loading branch information
Vars-07 committed Mar 4, 2025
1 parent a011f3b commit 378f9ba
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,40 @@
import com.yugabyte.yw.commissioner.BaseTaskDependencies;
import com.yugabyte.yw.commissioner.tasks.params.NodeTaskParams;
import com.yugabyte.yw.common.NodeAgentManager;
import com.yugabyte.yw.common.NodeAgentManager.InstallerFiles;
import com.yugabyte.yw.common.NodeUniverseManager;
import com.yugabyte.yw.common.ShellProcessContext;
import com.yugabyte.yw.common.config.RuntimeConfGetter;
import com.yugabyte.yw.common.gflags.GFlagsUtil;
import com.yugabyte.yw.models.NodeAgent;
import com.yugabyte.yw.models.NodeAgent.ArchType;
import com.yugabyte.yw.models.NodeAgent.OSType;
import com.yugabyte.yw.models.NodeAgent.State;
import com.yugabyte.yw.models.Universe;
import com.yugabyte.yw.models.helpers.NodeDetails;
import com.yugabyte.yw.models.helpers.YBAError;
import com.yugabyte.yw.models.helpers.YBAError.Code;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class InstallNodeAgent extends AbstractTaskBase {
public static final int DEFAULT_NODE_AGENT_PORT = 9070;

private final NodeUniverseManager nodeUniverseManager;
private final NodeAgentManager nodeAgentManager;
private final RuntimeConfGetter confGetter;
private ShellProcessContext shellContext =
ShellProcessContext.builder().logCmdOutput(true).build();

@Inject
protected InstallNodeAgent(
BaseTaskDependencies baseTaskDependencies,
NodeUniverseManager nodeUniverseManager,
NodeAgentManager nodeAgentManager,
RuntimeConfGetter confGetter) {
NodeAgentManager nodeAgentManager) {
super(baseTaskDependencies);
this.nodeUniverseManager = nodeUniverseManager;
this.nodeAgentManager = nodeAgentManager;
this.confGetter = confGetter;
}

public static class Params extends NodeTaskParams {
Expand Down Expand Up @@ -80,33 +71,6 @@ private SetupYNP.Params getSetupYNPParams(InstallNodeAgent.Params taskParams) {
return params;
}

private NodeAgent createNodeAgent(Universe universe, NodeDetails node) {
String output =
nodeUniverseManager
.runCommand(node, universe, Arrays.asList("uname", "-sm"), shellContext)
.processErrors()
.extractRunCommandOutput();
if (StringUtils.isBlank(output)) {
throw new RuntimeException("Unknown OS and Arch output: " + output);
}
// Output is like Linux x86_64.
String[] parts = output.split("\\s+", 2);
if (parts.length != 2) {
throw new RuntimeException("Unknown OS and Arch output: " + output);
}
NodeAgent nodeAgent = new NodeAgent();
nodeAgent.setIp(node.cloudInfo.private_ip);
nodeAgent.setName(node.nodeName);
nodeAgent.setPort(taskParams().nodeAgentPort);
nodeAgent.setCustomerUuid(taskParams().customerUuid);
nodeAgent.setOsType(OSType.parse(parts[0].trim()));
nodeAgent.setArchType(ArchType.parse(parts[1].trim()));
nodeAgent.setVersion(nodeAgentManager.getSoftwareVersion());
nodeAgent.setHome(
Paths.get(taskParams().nodeAgentInstallDir, NodeAgent.NODE_AGENT_DIR).toString());
return nodeAgentManager.create(nodeAgent, false);
}

boolean doesNodeAgentDirectoryExists(
NodeDetails node, Universe universe, ShellProcessContext shellContext, String nodeAgentHome) {
StringBuilder sb = new StringBuilder();
Expand All @@ -127,13 +91,11 @@ public NodeAgent install() {
Universe universe = Universe.getOrBadRequest(taskParams().getUniverseUUID());
NodeDetails node = universe.getNodeOrBadRequest(taskParams().nodeName);
NodeAgent nodeAgent = null;
InstallerFiles installerFiles = null;
Optional<NodeAgent> optional = NodeAgent.maybeGetByIp(node.cloudInfo.private_ip);
if (taskParams().sshUser != null) {
shellContext = shellContext.toBuilder().sshUser(taskParams().sshUser).build();
}
String customTmpDirectory = GFlagsUtil.getCustomTmpDirectory(node, universe);
Path ynpStagingDir = Paths.get(customTmpDirectory, "ynp");

boolean shouldSetUp = !optional.isPresent();
if (optional.isPresent()) {
Expand Down Expand Up @@ -161,7 +123,6 @@ public NodeAgent install() {
}

StringBuilder sb = new StringBuilder();
Path stagingDir = null, nodeAgentSourcePath = null;
List<String> command;

Path nodeAgentHomePath = Paths.get(nodeAgent.getHome());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.yugabyte.yw.models.helpers.NodeDetails;
import com.yugabyte.yw.models.helpers.YBAError;
import com.yugabyte.yw.models.helpers.YBAError.Code;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -75,79 +76,100 @@ public void getProvisionArguments(
Provider provider,
String outputFilePath,
Path nodeAgentHome) {
ObjectNode rootNode = mapper.createObjectNode();
ObjectNode ynpNode = mapper.createObjectNode();
ynpNode.put("node_ip", node.cloudInfo.private_ip);
ynpNode.put("is_install_node_agent", false);
ynpNode.put("yb_user_id", "1994");
ynpNode.put("is_airgap", provider.getDetails().airGapInstall);
ynpNode.put(
"tmp_directory", confGetter.getConfForScope(provider, ProviderConfKeys.remoteTmpDirectory));
rootNode.set("ynp", ynpNode);

ObjectNode extraNode = mapper.createObjectNode();
extraNode.put("cloud_type", node.cloudInfo.cloud);
extraNode.put("is_cloud", true);
if (taskParams().deviceInfo.mountPoints != null) {
extraNode.put("mount_paths", taskParams().deviceInfo.mountPoints);
} else {
int numVolumes =
universe.getCluster(node.placementUuid).userIntent.getDeviceInfoForNode(node).numVolumes;
StringBuilder volumePaths = new StringBuilder();
for (int i = 0; i < numVolumes; i++) {
if (i > 0) {
volumePaths.append(" ");

ObjectMapper mapper = new ObjectMapper();

try {
ObjectNode rootNode = mapper.createObjectNode();

// "ynp" JSON Object
ObjectNode ynpNode = mapper.createObjectNode();
ynpNode.put("node_ip", node.cloudInfo.private_ip);
ynpNode.put("is_install_node_agent", false);
ynpNode.put("yb_user_id", "1994");
ynpNode.put("is_airgap", provider.getDetails().airGapInstall);
ynpNode.put(
"tmp_directory",
confGetter.getConfForScope(provider, ProviderConfKeys.remoteTmpDirectory));
rootNode.set("ynp", ynpNode);

// "extra" JSON Object
ObjectNode extraNode = mapper.createObjectNode();
extraNode.put("cloud_type", node.cloudInfo.cloud);
extraNode.put("is_cloud", true);

// Set mount paths
if (taskParams().deviceInfo.mountPoints != null) {
extraNode.put("mount_paths", taskParams().deviceInfo.mountPoints);
} else {
int numVolumes =
universe
.getCluster(node.placementUuid)
.userIntent
.getDeviceInfoForNode(node)
.numVolumes;
StringBuilder volumePaths = new StringBuilder();
for (int i = 0; i < numVolumes; i++) {
if (i > 0) {
volumePaths.append(" ");
}
volumePaths.append("/mnt/d").append(i);
}
volumePaths.append("/mnt/d").append(i);
extraNode.put("mount_paths", volumePaths.toString());
}
extraNode.put("mount_paths", volumePaths.toString());
}
if (node.cloudInfo.cloud.equals(Common.CloudType.azu.toString())
&& node.cloudInfo.lun_indexes.length > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < node.cloudInfo.lun_indexes.length; i++) {
sb.append(node.cloudInfo.lun_indexes[i]);
if (i < node.cloudInfo.lun_indexes.length - 1) {
sb.append(" ");

// Azure-specific disk lun indexes
if (node.cloudInfo.cloud.equals(Common.CloudType.azu.toString())
&& node.cloudInfo.lun_indexes.length > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < node.cloudInfo.lun_indexes.length; i++) {
sb.append(node.cloudInfo.lun_indexes[i]);
if (i < node.cloudInfo.lun_indexes.length - 1) {
sb.append(" ");
}
}
extraNode.put("disk_lun_indexes", sb.toString());
}
extraNode.put("disk_lun_indexes", sb.toString());
}
String buildRelease = nodeAgentManager.getSoftwareVersion();
String localPackagePath = nodeAgentHome.toString() + "/thirdparty";
if (localPackagePath != null) {

// Set package path
String localPackagePath = nodeAgentHome.toString() + "/thirdparty";
extraNode.put("package_path", localPackagePath);
}

if (!provider.getCode().equals(CloudType.onprem.toString())) {
List<String> devicePaths =
this.queryHelper.getDeviceNames(
provider,
Common.CloudType.valueOf(node.cloudInfo.cloud),
Integer.toString(taskParams().deviceInfo.numVolumes),
taskParams().deviceInfo.storageType.toString().toLowerCase(),
node.cloudInfo.region,
node.cloudInfo.instance_type);
String paths = String.join(" ", devicePaths);
extraNode.put("device_paths", paths);
}
rootNode.set("extra", extraNode);
// Set device paths for cloud providers
if (!provider.getCode().equals(CloudType.onprem.toString())) {
List<String> devicePaths =
this.queryHelper.getDeviceNames(
provider,
Common.CloudType.valueOf(node.cloudInfo.cloud),
Integer.toString(taskParams().deviceInfo.numVolumes),
taskParams().deviceInfo.storageType.toString().toLowerCase(),
node.cloudInfo.region,
node.cloudInfo.instance_type);
extraNode.put("device_paths", String.join(" ", devicePaths));
}

ObjectNode loggingNode = mapper.createObjectNode();
loggingNode.put("level", "DEBUG");
rootNode.set("logging", loggingNode);
rootNode.set("extra", extraNode);

try {
// "logging" JSON Object
ObjectNode loggingNode = mapper.createObjectNode();
loggingNode.put("level", "DEBUG");
rootNode.set("logging", loggingNode);

// Convert to JSON string
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
// Write the JSON string to the specified file
// Log JSON for debugging
log.debug("Generated JSON:\n{}", jsonString);

// Write to file with proper truncation
Path outputPath = Paths.get(outputFilePath);
Files.write(
outputPath,
jsonString.getBytes(),
jsonString.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);

} catch (Exception e) {
log.error("Failed parsing JSON file: {}", e.getMessage());
log.error("Failed generating JSON file: ", e);
}
}

Expand Down

0 comments on commit 378f9ba

Please sign in to comment.