Skip to content

Commit

Permalink
Cluster and Node arguments cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Aravinda Vishwanathapura <[email protected]>
  • Loading branch information
aravindavk committed Nov 29, 2021
1 parent 1c76601 commit 2c12af5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion mgr/src/cmds/clusters.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ handler "cluster.list" do |args|

puts "No clusters. Run `kadalu cluster create <name>` to create a Cluster." if clusters.size == 0

printf("%36s %s\n", "ID", "Name")
printf("%36s %s\n", "ID", "Name") if clusters.size > 0

clusters.each do |cluster|
printf("%36s %s\n", cluster.id, cluster.name)
Expand Down
45 changes: 24 additions & 21 deletions mgr/src/cmds/nodes.cr
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
require "./helpers"

struct NodeArgs
property status = false
property status = false, endpoint = ""
end

class Args
property node_args = NodeArgs.new
end

command "node.join", "Join a node to Kadalu Storage Cluster" do |parser, args|
parser.banner = "Usage: kadalu node join NAME ENDPOINT [arguments]"
parser.on("-c NAME", "--cluster=NAME", "Cluster name") do |name|
args.cluster_name = name
parser.banner = "Usage: kadalu node join CLUSTER/NAME ENDPOINT [arguments]"
parser.on("--endpoint", "Node Endpoint. Default is http://<nodename>:3000") do |endpoint|
args.node_args.endpoint = endpoint
end
end

def cluster_and_node_name(value)
parts = value.split("/")
return {parts[0], parts[1]} if parts.size == 2

{parts[0], ""}
end

handler "node.join" do |args|
if args.pos_args.size < 2
STDERR.puts "Node name and Node endpoint are required."
args.cluster_name, name = cluster_and_node_name(args.pos_args.size < 1 ? "" : args.pos_args[0])
if args.cluster_name == ""
STDERR.puts "Cluster name is required."
exit 1
end

# TODO: Add default Cluster logic once it is available
if args.cluster_name == ""
STDERR.puts "--cluster=NAME is required."
if name == ""
STDERR.puts "Node name is required."
exit 1
end

name = args.pos_args[0]
endpoint = args.pos_args[1]
api_call(args, "Failed to join the Node") do |client|
node = client.cluster(args.cluster_name).join_node(name, endpoint)
puts "Node #{name}(#{endpoint}) joined to #{args.cluster_name} successfully"
node = client.cluster(args.cluster_name).join_node(name, args.node_args.endpoint)
puts "Node #{name} joined to #{args.cluster_name} successfully"
puts "ID: #{node.id}"
end
end

command "node.list", "Nodes list of a Kadalu Storage Cluster" do |parser, args|
parser.banner = "Usage: kadalu node list [arguments]"
parser.on("-c NAME", "--cluster=NAME", "Cluster name") do |name|
args.cluster_name = name
end
parser.banner = "Usage: kadalu node list CLUSTER [arguments]"
parser.on("--status", "Show nodes states") do
args.node_args.status = true
end
end

handler "node.list" do |args|
args.cluster_name, _ = cluster_and_node_name(args.pos_args.size < 1 ? "" : args.pos_args[0])
if args.cluster_name == ""
STDERR.puts "--cluster=NAME is required."
STDERR.puts "Cluster name is required."
exit 1
end

api_call(args, "Failed to get list of nodes") do |client|
nodes = client.cluster(args.cluster_name).list_nodes(state: args.node_args.status)
puts "No nodes added to the Cluster. Run `kadalu node join -c #{args.cluster_name} <node-name> <node-endpoint>` to add a node." if nodes.size == 0
puts "No nodes added to the Cluster. Run `kadalu node join #{args.cluster_name}/<node-name>` to add a node." if nodes.size == 0

if args.node_args.status
printf("%36s %6s %20s %s\n", "ID", "State", "Name", "Endpoint")
printf("%36s %6s %20s %s\n", "ID", "State", "Name", "Endpoint") if nodes.size > 0
else
printf("%36s %20s %s\n", "ID", "Name", "Endpoint")
printf("%36s %20s %s\n", "ID", "Name", "Endpoint") if nodes.size > 0
end

nodes.each do |node|
Expand Down
6 changes: 5 additions & 1 deletion mgr/src/server/datastore/clusters.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ module Datastore

def self.list_clusters
clusters = [] of MoanaTypes::Cluster
Dir.entries(Path.new(@@rootdir, "clusters")).each do |cluster_name|
clusters_dir = Path.new(@@rootdir, "clusters")

return clusters unless File.exists?(clusters_dir)

Dir.entries(clusters_dir).each do |cluster_name|
if cluster_name != "." && cluster_name != ".."
clusters << get_cluster(cluster_name).not_nil!
end
Expand Down
6 changes: 5 additions & 1 deletion mgr/src/server/datastore/nodes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ module Datastore

def self.list_nodes(cluster_name)
nodes = [] of MoanaTypes::Node
Dir.entries(Path.new(@@rootdir, "clusters", cluster_name, "nodes")).each do |node_name|
nodes_dir = Path.new(@@rootdir, "clusters", cluster_name, "nodes")

return nodes unless File.exists?(nodes_dir)

Dir.entries(nodes_dir).each do |node_name|
if node_name != "." && node_name != ".."
nodes << get_node(cluster_name, node_name).not_nil!
end
Expand Down
6 changes: 5 additions & 1 deletion mgr/src/server/plugins/node_join.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ end
post "/api/v1/clusters/:cluster_name/nodes" do |env|
cluster_name = env.params.url["cluster_name"]
node_name = env.params.json["name"].as(String)
endpoint = env.params.json["endpoint"].as(String)

endpoint = env.params.json.fetch("endpoint", "").as(String)
# TODO: Add detault http/https and port values from config
endpoint = "http://#{node_name}:3000" if endpoint == ""

node = Datastore.get_node(cluster_name, node_name)

if !node.nil?
Expand Down
6 changes: 3 additions & 3 deletions tests/all/nodes.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ end

nodes.each do |node|
USE_NODE nodes[0]
TEST "kadalu node join #{node} http://#{node}:3000 -c mycluster"
TEST "kadalu node join mycluster/#{node}"
puts TEST "cat /var/lib/kadalu/meta/clusters/mycluster/nodes/server1/info"

USE_NODE node
puts TEST "cat /var/lib/kadalu/info"
end

USE_NODE nodes[0]
puts TEST "kadalu node list -c mycluster"
puts TEST "kadalu node list -c mycluster --status"
puts TEST "kadalu node list mycluster"
puts TEST "kadalu node list mycluster --status"
2 changes: 1 addition & 1 deletion tests/all/volumes.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end

nodes.each do |node|
USE_NODE nodes[0]
TEST "kadalu node join #{node} http://#{node}:3000 -c mycluster"
TEST "kadalu node join mycluster/#{node}"
puts TEST "cat /var/lib/kadalu/meta/clusters/mycluster/nodes/server1/info"

USE_NODE node
Expand Down

0 comments on commit 2c12af5

Please sign in to comment.