diff --git a/mgr/src/cmds/clusters.cr b/mgr/src/cmds/clusters.cr index 52357c4..7d6bbae 100644 --- a/mgr/src/cmds/clusters.cr +++ b/mgr/src/cmds/clusters.cr @@ -28,7 +28,7 @@ handler "cluster.list" do |args| puts "No clusters. Run `kadalu cluster create ` 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) diff --git a/mgr/src/cmds/nodes.cr b/mgr/src/cmds/nodes.cr index c8f8f97..d248daa 100644 --- a/mgr/src/cmds/nodes.cr +++ b/mgr/src/cmds/nodes.cr @@ -1,7 +1,7 @@ require "./helpers" struct NodeArgs - property status = false + property status = false, endpoint = "" end class Args @@ -9,57 +9,60 @@ class Args 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://: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} ` to add a node." if nodes.size == 0 + puts "No nodes added to the Cluster. Run `kadalu node join #{args.cluster_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| diff --git a/mgr/src/server/datastore/clusters.cr b/mgr/src/server/datastore/clusters.cr index 88e3088..5c47c42 100644 --- a/mgr/src/server/datastore/clusters.cr +++ b/mgr/src/server/datastore/clusters.cr @@ -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 diff --git a/mgr/src/server/datastore/nodes.cr b/mgr/src/server/datastore/nodes.cr index 55c1af8..ba6d926 100644 --- a/mgr/src/server/datastore/nodes.cr +++ b/mgr/src/server/datastore/nodes.cr @@ -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 diff --git a/mgr/src/server/plugins/node_join.cr b/mgr/src/server/plugins/node_join.cr index 77ac46b..7e157e0 100644 --- a/mgr/src/server/plugins/node_join.cr +++ b/mgr/src/server/plugins/node_join.cr @@ -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? diff --git a/tests/all/nodes.t b/tests/all/nodes.t index f78e847..670799a 100644 --- a/tests/all/nodes.t +++ b/tests/all/nodes.t @@ -19,7 +19,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 @@ -27,5 +27,5 @@ nodes.each do |node| 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" diff --git a/tests/all/volumes.t b/tests/all/volumes.t index 765153e..7957e14 100644 --- a/tests/all/volumes.t +++ b/tests/all/volumes.t @@ -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