Skip to content

Commit

Permalink
Node list and Status command
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 e87f6d6 commit 1c76601
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 32 deletions.
4 changes: 4 additions & 0 deletions clients/crystal/src/clusters.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ module MoanaClient
Node.join(@client, @name, name, endpoint)
end

def list_nodes(state = false)
Node.list(@client, @name, state)
end

def create_volume(req : MoanaTypes::Volume)
Volume.create(@client, @name, req)
end
Expand Down
13 changes: 13 additions & 0 deletions clients/crystal/src/nodes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,18 @@ module MoanaClient
MoanaClient.error_response(response)
end
end

def self.list(client : Client, cluster_name : String, state = false)
url = "#{client.url}/api/v1/clusters/#{cluster_name}/nodes?state=#{state ? 1 : 0}"
response = MoanaClient.http_get(
url,
headers: client.auth_header
)
if response.status_code == 200
Array(MoanaTypes::Node).from_json(response.body)
else
MoanaClient.error_response(response)
end
end
end
end
29 changes: 0 additions & 29 deletions mgr/src/cmds/node.cr

This file was deleted.

73 changes: 73 additions & 0 deletions mgr/src/cmds/nodes.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "./helpers"

struct NodeArgs
property status = false
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
end
end

handler "node.join" do |args|
if args.pos_args.size < 2
STDERR.puts "Node name and Node endpoint are required."
exit 1
end

# TODO: Add default Cluster logic once it is available
if args.cluster_name == ""
STDERR.puts "--cluster=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"
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.on("--status", "Show nodes states") do
args.node_args.status = true
end
end

handler "node.list" do |args|
if args.cluster_name == ""
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

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

nodes.each do |node|
if args.node_args.status
printf("%36s %6s %20s %s\n", node.id, node.state, node.name, node.endpoint)
else
printf("%36s %20s %s\n", node.id, node.name, node.endpoint)
end
end
end
end
11 changes: 11 additions & 0 deletions mgr/src/server/datastore/nodes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ module Datastore
node
end

def self.list_nodes(cluster_name)
nodes = [] of MoanaTypes::Node
Dir.entries(Path.new(@@rootdir, "clusters", cluster_name, "nodes")).each do |node_name|
if node_name != "." && node_name != ".."
nodes << get_node(cluster_name, node_name).not_nil!
end
end

nodes
end

def self.get_nodes(cluster_name, node_names)
nodes = [] of MoanaTypes::Node
node_names.map do |node_name|
Expand Down
4 changes: 4 additions & 0 deletions mgr/src/server/plugins/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def dispatch_action(name, cluster_name, nodes, data)
Action.dispatch(name, cluster_name, nodes, data)
end

def dispatch_action(name, cluster_name, nodes)
Action.dispatch(name, cluster_name, nodes, "")
end

def metrics_collector(name : String, &block : MgrTypes::Cluster -> Nil)
MetricsCollector.add(name, &block)
end
Expand Down
24 changes: 24 additions & 0 deletions mgr/src/server/plugins/nodes.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "./helpers"
require "../conf"
require "../datastore/*"

get "/api/v1/clusters/:cluster_name/nodes" do |env|
cluster_name = env.params.url["cluster_name"]
state = env.params.query["state"]

nodes = Datastore.list_nodes(cluster_name)

next nodes.to_json unless state

resp = dispatch_action(
ACTION_PING,
cluster_name,
nodes
)

nodes.each do |node|
node.state = resp.node_responses[node.name].ok ? "Up" : "Down"
end

nodes.to_json
end
3 changes: 2 additions & 1 deletion tests/all/clusters.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ nodes[1 .. -1].each do |node|
TEST "systemctl start kadalu-agent"
end


USE_NODE nodes[0]
puts TEST "kadalu cluster list"
4 changes: 4 additions & 0 deletions tests/all/nodes.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ nodes.each do |node|
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"
4 changes: 2 additions & 2 deletions types/src/moana_types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ module MoanaTypes
end
end

struct Node
class Node
include JSON::Serializable

property id = "", name = "", endpoint = "", addresses = [] of String, token = ""
property id = "", name = "", state = "", endpoint = "", addresses = [] of String, token = ""

def initialize
end
Expand Down

0 comments on commit 1c76601

Please sign in to comment.