Skip to content

Commit

Permalink
get PCS cluster nodes from corosync.conf since Ubuntu 20.04 pcs versi…
Browse files Browse the repository at this point in the history
…on does not have JSON output
  • Loading branch information
joshuaboud committed Dec 19, 2024
1 parent 134e61b commit 7e7fd1d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
40 changes: 40 additions & 0 deletions houston-common-lib/lib/getServerCluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@ import { _internal } from "./houston";

const pcsClusterConfigOutput = `{"cluster_name": "hacluster", "cluster_uuid": "8a916dffca8c419e9e8957b112f62382", "transport": "KNET", "totem_options": {}, "transport_options": {}, "compression_options": {}, "crypto_options": {"cipher": "aes256", "hash": "sha256"}, "nodes": [{"name": "192.168.45.13", "nodeid": "1", "addrs": [{"addr": "192.168.45.13", "link": "0", "type": "IPv4"}]}, {"name": "192.168.45.14", "nodeid": "2", "addrs": [{"addr": "192.168.45.14", "link": "0", "type": "IPv4"}]}], "links_options": {}, "quorum_options": {}, "quorum_device": null}`;


const corosyncConfContents = `totem {
version: 2
cluster_name: hacluster
transport: knet
crypto_cipher: aes256
crypto_hash: sha256
}
nodelist {
node {
ring0_addr: 192.168.102.4
name: 192.168.102.4
nodeid: 1
}
node {
ring0_addr: 192.168.102.5
name: 192.168.102.5
nodeid: 2
}
}
quorum {
provider: corosync_votequorum
two_node: 1
}
logging {
to_logfile: yes
logfile: /var/log/corosync/corosync.log
to_syslog: yes
timestamp: on
}
`;

suite("getServerCluster('pcs')", () => {
test("pcsNodesParseAddrs", () => {
const result = _internal.pcsNodesParseAddrs(pcsClusterConfigOutput);
Expand All @@ -11,4 +47,8 @@ suite("getServerCluster('pcs')", () => {
expect(addrs).toEqual(["192.168.45.13", "192.168.45.14"]);
});
});

test("parseCorosyncConfNodeIps", () => {
expect(_internal.parseCorosyncConfNodeIps(corosyncConfContents)).toEqual(["192.168.102.4", "192.168.102.5"])
})
});
47 changes: 25 additions & 22 deletions houston-common-lib/lib/houston.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NotFound, ParsingError, ProcessError } from "@/errors";
import { Command } from "@/process";
import { ParsingError, ProcessError } from "@/errors";
import { Server } from "@/server";
import { RegexSnippets } from "@/syntax";
import { safeJsonParse } from "@/utils";
Expand Down Expand Up @@ -34,6 +33,12 @@ export namespace _internal {
}
return ok(addrs);
});

export const parseCorosyncConfNodeIps = (corosyncConf: string) =>
corosyncConf
.split(RegexSnippets.newlineSplitter)
.filter((line) => /^\s*ring0_addr\s*:\s*.+$/.test(line))
.map((ring0Addr) => ring0Addr.split(":")[1]!.trim());
}

export function getServerCluster(
Expand Down Expand Up @@ -69,24 +74,22 @@ export function getServerCluster(
});
});
case "pcs":
return localServerResult.andThen((localServer) =>
localServer
.execute(
new Command(["pcs", "cluster", "config", "--output-format", "json"], {
superuser: "try",
})
)
.map((proc) => proc.getStdout())
.andThen(_internal.pcsNodesParseAddrs)
.map((hosts) => hosts.map((host) => getServer(host)))
.orElse((e) => {
if (e instanceof NotFound) {
console.warn("pcs command not found. Assuming single-server.");
return okAsync([okAsync<Server, ProcessError | ParsingError>(localServer)]);
}
return err(e);
})
);
return localServerResult.andThen((server) => {
const corosyncConfFile = new File(server, "/etc/corosync/corosync.conf");
return corosyncConfFile.exists().andThen((corosyncConfFileExists) => {
if (corosyncConfFileExists) {
return corosyncConfFile.read({ superuser: "try" }).map((confString) =>
_internal.parseCorosyncConfNodeIps(confString)
.map((ip) => getServer(ip))
);
} else {
console.warn(
"getServerCluster('pcs'): File not found: /etc/corosync/corosync.conf. Assuming single-server."
);
return okAsync([okAsync<Server, ProcessError | ParsingError>(server)]);
}
});
});
}
};

Expand All @@ -104,10 +107,10 @@ export function getServerCluster(
}
)
)
.filter((s): s is Server => s !== null) as [Server, ...Server[]]
.filter((s): s is Server => s !== null)
)
.andThen((servers) =>
servers.length > 0 ? ok(servers) : err(new ProcessError("No acessible servers in cluster."))
servers.length > 0 ? ok(servers as [Server, ...Server[]]) : err(new ProcessError("No acessible servers in cluster."))
)
.orElse((e) => {
window.reportHoustonError(e, "Assuming single server:");
Expand Down

0 comments on commit 7e7fd1d

Please sign in to comment.