From c7fe5aa1e0cbafb722ab3ad06c3bc399735e284c Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 19 May 2021 16:28:57 -0400 Subject: [PATCH 01/11] fix(cgi-bin): add boilerplate to set_membership --- cgi-bin/set_membership | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index e69de29b..d6c83ffb 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -0,0 +1,44 @@ +#!/usr/bin/perl +# +# Accepts a HTTP PUT request to set the cluster membership of a node. +# + +use strict; +use warnings; +use Anvil::Tools; +use Data::Dumper; +use JSON + +$| = 1; + +my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0]; +my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0]; +if (($running_directory =~ /^\./) && ($ENV{PWD})) +{ + $running_directory =~ s/^\./$ENV{PWD}/; +} + +my $anvil = Anvil::Tools->new(); + +$anvil->Get->switches; + +$anvil->Database->connect; +$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); +if (not $anvil->data->{sys}{database}{connections}) +{ + # No databases, exit. + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003"}); + $anvil->nice_exit({exit_code => 1}); +} + +# Read in any CGI variables, if needed. +$anvil->Get->cgi(); + +$anvil->Database->get_hosts(); +$anvil->Database->get_anvils(); + +print $anvil->Template->get({file => "shared.html", name => "json_headers", show_name => 0})."\n"; + +my $response_body = {}; + +print JSON->new->utf8->encode($response_body)."\n"; From da6e3500a3a7cca304eae6c5efe7e2573e4627dd Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 20 May 2021 16:46:22 -0400 Subject: [PATCH 02/11] fix(cgi-bin): add request body parsing to set_membership --- cgi-bin/set_membership | 79 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index d6c83ffb..c130ae64 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# Accepts a HTTP PUT request to set the cluster membership of a node. +# Accepts a HTTP PUT request to set the cluster membership of a host. # use strict; @@ -20,8 +20,40 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); +sub alter_host_membership +{ + # Expects the first of positional parameters to be a hash. + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + my $join_cluster = $parameters->{join_cluster}; + + $anvil->Log->variables({ + source => $THIS_FILE, + line => __LINE__, + level => 2, + list => { + host_membership_status => Dumper($anvil->data->{hosts}{host_uuid}{$host_uuid}) + } + }); +} + +# Re-adds a host to its anvil cluster. +# +# Note: not to be confused wtih adding a new host the an anvil. +#sub join_cluster +#{} + +# Removes a host from its anvil cluster. +# +# Note: does not permanently remove the host; can be re-added. +#sub leave_cluster +#{} + $anvil->Get->switches; +# TODO: remove when done; temporarily set log level to 2 +$anvil->Log->level({ set => 2 }); + $anvil->Database->connect; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); if (not $anvil->data->{sys}{database}{connections}) @@ -41,4 +73,49 @@ print $anvil->Template->get({file => "shared.html", name => "json_headers", show my $response_body = {}; +my $request_body; + +my $is_decode_json_success = eval { + $request_body = decode_json($anvil->data->{cgi}{PUTDATA}{value}); +}; + +if (not $is_decode_json_success) +{ + $anvil->Log->entry({ + source => $THIS_FILE, + line => __LINE__, + level => 0, + 'print' => 1, + priority => "err", + key => "error_0304", + variables => { request_body_string => $anvil->data->{cgi}{PUTDATA}{value}, json_decode_error => $_ } + }); + $anvil->nice_exit({ exit_code => 1 }); +} + +my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : ""; +# Defaults to join; will check whether host is already part of its anvil cluster. +my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : 1; + +if ($host_uuid) +{ + if (exists $anvil->data->{hosts}{host_uuid}{$host_uuid}) + { + alter_host_membership({ host_uuid => $host_uuid, join_cluster => $is_member }); + } + else + { + $anvil->Log->entry({ + source => $THIS_FILE, + line => __LINE__, + level => 0, + 'print' => 1, + priority => "err", + key => "error_0160", + variables => { name => "host UUID", uuid => $host_uuid } + }); + $anvil->nice_exit({ exit_code => 1 }); + } +} + print JSON->new->utf8->encode($response_body)."\n"; From 314e8db7576d23604f665013d78737bec091c440 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 20 May 2021 18:16:24 -0400 Subject: [PATCH 03/11] fix(cgi-bin): get host's cluster membership status in set_membership --- cgi-bin/set_membership | 55 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index c130ae64..89060894 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -20,6 +20,47 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); +sub is_active_member +{ + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + + my $query = " +SELECT + scan_cluster_node_in_ccm, + scan_cluster_node_crmd_member, + scan_cluster_node_cluster_member +FROM + scan_cluster_nodes +WHERE + scan_cluster_node_host_uuid = ".$anvil->Database->quote($host_uuid)." +"; + $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query } }); + my $results = $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__ }); + my $count = @{$results}; + $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { + results => $results, + count => $count + } }); + + if ($count == 1) + { + my $row = $results->[0]; + my $is_in_ccm = $row->[0]; + my $is_crmd_member = $row->[1]; + my $is_cluster_member = $row->[2]; + $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { + is_in_ccm => $is_in_ccm, + is_crmd_member => $is_crmd_member, + is_cluster_member => $is_cluster_member + } }); + + return $is_in_ccm && $is_crmd_member && $is_cluster_member; + } + + return 0; +} + sub alter_host_membership { # Expects the first of positional parameters to be a hash. @@ -27,14 +68,12 @@ sub alter_host_membership my $host_uuid = $parameters->{host_uuid}; my $join_cluster = $parameters->{join_cluster}; - $anvil->Log->variables({ - source => $THIS_FILE, - line => __LINE__, - level => 2, - list => { - host_membership_status => Dumper($anvil->data->{hosts}{host_uuid}{$host_uuid}) - } - }); + # Determine whether host is already a cluster member. + my $is_already_member = is_active_member({ host_uuid => $host_uuid }); + + $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { + is_already_member => $is_already_member + } }); } # Re-adds a host to its anvil cluster. From d6c101397684725032db7b71a1338a4b4a71b15a Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 21 May 2021 12:33:34 -0400 Subject: [PATCH 04/11] fix(cgi-bin): add join/leave cluster logic to set_membership --- cgi-bin/set_membership | 81 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index 89060894..5befd101 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -20,7 +20,7 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); -sub is_active_member +sub get_active_member { my $parameters = shift; my $host_uuid = $parameters->{host_uuid}; @@ -64,29 +64,80 @@ WHERE sub alter_host_membership { # Expects the first of positional parameters to be a hash. - my $parameters = shift; - my $host_uuid = $parameters->{host_uuid}; - my $join_cluster = $parameters->{join_cluster}; + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + my $is_join_cluster = $parameters->{is_join_cluster}; - # Determine whether host is already a cluster member. - my $is_already_member = is_active_member({ host_uuid => $host_uuid }); - - $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { - is_already_member => $is_already_member - } }); + if ($is_join_cluster) + { + join_cluster({ host_uuid => $host_uuid }); + } + else + { + leave_cluster({ host_uuid => $host_uuid }); + } } # Re-adds a host to its anvil cluster. # # Note: not to be confused wtih adding a new host the an anvil. -#sub join_cluster -#{} +sub join_cluster +{ + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + + # Determine whether host is already a cluster member. + my $is_active_member = get_active_member({ host_uuid => $host_uuid }); + # Find out whether the host is online before trying to make it join the cluster. + my $is_online = $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_status} eq "online" ? 1 : 0; + + $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { + is_active_member => $is_active_member, + is_online => $is_online + } }); + + if (not $is_active_member && $is_online) + { + # TODO: Uncomment when testing against real hosts. + # + #$anvil->Database->insert_or_update_jobs({ + # job_command => $anvil->data->{path}{exe}{'anvil-safe-start'}, + # job_description => "", + # job_name => "cgi-bin::set_membership::join", + # job_progress => 0, + # job_title => "" + #}); + } +} # Removes a host from its anvil cluster. # # Note: does not permanently remove the host; can be re-added. -#sub leave_cluster -#{} +sub leave_cluster +{ + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + + # Determine whether host is already a cluster member. + my $is_active_member = get_active_member({ host_uuid => $host_uuid }); + + $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { + is_active_member => $is_active_member + } }); + + if ($is_active_member) + { + # TODO: Uncomment when testing against real hosts. + # + #$anvil->Database->insert_or_update_jobs({ + # job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}, + # job_description => "", + # job_name => "cgi-bin::set_membership::leave", + # job_progress => 0, + # job_title => "" + #}); + } +} $anvil->Get->switches; @@ -140,7 +191,7 @@ if ($host_uuid) { if (exists $anvil->data->{hosts}{host_uuid}{$host_uuid}) { - alter_host_membership({ host_uuid => $host_uuid, join_cluster => $is_member }); + alter_host_membership({ host_uuid => $host_uuid, is_join_cluster => $is_member }); } else { From 419ec52d2b83633295737f80194c847d3132c66e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 21 May 2021 14:24:40 -0400 Subject: [PATCH 05/11] fix(cgi-bin): add job title and description to set_membership --- cgi-bin/set_membership | 8 ++++---- share/words.xml | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index 5befd101..e4ebaaee 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -102,10 +102,10 @@ sub join_cluster # #$anvil->Database->insert_or_update_jobs({ # job_command => $anvil->data->{path}{exe}{'anvil-safe-start'}, - # job_description => "", + # job_description => "job_0337", # job_name => "cgi-bin::set_membership::join", # job_progress => 0, - # job_title => "" + # job_title => "job_0336" #}); } } @@ -131,10 +131,10 @@ sub leave_cluster # #$anvil->Database->insert_or_update_jobs({ # job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}, - # job_description => "", + # job_description => "job_0339", # job_name => "cgi-bin::set_membership::leave", # job_progress => 0, - # job_title => "" + # job_title => "job_0338" #}); } } diff --git a/share/words.xml b/share/words.xml index 67bbc9d2..5136abf4 100644 --- a/share/words.xml +++ b/share/words.xml @@ -1081,6 +1081,14 @@ It should be provisioned in the next minute or two. Power Off Host Power off the target host by executing a stop script on the host itself. + + Host Join Cluster + + Make target host join its anvil cluster. + + Host Leave Cluster + + Make target host leave its anvil cluster. Starting: [#!variable!program!#]. From 1cc0a9b087d662769cb2129881c89447cfabd223 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 28 May 2021 16:38:59 -0400 Subject: [PATCH 06/11] fix(cgi-bin): add switch support to set_membership --- cgi-bin/set_membership | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index e4ebaaee..5a8aca7e 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -20,6 +20,24 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); +sub handle_invalid_uuid +{ + my $parameters = shift; + my $name = $parameters->{name}; + my $uuid = $parameters->{uuid}; + + $anvil->Log->entry({ + source => $THIS_FILE, + line => __LINE__, + level => 0, + 'print' => 1, + priority => "err", + key => "error_0160", + variables => { name => $name, uuid => $uuid } + }); + $anvil->nice_exit({ exit_code => 1 }); +} + sub get_active_member { my $parameters = shift; @@ -183,9 +201,10 @@ if (not $is_decode_json_success) $anvil->nice_exit({ exit_code => 1 }); } -my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : ""; +my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : $anvil->data->{switches}{'host-uuid'}; # Defaults to join; will check whether host is already part of its anvil cluster. -my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : 1; +my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : 1; +my $host_uuid_variable_name = "host UUID"; if ($host_uuid) { @@ -195,16 +214,7 @@ if ($host_uuid) } else { - $anvil->Log->entry({ - source => $THIS_FILE, - line => __LINE__, - level => 0, - 'print' => 1, - priority => "err", - key => "error_0160", - variables => { name => "host UUID", uuid => $host_uuid } - }); - $anvil->nice_exit({ exit_code => 1 }); + handle_invalid_uuid({ name => $host_uuid_variable_name, uuid => $host_uuid }); } } From 50c509823032ca0acb40cfe71e93e0bb3067b4bf Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 28 May 2021 16:40:36 -0400 Subject: [PATCH 07/11] fix(cgi-bin): release job dispatching logic in set_membership --- cgi-bin/set_membership | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index 5a8aca7e..ccf423bf 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -116,15 +116,13 @@ sub join_cluster if (not $is_active_member && $is_online) { - # TODO: Uncomment when testing against real hosts. - # - #$anvil->Database->insert_or_update_jobs({ - # job_command => $anvil->data->{path}{exe}{'anvil-safe-start'}, - # job_description => "job_0337", - # job_name => "cgi-bin::set_membership::join", - # job_progress => 0, - # job_title => "job_0336" - #}); + $anvil->Database->insert_or_update_jobs({ + job_command => $anvil->data->{path}{exe}{'anvil-safe-start'}, + job_description => "job_0337", + job_name => "cgi-bin::set_membership::join", + job_progress => 0, + job_title => "job_0336" + }); } } @@ -145,15 +143,13 @@ sub leave_cluster if ($is_active_member) { - # TODO: Uncomment when testing against real hosts. - # - #$anvil->Database->insert_or_update_jobs({ - # job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}, - # job_description => "job_0339", - # job_name => "cgi-bin::set_membership::leave", - # job_progress => 0, - # job_title => "job_0338" - #}); + $anvil->Database->insert_or_update_jobs({ + job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}, + job_description => "job_0339", + job_name => "cgi-bin::set_membership::leave", + job_progress => 0, + job_title => "job_0338" + }); } } From 7bf604b3e2f4c3f51317c33f66cae96a744ad101 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 28 May 2021 17:16:16 -0400 Subject: [PATCH 08/11] fix(cgi-bin): add switch for is_member in set_membership --- cgi-bin/set_membership | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index ccf423bf..958ec317 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -199,7 +199,7 @@ if (not $is_decode_json_success) my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : $anvil->data->{switches}{'host-uuid'}; # Defaults to join; will check whether host is already part of its anvil cluster. -my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : 1; +my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : $anvil->data->{switches}{'is-member'}; my $host_uuid_variable_name = "host UUID"; if ($host_uuid) From 52a524564f42d2c403d4b5bc8f3cb46ef6c4525e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 28 May 2021 18:18:10 -0400 Subject: [PATCH 09/11] fix(cgi-bin): try to use switches when failed to parse request body in get_membership --- cgi-bin/set_membership | 1 - 1 file changed, 1 deletion(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index 958ec317..2fa60a23 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -194,7 +194,6 @@ if (not $is_decode_json_success) key => "error_0304", variables => { request_body_string => $anvil->data->{cgi}{PUTDATA}{value}, json_decode_error => $_ } }); - $anvil->nice_exit({ exit_code => 1 }); } my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : $anvil->data->{switches}{'host-uuid'}; From 7ffb84f9a3a2941825b20206377616474481e48e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 28 May 2021 18:40:28 -0400 Subject: [PATCH 10/11] fix(cgi-bin): set join and leave cluster jobs to run on target host --- cgi-bin/set_membership | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index 2fa60a23..76a2f165 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -118,6 +118,7 @@ sub join_cluster { $anvil->Database->insert_or_update_jobs({ job_command => $anvil->data->{path}{exe}{'anvil-safe-start'}, + job_host_uuid => $host_uuid, job_description => "job_0337", job_name => "cgi-bin::set_membership::join", job_progress => 0, @@ -145,6 +146,7 @@ sub leave_cluster { $anvil->Database->insert_or_update_jobs({ job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}, + job_host_uuid => $host_uuid, job_description => "job_0339", job_name => "cgi-bin::set_membership::leave", job_progress => 0, From 028d547d7c7a73e80011e4843218f61b7935d220 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 1 Jun 2021 14:19:31 -0400 Subject: [PATCH 11/11] fix(cgi-bin): reset log level in set_membership --- cgi-bin/set_membership | 3 --- 1 file changed, 3 deletions(-) diff --git a/cgi-bin/set_membership b/cgi-bin/set_membership index 76a2f165..eb62c5ae 100755 --- a/cgi-bin/set_membership +++ b/cgi-bin/set_membership @@ -157,9 +157,6 @@ sub leave_cluster $anvil->Get->switches; -# TODO: remove when done; temporarily set log level to 2 -$anvil->Log->level({ set => 2 }); - $anvil->Database->connect; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); if (not $anvil->data->{sys}{database}{connections})